blob: 58e3674c59f37758479427b76716c07a4fc4dad0 [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 Yamasani734983f2014-03-04 16:48:05 -080069import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070070import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.res.Resources;
72import android.content.res.TypedArray;
73import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080074import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080075import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080076import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070077import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040079import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070080import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090081import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Handler;
83import android.os.IBinder;
84import android.os.IInterface;
85import android.os.Message;
86import 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;
Yohei Yukawae985c242016-02-24 18:27:04 -0800101import android.util.LocaleList;
satokf9f01002011-05-19 21:31:50 +0900102import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900103import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.util.PrintWriterPrinter;
105import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900106import android.util.Slog;
107import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900108import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700110import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.view.LayoutInflater;
112import android.view.View;
113import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700115import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900116import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700118import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.view.inputmethod.InputMethod;
120import android.view.inputmethod.InputMethodInfo;
121import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700122import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900123import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900124import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900126import android.widget.CompoundButton;
127import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900128import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900129import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900130import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700131import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
satoke7c6998e2011-06-03 17:57:59 +0900133import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900135import java.io.FileInputStream;
136import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.io.IOException;
138import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700139import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100140import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900142import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.util.HashMap;
144import java.util.List;
145
146/**
147 * This class provides a system service that manages input methods.
148 */
149public class InputMethodManagerService extends IInputMethodManager.Stub
150 implements ServiceConnection, Handler.Callback {
151 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700152 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700153 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700155 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
156 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
157 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 static final int MSG_UNBIND_INPUT = 1000;
160 static final int MSG_BIND_INPUT = 1010;
161 static final int MSG_SHOW_SOFT_INPUT = 1020;
162 static final int MSG_HIDE_SOFT_INPUT = 1030;
163 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 =
1024 !mSystemReady || !mUserManager.isUserUnlocked(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() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001053 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001054 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1055 for (int i = 0; i < currentProfileIds.length; i++) {
1056 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001057 }
Kenny Guy2a764942014-04-02 13:29:20 +01001058 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001059 }
1060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 @Override
1062 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1063 throws RemoteException {
1064 try {
1065 return super.onTransact(code, data, reply, flags);
1066 } catch (RuntimeException e) {
1067 // The input method manager only throws security exceptions, so let's
1068 // log all others.
1069 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001070 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 throw e;
1073 }
1074 }
1075
Svetoslav Ganova0027152013-06-25 14:59:53 -07001076 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001077 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001078 if (DEBUG) {
1079 Slog.d(TAG, "--- systemReady");
1080 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001081 if (!mSystemReady) {
1082 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001083 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001084 mSettings.switchCurrentUser(currentUserId,
1085 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001086 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1087 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001088 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001089 if (mStatusBar != null) {
1090 mStatusBar.setIconVisibility(mSlotIme, false);
1091 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001092 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001093 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1094 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001095 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001096 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001097 mHardKeyboardListener);
1098 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001099 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001100 if (!mImeSelectedOnBoot) {
1101 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001102 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001103 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1104 mSettings.getEnabledInputMethodListLocked(),
1105 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001106 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001107 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001108 try {
1109 startInputInnerLocked();
1110 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001112 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001113 }
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 // ---------------------------------------------------------------------------------------
1118 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1119 // 1) it comes from the system process
1120 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1121 private boolean calledFromValidUser() {
1122 final int uid = Binder.getCallingUid();
1123 final int userId = UserHandle.getUserId(uid);
1124 if (DEBUG) {
1125 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1126 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1127 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001128 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1129 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001130 }
Kenny Guy2a764942014-04-02 13:29:20 +01001131 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001134
1135 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1136 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1137 // must not manage background users' states in any functions.
1138 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1139 // by a token.
1140 if (mContext.checkCallingOrSelfPermission(
1141 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1142 == PackageManager.PERMISSION_GRANTED) {
1143 if (DEBUG) {
1144 Slog.d(TAG, "--- Access granted because the calling process has "
1145 + "the INTERACT_ACROSS_USERS_FULL permission");
1146 }
1147 return true;
1148 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001149 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1150 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001151 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001152 }
1153
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001154
1155 /**
1156 * Returns true iff the caller is identified to be the current input method with the token.
1157 * @param token The window token given to the input method when it was started.
1158 * @return true if and only if non-null valid token is specified.
1159 */
1160 private boolean calledWithValidToken(IBinder token) {
1161 if (token == null || mCurToken != token) {
1162 return false;
1163 }
1164 return true;
1165 }
1166
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 private boolean bindCurrentInputMethodService(
1168 Intent service, ServiceConnection conn, int flags) {
1169 if (service == null || conn == null) {
1170 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1171 return false;
1172 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001173 return mContext.bindServiceAsUser(service, conn, flags,
1174 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 }
1176
satoke7c6998e2011-06-03 17:57:59 +09001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001179 // TODO: Make this work even for non-current users?
1180 if (!calledFromValidUser()) {
1181 return Collections.emptyList();
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001184 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
1187
satoke7c6998e2011-06-03 17:57:59 +09001188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 // TODO: Make this work even for non-current users?
1191 if (!calledFromValidUser()) {
1192 return Collections.emptyList();
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001195 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
1198
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001199 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001200 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001201 * @return enabled subtypes of the specified imi
1202 */
satoke7c6998e2011-06-03 17:57:59 +09001203 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001204 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001205 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001206 // TODO: Make this work even for non-current users?
1207 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001208 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001209 }
satok67ddf9c2010-11-17 09:45:54 +09001210 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001211 final InputMethodInfo imi;
1212 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001213 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001214 } else {
1215 imi = mMethodMap.get(imiId);
1216 }
1217 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001218 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001219 }
1220 return mSettings.getEnabledInputMethodSubtypeListLocked(
1221 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001222 }
1223 }
1224
satoke7c6998e2011-06-03 17:57:59 +09001225 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 public void addClient(IInputMethodClient client,
1227 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001228 if (!calledFromValidUser()) {
1229 return;
1230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 synchronized (mMethodMap) {
1232 mClients.put(client.asBinder(), new ClientState(client,
1233 inputContext, uid, pid));
1234 }
1235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236
satoke7c6998e2011-06-03 17:57:59 +09001237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001239 if (!calledFromValidUser()) {
1240 return;
1241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001243 ClientState cs = mClients.remove(client.asBinder());
1244 if (cs != null) {
1245 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001246 if (mCurClient == cs) {
1247 mCurClient = null;
1248 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001249 if (mCurFocusedWindowClient == cs) {
1250 mCurFocusedWindowClient = null;
1251 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 void executeOrSendMessage(IInterface target, Message msg) {
1257 if (target.asBinder() instanceof Binder) {
1258 mCaller.sendMessage(msg);
1259 } else {
1260 handleMessage(msg);
1261 msg.recycle();
1262 }
1263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
Yohei Yukawa33e81792015-11-17 21:14:42 -08001265 void unbindCurrentClientLocked(
1266 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001268 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 + mCurClient.client.asBinder());
1270 if (mBoundToMethod) {
1271 mBoundToMethod = false;
1272 if (mCurMethod != null) {
1273 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1274 MSG_UNBIND_INPUT, mCurMethod));
1275 }
1276 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001277
1278 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1279 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001280 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1281 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project10592532009-03-18 17:39:46 -07001285 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 private int getImeShowFlags() {
1290 int flags = 0;
1291 if (mShowForced) {
1292 flags |= InputMethod.SHOW_FORCED
1293 | InputMethod.SHOW_EXPLICIT;
1294 } else if (mShowExplicitlyRequested) {
1295 flags |= InputMethod.SHOW_EXPLICIT;
1296 }
1297 return flags;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 private int getAppShowFlags() {
1301 int flags = 0;
1302 if (mShowForced) {
1303 flags |= InputMethodManager.SHOW_FORCED;
1304 } else if (!mShowExplicitlyRequested) {
1305 flags |= InputMethodManager.SHOW_IMPLICIT;
1306 }
1307 return flags;
1308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 if (!mBoundToMethod) {
1312 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1313 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1314 mBoundToMethod = true;
1315 }
1316 final SessionState session = mCurClient.curSession;
1317 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001318 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1319 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1320 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001322 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1323 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1324 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001327 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001330 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001331 (session.channel != null ? session.channel.dup() : null),
1332 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001335 InputBindResult startInputLocked(
1336 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001337 IInputMethodClient client, IInputContext inputContext,
1338 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001339 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // If no method is currently selected, do nothing.
1341 if (mCurMethodId == null) {
1342 return mNoBinding;
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 ClientState cs = mClients.get(client.asBinder());
1346 if (cs == null) {
1347 throw new IllegalArgumentException("unknown client "
1348 + client.asBinder());
1349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001350
Yohei Yukawa74750f22016-03-22 12:54:22 -07001351 if (attribute == null) {
1352 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1353 + " uid=" + cs.uid + " pid=" + cs.pid);
1354 return null;
1355 }
1356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 try {
1358 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1359 // Check with the window manager to make sure this client actually
1360 // has a window with focus. If not, reject. This is thread safe
1361 // because if the focus changes some time before or after, the
1362 // next client receiving focus that has any interest in input will
1363 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1366 return null;
1367 }
1368 } catch (RemoteException e) {
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001371 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1372 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001373 }
1374
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001375 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001376 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001377 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001378 // If no method is currently selected, do nothing.
1379 if (mCurMethodId == null) {
1380 return mNoBinding;
1381 }
1382
Yohei Yukawad57ba672015-06-08 16:39:46 -07001383 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1384 attribute.packageName)) {
1385 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1386 + " uid=" + cs.uid + " package=" + attribute.packageName);
1387 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001391 // Was the keyguard locked when switching over to the new client?
1392 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 // If the client is changing, we need to switch over to the new
1394 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001395 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001396 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001397 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398
1399 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001400 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001401 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001402 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // Bump up the sequence for this client and attach it.
1407 mCurSeq++;
1408 if (mCurSeq <= 0) mCurSeq = 1;
1409 mCurClient = cs;
1410 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001411 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 // Check if the input method is changing.
1415 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1416 if (cs.curSession != null) {
1417 // Fast case: if we are already connected to the input method,
1418 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001419 return attachNewInputLocked(
1420 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422 if (mHaveConnection) {
1423 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 // Return to client, and we will get back with it when
1425 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001426 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001427 return new InputBindResult(null, null, mCurId, mCurSeq,
1428 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 } else if (SystemClock.uptimeMillis()
1430 < (mLastBindTime+TIME_TO_RECONNECT)) {
1431 // In this case we have connected to the service, but
1432 // don't yet have its interface. If it hasn't been too
1433 // long since we did the connection, we'll return to
1434 // the client and wait to get the service interface so
1435 // we can report back. If it has been too long, we want
1436 // to fall through so we can try a disconnect/reconnect
1437 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001438 return new InputBindResult(null, null, mCurId, mCurSeq,
1439 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1442 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 }
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001447 return startInputInnerLocked();
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001450 InputBindResult startInputInnerLocked() {
1451 if (mCurMethodId == null) {
1452 return mNoBinding;
1453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001455 if (!mSystemReady) {
1456 // If the system is not yet ready, we shouldn't be running third
1457 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001458 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1459 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1463 if (info == null) {
1464 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001467 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1470 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001471 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1472 com.android.internal.R.string.input_method_binding_label);
1473 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1474 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001476 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1477 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mLastBindTime = SystemClock.uptimeMillis();
1479 mHaveConnection = true;
1480 mCurId = info.getId();
1481 mCurToken = new Binder();
1482 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001483 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mIWindowManager.addWindowToken(mCurToken,
1485 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1486 } catch (RemoteException e) {
1487 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001488 return new InputBindResult(null, null, mCurId, mCurSeq,
1489 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 } else {
1491 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001492 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 + mCurIntent);
1494 }
1495 return null;
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001498 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001499 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001500 IInputMethodClient client, IInputContext inputContext,
1501 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001502 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001503 if (!calledFromValidUser()) {
1504 return null;
1505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001507 if (DEBUG) {
1508 Slog.v(TAG, "startInput: reason="
1509 + InputMethodClient.getStartInputReason(startInputReason)
1510 + " client = " + client.asBinder()
1511 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001512 + " missingMethods="
1513 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001514 + " attribute=" + attribute
1515 + " controlFlags=#" + Integer.toHexString(controlFlags));
1516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 final long ident = Binder.clearCallingIdentity();
1518 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001519 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1520 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } finally {
1522 Binder.restoreCallingIdentity(ident);
1523 }
1524 }
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 finishInput(IInputMethodClient client) {
1529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530
satoke7c6998e2011-06-03 17:57:59 +09001531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void onServiceConnected(ComponentName name, IBinder service) {
1533 synchronized (mMethodMap) {
1534 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1535 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001536 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001537 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001538 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001539 return;
1540 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001541 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001542 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1543 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001545 clearClientSessionLocked(mCurClient);
1546 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549 }
1550 }
1551
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1553 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 synchronized (mMethodMap) {
1555 if (mCurMethod != null && method != null
1556 && mCurMethod.asBinder() == method.asBinder()) {
1557 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001560 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001561 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 if (res.method != null) {
1563 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001564 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 }
1569 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001570
1571 // Session abandoned. Close its associated input channel.
1572 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001575 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001576 if (mVisibleBound) {
1577 mContext.unbindService(mVisibleConnection);
1578 mVisibleBound = false;
1579 }
1580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001581 if (mHaveConnection) {
1582 mContext.unbindService(this);
1583 mHaveConnection = false;
1584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001586 if (mCurToken != null) {
1587 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001588 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001589 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001590 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001591 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001592 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001593 mIWindowManager.removeWindowToken(mCurToken);
1594 } catch (RemoteException e) {
1595 }
1596 mCurToken = null;
1597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
The Android Open Source Project10592532009-03-18 17:39:46 -07001599 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001600 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
Yohei Yukawa33e81792015-11-17 21:14:42 -08001603 void resetCurrentMethodAndClient(
1604 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001605 mCurMethodId = null;
1606 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001607 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001609
1610 void requestClientSessionLocked(ClientState cs) {
1611 if (!cs.sessionRequested) {
1612 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1613 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1614 cs.sessionRequested = true;
1615 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1616 MSG_CREATE_SESSION, mCurMethod, channels[1],
1617 new MethodCallback(this, mCurMethod, channels[0])));
1618 }
1619 }
1620
1621 void clearClientSessionLocked(ClientState cs) {
1622 finishSessionLocked(cs.curSession);
1623 cs.curSession = null;
1624 cs.sessionRequested = false;
1625 }
1626
1627 private void finishSessionLocked(SessionState sessionState) {
1628 if (sessionState != null) {
1629 if (sessionState.session != null) {
1630 try {
1631 sessionState.session.finishSession();
1632 } catch (RemoteException e) {
1633 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001634 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001635 }
1636 sessionState.session = null;
1637 }
1638 if (sessionState.channel != null) {
1639 sessionState.channel.dispose();
1640 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001641 }
1642 }
1643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001644
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001645 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (mCurMethod != null) {
1647 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001648 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001650
Jeff Brownc28867a2013-03-26 15:42:39 -07001651 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001652 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 mCurMethod = null;
1654 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001655 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001656 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
satoke7c6998e2011-06-03 17:57:59 +09001660 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 public void onServiceDisconnected(ComponentName name) {
1662 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 + " mCurIntent=" + mCurIntent);
1665 if (mCurMethod != null && mCurIntent != null
1666 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 // We consider this to be a new bind attempt, since the system
1669 // should now try to restart the service for us.
1670 mLastBindTime = SystemClock.uptimeMillis();
1671 mShowRequested = mInputShown;
1672 mInputShown = false;
1673 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001674 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1675 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1676 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678 }
1679 }
1680 }
1681
satokf9f01002011-05-19 21:31:50 +09001682 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1684 long ident = Binder.clearCallingIdentity();
1685 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001687 if (!calledWithValidToken(token)) {
1688 final int uid = Binder.getCallingUid();
1689 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1690 + " token:" + token);
1691 return;
1692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001694 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001695 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001696 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001699 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001700 CharSequence contentDescription = null;
1701 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001702 // Use PackageManager to load label
1703 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001704 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001705 mIPackageManager.getApplicationInfo(packageName, 0,
1706 mSettings.getCurrentUserId()));
1707 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001708 /* ignore */
1709 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001710 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001711 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001712 contentDescription != null
1713 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001714 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717 }
1718 } finally {
1719 Binder.restoreCallingIdentity(ident);
1720 }
1721 }
1722
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001723 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001724 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001725 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001726 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001727 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001728 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001729 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1730 // When physical keyboard is attached, we show the ime switcher (or notification if
1731 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1732 // exists in the IME switcher dialog. Might be OK to remove this condition once
1733 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1734 return true;
1735 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001736 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1737 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001738 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001739
1740 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1741 final int N = imis.size();
1742 if (N > 2) return true;
1743 if (N < 1) return false;
1744 int nonAuxCount = 0;
1745 int auxCount = 0;
1746 InputMethodSubtype nonAuxSubtype = null;
1747 InputMethodSubtype auxSubtype = null;
1748 for(int i = 0; i < N; ++i) {
1749 final InputMethodInfo imi = imis.get(i);
1750 final List<InputMethodSubtype> subtypes =
1751 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1752 final int subtypeCount = subtypes.size();
1753 if (subtypeCount == 0) {
1754 ++nonAuxCount;
1755 } else {
1756 for (int j = 0; j < subtypeCount; ++j) {
1757 final InputMethodSubtype subtype = subtypes.get(j);
1758 if (!subtype.isAuxiliary()) {
1759 ++nonAuxCount;
1760 nonAuxSubtype = subtype;
1761 } else {
1762 ++auxCount;
1763 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001764 }
1765 }
satok7cfc0ed2011-06-20 21:29:36 +09001766 }
1767 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001768 if (nonAuxCount > 1 || auxCount > 1) {
1769 return true;
1770 } else if (nonAuxCount == 1 && auxCount == 1) {
1771 if (nonAuxSubtype != null && auxSubtype != null
1772 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1773 || auxSubtype.overridesImplicitlyEnabledSubtype()
1774 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1775 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1776 return false;
1777 }
1778 return true;
1779 }
1780 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001781 }
1782
John Spurlocke0980502013-10-25 11:59:29 -04001783 private boolean isKeyguardLocked() {
1784 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1785 }
1786
satokdbf29502011-08-25 15:28:23 +09001787 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001788 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001789 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001790 if (!calledWithValidToken(token)) {
1791 final int uid = Binder.getCallingUid();
1792 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1793 + " token:" + token);
1794 return;
1795 }
1796
1797 synchronized (mMethodMap) {
1798 mImeWindowVis = vis;
1799 mBackDisposition = backDisposition;
1800 updateSystemUiLocked(token, vis, backDisposition);
1801 }
1802 }
1803
1804 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1805 synchronized (mMethodMap) {
1806 updateSystemUiLocked(token, vis, backDisposition);
1807 }
1808 }
1809
1810 // Caution! This method is called in this class. Handle multi-user carefully
1811 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1812 if (!calledWithValidToken(token)) {
1813 final int uid = Binder.getCallingUid();
1814 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1815 + " token:" + token);
1816 return;
1817 }
1818
1819 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1820 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001821 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001822 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001823 // apply policy for binder calls
1824 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1825 vis = 0;
satok06487a52010-10-29 11:37:18 +09001826 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001827 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1828 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1829 if (mStatusBar != null) {
1830 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1831 needsToShowImeSwitcher);
1832 }
1833 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1834 if (imi != null && needsToShowImeSwitcher) {
1835 // Used to load label
1836 final CharSequence title = mRes.getText(
1837 com.android.internal.R.string.select_input_method);
1838 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1839 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001840 mImeSwitcherNotification.setContentTitle(title)
1841 .setContentText(summary)
1842 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001843 try {
1844 if ((mNotificationManager != null)
1845 && !mIWindowManager.hasNavigationBar()) {
1846 if (DEBUG) {
1847 Slog.d(TAG, "--- show notification: label = " + summary);
1848 }
1849 mNotificationManager.notifyAsUser(null,
1850 com.android.internal.R.string.select_input_method,
1851 mImeSwitcherNotification.build(), UserHandle.ALL);
1852 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001853 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001854 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001855 }
1856 } else {
1857 if (mNotificationShown && mNotificationManager != null) {
1858 if (DEBUG) {
1859 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001860 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001861 mNotificationManager.cancelAsUser(null,
1862 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1863 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001864 }
satok06487a52010-10-29 11:37:18 +09001865 }
1866 } finally {
1867 Binder.restoreCallingIdentity(ident);
1868 }
1869 }
1870
satoke7c6998e2011-06-03 17:57:59 +09001871 @Override
satokf9f01002011-05-19 21:31:50 +09001872 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001873 if (!calledFromValidUser()) {
1874 return;
1875 }
satokf9f01002011-05-19 21:31:50 +09001876 synchronized (mMethodMap) {
1877 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1878 for (int i = 0; i < spans.length; ++i) {
1879 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001880 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001881 mSecureSuggestionSpans.put(ss, currentImi);
1882 }
1883 }
1884 }
1885 }
1886
satoke7c6998e2011-06-03 17:57:59 +09001887 @Override
satokf9f01002011-05-19 21:31:50 +09001888 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001889 if (!calledFromValidUser()) {
1890 return false;
1891 }
satokf9f01002011-05-19 21:31:50 +09001892 synchronized (mMethodMap) {
1893 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1894 // TODO: Do not send the intent if the process of the targetImi is already dead.
1895 if (targetImi != null) {
1896 final String[] suggestions = span.getSuggestions();
1897 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001898 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001899 final Intent intent = new Intent();
1900 // Ensures that only a class in the original IME package will receive the
1901 // notification.
satok42c5a162011-05-26 16:46:14 +09001902 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001903 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1904 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1905 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1906 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001907 final long ident = Binder.clearCallingIdentity();
1908 try {
1909 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1910 } finally {
1911 Binder.restoreCallingIdentity(ident);
1912 }
satokf9f01002011-05-19 21:31:50 +09001913 return true;
1914 }
1915 }
1916 return false;
1917 }
1918
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001919 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001920 updateInputMethodsFromSettingsLocked(enabledMayChange);
1921 updateKeyboardFromSettingsLocked();
1922 }
1923
1924 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001925 if (enabledMayChange) {
1926 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1927 for (int i=0; i<enabled.size(); i++) {
1928 // We allow the user to select "disabled until used" apps, so if they
1929 // are enabling one of those here we now need to make it enabled.
1930 InputMethodInfo imm = enabled.get(i);
1931 try {
1932 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1933 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1934 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001935 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001936 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001937 if (DEBUG) {
1938 Slog.d(TAG, "Update state(" + imm.getId()
1939 + "): DISABLED_UNTIL_USED -> DEFAULT");
1940 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001941 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1942 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001943 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1944 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001945 }
1946 } catch (RemoteException e) {
1947 }
1948 }
1949 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001950 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1951 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1952 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1953 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001954 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001955 // There is no input method selected, try to choose new applicable input method.
1956 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001957 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001958 }
1959 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001961 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001963 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001964 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
satokf3db1af2010-11-23 13:34:33 +09001966 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 } else {
1968 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001969 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001971 // Here is not the perfect place to reset the switching controller. Ideally
1972 // mSwitchingController and mSettings should be able to share the same state.
1973 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1974 // the same enabled IMEs list.
1975 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001976
1977 }
1978
1979 public void updateKeyboardFromSettingsLocked() {
1980 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1981 if (mSwitchingDialog != null
1982 && mSwitchingDialogTitleView != null
1983 && mSwitchingDialog.isShowing()) {
1984 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1985 com.android.internal.R.id.hard_keyboard_switch);
1986 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
Yohei Yukawab097b822015-12-01 10:43:08 -08001990 private void notifyInputMethodSubtypeChanged(final int userId,
1991 @Nullable final InputMethodInfo inputMethodInfo,
1992 @Nullable final InputMethodSubtype subtype) {
1993 final InputManagerInternal inputManagerInternal =
1994 LocalServices.getService(InputManagerInternal.class);
1995 if (inputManagerInternal != null) {
1996 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1997 }
1998 }
1999
satokab751aa2010-09-14 19:17:36 +09002000 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 InputMethodInfo info = mMethodMap.get(id);
2002 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002003 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
satokd81e9502012-05-21 12:58:45 +09002006 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002008 final int subtypeCount = info.getSubtypeCount();
2009 if (subtypeCount <= 0) {
2010 return;
satokcd7cd292010-11-20 15:46:23 +09002011 }
satokd81e9502012-05-21 12:58:45 +09002012 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2013 final InputMethodSubtype newSubtype;
2014 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2015 newSubtype = info.getSubtypeAt(subtypeId);
2016 } else {
2017 // If subtype is null, try to find the most applicable one from
2018 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002019 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002020 }
2021 if (newSubtype == null || oldSubtype == null) {
2022 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2023 + ", new subtype = " + newSubtype);
2024 return;
2025 }
2026 if (newSubtype != oldSubtype) {
2027 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2028 if (mCurMethod != null) {
2029 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002030 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002031 mCurMethod.changeInputMethodSubtype(newSubtype);
2032 } catch (RemoteException e) {
2033 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002034 return;
satokab751aa2010-09-14 19:17:36 +09002035 }
2036 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002037 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 return;
2040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041
satokd81e9502012-05-21 12:58:45 +09002042 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 final long ident = Binder.clearCallingIdentity();
2044 try {
satokab751aa2010-09-14 19:17:36 +09002045 // Set a subtype to this input method.
2046 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002047 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2048 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2049 // because mCurMethodId is stored as a history in
2050 // setSelectedInputMethodAndSubtypeLocked().
2051 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052
2053 if (ActivityManagerNative.isSystemReady()) {
2054 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002055 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002057 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002059 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 } finally {
2061 Binder.restoreCallingIdentity(ident);
2062 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002063
2064 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2065 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
satok42c5a162011-05-26 16:46:14 +09002068 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002069 public boolean showSoftInput(IInputMethodClient client, int flags,
2070 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002071 if (!calledFromValidUser()) {
2072 return false;
2073 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002074 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 long ident = Binder.clearCallingIdentity();
2076 try {
2077 synchronized (mMethodMap) {
2078 if (mCurClient == null || client == null
2079 || mCurClient.client.asBinder() != client.asBinder()) {
2080 try {
2081 // We need to check if this is the current client with
2082 // focus in the window manager, to allow this call to
2083 // be made before input is started in it.
2084 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002085 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002089 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002093 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 } finally {
2097 Binder.restoreCallingIdentity(ident);
2098 }
2099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002100
The Android Open Source Project4df24232009-03-05 14:34:35 -08002101 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002103 if (mAccessibilityRequestingNoSoftKeyboard) {
2104 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Anna Galusza9b278112016-01-04 11:37:37 -08002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2108 mShowExplicitlyRequested = true;
2109 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002110 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2111 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002113
Dianne Hackborncc278702009-09-02 23:07:23 -07002114 if (!mSystemReady) {
2115 return false;
2116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
The Android Open Source Project4df24232009-03-05 14:34:35 -08002118 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002120 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002121 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2122 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2123 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002125 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002126 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002127 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002128 | Context.BIND_TREAT_LIKE_ACTIVITY
2129 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002130 mVisibleBound = true;
2131 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002134 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 // The client has asked to have the input method shown, but
2136 // we have been sitting here too long with a connection to the
2137 // service and no interface received, so let's disconnect/connect
2138 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002141 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002144 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002145 } else {
2146 if (DEBUG) {
2147 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2148 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002151
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002154
satok42c5a162011-05-26 16:46:14 +09002155 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 public boolean hideSoftInput(IInputMethodClient client, int flags,
2157 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002158 if (!calledFromValidUser()) {
2159 return false;
2160 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002161 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 long ident = Binder.clearCallingIdentity();
2163 try {
2164 synchronized (mMethodMap) {
2165 if (mCurClient == null || client == null
2166 || mCurClient.client.asBinder() != client.asBinder()) {
2167 try {
2168 // We need to check if this is the current client with
2169 // focus in the window manager, to allow this call to
2170 // be made before input is started in it.
2171 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002172 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2173 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002174 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002177 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002180
Joe Onorato8a9b2202010-02-26 18:56:32 -08002181 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002182 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 } finally {
2185 Binder.restoreCallingIdentity(ident);
2186 }
2187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2191 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002192 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 -08002193 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002196 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 -08002197 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002199
2200 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2201 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2202 // to be updated with the new value sent from IME process. Even in such a transient state
2203 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2204 // application process as a valid request, and have even promised such a behavior with CTS
2205 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2206 // IMMS#InputShown indicates that the software keyboard is shown.
2207 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2208 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2209 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002210 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002211 if (shouldHideSoftInput) {
2212 // The IME will report its visible state again after the following message finally
2213 // delivered to the IME process as an IPC. Hence the inconsistency between
2214 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2215 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002216 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2217 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2218 res = true;
2219 } else {
2220 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002222 if (mHaveConnection && mVisibleBound) {
2223 mContext.unbindService(mVisibleConnection);
2224 mVisibleBound = false;
2225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 mInputShown = false;
2227 mShowRequested = false;
2228 mShowExplicitlyRequested = false;
2229 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002230 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002232
satok42c5a162011-05-26 16:46:14 +09002233 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002234 public InputBindResult startInputOrWindowGainedFocus(
2235 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2236 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002237 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002238 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002239 if (windowToken != null) {
2240 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002241 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002242 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002243 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2244 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002245 }
2246 }
2247
2248 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002249 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2250 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002251 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2252 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002253 // Needs to check the validity before clearing calling identity
2254 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002255 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 long ident = Binder.clearCallingIdentity();
2257 try {
2258 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002259 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2260 + InputMethodClient.getStartInputReason(startInputReason)
2261 + " client=" + client.asBinder()
2262 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002263 + " missingMethods="
2264 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002265 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002268 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002269
Dianne Hackborn7663d802012-02-24 13:08:49 -08002270 ClientState cs = mClients.get(client.asBinder());
2271 if (cs == null) {
2272 throw new IllegalArgumentException("unknown client "
2273 + client.asBinder());
2274 }
2275
2276 try {
2277 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2278 // Check with the window manager to make sure this client actually
2279 // has a window with focus. If not, reject. This is thread safe
2280 // because if the focus changes some time before or after, the
2281 // next client receiving focus that has any interest in input will
2282 // be calling through here after that change happens.
2283 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2284 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2285 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002287 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002290 if (!calledFromValidUser) {
2291 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2292 Slog.w(TAG, "If you want to interect with IME, you need "
2293 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2294 hideCurrentInputLocked(0, null);
2295 return null;
2296 }
2297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002298 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002299 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002300 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002302 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2303 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002304 }
2305 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002306 }
2307 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002308 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002309
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002310 // Should we auto-show the IME even if the caller has not
2311 // specified what should be done with it?
2312 // We only do this automatically if the window can resize
2313 // to accommodate the IME (so what the user sees will give
2314 // them good context without input information being obscured
2315 // by the IME) or if running on a large screen where there
2316 // is more room for the target window + IME.
2317 final boolean doAutoShow =
2318 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2319 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2320 || mRes.getConfiguration().isLayoutSizeAtLeast(
2321 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002322 final boolean isTextEditor =
2323 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2324
2325 // We want to start input before showing the IME, but after closing
2326 // it. We want to do this after closing it to help the IME disappear
2327 // more quickly (not get stuck behind it initializing itself for the
2328 // new focused input, even if its window wants to hide the IME).
2329 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2332 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002333 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2335 // There is no focus view, and this window will
2336 // be behind any soft input window, so hide the
2337 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002338 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002339 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002341 } else if (isTextEditor && doAutoShow && (softInputMode &
2342 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 // There is a focus view, and we are navigating forward
2344 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002345 // We only do this automatically if the window can resize
2346 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002347 // them good context without input information being obscured
2348 // by the IME) or if running on a large screen where there
2349 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002350 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002351 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002352 res = startInputUncheckedLocked(cs, inputContext,
2353 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002354 didStart = true;
2355 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002356 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
2358 break;
2359 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2360 // Do nothing.
2361 break;
2362 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2363 if ((softInputMode &
2364 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002366 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 break;
2373 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2374 if ((softInputMode &
2375 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002376 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002377 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002378 res = startInputUncheckedLocked(cs, inputContext,
2379 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 didStart = true;
2381 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002382 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
2384 break;
2385 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002386 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002388 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2389 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002390 didStart = true;
2391 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002392 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 break;
2394 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002395
2396 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002397 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002398 controlFlags);
2399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401 } finally {
2402 Binder.restoreCallingIdentity(ident);
2403 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002404
2405 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
satok42c5a162011-05-26 16:46:14 +09002408 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002409 public void showInputMethodPickerFromClient(
2410 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002411 if (!calledFromValidUser()) {
2412 return;
2413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 synchronized (mMethodMap) {
2415 if (mCurClient == null || client == null
2416 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002417 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002418 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 }
2420
satok440aab52010-11-25 09:43:11 +09002421 // Always call subtype picker, because subtype picker is a superset of input method
2422 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002423 mHandler.sendMessage(mCaller.obtainMessageI(
2424 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002425 }
2426 }
2427
satok42c5a162011-05-26 16:46:14 +09002428 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 if (!calledFromValidUser()) {
2431 return;
2432 }
satok28203512010-11-24 11:06:49 +09002433 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2434 }
2435
satok42c5a162011-05-26 16:46:14 +09002436 @Override
satok28203512010-11-24 11:06:49 +09002437 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002438 if (!calledFromValidUser()) {
2439 return;
2440 }
satok28203512010-11-24 11:06:49 +09002441 synchronized (mMethodMap) {
2442 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002443 setInputMethodWithSubtypeIdLocked(token, id,
2444 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2445 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002446 } else {
2447 setInputMethod(token, id);
2448 }
2449 }
satokab751aa2010-09-14 19:17:36 +09002450 }
2451
satok42c5a162011-05-26 16:46:14 +09002452 @Override
satokb416a712010-11-25 20:42:14 +09002453 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002454 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 if (!calledFromValidUser()) {
2456 return;
2457 }
satokb416a712010-11-25 20:42:14 +09002458 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002459 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2460 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002461 }
2462 }
2463
satok4fc87d62011-05-20 16:13:43 +09002464 @Override
satok735cf382010-11-11 20:40:09 +09002465 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002466 if (!calledFromValidUser()) {
2467 return false;
2468 }
satok735cf382010-11-11 20:40:09 +09002469 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002470 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002471 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002472 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002473 lastImi = mMethodMap.get(lastIme.first);
2474 } else {
2475 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002476 }
satok4fc87d62011-05-20 16:13:43 +09002477 String targetLastImiId = null;
2478 int subtypeId = NOT_A_SUBTYPE_ID;
2479 if (lastIme != null && lastImi != null) {
2480 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2481 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2482 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2483 : mCurrentSubtype.hashCode();
2484 // If the last IME is the same as the current IME and the last subtype is not
2485 // defined, there is no need to switch to the last IME.
2486 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2487 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002488 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002489 }
2490 }
2491
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002492 if (TextUtils.isEmpty(targetLastImiId)
2493 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002494 // This is a safety net. If the currentSubtype can't be added to the history
2495 // and the framework couldn't find the last ime, we will make the last ime be
2496 // the most applicable enabled keyboard subtype of the system imes.
2497 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2498 if (enabled != null) {
2499 final int N = enabled.size();
2500 final String locale = mCurrentSubtype == null
2501 ? mRes.getConfiguration().locale.toString()
2502 : mCurrentSubtype.getLocale();
2503 for (int i = 0; i < N; ++i) {
2504 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002505 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002506 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002507 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2508 InputMethodUtils.getSubtypes(imi),
2509 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002510 if (keyboardSubtype != null) {
2511 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002512 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002513 imi, keyboardSubtype.hashCode());
2514 if(keyboardSubtype.getLocale().equals(locale)) {
2515 break;
2516 }
2517 }
2518 }
2519 }
2520 }
2521 }
2522
2523 if (!TextUtils.isEmpty(targetLastImiId)) {
2524 if (DEBUG) {
2525 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2526 + ", from: " + mCurMethodId + ", " + subtypeId);
2527 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002528 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002529 return true;
2530 } else {
2531 return false;
2532 }
satok735cf382010-11-11 20:40:09 +09002533 }
2534 }
2535
satoke7c6998e2011-06-03 17:57:59 +09002536 @Override
satok688bd472012-02-09 20:09:17 +09002537 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002538 if (!calledFromValidUser()) {
2539 return false;
2540 }
satok688bd472012-02-09 20:09:17 +09002541 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002542 if (!calledWithValidToken(token)) {
2543 final int uid = Binder.getCallingUid();
2544 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2545 + " token:" + token);
2546 return false;
2547 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002548 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002549 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2550 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002551 if (nextSubtype == null) {
2552 return false;
2553 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002554 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2555 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002556 return true;
2557 }
2558 }
2559
2560 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002561 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2562 if (!calledFromValidUser()) {
2563 return false;
2564 }
2565 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002566 if (!calledWithValidToken(token)) {
2567 final int uid = Binder.getCallingUid();
2568 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2569 + "token. uid:" + uid + " token:" + token);
2570 return false;
2571 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002572 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002573 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2574 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002575 if (nextSubtype == null) {
2576 return false;
2577 }
2578 return true;
2579 }
2580 }
2581
2582 @Override
satok68f1b782011-04-11 14:26:04 +09002583 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002584 if (!calledFromValidUser()) {
2585 return null;
2586 }
satok68f1b782011-04-11 14:26:04 +09002587 synchronized (mMethodMap) {
2588 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2589 // TODO: Handle the case of the last IME with no subtypes
2590 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2591 || TextUtils.isEmpty(lastIme.second)) return null;
2592 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2593 if (lastImi == null) return null;
2594 try {
2595 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002596 final int lastSubtypeId =
2597 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002598 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2599 return null;
2600 }
2601 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002602 } catch (NumberFormatException e) {
2603 return null;
2604 }
2605 }
2606 }
2607
satoke7c6998e2011-06-03 17:57:59 +09002608 @Override
satokee5e77c2011-09-02 18:50:15 +09002609 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002610 if (!calledFromValidUser()) {
2611 return;
2612 }
satok91e88122011-07-18 11:11:42 +09002613 // By this IPC call, only a process which shares the same uid with the IME can add
2614 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002615 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002616 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002617 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002618 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002619 final String[] packageInfos;
2620 try {
2621 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2622 } catch (RemoteException e) {
2623 Slog.e(TAG, "Failed to get package infos");
2624 return;
2625 }
satok91e88122011-07-18 11:11:42 +09002626 if (packageInfos != null) {
2627 final int packageNum = packageInfos.length;
2628 for (int i = 0; i < packageNum; ++i) {
2629 if (packageInfos[i].equals(imi.getPackageName())) {
2630 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002631 final long ident = Binder.clearCallingIdentity();
2632 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002633 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002634 } finally {
2635 Binder.restoreCallingIdentity(ident);
2636 }
satokee5e77c2011-09-02 18:50:15 +09002637 return;
satok91e88122011-07-18 11:11:42 +09002638 }
2639 }
2640 }
satoke7c6998e2011-06-03 17:57:59 +09002641 }
satokee5e77c2011-09-02 18:50:15 +09002642 return;
satoke7c6998e2011-06-03 17:57:59 +09002643 }
2644
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002645 @Override
2646 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002647 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002648 }
2649
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002650 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002651 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002652 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002653 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002654 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002655 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002656 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2657 if (DEBUG) {
2658 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2659 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2660 + " actual: " + sequenceNumber);
2661 }
2662 return;
2663 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002664 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2665 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002666 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002667 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002668 }
2669 }
2670
satok28203512010-11-24 11:06:49 +09002671 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002673 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2674 }
2675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002677 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2678 if (token == null) {
2679 if (mContext.checkCallingOrSelfPermission(
2680 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2681 != PackageManager.PERMISSION_GRANTED) {
2682 throw new SecurityException(
2683 "Using null token requires permission "
2684 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002686 } else if (mCurToken != token) {
2687 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2688 + " token: " + token);
2689 return;
2690 }
2691
2692 final long ident = Binder.clearCallingIdentity();
2693 try {
2694 setInputMethodLocked(id, subtypeId);
2695 } finally {
2696 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 }
2698 }
2699
satok42c5a162011-05-26 16:46:14 +09002700 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002702 if (!calledFromValidUser()) {
2703 return;
2704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002706 if (!calledWithValidToken(token)) {
2707 final int uid = Binder.getCallingUid();
2708 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2709 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 return;
2711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 long ident = Binder.clearCallingIdentity();
2713 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002714 hideCurrentInputLocked(flags, null);
2715 } finally {
2716 Binder.restoreCallingIdentity(ident);
2717 }
2718 }
2719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002720
satok42c5a162011-05-26 16:46:14 +09002721 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002722 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002723 if (!calledFromValidUser()) {
2724 return;
2725 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002726 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002727 if (!calledWithValidToken(token)) {
2728 final int uid = Binder.getCallingUid();
2729 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2730 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002731 return;
2732 }
2733 long ident = Binder.clearCallingIdentity();
2734 try {
2735 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 } finally {
2737 Binder.restoreCallingIdentity(ident);
2738 }
2739 }
2740 }
2741
2742 void setEnabledSessionInMainThread(SessionState session) {
2743 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002744 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002746 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002747 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 } catch (RemoteException e) {
2749 }
2750 }
2751 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002752 if (mEnabledSession != null && mEnabledSession.session != null) {
2753 try {
2754 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2755 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2756 } catch (RemoteException e) {
2757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
2759 }
2760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002761
satok42c5a162011-05-26 16:46:14 +09002762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002764 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002766 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002767 final boolean showAuxSubtypes;
2768 switch (msg.arg1) {
2769 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2770 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2771 // implemented so that auxiliary subtypes will be excluded when the soft
2772 // keyboard is invisible.
2773 showAuxSubtypes = mInputShown;
2774 break;
2775 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2776 showAuxSubtypes = true;
2777 break;
2778 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2779 showAuxSubtypes = false;
2780 break;
2781 default:
2782 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2783 return false;
2784 }
2785 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002786 return true;
2787
satok47a44912010-10-06 16:03:58 +09002788 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002789 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002790 return true;
2791
2792 case MSG_SHOW_IM_CONFIG:
2793 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002794 return true;
2795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 case MSG_UNBIND_INPUT:
2799 try {
2800 ((IInputMethod)msg.obj).unbindInput();
2801 } catch (RemoteException e) {
2802 // There is nothing interesting about the method dying.
2803 }
2804 return true;
2805 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002806 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 try {
2808 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2809 } catch (RemoteException e) {
2810 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002811 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 return true;
2813 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002816 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002817 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002818 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 } catch (RemoteException e) {
2820 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002821 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 return true;
2823 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002824 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002826 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002827 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002828 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 } catch (RemoteException e) {
2830 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 return true;
2833 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002834 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002836 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2838 } catch (RemoteException e) {
2839 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002840 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002842 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002844 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002845 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002847 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002849 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002850 // Dispose the channel if the input method is not local to this process
2851 // because the remote proxy will get its own copy when unparceled.
2852 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002853 channel.dispose();
2854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002856 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002860
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002861 case MSG_START_INPUT: {
2862 int missingMethods = msg.arg1;
2863 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002865 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002867 session.method.startInput((IInputContext) args.arg2, missingMethods,
2868 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 } catch (RemoteException e) {
2870 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002871 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002873 }
2874 case MSG_RESTART_INPUT: {
2875 int missingMethods = msg.arg1;
2876 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002878 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002880 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2881 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 } catch (RemoteException e) {
2883 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002884 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002889
Yohei Yukawa33e81792015-11-17 21:14:42 -08002890 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002892 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 } catch (RemoteException e) {
2894 // There is nothing interesting about the last client dying.
2895 }
2896 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002897 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002898 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002899 IInputMethodClient client = (IInputMethodClient)args.arg1;
2900 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002902 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002904 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002905 } finally {
2906 // Dispose the channel if the input method is not local to this process
2907 // because the remote proxy will get its own copy when unparceled.
2908 if (res.channel != null && Binder.isProxy(client)) {
2909 res.channel.dispose();
2910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002912 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002914 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002915 case MSG_SET_ACTIVE:
2916 try {
2917 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2918 } catch (RemoteException e) {
2919 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2920 + ((ClientState)msg.obj).pid + " uid "
2921 + ((ClientState)msg.obj).uid);
2922 }
2923 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002924 case MSG_SET_INTERACTIVE:
2925 handleSetInteractive(msg.arg1 != 0);
2926 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002927 case MSG_SWITCH_IME:
2928 handleSwitchInputMethod(msg.arg1 != 0);
2929 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002930 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2931 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002932 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002933 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002934 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002935 } catch (RemoteException e) {
2936 Slog.w(TAG, "Got RemoteException sending "
2937 + "setUserActionNotificationSequenceNumber("
2938 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002939 + clientState.pid + " uid "
2940 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002941 }
2942 return true;
2943 }
satok01038492012-04-09 21:08:27 +09002944
2945 // --------------------------------------------------------------
2946 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002947 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002948 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
2950 return false;
2951 }
2952
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002953 private void handleSetInteractive(final boolean interactive) {
2954 synchronized (mMethodMap) {
2955 mIsInteractive = interactive;
2956 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2957
2958 // Inform the current client of the change in active status
2959 if (mCurClient != null && mCurClient.client != null) {
2960 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2961 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2962 }
2963 }
2964 }
2965
Yohei Yukawaae61f712015-12-09 13:00:10 -08002966 private void handleSwitchInputMethod(final boolean forwardDirection) {
2967 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002968 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002969 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002970 if (nextSubtype == null) {
2971 return;
2972 }
2973 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002974 if (mSubtypeSwitchedByShortCutToast != null) {
2975 mSubtypeSwitchedByShortCutToast.cancel();
2976 mSubtypeSwitchedByShortCutToast = null;
2977 }
2978 if ((mImeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
2979 // IME window is shown. The user should be able to visually understand that the
2980 // subtype is changed in most of cases. To avoid UI overlap, we do not show a toast
2981 // in this case.
2982 return;
2983 }
2984 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2985 if (newInputMethodInfo == null) {
2986 return;
2987 }
2988 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2989 newInputMethodInfo, mCurrentSubtype);
2990 if (!TextUtils.isEmpty(toastText)) {
2991 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText.toString(),
2992 Toast.LENGTH_SHORT);
2993 mSubtypeSwitchedByShortCutToast.show();
2994 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002995 }
2996 }
2997
satokdc9ddae2011-10-06 12:22:36 +09002998 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002999 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3000 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003001 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003002 if (DEBUG) {
3003 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3004 }
satok723a27e2010-11-11 14:58:11 +09003005 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003006 return true;
3007 }
3008
3009 return false;
3010 }
3011
Yohei Yukawa94e33302016-02-12 19:37:03 -08003012 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003013 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003014 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003015 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003016 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003017 mMethodList.clear();
3018 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003019
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 // Use for queryIntentServicesAsUser
3021 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003023 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3024 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3025 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003026 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003028 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3029 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003030
satoke7c6998e2011-06-03 17:57:59 +09003031 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3032 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 for (int i = 0; i < services.size(); ++i) {
3034 ResolveInfo ri = services.get(i);
3035 ServiceInfo si = ri.serviceInfo;
3036 ComponentName compName = new ComponentName(si.packageName, si.name);
3037 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3038 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003039 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 + ": it does not require the permission "
3041 + android.Manifest.permission.BIND_INPUT_METHOD);
3042 continue;
3043 }
3044
Joe Onorato8a9b2202010-02-26 18:56:32 -08003045 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
3047 try {
satoke7c6998e2011-06-03 17:57:59 +09003048 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003049 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003050 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003051 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052
3053 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003054 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003056 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003057 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003060
Yohei Yukawa859df052016-02-17 07:56:46 -08003061 // TODO: The following code should find better place to live.
3062 if (!resetDefaultEnabledIme) {
3063 boolean enabledImeFound = false;
3064 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3065 final int N = enabledImes.size();
3066 for (int i = 0; i < N; ++i) {
3067 final InputMethodInfo imi = enabledImes.get(i);
3068 if (mMethodList.contains(imi)) {
3069 enabledImeFound = true;
3070 break;
3071 }
3072 }
3073 if (!enabledImeFound) {
3074 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3075 resetDefaultEnabledIme = true;
3076 resetSelectedInputMethodAndSubtypeLocked("");
3077 }
3078 }
3079
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003080 if (resetDefaultEnabledIme) {
3081 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003082 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003083 final int N = defaultEnabledIme.size();
3084 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003085 final InputMethodInfo imi = defaultEnabledIme.get(i);
3086 if (DEBUG) {
3087 Slog.d(TAG, "--- enable ime = " + imi);
3088 }
3089 setInputMethodEnabledLocked(imi.getId(), true);
3090 }
3091 }
3092
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003094 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003095 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003096 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3097 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003098 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003099 }
3100 } else {
3101 // Double check that the default IME is certainly enabled.
3102 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003103 }
3104 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003105 // Here is not the perfect place to reset the switching controller. Ideally
3106 // mSwitchingController and mSettings should be able to share the same state.
3107 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3108 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003109 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003113
satok217f5482010-12-15 05:19:19 +09003114 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003115 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003116 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003117 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3118 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003119 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003120 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003121 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003122 final int userId;
3123 synchronized (mMethodMap) {
3124 userId = mSettings.getCurrentUserId();
3125 }
3126 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003127 }
3128
3129 private void showConfigureInputMethods() {
3130 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3131 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3132 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3133 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003134 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003135 }
3136
satok2c93efc2012-04-02 19:33:47 +09003137 private boolean isScreenLocked() {
3138 return mKeyguardManager != null
3139 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3140 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003141
Seigo Nonaka14e13912015-05-06 21:04:13 -07003142 private void showInputMethodMenu(boolean showAuxSubtypes) {
3143 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003146 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003147
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003148 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003149 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003150 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003151
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003152 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003153 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003154 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3155 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003156 if (immis == null || immis.size() == 0) {
3157 return;
3158 }
3159
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003160 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161
satok688bd472012-02-09 20:09:17 +09003162 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003163 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003164 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003165
satokc3690562012-01-10 20:14:43 +09003166 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003167 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003168 if (currentSubtype != null) {
3169 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003170 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3171 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003172 }
3173 }
3174
Ken Wakasa761eb372011-03-04 19:06:18 +09003175 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003176 mIms = new InputMethodInfo[N];
3177 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003178 int checkedItem = 0;
3179 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003180 final ImeSubtypeListItem item = imList.get(i);
3181 mIms[i] = item.mImi;
3182 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003183 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003184 int subtypeId = mSubtypeIds[i];
3185 if ((subtypeId == NOT_A_SUBTYPE_ID)
3186 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3187 || (subtypeId == lastInputMethodSubtypeId)) {
3188 checkedItem = i;
3189 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003192
3193 final Context settingsContext = new ContextThemeWrapper(context,
3194 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3195
3196 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003197 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3198 @Override
3199 public void onCancel(DialogInterface dialog) {
3200 hideInputMethodMenu();
3201 }
3202 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003203
3204 final Context dialogContext = mDialogBuilder.getContext();
3205 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3206 com.android.internal.R.styleable.DialogPreference,
3207 com.android.internal.R.attr.alertDialogStyle, 0);
3208 final Drawable dialogIcon = a.getDrawable(
3209 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3210 a.recycle();
3211
3212 mDialogBuilder.setIcon(dialogIcon);
3213
Yohei Yukawad34e1482016-02-11 08:03:52 -08003214 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003215 final View tv = inflater.inflate(
3216 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3217 mDialogBuilder.setCustomTitle(tv);
3218
3219 // Setup layout for a toggle switch of the hardware keyboard
3220 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003221 mSwitchingDialogTitleView
3222 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003223 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003224 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003225 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003227 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003228 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3229 @Override
3230 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003231 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003232 // Ensure that the input method dialog is dismissed when changing
3233 // the hardware keyboard state.
3234 hideInputMethodMenu();
3235 }
3236 });
3237
Alan Viverette505e3ab2014-11-24 15:22:11 -08003238 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003239 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3240 final OnClickListener choiceListener = new OnClickListener() {
3241 @Override
3242 public void onClick(final DialogInterface dialog, final int which) {
3243 synchronized (mMethodMap) {
3244 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3245 || mSubtypeIds.length <= which) {
3246 return;
satok01038492012-04-09 21:08:27 +09003247 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003248 final InputMethodInfo im = mIms[which];
3249 int subtypeId = mSubtypeIds[which];
3250 adapter.mCheckedItem = which;
3251 adapter.notifyDataSetChanged();
3252 hideInputMethodMenu();
3253 if (im != null) {
3254 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3255 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003256 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003259 }
3260 }
3261 };
3262 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003265 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 mSwitchingDialog.getWindow().setType(
3267 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003268 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3269 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003270 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003271 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 mSwitchingDialog.show();
3273 }
3274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003275
Ken Wakasa05dbb652011-08-22 15:22:43 +09003276 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3277 private final LayoutInflater mInflater;
3278 private final int mTextViewResourceId;
3279 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003280 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003281 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3282 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3283 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003284
Ken Wakasa05dbb652011-08-22 15:22:43 +09003285 mTextViewResourceId = textViewResourceId;
3286 mItemsList = itemsList;
3287 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003288 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003289 }
3290
3291 @Override
3292 public View getView(int position, View convertView, ViewGroup parent) {
3293 final View view = convertView != null ? convertView
3294 : mInflater.inflate(mTextViewResourceId, null);
3295 if (position < 0 || position >= mItemsList.size()) return view;
3296 final ImeSubtypeListItem item = mItemsList.get(position);
3297 final CharSequence imeName = item.mImeName;
3298 final CharSequence subtypeName = item.mSubtypeName;
3299 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3300 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3301 if (TextUtils.isEmpty(subtypeName)) {
3302 firstTextView.setText(imeName);
3303 secondTextView.setVisibility(View.GONE);
3304 } else {
3305 firstTextView.setText(subtypeName);
3306 secondTextView.setText(imeName);
3307 secondTextView.setVisibility(View.VISIBLE);
3308 }
3309 final RadioButton radioButton =
3310 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3311 radioButton.setChecked(position == mCheckedItem);
3312 return view;
3313 }
3314 }
3315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003317 synchronized (mMethodMap) {
3318 hideInputMethodMenuLocked();
3319 }
3320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003321
The Android Open Source Project10592532009-03-18 17:39:46 -07003322 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003323 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324
The Android Open Source Project10592532009-03-18 17:39:46 -07003325 if (mSwitchingDialog != null) {
3326 mSwitchingDialog.dismiss();
3327 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003329
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003330 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003331 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003332 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003336
satok42c5a162011-05-26 16:46:14 +09003337 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003339 // TODO: Make this work even for non-current users?
3340 if (!calledFromValidUser()) {
3341 return false;
3342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 synchronized (mMethodMap) {
3344 if (mContext.checkCallingOrSelfPermission(
3345 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3346 != PackageManager.PERMISSION_GRANTED) {
3347 throw new SecurityException(
3348 "Requires permission "
3349 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3350 }
Anna Galusza9b278112016-01-04 11:37:37 -08003351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 long ident = Binder.clearCallingIdentity();
3353 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003354 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 } finally {
3356 Binder.restoreCallingIdentity(ident);
3357 }
3358 }
3359 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003360
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003361 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3362 // Make sure this is a valid input method.
3363 InputMethodInfo imm = mMethodMap.get(id);
3364 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003365 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003366 }
3367
satokd87c2592010-09-29 11:52:06 +09003368 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3369 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003370
satokd87c2592010-09-29 11:52:06 +09003371 if (enabled) {
3372 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3373 if (pair.first.equals(id)) {
3374 // We are enabling this input method, but it is already enabled.
3375 // Nothing to do. The previous state was enabled.
3376 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003377 }
3378 }
satokd87c2592010-09-29 11:52:06 +09003379 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3380 // Previous state was disabled.
3381 return false;
3382 } else {
3383 StringBuilder builder = new StringBuilder();
3384 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3385 builder, enabledInputMethodsList, id)) {
3386 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003387 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003388 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3389 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3390 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003391 }
3392 // Previous state was enabled.
3393 return true;
3394 } else {
3395 // We are disabling the input method but it is already disabled.
3396 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003397 return false;
3398 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003399 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003400 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003401
satok723a27e2010-11-11 14:58:11 +09003402 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3403 boolean setSubtypeOnly) {
3404 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003405 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003406
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003407 mCurUserActionNotificationSequenceNumber =
3408 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3409 if (DEBUG) {
3410 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3411 + mCurUserActionNotificationSequenceNumber);
3412 }
3413
3414 if (mCurClient != null && mCurClient.client != null) {
3415 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3416 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003417 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003418 }
3419
satok723a27e2010-11-11 14:58:11 +09003420 // Set Subtype here
3421 if (imi == null || subtypeId < 0) {
3422 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003423 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003424 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003425 if (subtypeId < imi.getSubtypeCount()) {
3426 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3427 mSettings.putSelectedSubtype(subtype.hashCode());
3428 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003429 } else {
3430 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003431 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003432 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003433 }
satokab751aa2010-09-14 19:17:36 +09003434 }
satok723a27e2010-11-11 14:58:11 +09003435
Yohei Yukawa68645a62016-02-17 07:54:20 -08003436 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003437 // Set InputMethod here
3438 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3439 }
3440 }
3441
3442 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3443 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3444 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3445 // newDefaultIme is empty when there is no candidate for the selected IME.
3446 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3447 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3448 if (subtypeHashCode != null) {
3449 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003450 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003451 imi, Integer.valueOf(subtypeHashCode));
3452 } catch (NumberFormatException e) {
3453 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3454 }
3455 }
3456 }
3457 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003458 }
3459
satok4e4569d2010-11-19 18:45:53 +09003460 // If there are no selected shortcuts, tries finding the most applicable ones.
3461 private Pair<InputMethodInfo, InputMethodSubtype>
3462 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3463 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3464 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003465 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003466 boolean foundInSystemIME = false;
3467
3468 // Search applicable subtype for each InputMethodInfo
3469 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003470 final String imiId = imi.getId();
3471 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3472 continue;
3473 }
satokcd7cd292010-11-20 15:46:23 +09003474 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003475 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003476 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003477 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003478 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003480 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003481 }
satokdf31ae62011-01-15 06:19:44 +09003482 // 2. Search by the system locale from enabledSubtypes.
3483 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003484 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003485 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003486 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003487 }
satoka86f5e42011-09-02 17:12:42 +09003488 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003489 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003490 final ArrayList<InputMethodSubtype> subtypesForSearch =
3491 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003492 ? InputMethodUtils.getSubtypes(imi)
3493 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003494 // 4. Search by the current subtype's locale from all subtypes.
3495 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003496 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003497 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003498 }
3499 // 5. Search by the system locale from all subtypes.
3500 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003501 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003502 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003503 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003504 }
satokcd7cd292010-11-20 15:46:23 +09003505 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003506 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003507 // The current input method is the most applicable IME.
3508 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003509 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003510 break;
satok7599a7f2010-12-22 13:45:23 +09003511 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003512 // The system input method is 2nd applicable IME.
3513 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003514 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003515 if ((imi.getServiceInfo().applicationInfo.flags
3516 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3517 foundInSystemIME = true;
3518 }
satok4e4569d2010-11-19 18:45:53 +09003519 }
3520 }
3521 }
3522 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003523 if (mostApplicableIMI != null) {
3524 Slog.w(TAG, "Most applicable shortcut input method was:"
3525 + mostApplicableIMI.getId());
3526 if (mostApplicableSubtype != null) {
3527 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3528 + "," + mostApplicableSubtype.getMode() + ","
3529 + mostApplicableSubtype.getLocale());
3530 }
3531 }
satok4e4569d2010-11-19 18:45:53 +09003532 }
satokcd7cd292010-11-20 15:46:23 +09003533 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003534 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003535 } else {
3536 return null;
3537 }
3538 }
3539
satokab751aa2010-09-14 19:17:36 +09003540 /**
3541 * @return Return the current subtype of this input method.
3542 */
satok42c5a162011-05-26 16:46:14 +09003543 @Override
satokab751aa2010-09-14 19:17:36 +09003544 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003545 // TODO: Make this work even for non-current users?
3546 if (!calledFromValidUser()) {
3547 return null;
3548 }
3549 synchronized (mMethodMap) {
3550 return getCurrentInputMethodSubtypeLocked();
3551 }
3552 }
3553
3554 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003555 if (mCurMethodId == null) {
3556 return null;
3557 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003558 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003559 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3560 if (imi == null || imi.getSubtypeCount() == 0) {
3561 return null;
satok4e4569d2010-11-19 18:45:53 +09003562 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003563 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003564 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3565 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003566 if (subtypeId == NOT_A_SUBTYPE_ID) {
3567 // If there are no selected subtypes, the framework will try to find
3568 // the most applicable subtype from explicitly or implicitly enabled
3569 // subtypes.
3570 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003571 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003572 // If there is only one explicitly or implicitly enabled subtype,
3573 // just returns it.
3574 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3575 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3576 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003577 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003578 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003579 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003580 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003581 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003582 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3583 true);
satok4e4569d2010-11-19 18:45:53 +09003584 }
satok3ef8b292010-11-23 06:06:29 +09003585 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003586 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003587 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003588 }
3589 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003590 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003591 }
3592
satok4e4569d2010-11-19 18:45:53 +09003593 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003594 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003595 @Override
satok4e4569d2010-11-19 18:45:53 +09003596 public List getShortcutInputMethodsAndSubtypes() {
3597 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003598 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003599 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003600 // If there are no selected shortcut subtypes, the framework will try to find
3601 // the most applicable subtype from all subtypes whose mode is
3602 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003603 Pair<InputMethodInfo, InputMethodSubtype> info =
3604 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003605 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003606 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003607 ret.add(info.first);
3608 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003609 }
satok3da92232011-01-11 22:46:30 +09003610 return ret;
satokf3db1af2010-11-23 13:34:33 +09003611 }
satokf3db1af2010-11-23 13:34:33 +09003612 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3613 ret.add(imi);
3614 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3615 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003616 }
3617 }
satokf3db1af2010-11-23 13:34:33 +09003618 return ret;
satok4e4569d2010-11-19 18:45:53 +09003619 }
3620 }
3621
satok42c5a162011-05-26 16:46:14 +09003622 @Override
satokb66d2872010-11-10 01:04:04 +09003623 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003624 // TODO: Make this work even for non-current users?
3625 if (!calledFromValidUser()) {
3626 return false;
3627 }
satokb66d2872010-11-10 01:04:04 +09003628 synchronized (mMethodMap) {
3629 if (subtype != null && mCurMethodId != null) {
3630 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003631 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003632 if (subtypeId != NOT_A_SUBTYPE_ID) {
3633 setInputMethodLocked(mCurMethodId, subtypeId);
3634 return true;
3635 }
3636 }
3637 return false;
3638 }
3639 }
3640
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003641 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003642 private static class InputMethodFileManager {
3643 private static final String SYSTEM_PATH = "system";
3644 private static final String INPUT_METHOD_PATH = "inputmethod";
3645 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3646 private static final String NODE_SUBTYPES = "subtypes";
3647 private static final String NODE_SUBTYPE = "subtype";
3648 private static final String NODE_IMI = "imi";
3649 private static final String ATTR_ID = "id";
3650 private static final String ATTR_LABEL = "label";
3651 private static final String ATTR_ICON = "icon";
3652 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003653 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003654 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3655 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3656 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003657 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003658 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3659 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003660 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003661 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003662 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003663 if (methodMap == null) {
3664 throw new NullPointerException("methodMap is null");
3665 }
3666 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003667 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003668 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3669 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003670 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003671 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003672 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3673 }
3674 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3675 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3676 if (!subtypeFile.exists()) {
3677 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003678 writeAdditionalInputMethodSubtypes(
3679 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003680 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003681 readAdditionalInputMethodSubtypes(
3682 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003683 }
3684 }
3685
3686 private void deleteAllInputMethodSubtypes(String imiId) {
3687 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003688 mAdditionalSubtypesMap.remove(imiId);
3689 writeAdditionalInputMethodSubtypes(
3690 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003691 }
3692 }
3693
3694 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003695 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003696 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003697 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003698 final int N = additionalSubtypes.length;
3699 for (int i = 0; i < N; ++i) {
3700 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003701 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003702 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003703 } else {
3704 Slog.w(TAG, "Duplicated subtype definition found: "
3705 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003706 }
3707 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003708 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3709 writeAdditionalInputMethodSubtypes(
3710 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003711 }
3712 }
3713
3714 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3715 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003716 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003717 }
3718 }
3719
3720 private static void writeAdditionalInputMethodSubtypes(
3721 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3722 HashMap<String, InputMethodInfo> methodMap) {
3723 // Safety net for the case that this function is called before methodMap is set.
3724 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3725 FileOutputStream fos = null;
3726 try {
3727 fos = subtypesFile.startWrite();
3728 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003729 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003730 out.startDocument(null, true);
3731 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3732 out.startTag(null, NODE_SUBTYPES);
3733 for (String imiId : allSubtypes.keySet()) {
3734 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3735 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3736 continue;
3737 }
3738 out.startTag(null, NODE_IMI);
3739 out.attribute(null, ATTR_ID, imiId);
3740 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3741 final int N = subtypesList.size();
3742 for (int i = 0; i < N; ++i) {
3743 final InputMethodSubtype subtype = subtypesList.get(i);
3744 out.startTag(null, NODE_SUBTYPE);
3745 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3746 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3747 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003748 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3749 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003750 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3751 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3752 out.attribute(null, ATTR_IS_AUXILIARY,
3753 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003754 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3755 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003756 out.endTag(null, NODE_SUBTYPE);
3757 }
3758 out.endTag(null, NODE_IMI);
3759 }
3760 out.endTag(null, NODE_SUBTYPES);
3761 out.endDocument();
3762 subtypesFile.finishWrite(fos);
3763 } catch (java.io.IOException e) {
3764 Slog.w(TAG, "Error writing subtypes", e);
3765 if (fos != null) {
3766 subtypesFile.failWrite(fos);
3767 }
3768 }
3769 }
3770
3771 private static void readAdditionalInputMethodSubtypes(
3772 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3773 if (allSubtypes == null || subtypesFile == null) return;
3774 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003775 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003776 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003777 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003778 int type = parser.getEventType();
3779 // Skip parsing until START_TAG
3780 while ((type = parser.next()) != XmlPullParser.START_TAG
3781 && type != XmlPullParser.END_DOCUMENT) {}
3782 String firstNodeName = parser.getName();
3783 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3784 throw new XmlPullParserException("Xml doesn't start with subtypes");
3785 }
3786 final int depth =parser.getDepth();
3787 String currentImiId = null;
3788 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3789 while (((type = parser.next()) != XmlPullParser.END_TAG
3790 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3791 if (type != XmlPullParser.START_TAG)
3792 continue;
3793 final String nodeName = parser.getName();
3794 if (NODE_IMI.equals(nodeName)) {
3795 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3796 if (TextUtils.isEmpty(currentImiId)) {
3797 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3798 continue;
3799 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003800 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003801 allSubtypes.put(currentImiId, tempSubtypesArray);
3802 } else if (NODE_SUBTYPE.equals(nodeName)) {
3803 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3804 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3805 continue;
3806 }
3807 final int icon = Integer.valueOf(
3808 parser.getAttributeValue(null, ATTR_ICON));
3809 final int label = Integer.valueOf(
3810 parser.getAttributeValue(null, ATTR_LABEL));
3811 final String imeSubtypeLocale =
3812 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003813 final String languageTag =
3814 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003815 final String imeSubtypeMode =
3816 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3817 final String imeSubtypeExtraValue =
3818 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003819 final boolean isAuxiliary = "1".equals(String.valueOf(
3820 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003821 final boolean isAsciiCapable = "1".equals(String.valueOf(
3822 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003823 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3824 .setSubtypeNameResId(label)
3825 .setSubtypeIconResId(icon)
3826 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003827 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003828 .setSubtypeMode(imeSubtypeMode)
3829 .setSubtypeExtraValue(imeSubtypeExtraValue)
3830 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003831 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003832 .build();
satoke7c6998e2011-06-03 17:57:59 +09003833 tempSubtypesArray.add(subtype);
3834 }
3835 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003836 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3837 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003838 return;
satoke7c6998e2011-06-03 17:57:59 +09003839 }
3840 }
3841 }
3842
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003843 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3844 @NonNull
3845 private final Handler mHandler;
3846
3847 LocalServiceImpl(@NonNull final Handler handler) {
3848 mHandler = handler;
3849 }
3850
3851 @Override
3852 public void setInteractive(boolean interactive) {
3853 // Do everything in handler so as not to block the caller.
3854 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3855 interactive ? 1 : 0, 0));
3856 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003857
3858 @Override
3859 public void switchInputMethod(boolean forwardDirection) {
3860 // Do everything in handler so as not to block the caller.
3861 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3862 forwardDirection ? 1 : 0, 0));
3863 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003864 }
3865
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003866 private static String imeWindowStatusToString(final int imeWindowVis) {
3867 final StringBuilder sb = new StringBuilder();
3868 boolean first = true;
3869 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3870 sb.append("Active");
3871 first = false;
3872 }
3873 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3874 if (!first) {
3875 sb.append("|");
3876 }
3877 sb.append("Visible");
3878 }
3879 return sb.toString();
3880 }
3881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 @Override
3883 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3884 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3885 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3888 + Binder.getCallingPid()
3889 + ", uid=" + Binder.getCallingUid());
3890 return;
3891 }
3892
3893 IInputMethod method;
3894 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003895 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 synchronized (mMethodMap) {
3900 p.println("Current Input Method Manager state:");
3901 int N = mMethodList.size();
3902 p.println(" Input Methods:");
3903 for (int i=0; i<N; i++) {
3904 InputMethodInfo info = mMethodList.get(i);
3905 p.println(" InputMethod #" + i + ":");
3906 info.dump(p, " ");
3907 }
3908 p.println(" Clients:");
3909 for (ClientState ci : mClients.values()) {
3910 p.println(" Client " + ci + ":");
3911 p.println(" client=" + ci.client);
3912 p.println(" inputContext=" + ci.inputContext);
3913 p.println(" sessionRequested=" + ci.sessionRequested);
3914 p.println(" curSession=" + ci.curSession);
3915 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003916 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003918 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3919 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003920 focusedWindowClient = mCurFocusedWindowClient;
3921 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3923 + " mBoundToMethod=" + mBoundToMethod);
3924 p.println(" mCurToken=" + mCurToken);
3925 p.println(" mCurIntent=" + mCurIntent);
3926 method = mCurMethod;
3927 p.println(" mCurMethod=" + mCurMethod);
3928 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003929 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 p.println(" mShowRequested=" + mShowRequested
3931 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3932 + " mShowForced=" + mShowForced
3933 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003934 p.println(" mCurUserActionNotificationSequenceNumber="
3935 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003936 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003937 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003938 p.println(" mSwitchingController:");
3939 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003940 p.println(" mSettings:");
3941 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003943
Jeff Brownb88102f2010-09-08 11:49:43 -07003944 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 pw.flush();
3947 try {
3948 client.client.asBinder().dump(fd, args);
3949 } catch (RemoteException e) {
3950 p.println("Input method client dead: " + e);
3951 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003952 } else {
3953 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003955
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003956 if (focusedWindowClient != null && client != focusedWindowClient) {
3957 p.println(" ");
3958 p.println("Warning: Current input method client doesn't match the last focused. "
3959 + "window.");
3960 p.println("Dumping input method client in the last focused window just in case.");
3961 p.println(" ");
3962 pw.flush();
3963 try {
3964 focusedWindowClient.client.asBinder().dump(fd, args);
3965 } catch (RemoteException e) {
3966 p.println("Input method client in focused window dead: " + e);
3967 }
3968 }
3969
Jeff Brownb88102f2010-09-08 11:49:43 -07003970 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 pw.flush();
3973 try {
3974 method.asBinder().dump(fd, args);
3975 } catch (RemoteException e) {
3976 p.println("Input method service dead: " + e);
3977 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003978 } else {
3979 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 }
3981 }
3982}