blob: da65daefcbb6d96a2c51485998ba4dd7b1eb72a1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070018import static java.lang.annotation.RetentionPolicy.SOURCE;
19
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputMethodClient;
31import com.android.internal.view.IInputMethodManager;
32import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070033import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080035import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080036import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090040import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070042import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070044import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080045import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.app.ActivityManagerNative;
47import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070048import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090049import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090050import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090051import android.app.Notification;
52import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070053import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090054import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.ComponentName;
56import android.content.ContentResolver;
57import android.content.Context;
58import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090060import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090062import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070064import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090065import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.pm.PackageManager;
67import android.content.pm.ResolveInfo;
68import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070069import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.res.Resources;
71import android.content.res.TypedArray;
72import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080073import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080074import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080075import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070076import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040078import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070079import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090080import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Handler;
82import android.os.IBinder;
83import android.os.IInterface;
84import android.os.Message;
85import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070086import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080088import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.ServiceManager;
90import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090091import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080092import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.provider.Settings;
94import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090095import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070096import android.util.ArrayMap;
97import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070098import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.EventLog;
Yohei Yukawae985c242016-02-24 18:27:04 -0800100import android.util.LocaleList;
satokf9f01002011-05-19 21:31:50 +0900101import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900102import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.util.PrintWriterPrinter;
104import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900105import android.util.Slog;
106import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900107import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700109import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900110import android.view.LayoutInflater;
111import android.view.View;
112import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700114import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900115import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700117import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.inputmethod.InputMethod;
119import android.view.inputmethod.InputMethodInfo;
120import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700121import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900122import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900123import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900125import android.widget.CompoundButton;
126import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900128import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900129import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700130import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
satoke7c6998e2011-06-03 17:57:59 +0900132import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900134import java.io.FileInputStream;
135import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.io.IOException;
137import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700138import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100139import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900141import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.util.HashMap;
143import java.util.List;
144
145/**
146 * This class provides a system service that manages input methods.
147 */
148public class InputMethodManagerService extends IInputMethodManager.Stub
149 implements ServiceConnection, Handler.Callback {
150 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700151 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700152 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700154 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
155 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
156 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 static final int MSG_UNBIND_INPUT = 1000;
159 static final int MSG_BIND_INPUT = 1010;
160 static final int MSG_SHOW_SOFT_INPUT = 1020;
161 static final int MSG_HIDE_SOFT_INPUT = 1030;
162 static final int MSG_ATTACH_TOKEN = 1040;
163 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 static final int MSG_START_INPUT = 2000;
166 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
Yohei Yukawa33e81792015-11-17 21:14:42 -0800168 static final int MSG_UNBIND_CLIENT = 3000;
169 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700170 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700171 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900172 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800173 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satok01038492012-04-09 21:08:27 +0900175 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
176
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700177 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
satokf9f01002011-05-19 21:31:50 +0900179 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
180
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900181 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900182 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900183
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700184 @Retention(SOURCE)
185 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
186 private @interface HardKeyboardBehavior {
187 int WIRELESS_AFFORDANCE = 0;
188 int WIRED_AFFORDANCE = 1;
189 }
satok4e4569d2010-11-19 18:45:53 +0900190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800192 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900194 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700197 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700199 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900200 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900201 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900202 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800203 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900205 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 // All known input methods. mMethodMap also serves as the global
208 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700209 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
210 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900211 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700212 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900213 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700215 // Used to bring IME service up to visible adjustment while it is being shown.
216 final ServiceConnection mVisibleConnection = new ServiceConnection() {
217 @Override public void onServiceConnected(ComponentName name, IBinder service) {
218 }
219
220 @Override public void onServiceDisconnected(ComponentName name) {
221 }
222 };
223 boolean mVisibleBound = false;
224
satok7cfc0ed2011-06-20 21:29:36 +0900225 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700226 private NotificationManager mNotificationManager;
227 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700228 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400229 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700230 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900231 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900232 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900233 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900234
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900235 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final ClientState client;
237 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700238
239 IInputMethodSession session;
240 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public String toString() {
244 return "SessionState{uid " + client.uid + " pid " + client.pid
245 + " method " + Integer.toHexString(
246 System.identityHashCode(method))
247 + " session " + Integer.toHexString(
248 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 + "}";
251 }
252
253 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700254 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 client = _client;
256 method = _method;
257 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700258 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
Jeff Brownc28867a2013-03-26 15:42:39 -0700262 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 final IInputMethodClient client;
264 final IInputContext inputContext;
265 final int uid;
266 final int pid;
267 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 boolean sessionRequested;
270 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 @Override
273 public String toString() {
274 return "ClientState{" + Integer.toHexString(
275 System.identityHashCode(this)) + " uid " + uid
276 + " pid " + pid + "}";
277 }
278
279 ClientState(IInputMethodClient _client, IInputContext _inputContext,
280 int _uid, int _pid) {
281 client = _client;
282 inputContext = _inputContext;
283 uid = _uid;
284 pid = _pid;
285 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
286 }
287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700289 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700292 * Set once the system is ready to run third party code.
293 */
294 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700296 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700297 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
298 * method. This is to be synchronized with the secure settings keyed with
299 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
300 *
301 * <p>This can be transiently {@code null} when the system is re-initializing input method
302 * settings, e.g., the system locale is just changed.</p>
303 *
304 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
305 * {@link InputMethodManagerService}.</p>
306 *
307 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700309 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * The current binding sequence number, incremented every time there is
314 * a new bind performed.
315 */
316 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * The client that is currently bound to an input method.
320 */
321 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800324 * The last window token that we confirmed to be focused. This is always updated upon reports
325 * from the input method client. If the window state is already changed before the report is
326 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700327 */
328 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700330 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800331 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
332 */
333 ClientState mCurFocusedWindowClient;
334
335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * The input context last provided by the current client.
337 */
338 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700341 * The missing method flags for the input context last provided by the current client.
342 *
343 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
344 */
345 int mCurInputContextMissingMethods;
346
347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 * The attributes last provided by the current client.
349 */
350 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700353 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700355 *
356 * <p>This can be {@code null} when no input method is connected.</p>
357 *
358 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700360 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
satokab751aa2010-09-14 19:17:36 +0900364 * The current subtype of the current input method.
365 */
366 private InputMethodSubtype mCurrentSubtype;
367
satok4e4569d2010-11-19 18:45:53 +0900368 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900369 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700370 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900371
John Spurlocke0980502013-10-25 11:59:29 -0400372 // Was the keyguard locked when this client became current?
373 private boolean mCurClientInKeyguard;
374
satokab751aa2010-09-14 19:17:36 +0900375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * Set to true if our ServiceConnection is currently actively bound to
377 * a service (whether or not we have gotten its IBinder back yet).
378 */
379 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Set if the client has asked for the input method to be shown.
383 */
384 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Set if we were explicitly told to show the input method.
388 */
389 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * Set if we were forced to be shown.
393 */
394 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * Set if we last told the input method to show itself.
398 */
399 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 /**
402 * The Intent used to connect to the current input method.
403 */
404 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * The token we have made for the currently active input method, to
408 * identify it in the future.
409 */
410 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * If non-null, this is the input method service we are currently connected
414 * to.
415 */
416 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * Time that we last initiated a bind to the input method, to determine
420 * if we should try to disconnect and reconnect to it.
421 */
422 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Have we called mCurMethod.bindInput()?
426 */
427 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
430 * Currently enabled session. Only touched by service thread, not
431 * protected by a lock.
432 */
433 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700436 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700438 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900440 int mCurUserActionNotificationSequenceNumber = 0;
441
Joe Onorato857fd9b2011-01-27 15:08:35 -0800442 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900443
444 /**
445 * A set of status bits regarding the active IME.
446 *
447 * <p>This value is a combination of following two bits:</p>
448 * <dl>
449 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
450 * <dd>
451 * If this bit is ON, connected IME is ready to accept touch/key events.
452 * </dd>
453 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
454 * <dd>
455 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
456 * </dd>
457 * </dl>
458 * <em>Do not update this value outside of setImeWindowStatus.</em>
459 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800460 int mImeWindowVis;
461
Ken Wakasa05dbb652011-08-22 15:22:43 +0900462 private AlertDialog.Builder mDialogBuilder;
463 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900464 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700465 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900466 private InputMethodInfo[] mIms;
467 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800468 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700469 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800470 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900471 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
472 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500473 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700474 @HardKeyboardBehavior
475 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700478 int mUserId;
479 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700480 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800481 String mLastEnabled = "";
482
Yohei Yukawa81482972015-06-04 00:58:59 -0700483 /**
484 * <em>This constructor must be called within the lock.</em>
485 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 SettingsObserver(Handler handler) {
487 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700488 }
489
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800490 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 if (mRegistered && mUserId == userId) {
492 return;
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 if (mRegistered) {
496 mContext.getContentResolver().unregisterContentObserver(this);
497 mRegistered = false;
498 }
499 if (mUserId != userId) {
500 mLastEnabled = "";
501 mUserId = userId;
502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700504 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900505 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700506 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900507 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700508 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700509 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700510 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800511 resolver.registerContentObserver(Settings.Secure.getUriFor(
512 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700513 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
Michael Wright7b5a96b2014-08-09 19:28:42 -0700516 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800517 final Uri showImeUri = Settings.Secure.getUriFor(
518 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
519 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
520 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700522 if (showImeUri.equals(uri)) {
523 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800524 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
525 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
526 mContext.getContentResolver(),
527 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
528 0, mUserId) == 1;
529 if (mAccessibilityRequestingNoSoftKeyboard) {
530 final boolean showRequested = mShowRequested;
531 hideCurrentInputLocked(0, null);
532 mShowRequested = showRequested;
533 } else if (mShowRequested) {
534 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
535 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700536 } else {
537 boolean enabledChanged = false;
538 String newEnabled = mSettings.getEnabledInputMethodsStr();
539 if (!mLastEnabled.equals(newEnabled)) {
540 mLastEnabled = newEnabled;
541 enabledChanged = true;
542 }
543 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700547
548 @Override
549 public String toString() {
550 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
551 + " mLastEnabled=" + mLastEnabled + "}";
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900555 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900556 @Override
557 public void onReceive(Context context, Intent intent) {
558 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700559 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900560 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700561 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900562 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800563 } else if (Intent.ACTION_USER_ADDED.equals(action)
564 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100565 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800566 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700567 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
568 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
569 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
570 final String prevValue = intent.getStringExtra(
571 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
572 final String newValue = intent.getStringExtra(
573 Intent.EXTRA_SETTING_NEW_VALUE);
574 restoreEnabledInputMethods(mContext, prevValue, newValue);
575 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900576 } else {
577 Slog.w(TAG, "Unexpected intent " + intent);
578 }
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800581
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700582 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
583 // does not attempt to validate on the fly with any installed device policy, so must only
584 // be run in the context of initial device setup.
585 //
586 // TODO: Move this method to InputMethodUtils with adding unit tests.
587 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
588 if (DEBUG_RESTORE) {
589 Slog.i(TAG, "Restoring enabled input methods:");
590 Slog.i(TAG, "prev=" + prevValue);
591 Slog.i(TAG, " new=" + newValue);
592 }
593 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900594 ArrayMap<String, ArraySet<String>> prevMap =
595 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
596 ArrayMap<String, ArraySet<String>> newMap =
597 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700598
599 // Merge the restored ime+subtype enabled states into the live state
600 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
601 final String imeId = entry.getKey();
602 ArraySet<String> prevSubtypes = prevMap.get(imeId);
603 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700604 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700605 prevMap.put(imeId, prevSubtypes);
606 }
607 prevSubtypes.addAll(entry.getValue());
608 }
609
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700610 final String mergedImesAndSubtypesString =
611 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700612 if (DEBUG_RESTORE) {
613 Slog.i(TAG, "Merged IME string:");
614 Slog.i(TAG, " " + mergedImesAndSubtypesString);
615 }
616 Settings.Secure.putString(context.getContentResolver(),
617 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
618 }
619
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 private boolean isChangingPackagesOfCurrentUser() {
622 final int userId = getChangingUserId();
623 final boolean retval = userId == mSettings.getCurrentUserId();
624 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900625 if (!retval) {
626 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
627 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900628 }
629 return retval;
630 }
631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900634 if (!isChangingPackagesOfCurrentUser()) {
635 return false;
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 final int N = mMethodList.size();
640 if (curInputMethodId != null) {
641 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800642 InputMethodInfo imi = mMethodList.get(i);
643 if (imi.getId().equals(curInputMethodId)) {
644 for (String pkg : packages) {
645 if (imi.getPackageName().equals(pkg)) {
646 if (!doit) {
647 return true;
648 }
satok723a27e2010-11-11 14:58:11 +0900649 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 chooseNewDefaultIMELocked();
651 return true;
652 }
653 }
654 }
655 }
656 }
657 }
658 return false;
659 }
660
661 @Override
662 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900663 if (!isChangingPackagesOfCurrentUser()) {
664 return;
665 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800666 synchronized (mMethodMap) {
667 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900668 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 final int N = mMethodList.size();
670 if (curInputMethodId != null) {
671 for (int i=0; i<N; i++) {
672 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900673 final String imiId = imi.getId();
674 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 curIm = imi;
676 }
satoke7c6998e2011-06-03 17:57:59 +0900677
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800678 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900679 if (isPackageModified(imi.getPackageName())) {
680 mFileManager.deleteAllInputMethodSubtypes(imiId);
681 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 if (change == PACKAGE_TEMPORARY_CHANGE
683 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800684 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800685 + imi.getComponent());
686 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688 }
689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
Yohei Yukawa94e33302016-02-12 19:37:03 -0800691 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800696 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697 if (change == PACKAGE_TEMPORARY_CHANGE
698 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800699 ServiceInfo si = null;
700 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900701 si = mIPackageManager.getServiceInfo(
702 curIm.getComponent(), 0, mSettings.getCurrentUserId());
703 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 }
705 if (si == null) {
706 // Uh oh, current input method is no longer around!
707 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800708 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900709 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800710 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800711 changed = true;
712 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900714 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800715 }
716 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800717 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800718 }
satokab751aa2010-09-14 19:17:36 +0900719
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800720 if (curIm == null) {
721 // We currently don't have a default input method... is
722 // one now available?
723 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700724 } else if (!changed && isPackageModified(curIm.getPackageName())) {
725 // Even if the current input method is still available, mCurrentSubtype could
726 // be obsolete when the package is modified in practice.
727 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800730 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800731 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733 }
734 }
735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
Jeff Brownc28867a2013-03-26 15:42:39 -0700737 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900738 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700739 private final IInputMethod mMethod;
740 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Jeff Brownc28867a2013-03-26 15:42:39 -0700742 MethodCallback(InputMethodManagerService imms, IInputMethod method,
743 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900744 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700745 mMethod = method;
746 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
satoke7c6998e2011-06-03 17:57:59 +0900749 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700750 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700751 long ident = Binder.clearCallingIdentity();
752 try {
753 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
754 } finally {
755 Binder.restoreCallingIdentity(ident);
756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
satok01038492012-04-09 21:08:27 +0900760 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700761 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900762 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700763 public void onHardKeyboardStatusChange(boolean available) {
764 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
765 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900766 }
767
Michael Wright7b5a96b2014-08-09 19:28:42 -0700768 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900769 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700770 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900771 }
772 synchronized(mMethodMap) {
773 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
774 && mSwitchingDialog.isShowing()) {
775 mSwitchingDialogTitleView.findViewById(
776 com.android.internal.R.id.hard_keyboard_section).setVisibility(
777 available ? View.VISIBLE : View.GONE);
778 }
779 }
780 }
781 }
782
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800783 public static final class Lifecycle extends SystemService {
784 private InputMethodManagerService mService;
785
786 public Lifecycle(Context context) {
787 super(context);
788 mService = new InputMethodManagerService(context);
789 }
790
791 @Override
792 public void onStart() {
793 LocalServices.addService(InputMethodManagerInternal.class,
794 new LocalServiceImpl(mService.mHandler));
795 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
796 }
797
798 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800799 public void onSwitchUser(@UserIdInt int userHandle) {
800 // Called on the system server's main looper thread.
801 // TODO: Dispatch this to a worker thread as needed.
802 mService.onSwitchUser(userHandle);
803 }
804
805 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800806 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800807 // Called on the system server's main looper thread.
808 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800809 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
810 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
811 .getService(Context.STATUS_BAR_SERVICE);
812 mService.systemRunning(statusBarService);
813 }
814 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800815
816 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800817 public void onUnlockUser(@UserIdInt int userHandle) {
818 // Called on the system server's main looper thread.
819 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800820 mService.onUnlockUser(userHandle);
821 }
822 }
823
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800824 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800825 synchronized(mMethodMap) {
826 final int currentUserId = mSettings.getCurrentUserId();
827 if (DEBUG) {
828 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
829 }
830 if (userId != currentUserId) {
831 return;
832 }
833 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
834 // We need to rebuild IMEs.
835 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
836 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
837 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800838 }
839
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800840 void onSwitchUser(@UserIdInt int userId) {
841 synchronized (mMethodMap) {
842 switchUserLocked(userId);
843 }
844 }
845
Seigo Nonaka7309b122015-08-17 18:34:13 -0700846 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900847 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800849 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700851 // Note: SettingsObserver doesn't register observers in its constructor.
852 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 mIWindowManager = IWindowManager.Stub.asInterface(
854 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700855 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800856 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900857 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public void executeMessage(Message msg) {
859 handleMessage(msg);
860 }
Mita Yuned218c72012-12-06 17:18:25 -0800861 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800862 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800863 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900864 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700865 mHasFeature = context.getPackageManager().hasSystemFeature(
866 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500867 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700868 mHardKeyboardBehavior = mContext.getResources().getInteger(
869 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900870
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400871 Bundle extras = new Bundle();
872 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
873 mImeSwitcherNotification = new Notification.Builder(mContext)
874 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
875 .setWhen(0)
876 .setOngoing(true)
877 .addExtras(extras)
878 .setCategory(Notification.CATEGORY_SYSTEM)
879 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400880
satok7cfc0ed2011-06-20 21:29:36 +0900881 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900882 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900883
884 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900885
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900886 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900887 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800888 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
889 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700890 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900891 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800892
satok7cfc0ed2011-06-20 21:29:36 +0900893 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 int userId = 0;
895 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900896 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
897 } catch (RemoteException e) {
898 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
899 }
satok81f8b7c2012-12-04 20:42:56 +0900900 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900901
satokd87c2592010-09-29 11:52:06 +0900902 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900903 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800904 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700905
Kenny Guy2a764942014-04-02 13:29:20 +0100906 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900907 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900908 synchronized (mMethodMap) {
909 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
910 mSettings, context);
911 }
satok0a1bcf42012-05-16 19:26:31 +0900912
913 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900914 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900915 if (DEBUG) {
916 Slog.d(TAG, "Initial default ime = " + defaultImiId);
917 }
satok0a1bcf42012-05-16 19:26:31 +0900918 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
919
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900920 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800921 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900922 }
satokd87c2592010-09-29 11:52:06 +0900923 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924
satok0a1bcf42012-05-16 19:26:31 +0900925 if (!mImeSelectedOnBoot) {
926 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900927 synchronized (mMethodMap) {
928 resetDefaultImeLocked(context);
929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900932 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700933 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900934 updateFromSettingsLocked(true);
935 }
satok5b927c432012-05-01 20:09:34 +0900936
937 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
938 // according to the new system locale.
939 final IntentFilter filter = new IntentFilter();
940 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
941 mContext.registerReceiver(
942 new BroadcastReceiver() {
943 @Override
944 public void onReceive(Context context, Intent intent) {
945 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900946 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900947 }
948 }
949 }, filter);
950 }
951
satok5b927c432012-05-01 20:09:34 +0900952 private void resetDefaultImeLocked(Context context) {
953 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800954 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900955 return;
956 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800957 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
958 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
959 if (suitableImes.isEmpty()) {
960 Slog.i(TAG, "No default found");
961 return;
satok5b927c432012-05-01 20:09:34 +0900962 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800963 final InputMethodInfo defIm = suitableImes.get(0);
964 Slog.i(TAG, "Default found, using " + defIm.getId());
965 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900966 }
967
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900968 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
969 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 if (!mSystemReady) {
971 // not system ready
972 return;
973 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800974 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800976 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!updateOnlyWhenLocaleChanged) {
978 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800979 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 }
981 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800982 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800984 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 if (!updateOnlyWhenLocaleChanged) {
986 final String selectedImiId = mSettings.getSelectedInputMethod();
987 if (TextUtils.isEmpty(selectedImiId)) {
988 // This is the first time of the user switch and
989 // set the current ime to the proper one.
990 resetDefaultImeLocked(mContext);
991 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900992 } else {
993 // If the locale is changed, needs to reset the default ime
994 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800996 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800997 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 if (!updateOnlyWhenLocaleChanged) {
999 try {
1000 startInputInnerLocked();
1001 } catch (RuntimeException e) {
1002 Slog.w(TAG, "Unexpected exception", e);
1003 }
1004 }
1005 }
1006 }
1007
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001008 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001009 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1010 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 }
1012
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001013 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001014 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1015 + " currentUserId=" + mSettings.getCurrentUserId());
1016
Yohei Yukawa81482972015-06-04 00:58:59 -07001017 // ContentObserver should be registered again when the user is changed
1018 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001019
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001020 // If the system is not ready or the device is not yed unlocked by the user, then we use
1021 // copy-on-write settings.
1022 final boolean useCopyOnWriteSettings =
1023 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001024 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001025 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001026 // InputMethodFileManager should be reset when the user is changed
1027 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001028 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001029
1030 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1031 + " defaultImiId=" + defaultImiId);
1032
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001033 // For secondary users, the list of enabled IMEs may not have been updated since the
1034 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1035 // not be empty even if the IME has been uninstalled by the primary user.
1036 // Even in such cases, IMMS works fine because it will find the most applicable
1037 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001038 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001039 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001040 initialUserSwitch /* needsToResetDefaultIme */);
1041 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001042 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1043 mSettings.getEnabledInputMethodListLocked(), newUserId,
1044 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001045 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001046
1047 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1048 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001049 }
1050
Kenny Guy2a764942014-04-02 13:29:20 +01001051 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001052 mSettings.setCurrentProfileIds(
1053 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001054 }
1055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 @Override
1057 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1058 throws RemoteException {
1059 try {
1060 return super.onTransact(code, data, reply, flags);
1061 } catch (RuntimeException e) {
1062 // The input method manager only throws security exceptions, so let's
1063 // log all others.
1064 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001065 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 throw e;
1068 }
1069 }
1070
Svetoslav Ganova0027152013-06-25 14:59:53 -07001071 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001072 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001073 if (DEBUG) {
1074 Slog.d(TAG, "--- systemReady");
1075 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001076 if (!mSystemReady) {
1077 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001078 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001079 mSettings.switchCurrentUser(currentUserId,
1080 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001081 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1082 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001083 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001084 if (mStatusBar != null) {
1085 mStatusBar.setIconVisibility(mSlotIme, false);
1086 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001087 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001088 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1089 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001090 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001091 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001092 mHardKeyboardListener);
1093 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001094 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001095 if (!mImeSelectedOnBoot) {
1096 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001097 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001098 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1099 mSettings.getEnabledInputMethodListLocked(),
1100 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001101 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001102 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001103 try {
1104 startInputInnerLocked();
1105 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001106 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001107 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001108 }
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 // ---------------------------------------------------------------------------------------
1113 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1114 // 1) it comes from the system process
1115 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1116 private boolean calledFromValidUser() {
1117 final int uid = Binder.getCallingUid();
1118 final int userId = UserHandle.getUserId(uid);
1119 if (DEBUG) {
1120 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1121 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1122 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001123 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1124 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001125 }
Kenny Guy2a764942014-04-02 13:29:20 +01001126 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001129
1130 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1131 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1132 // must not manage background users' states in any functions.
1133 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1134 // by a token.
1135 if (mContext.checkCallingOrSelfPermission(
1136 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1137 == PackageManager.PERMISSION_GRANTED) {
1138 if (DEBUG) {
1139 Slog.d(TAG, "--- Access granted because the calling process has "
1140 + "the INTERACT_ACROSS_USERS_FULL permission");
1141 }
1142 return true;
1143 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001144 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1145 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001146 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001147 }
1148
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001149
1150 /**
1151 * Returns true iff the caller is identified to be the current input method with the token.
1152 * @param token The window token given to the input method when it was started.
1153 * @return true if and only if non-null valid token is specified.
1154 */
1155 private boolean calledWithValidToken(IBinder token) {
1156 if (token == null || mCurToken != token) {
1157 return false;
1158 }
1159 return true;
1160 }
1161
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001162 private boolean bindCurrentInputMethodService(
1163 Intent service, ServiceConnection conn, int flags) {
1164 if (service == null || conn == null) {
1165 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1166 return false;
1167 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001168 return mContext.bindServiceAsUser(service, conn, flags,
1169 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001170 }
1171
satoke7c6998e2011-06-03 17:57:59 +09001172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 // TODO: Make this work even for non-current users?
1175 if (!calledFromValidUser()) {
1176 return Collections.emptyList();
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001179 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182
satoke7c6998e2011-06-03 17:57:59 +09001183 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001185 // TODO: Make this work even for non-current users?
1186 if (!calledFromValidUser()) {
1187 return Collections.emptyList();
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001190 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001194 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001195 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001196 * @return enabled subtypes of the specified imi
1197 */
satoke7c6998e2011-06-03 17:57:59 +09001198 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001199 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001200 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001201 // TODO: Make this work even for non-current users?
1202 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001203 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001204 }
satok67ddf9c2010-11-17 09:45:54 +09001205 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001206 final InputMethodInfo imi;
1207 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001208 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001209 } else {
1210 imi = mMethodMap.get(imiId);
1211 }
1212 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001213 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001214 }
1215 return mSettings.getEnabledInputMethodSubtypeListLocked(
1216 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001217 }
1218 }
1219
satoke7c6998e2011-06-03 17:57:59 +09001220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 public void addClient(IInputMethodClient client,
1222 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (!calledFromValidUser()) {
1224 return;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
1227 mClients.put(client.asBinder(), new ClientState(client,
1228 inputContext, uid, pid));
1229 }
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
satoke7c6998e2011-06-03 17:57:59 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001234 if (!calledFromValidUser()) {
1235 return;
1236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001238 ClientState cs = mClients.remove(client.asBinder());
1239 if (cs != null) {
1240 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001241 if (mCurClient == cs) {
1242 mCurClient = null;
1243 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001244 if (mCurFocusedWindowClient == cs) {
1245 mCurFocusedWindowClient = null;
1246 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 void executeOrSendMessage(IInterface target, Message msg) {
1252 if (target.asBinder() instanceof Binder) {
1253 mCaller.sendMessage(msg);
1254 } else {
1255 handleMessage(msg);
1256 msg.recycle();
1257 }
1258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
Yohei Yukawa33e81792015-11-17 21:14:42 -08001260 void unbindCurrentClientLocked(
1261 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001263 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 + mCurClient.client.asBinder());
1265 if (mBoundToMethod) {
1266 mBoundToMethod = false;
1267 if (mCurMethod != null) {
1268 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1269 MSG_UNBIND_INPUT, mCurMethod));
1270 }
1271 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001272
1273 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1274 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001275 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1276 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001279
The Android Open Source Project10592532009-03-18 17:39:46 -07001280 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 }
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 private int getImeShowFlags() {
1285 int flags = 0;
1286 if (mShowForced) {
1287 flags |= InputMethod.SHOW_FORCED
1288 | InputMethod.SHOW_EXPLICIT;
1289 } else if (mShowExplicitlyRequested) {
1290 flags |= InputMethod.SHOW_EXPLICIT;
1291 }
1292 return flags;
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 private int getAppShowFlags() {
1296 int flags = 0;
1297 if (mShowForced) {
1298 flags |= InputMethodManager.SHOW_FORCED;
1299 } else if (!mShowExplicitlyRequested) {
1300 flags |= InputMethodManager.SHOW_IMPLICIT;
1301 }
1302 return flags;
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn7663d802012-02-24 13:08:49 -08001305 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 if (!mBoundToMethod) {
1307 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1308 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1309 mBoundToMethod = true;
1310 }
1311 final SessionState session = mCurClient.curSession;
1312 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001313 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1314 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1315 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001317 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1318 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1319 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001322 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001323 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001325 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001326 (session.channel != null ? session.channel.dup() : null),
1327 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001330 InputBindResult startInputLocked(
1331 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001332 IInputMethodClient client, IInputContext inputContext,
1333 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001334 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 // If no method is currently selected, do nothing.
1336 if (mCurMethodId == null) {
1337 return mNoBinding;
1338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 ClientState cs = mClients.get(client.asBinder());
1341 if (cs == null) {
1342 throw new IllegalArgumentException("unknown client "
1343 + client.asBinder());
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Yohei Yukawa74750f22016-03-22 12:54:22 -07001346 if (attribute == null) {
1347 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1348 + " uid=" + cs.uid + " pid=" + cs.pid);
1349 return null;
1350 }
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 try {
1353 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1354 // Check with the window manager to make sure this client actually
1355 // has a window with focus. If not, reject. This is thread safe
1356 // because if the focus changes some time before or after, the
1357 // next client receiving focus that has any interest in input will
1358 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001359 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1361 return null;
1362 }
1363 } catch (RemoteException e) {
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001366 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1367 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001368 }
1369
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001370 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001371 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001372 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001373 // If no method is currently selected, do nothing.
1374 if (mCurMethodId == null) {
1375 return mNoBinding;
1376 }
1377
Yohei Yukawad57ba672015-06-08 16:39:46 -07001378 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1379 attribute.packageName)) {
1380 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1381 + " uid=" + cs.uid + " package=" + attribute.packageName);
1382 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001383 }
1384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001386 // Was the keyguard locked when switching over to the new client?
1387 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 // If the client is changing, we need to switch over to the new
1389 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001390 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001391 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001392 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393
1394 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001395 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001396 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001397 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 // Bump up the sequence for this client and attach it.
1402 mCurSeq++;
1403 if (mCurSeq <= 0) mCurSeq = 1;
1404 mCurClient = cs;
1405 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001406 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 // Check if the input method is changing.
1410 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1411 if (cs.curSession != null) {
1412 // Fast case: if we are already connected to the input method,
1413 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001414 return attachNewInputLocked(
1415 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 }
1417 if (mHaveConnection) {
1418 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 // Return to client, and we will get back with it when
1420 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001421 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001422 return new InputBindResult(null, null, mCurId, mCurSeq,
1423 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } else if (SystemClock.uptimeMillis()
1425 < (mLastBindTime+TIME_TO_RECONNECT)) {
1426 // In this case we have connected to the service, but
1427 // don't yet have its interface. If it hasn't been too
1428 // long since we did the connection, we'll return to
1429 // the client and wait to get the service interface so
1430 // we can report back. If it has been too long, we want
1431 // to fall through so we can try a disconnect/reconnect
1432 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001433 return new InputBindResult(null, null, mCurId, mCurSeq,
1434 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1437 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
1439 }
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001442 return startInputInnerLocked();
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001445 InputBindResult startInputInnerLocked() {
1446 if (mCurMethodId == null) {
1447 return mNoBinding;
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001450 if (!mSystemReady) {
1451 // If the system is not yet ready, we shouldn't be running third
1452 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001453 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1454 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1458 if (info == null) {
1459 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001462 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1465 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001466 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1467 com.android.internal.R.string.input_method_binding_label);
1468 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1469 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001470 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001471 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1472 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 mLastBindTime = SystemClock.uptimeMillis();
1474 mHaveConnection = true;
1475 mCurId = info.getId();
1476 mCurToken = new Binder();
1477 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001478 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mIWindowManager.addWindowToken(mCurToken,
1480 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1481 } catch (RemoteException e) {
1482 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001483 return new InputBindResult(null, null, mCurId, mCurSeq,
1484 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 } else {
1486 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001487 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 + mCurIntent);
1489 }
1490 return null;
1491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001493 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001494 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001495 IInputMethodClient client, IInputContext inputContext,
1496 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001497 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001498 if (!calledFromValidUser()) {
1499 return null;
1500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001502 if (DEBUG) {
1503 Slog.v(TAG, "startInput: reason="
1504 + InputMethodClient.getStartInputReason(startInputReason)
1505 + " client = " + client.asBinder()
1506 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001507 + " missingMethods="
1508 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001509 + " attribute=" + attribute
1510 + " controlFlags=#" + Integer.toHexString(controlFlags));
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 final long ident = Binder.clearCallingIdentity();
1513 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001514 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1515 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 } finally {
1517 Binder.restoreCallingIdentity(ident);
1518 }
1519 }
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
satoke7c6998e2011-06-03 17:57:59 +09001522 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 public void finishInput(IInputMethodClient client) {
1524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
satoke7c6998e2011-06-03 17:57:59 +09001526 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 public void onServiceConnected(ComponentName name, IBinder service) {
1528 synchronized (mMethodMap) {
1529 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1530 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001531 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001532 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001533 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001534 return;
1535 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001536 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001537 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1538 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001540 clearClientSessionLocked(mCurClient);
1541 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 }
1544 }
1545 }
1546
Jeff Brownc28867a2013-03-26 15:42:39 -07001547 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1548 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 synchronized (mMethodMap) {
1550 if (mCurMethod != null && method != null
1551 && mCurMethod.asBinder() == method.asBinder()) {
1552 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001555 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001556 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 if (res.method != null) {
1558 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001559 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001561 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
1563 }
1564 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001565
1566 // Session abandoned. Close its associated input channel.
1567 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001569
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001570 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001571 if (mVisibleBound) {
1572 mContext.unbindService(mVisibleConnection);
1573 mVisibleBound = false;
1574 }
1575
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001576 if (mHaveConnection) {
1577 mContext.unbindService(this);
1578 mHaveConnection = false;
1579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001581 if (mCurToken != null) {
1582 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001583 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001584 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001585 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001586 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001587 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001588 mIWindowManager.removeWindowToken(mCurToken);
1589 } catch (RemoteException e) {
1590 }
1591 mCurToken = null;
1592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
The Android Open Source Project10592532009-03-18 17:39:46 -07001594 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001595 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001597
Yohei Yukawa33e81792015-11-17 21:14:42 -08001598 void resetCurrentMethodAndClient(
1599 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001600 mCurMethodId = null;
1601 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001602 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001603 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001604
1605 void requestClientSessionLocked(ClientState cs) {
1606 if (!cs.sessionRequested) {
1607 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1608 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1609 cs.sessionRequested = true;
1610 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1611 MSG_CREATE_SESSION, mCurMethod, channels[1],
1612 new MethodCallback(this, mCurMethod, channels[0])));
1613 }
1614 }
1615
1616 void clearClientSessionLocked(ClientState cs) {
1617 finishSessionLocked(cs.curSession);
1618 cs.curSession = null;
1619 cs.sessionRequested = false;
1620 }
1621
1622 private void finishSessionLocked(SessionState sessionState) {
1623 if (sessionState != null) {
1624 if (sessionState.session != null) {
1625 try {
1626 sessionState.session.finishSession();
1627 } catch (RemoteException e) {
1628 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001629 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001630 }
1631 sessionState.session = null;
1632 }
1633 if (sessionState.channel != null) {
1634 sessionState.channel.dispose();
1635 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001636 }
1637 }
1638 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001639
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001640 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 if (mCurMethod != null) {
1642 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001643 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001645
Jeff Brownc28867a2013-03-26 15:42:39 -07001646 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001647 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 mCurMethod = null;
1649 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001650 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001651 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001654
satoke7c6998e2011-06-03 17:57:59 +09001655 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 public void onServiceDisconnected(ComponentName name) {
1657 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001658 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 + " mCurIntent=" + mCurIntent);
1660 if (mCurMethod != null && mCurIntent != null
1661 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001662 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 // We consider this to be a new bind attempt, since the system
1664 // should now try to restart the service for us.
1665 mLastBindTime = SystemClock.uptimeMillis();
1666 mShowRequested = mInputShown;
1667 mInputShown = false;
1668 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001669 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1670 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1671 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 }
1674 }
1675 }
1676
satokf9f01002011-05-19 21:31:50 +09001677 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1679 long ident = Binder.clearCallingIdentity();
1680 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001682 if (!calledWithValidToken(token)) {
1683 final int uid = Binder.getCallingUid();
1684 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1685 + " token:" + token);
1686 return;
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001689 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001690 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001691 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001694 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001695 CharSequence contentDescription = null;
1696 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001697 // Use PackageManager to load label
1698 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001699 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001700 mIPackageManager.getApplicationInfo(packageName, 0,
1701 mSettings.getCurrentUserId()));
1702 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001703 /* ignore */
1704 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001705 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001706 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001707 contentDescription != null
1708 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001709 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712 }
1713 } finally {
1714 Binder.restoreCallingIdentity(ident);
1715 }
1716 }
1717
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001718 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001719 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001720 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001721 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001722 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001723 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001724 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1725 // When physical keyboard is attached, we show the ime switcher (or notification if
1726 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1727 // exists in the IME switcher dialog. Might be OK to remove this condition once
1728 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1729 return true;
1730 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001731 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1732 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001733 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001734
1735 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1736 final int N = imis.size();
1737 if (N > 2) return true;
1738 if (N < 1) return false;
1739 int nonAuxCount = 0;
1740 int auxCount = 0;
1741 InputMethodSubtype nonAuxSubtype = null;
1742 InputMethodSubtype auxSubtype = null;
1743 for(int i = 0; i < N; ++i) {
1744 final InputMethodInfo imi = imis.get(i);
1745 final List<InputMethodSubtype> subtypes =
1746 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1747 final int subtypeCount = subtypes.size();
1748 if (subtypeCount == 0) {
1749 ++nonAuxCount;
1750 } else {
1751 for (int j = 0; j < subtypeCount; ++j) {
1752 final InputMethodSubtype subtype = subtypes.get(j);
1753 if (!subtype.isAuxiliary()) {
1754 ++nonAuxCount;
1755 nonAuxSubtype = subtype;
1756 } else {
1757 ++auxCount;
1758 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001759 }
1760 }
satok7cfc0ed2011-06-20 21:29:36 +09001761 }
1762 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001763 if (nonAuxCount > 1 || auxCount > 1) {
1764 return true;
1765 } else if (nonAuxCount == 1 && auxCount == 1) {
1766 if (nonAuxSubtype != null && auxSubtype != null
1767 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1768 || auxSubtype.overridesImplicitlyEnabledSubtype()
1769 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1770 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1771 return false;
1772 }
1773 return true;
1774 }
1775 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001776 }
1777
John Spurlocke0980502013-10-25 11:59:29 -04001778 private boolean isKeyguardLocked() {
1779 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1780 }
1781
satokdbf29502011-08-25 15:28:23 +09001782 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001783 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001784 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001785 if (!calledWithValidToken(token)) {
1786 final int uid = Binder.getCallingUid();
1787 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1788 + " token:" + token);
1789 return;
1790 }
1791
1792 synchronized (mMethodMap) {
1793 mImeWindowVis = vis;
1794 mBackDisposition = backDisposition;
1795 updateSystemUiLocked(token, vis, backDisposition);
1796 }
1797 }
1798
1799 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1800 synchronized (mMethodMap) {
1801 updateSystemUiLocked(token, vis, backDisposition);
1802 }
1803 }
1804
1805 // Caution! This method is called in this class. Handle multi-user carefully
1806 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1807 if (!calledWithValidToken(token)) {
1808 final int uid = Binder.getCallingUid();
1809 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1810 + " token:" + token);
1811 return;
1812 }
1813
1814 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1815 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001816 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001817 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001818 // apply policy for binder calls
1819 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1820 vis = 0;
satok06487a52010-10-29 11:37:18 +09001821 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001822 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1823 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1824 if (mStatusBar != null) {
1825 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1826 needsToShowImeSwitcher);
1827 }
1828 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1829 if (imi != null && needsToShowImeSwitcher) {
1830 // Used to load label
1831 final CharSequence title = mRes.getText(
1832 com.android.internal.R.string.select_input_method);
1833 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1834 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001835 mImeSwitcherNotification.setContentTitle(title)
1836 .setContentText(summary)
1837 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001838 try {
1839 if ((mNotificationManager != null)
1840 && !mIWindowManager.hasNavigationBar()) {
1841 if (DEBUG) {
1842 Slog.d(TAG, "--- show notification: label = " + summary);
1843 }
1844 mNotificationManager.notifyAsUser(null,
1845 com.android.internal.R.string.select_input_method,
1846 mImeSwitcherNotification.build(), UserHandle.ALL);
1847 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001848 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001849 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001850 }
1851 } else {
1852 if (mNotificationShown && mNotificationManager != null) {
1853 if (DEBUG) {
1854 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001855 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001856 mNotificationManager.cancelAsUser(null,
1857 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1858 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001859 }
satok06487a52010-10-29 11:37:18 +09001860 }
1861 } finally {
1862 Binder.restoreCallingIdentity(ident);
1863 }
1864 }
1865
satoke7c6998e2011-06-03 17:57:59 +09001866 @Override
satokf9f01002011-05-19 21:31:50 +09001867 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001868 if (!calledFromValidUser()) {
1869 return;
1870 }
satokf9f01002011-05-19 21:31:50 +09001871 synchronized (mMethodMap) {
1872 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1873 for (int i = 0; i < spans.length; ++i) {
1874 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001875 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001876 mSecureSuggestionSpans.put(ss, currentImi);
1877 }
1878 }
1879 }
1880 }
1881
satoke7c6998e2011-06-03 17:57:59 +09001882 @Override
satokf9f01002011-05-19 21:31:50 +09001883 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001884 if (!calledFromValidUser()) {
1885 return false;
1886 }
satokf9f01002011-05-19 21:31:50 +09001887 synchronized (mMethodMap) {
1888 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1889 // TODO: Do not send the intent if the process of the targetImi is already dead.
1890 if (targetImi != null) {
1891 final String[] suggestions = span.getSuggestions();
1892 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001893 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001894 final Intent intent = new Intent();
1895 // Ensures that only a class in the original IME package will receive the
1896 // notification.
satok42c5a162011-05-26 16:46:14 +09001897 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001898 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1899 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1900 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1901 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001902 final long ident = Binder.clearCallingIdentity();
1903 try {
1904 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1905 } finally {
1906 Binder.restoreCallingIdentity(ident);
1907 }
satokf9f01002011-05-19 21:31:50 +09001908 return true;
1909 }
1910 }
1911 return false;
1912 }
1913
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001914 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001915 updateInputMethodsFromSettingsLocked(enabledMayChange);
1916 updateKeyboardFromSettingsLocked();
1917 }
1918
1919 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 if (enabledMayChange) {
1921 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1922 for (int i=0; i<enabled.size(); i++) {
1923 // We allow the user to select "disabled until used" apps, so if they
1924 // are enabling one of those here we now need to make it enabled.
1925 InputMethodInfo imm = enabled.get(i);
1926 try {
1927 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1928 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1929 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001930 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001931 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001932 if (DEBUG) {
1933 Slog.d(TAG, "Update state(" + imm.getId()
1934 + "): DISABLED_UNTIL_USED -> DEFAULT");
1935 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001936 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1937 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001938 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1939 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001940 }
1941 } catch (RemoteException e) {
1942 }
1943 }
1944 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001945 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1946 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1947 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1948 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001949 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001950 // There is no input method selected, try to choose new applicable input method.
1951 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001952 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001953 }
1954 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001956 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001958 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001959 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
satokf3db1af2010-11-23 13:34:33 +09001961 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001962 } else {
1963 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001964 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001966 // Here is not the perfect place to reset the switching controller. Ideally
1967 // mSwitchingController and mSettings should be able to share the same state.
1968 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1969 // the same enabled IMEs list.
1970 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001971
1972 }
1973
1974 public void updateKeyboardFromSettingsLocked() {
1975 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1976 if (mSwitchingDialog != null
1977 && mSwitchingDialogTitleView != null
1978 && mSwitchingDialog.isShowing()) {
1979 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1980 com.android.internal.R.id.hard_keyboard_switch);
1981 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
Yohei Yukawab097b822015-12-01 10:43:08 -08001985 private void notifyInputMethodSubtypeChanged(final int userId,
1986 @Nullable final InputMethodInfo inputMethodInfo,
1987 @Nullable final InputMethodSubtype subtype) {
1988 final InputManagerInternal inputManagerInternal =
1989 LocalServices.getService(InputManagerInternal.class);
1990 if (inputManagerInternal != null) {
1991 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1992 }
1993 }
1994
satokab751aa2010-09-14 19:17:36 +09001995 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 InputMethodInfo info = mMethodMap.get(id);
1997 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001998 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002000
satokd81e9502012-05-21 12:58:45 +09002001 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002003 final int subtypeCount = info.getSubtypeCount();
2004 if (subtypeCount <= 0) {
2005 return;
satokcd7cd292010-11-20 15:46:23 +09002006 }
satokd81e9502012-05-21 12:58:45 +09002007 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2008 final InputMethodSubtype newSubtype;
2009 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2010 newSubtype = info.getSubtypeAt(subtypeId);
2011 } else {
2012 // If subtype is null, try to find the most applicable one from
2013 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002014 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002015 }
2016 if (newSubtype == null || oldSubtype == null) {
2017 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2018 + ", new subtype = " + newSubtype);
2019 return;
2020 }
2021 if (newSubtype != oldSubtype) {
2022 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2023 if (mCurMethod != null) {
2024 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002025 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002026 mCurMethod.changeInputMethodSubtype(newSubtype);
2027 } catch (RemoteException e) {
2028 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002029 return;
satokab751aa2010-09-14 19:17:36 +09002030 }
2031 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002032 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 return;
2035 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002036
satokd81e9502012-05-21 12:58:45 +09002037 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 final long ident = Binder.clearCallingIdentity();
2039 try {
satokab751aa2010-09-14 19:17:36 +09002040 // Set a subtype to this input method.
2041 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002042 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2043 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2044 // because mCurMethodId is stored as a history in
2045 // setSelectedInputMethodAndSubtypeLocked().
2046 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047
2048 if (ActivityManagerNative.isSystemReady()) {
2049 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002050 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002052 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002054 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 } finally {
2056 Binder.restoreCallingIdentity(ident);
2057 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002058
2059 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2060 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002062
satok42c5a162011-05-26 16:46:14 +09002063 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002064 public boolean showSoftInput(IInputMethodClient client, int flags,
2065 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002066 if (!calledFromValidUser()) {
2067 return false;
2068 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002069 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 long ident = Binder.clearCallingIdentity();
2071 try {
2072 synchronized (mMethodMap) {
2073 if (mCurClient == null || client == null
2074 || mCurClient.client.asBinder() != client.asBinder()) {
2075 try {
2076 // We need to check if this is the current client with
2077 // focus in the window manager, to allow this call to
2078 // be made before input is started in it.
2079 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002080 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002081 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002087
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002088 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002089 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091 } finally {
2092 Binder.restoreCallingIdentity(ident);
2093 }
2094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002095
The Android Open Source Project4df24232009-03-05 14:34:35 -08002096 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002098 if (mAccessibilityRequestingNoSoftKeyboard) {
2099 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
Anna Galusza9b278112016-01-04 11:37:37 -08002101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2103 mShowExplicitlyRequested = true;
2104 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002105 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2106 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002108
Dianne Hackborncc278702009-09-02 23:07:23 -07002109 if (!mSystemReady) {
2110 return false;
2111 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002112
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002115 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002116 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2117 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2118 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002120 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002121 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002122 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002123 | Context.BIND_TREAT_LIKE_ACTIVITY
2124 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002125 mVisibleBound = true;
2126 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002127 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002129 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 // The client has asked to have the input method shown, but
2131 // we have been sitting here too long with a connection to the
2132 // service and no interface received, so let's disconnect/connect
2133 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002134 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002136 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002138 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002139 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002140 } else {
2141 if (DEBUG) {
2142 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2143 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002146
The Android Open Source Project4df24232009-03-05 14:34:35 -08002147 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149
satok42c5a162011-05-26 16:46:14 +09002150 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002151 public boolean hideSoftInput(IInputMethodClient client, int flags,
2152 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002153 if (!calledFromValidUser()) {
2154 return false;
2155 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002156 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 long ident = Binder.clearCallingIdentity();
2158 try {
2159 synchronized (mMethodMap) {
2160 if (mCurClient == null || client == null
2161 || mCurClient.client.asBinder() != client.asBinder()) {
2162 try {
2163 // We need to check if this is the current client with
2164 // focus in the window manager, to allow this call to
2165 // be made before input is started in it.
2166 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002167 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2168 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002169 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002175
Joe Onorato8a9b2202010-02-26 18:56:32 -08002176 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002177 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179 } finally {
2180 Binder.restoreCallingIdentity(ident);
2181 }
2182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002183
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2186 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002187 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 -08002188 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
2190 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002191 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 -08002192 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002194
2195 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2196 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2197 // to be updated with the new value sent from IME process. Even in such a transient state
2198 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2199 // application process as a valid request, and have even promised such a behavior with CTS
2200 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2201 // IMMS#InputShown indicates that the software keyboard is shown.
2202 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2203 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2204 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002205 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002206 if (shouldHideSoftInput) {
2207 // The IME will report its visible state again after the following message finally
2208 // delivered to the IME process as an IPC. Hence the inconsistency between
2209 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2210 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2212 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2213 res = true;
2214 } else {
2215 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002217 if (mHaveConnection && mVisibleBound) {
2218 mContext.unbindService(mVisibleConnection);
2219 mVisibleBound = false;
2220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 mInputShown = false;
2222 mShowRequested = false;
2223 mShowExplicitlyRequested = false;
2224 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002225 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002227
satok42c5a162011-05-26 16:46:14 +09002228 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002229 public InputBindResult startInputOrWindowGainedFocus(
2230 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2231 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002232 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002233 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002234 if (windowToken != null) {
2235 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002236 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002237 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002238 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2239 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002240 }
2241 }
2242
2243 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002244 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2245 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002246 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2247 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002248 // Needs to check the validity before clearing calling identity
2249 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002250 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 long ident = Binder.clearCallingIdentity();
2252 try {
2253 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002254 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2255 + InputMethodClient.getStartInputReason(startInputReason)
2256 + " client=" + client.asBinder()
2257 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002258 + " missingMethods="
2259 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002260 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002261 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002263 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002264
Dianne Hackborn7663d802012-02-24 13:08:49 -08002265 ClientState cs = mClients.get(client.asBinder());
2266 if (cs == null) {
2267 throw new IllegalArgumentException("unknown client "
2268 + client.asBinder());
2269 }
2270
2271 try {
2272 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2273 // Check with the window manager to make sure this client actually
2274 // has a window with focus. If not, reject. This is thread safe
2275 // because if the focus changes some time before or after, the
2276 // next client receiving focus that has any interest in input will
2277 // be calling through here after that change happens.
2278 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2279 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2280 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002282 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002284
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002285 if (!calledFromValidUser) {
2286 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2287 Slog.w(TAG, "If you want to interect with IME, you need "
2288 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2289 hideCurrentInputLocked(0, null);
2290 return null;
2291 }
2292
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002293 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002294 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002295 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002296 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002297 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2298 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002299 }
2300 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002301 }
2302 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002303 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002304
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002305 // Should we auto-show the IME even if the caller has not
2306 // specified what should be done with it?
2307 // We only do this automatically if the window can resize
2308 // to accommodate the IME (so what the user sees will give
2309 // them good context without input information being obscured
2310 // by the IME) or if running on a large screen where there
2311 // is more room for the target window + IME.
2312 final boolean doAutoShow =
2313 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2314 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2315 || mRes.getConfiguration().isLayoutSizeAtLeast(
2316 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002317 final boolean isTextEditor =
2318 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2319
2320 // We want to start input before showing the IME, but after closing
2321 // it. We want to do this after closing it to help the IME disappear
2322 // more quickly (not get stuck behind it initializing itself for the
2323 // new focused input, even if its window wants to hide the IME).
2324 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2327 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002328 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2330 // There is no focus view, and this window will
2331 // be behind any soft input window, so hide the
2332 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002333 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002334 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002336 } else if (isTextEditor && doAutoShow && (softInputMode &
2337 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 // There is a focus view, and we are navigating forward
2339 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002340 // We only do this automatically if the window can resize
2341 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002342 // them good context without input information being obscured
2343 // by the IME) or if running on a large screen where there
2344 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002345 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002346 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002347 res = startInputUncheckedLocked(cs, inputContext,
2348 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002349 didStart = true;
2350 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002351 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353 break;
2354 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2355 // Do nothing.
2356 break;
2357 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2358 if ((softInputMode &
2359 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002360 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002361 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
2363 break;
2364 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
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 break;
2368 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2369 if ((softInputMode &
2370 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002371 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002372 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002373 res = startInputUncheckedLocked(cs, inputContext,
2374 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002375 didStart = true;
2376 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002377 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379 break;
2380 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002381 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002383 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2384 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002385 didStart = true;
2386 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002387 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 break;
2389 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002390
2391 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002392 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002393 controlFlags);
2394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
2396 } finally {
2397 Binder.restoreCallingIdentity(ident);
2398 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002399
2400 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002402
satok42c5a162011-05-26 16:46:14 +09002403 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002404 public void showInputMethodPickerFromClient(
2405 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002406 if (!calledFromValidUser()) {
2407 return;
2408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 synchronized (mMethodMap) {
2410 if (mCurClient == null || client == null
2411 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002412 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002413 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
2415
satok440aab52010-11-25 09:43:11 +09002416 // Always call subtype picker, because subtype picker is a superset of input method
2417 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002418 mHandler.sendMessage(mCaller.obtainMessageI(
2419 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002420 }
2421 }
2422
satok42c5a162011-05-26 16:46:14 +09002423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002425 if (!calledFromValidUser()) {
2426 return;
2427 }
satok28203512010-11-24 11:06:49 +09002428 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2429 }
2430
satok42c5a162011-05-26 16:46:14 +09002431 @Override
satok28203512010-11-24 11:06:49 +09002432 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002433 if (!calledFromValidUser()) {
2434 return;
2435 }
satok28203512010-11-24 11:06:49 +09002436 synchronized (mMethodMap) {
2437 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002438 setInputMethodWithSubtypeIdLocked(token, id,
2439 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2440 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002441 } else {
2442 setInputMethod(token, id);
2443 }
2444 }
satokab751aa2010-09-14 19:17:36 +09002445 }
2446
satok42c5a162011-05-26 16:46:14 +09002447 @Override
satokb416a712010-11-25 20:42:14 +09002448 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002449 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002450 if (!calledFromValidUser()) {
2451 return;
2452 }
satokb416a712010-11-25 20:42:14 +09002453 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002454 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2455 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002456 }
2457 }
2458
satok4fc87d62011-05-20 16:13:43 +09002459 @Override
satok735cf382010-11-11 20:40:09 +09002460 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002461 if (!calledFromValidUser()) {
2462 return false;
2463 }
satok735cf382010-11-11 20:40:09 +09002464 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002465 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002466 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002467 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002468 lastImi = mMethodMap.get(lastIme.first);
2469 } else {
2470 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002471 }
satok4fc87d62011-05-20 16:13:43 +09002472 String targetLastImiId = null;
2473 int subtypeId = NOT_A_SUBTYPE_ID;
2474 if (lastIme != null && lastImi != null) {
2475 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2476 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2477 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2478 : mCurrentSubtype.hashCode();
2479 // If the last IME is the same as the current IME and the last subtype is not
2480 // defined, there is no need to switch to the last IME.
2481 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2482 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002483 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002484 }
2485 }
2486
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002487 if (TextUtils.isEmpty(targetLastImiId)
2488 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002489 // This is a safety net. If the currentSubtype can't be added to the history
2490 // and the framework couldn't find the last ime, we will make the last ime be
2491 // the most applicable enabled keyboard subtype of the system imes.
2492 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2493 if (enabled != null) {
2494 final int N = enabled.size();
2495 final String locale = mCurrentSubtype == null
2496 ? mRes.getConfiguration().locale.toString()
2497 : mCurrentSubtype.getLocale();
2498 for (int i = 0; i < N; ++i) {
2499 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002500 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002501 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002502 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2503 InputMethodUtils.getSubtypes(imi),
2504 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002505 if (keyboardSubtype != null) {
2506 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002507 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002508 imi, keyboardSubtype.hashCode());
2509 if(keyboardSubtype.getLocale().equals(locale)) {
2510 break;
2511 }
2512 }
2513 }
2514 }
2515 }
2516 }
2517
2518 if (!TextUtils.isEmpty(targetLastImiId)) {
2519 if (DEBUG) {
2520 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2521 + ", from: " + mCurMethodId + ", " + subtypeId);
2522 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002523 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002524 return true;
2525 } else {
2526 return false;
2527 }
satok735cf382010-11-11 20:40:09 +09002528 }
2529 }
2530
satoke7c6998e2011-06-03 17:57:59 +09002531 @Override
satok688bd472012-02-09 20:09:17 +09002532 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002533 if (!calledFromValidUser()) {
2534 return false;
2535 }
satok688bd472012-02-09 20:09:17 +09002536 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002537 if (!calledWithValidToken(token)) {
2538 final int uid = Binder.getCallingUid();
2539 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2540 + " token:" + token);
2541 return false;
2542 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002543 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002544 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2545 if (nextSubtype == null) {
2546 return false;
2547 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002548 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2549 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002550 return true;
2551 }
2552 }
2553
2554 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002555 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2556 if (!calledFromValidUser()) {
2557 return false;
2558 }
2559 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002560 if (!calledWithValidToken(token)) {
2561 final int uid = Binder.getCallingUid();
2562 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2563 + "token. uid:" + uid + " token:" + token);
2564 return false;
2565 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002566 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002567 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2568 if (nextSubtype == null) {
2569 return false;
2570 }
2571 return true;
2572 }
2573 }
2574
2575 @Override
satok68f1b782011-04-11 14:26:04 +09002576 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002577 if (!calledFromValidUser()) {
2578 return null;
2579 }
satok68f1b782011-04-11 14:26:04 +09002580 synchronized (mMethodMap) {
2581 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2582 // TODO: Handle the case of the last IME with no subtypes
2583 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2584 || TextUtils.isEmpty(lastIme.second)) return null;
2585 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2586 if (lastImi == null) return null;
2587 try {
2588 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002589 final int lastSubtypeId =
2590 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002591 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2592 return null;
2593 }
2594 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002595 } catch (NumberFormatException e) {
2596 return null;
2597 }
2598 }
2599 }
2600
satoke7c6998e2011-06-03 17:57:59 +09002601 @Override
satokee5e77c2011-09-02 18:50:15 +09002602 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002603 if (!calledFromValidUser()) {
2604 return;
2605 }
satok91e88122011-07-18 11:11:42 +09002606 // By this IPC call, only a process which shares the same uid with the IME can add
2607 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002608 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002609 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002610 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002611 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002612 final String[] packageInfos;
2613 try {
2614 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2615 } catch (RemoteException e) {
2616 Slog.e(TAG, "Failed to get package infos");
2617 return;
2618 }
satok91e88122011-07-18 11:11:42 +09002619 if (packageInfos != null) {
2620 final int packageNum = packageInfos.length;
2621 for (int i = 0; i < packageNum; ++i) {
2622 if (packageInfos[i].equals(imi.getPackageName())) {
2623 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002624 final long ident = Binder.clearCallingIdentity();
2625 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002626 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002627 } finally {
2628 Binder.restoreCallingIdentity(ident);
2629 }
satokee5e77c2011-09-02 18:50:15 +09002630 return;
satok91e88122011-07-18 11:11:42 +09002631 }
2632 }
2633 }
satoke7c6998e2011-06-03 17:57:59 +09002634 }
satokee5e77c2011-09-02 18:50:15 +09002635 return;
satoke7c6998e2011-06-03 17:57:59 +09002636 }
2637
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002638 @Override
2639 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002640 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002641 }
2642
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002643 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002644 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002645 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002646 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002647 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002648 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002649 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2650 if (DEBUG) {
2651 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2652 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2653 + " actual: " + sequenceNumber);
2654 }
2655 return;
2656 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002657 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2658 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002659 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002660 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002661 }
2662 }
2663
satok28203512010-11-24 11:06:49 +09002664 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002666 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2667 }
2668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002670 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2671 if (token == null) {
2672 if (mContext.checkCallingOrSelfPermission(
2673 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2674 != PackageManager.PERMISSION_GRANTED) {
2675 throw new SecurityException(
2676 "Using null token requires permission "
2677 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002679 } else if (mCurToken != token) {
2680 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2681 + " token: " + token);
2682 return;
2683 }
2684
2685 final long ident = Binder.clearCallingIdentity();
2686 try {
2687 setInputMethodLocked(id, subtypeId);
2688 } finally {
2689 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691 }
2692
satok42c5a162011-05-26 16:46:14 +09002693 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002695 if (!calledFromValidUser()) {
2696 return;
2697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002699 if (!calledWithValidToken(token)) {
2700 final int uid = Binder.getCallingUid();
2701 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2702 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 return;
2704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 long ident = Binder.clearCallingIdentity();
2706 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002707 hideCurrentInputLocked(flags, null);
2708 } finally {
2709 Binder.restoreCallingIdentity(ident);
2710 }
2711 }
2712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002713
satok42c5a162011-05-26 16:46:14 +09002714 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002715 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002716 if (!calledFromValidUser()) {
2717 return;
2718 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002719 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002720 if (!calledWithValidToken(token)) {
2721 final int uid = Binder.getCallingUid();
2722 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2723 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002724 return;
2725 }
2726 long ident = Binder.clearCallingIdentity();
2727 try {
2728 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 } finally {
2730 Binder.restoreCallingIdentity(ident);
2731 }
2732 }
2733 }
2734
2735 void setEnabledSessionInMainThread(SessionState session) {
2736 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002737 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002739 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002740 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 } catch (RemoteException e) {
2742 }
2743 }
2744 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002745 if (mEnabledSession != null && mEnabledSession.session != null) {
2746 try {
2747 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2748 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2749 } catch (RemoteException e) {
2750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 }
2752 }
2753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
satok42c5a162011-05-26 16:46:14 +09002755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002759 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002760 final boolean showAuxSubtypes;
2761 switch (msg.arg1) {
2762 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2763 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2764 // implemented so that auxiliary subtypes will be excluded when the soft
2765 // keyboard is invisible.
2766 showAuxSubtypes = mInputShown;
2767 break;
2768 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2769 showAuxSubtypes = true;
2770 break;
2771 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2772 showAuxSubtypes = false;
2773 break;
2774 default:
2775 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2776 return false;
2777 }
2778 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002779 return true;
2780
satok47a44912010-10-06 16:03:58 +09002781 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002782 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002783 return true;
2784
2785 case MSG_SHOW_IM_CONFIG:
2786 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002787 return true;
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 case MSG_UNBIND_INPUT:
2792 try {
2793 ((IInputMethod)msg.obj).unbindInput();
2794 } catch (RemoteException e) {
2795 // There is nothing interesting about the method dying.
2796 }
2797 return true;
2798 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002799 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 try {
2801 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2802 } catch (RemoteException e) {
2803 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002804 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 return true;
2806 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002807 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002809 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002810 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002811 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 } catch (RemoteException e) {
2813 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 return true;
2816 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002817 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002819 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002820 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002821 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 } catch (RemoteException e) {
2823 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002824 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 return true;
2826 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002827 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002829 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2831 } catch (RemoteException e) {
2832 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002835 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002836 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002837 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002838 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002840 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002842 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002843 // Dispose the channel if the input method is not local to this process
2844 // because the remote proxy will get its own copy when unparceled.
2845 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002846 channel.dispose();
2847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002849 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002853
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002854 case MSG_START_INPUT: {
2855 int missingMethods = msg.arg1;
2856 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002858 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002860 session.method.startInput((IInputContext) args.arg2, missingMethods,
2861 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 } catch (RemoteException e) {
2863 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002866 }
2867 case MSG_RESTART_INPUT: {
2868 int missingMethods = msg.arg1;
2869 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002871 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002873 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2874 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } catch (RemoteException e) {
2876 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002877 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002882
Yohei Yukawa33e81792015-11-17 21:14:42 -08002883 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002885 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 } catch (RemoteException e) {
2887 // There is nothing interesting about the last client dying.
2888 }
2889 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002890 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002891 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002892 IInputMethodClient client = (IInputMethodClient)args.arg1;
2893 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002895 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002897 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002898 } finally {
2899 // Dispose the channel if the input method is not local to this process
2900 // because the remote proxy will get its own copy when unparceled.
2901 if (res.channel != null && Binder.isProxy(client)) {
2902 res.channel.dispose();
2903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002905 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002907 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002908 case MSG_SET_ACTIVE:
2909 try {
2910 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2911 } catch (RemoteException e) {
2912 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2913 + ((ClientState)msg.obj).pid + " uid "
2914 + ((ClientState)msg.obj).uid);
2915 }
2916 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002917 case MSG_SET_INTERACTIVE:
2918 handleSetInteractive(msg.arg1 != 0);
2919 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002920 case MSG_SWITCH_IME:
2921 handleSwitchInputMethod(msg.arg1 != 0);
2922 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002923 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2924 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002925 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002926 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002927 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002928 } catch (RemoteException e) {
2929 Slog.w(TAG, "Got RemoteException sending "
2930 + "setUserActionNotificationSequenceNumber("
2931 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002932 + clientState.pid + " uid "
2933 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002934 }
2935 return true;
2936 }
satok01038492012-04-09 21:08:27 +09002937
2938 // --------------------------------------------------------------
2939 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002940 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002941 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 }
2943 return false;
2944 }
2945
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002946 private void handleSetInteractive(final boolean interactive) {
2947 synchronized (mMethodMap) {
2948 mIsInteractive = interactive;
2949 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2950
2951 // Inform the current client of the change in active status
2952 if (mCurClient != null && mCurClient.client != null) {
2953 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2954 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2955 }
2956 }
2957 }
2958
Yohei Yukawaae61f712015-12-09 13:00:10 -08002959 private void handleSwitchInputMethod(final boolean forwardDirection) {
2960 synchronized (mMethodMap) {
2961 // TODO: Support forwardDirection.
2962 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2963 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2964 if (nextSubtype == null) {
2965 return;
2966 }
2967 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002968 if (mSubtypeSwitchedByShortCutToast != null) {
2969 mSubtypeSwitchedByShortCutToast.cancel();
2970 mSubtypeSwitchedByShortCutToast = null;
2971 }
2972 if ((mImeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
2973 // IME window is shown. The user should be able to visually understand that the
2974 // subtype is changed in most of cases. To avoid UI overlap, we do not show a toast
2975 // in this case.
2976 return;
2977 }
2978 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2979 if (newInputMethodInfo == null) {
2980 return;
2981 }
2982 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2983 newInputMethodInfo, mCurrentSubtype);
2984 if (!TextUtils.isEmpty(toastText)) {
2985 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText.toString(),
2986 Toast.LENGTH_SHORT);
2987 mSubtypeSwitchedByShortCutToast.show();
2988 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002989 }
2990 }
2991
satokdc9ddae2011-10-06 12:22:36 +09002992 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002993 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2994 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002995 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002996 if (DEBUG) {
2997 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2998 }
satok723a27e2010-11-11 14:58:11 +09002999 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003000 return true;
3001 }
3002
3003 return false;
3004 }
3005
Yohei Yukawa94e33302016-02-12 19:37:03 -08003006 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003007 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003008 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003009 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003010 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003011 mMethodList.clear();
3012 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003013
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003014 // Use for queryIntentServicesAsUser
3015 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003017 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3018 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3019 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003022 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3023 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003024
satoke7c6998e2011-06-03 17:57:59 +09003025 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3026 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 for (int i = 0; i < services.size(); ++i) {
3028 ResolveInfo ri = services.get(i);
3029 ServiceInfo si = ri.serviceInfo;
3030 ComponentName compName = new ComponentName(si.packageName, si.name);
3031 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3032 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003033 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 + ": it does not require the permission "
3035 + android.Manifest.permission.BIND_INPUT_METHOD);
3036 continue;
3037 }
3038
Joe Onorato8a9b2202010-02-26 18:56:32 -08003039 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040
3041 try {
satoke7c6998e2011-06-03 17:57:59 +09003042 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003043 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003044 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003045 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
3047 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003048 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003050 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003051 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 }
3053 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003054
Yohei Yukawa859df052016-02-17 07:56:46 -08003055 // TODO: The following code should find better place to live.
3056 if (!resetDefaultEnabledIme) {
3057 boolean enabledImeFound = false;
3058 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3059 final int N = enabledImes.size();
3060 for (int i = 0; i < N; ++i) {
3061 final InputMethodInfo imi = enabledImes.get(i);
3062 if (mMethodList.contains(imi)) {
3063 enabledImeFound = true;
3064 break;
3065 }
3066 }
3067 if (!enabledImeFound) {
3068 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3069 resetDefaultEnabledIme = true;
3070 resetSelectedInputMethodAndSubtypeLocked("");
3071 }
3072 }
3073
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003074 if (resetDefaultEnabledIme) {
3075 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003076 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003077 final int N = defaultEnabledIme.size();
3078 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003079 final InputMethodInfo imi = defaultEnabledIme.get(i);
3080 if (DEBUG) {
3081 Slog.d(TAG, "--- enable ime = " + imi);
3082 }
3083 setInputMethodEnabledLocked(imi.getId(), true);
3084 }
3085 }
3086
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003087 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003088 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003089 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003090 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3091 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003092 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003093 }
3094 } else {
3095 // Double check that the default IME is certainly enabled.
3096 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003097 }
3098 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003099 // Here is not the perfect place to reset the switching controller. Ideally
3100 // mSwitchingController and mSettings should be able to share the same state.
3101 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3102 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003103 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003107
satok217f5482010-12-15 05:19:19 +09003108 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003109 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003110 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003111 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3112 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003113 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003114 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003115 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003116 final int userId;
3117 synchronized (mMethodMap) {
3118 userId = mSettings.getCurrentUserId();
3119 }
3120 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003121 }
3122
3123 private void showConfigureInputMethods() {
3124 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3125 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3126 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3127 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003128 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003129 }
3130
satok2c93efc2012-04-02 19:33:47 +09003131 private boolean isScreenLocked() {
3132 return mKeyguardManager != null
3133 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3134 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003135
Seigo Nonaka14e13912015-05-06 21:04:13 -07003136 private void showInputMethodMenu(boolean showAuxSubtypes) {
3137 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003140 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003141
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003142 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003143 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003144 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003145
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003146 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003147 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003148 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3149 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003150 if (immis == null || immis.size() == 0) {
3151 return;
3152 }
3153
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003154 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155
satok688bd472012-02-09 20:09:17 +09003156 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003157 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003158 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003159
satokc3690562012-01-10 20:14:43 +09003160 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003161 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003162 if (currentSubtype != null) {
3163 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003164 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3165 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003166 }
3167 }
3168
Ken Wakasa761eb372011-03-04 19:06:18 +09003169 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003170 mIms = new InputMethodInfo[N];
3171 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003172 int checkedItem = 0;
3173 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003174 final ImeSubtypeListItem item = imList.get(i);
3175 mIms[i] = item.mImi;
3176 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003177 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003178 int subtypeId = mSubtypeIds[i];
3179 if ((subtypeId == NOT_A_SUBTYPE_ID)
3180 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3181 || (subtypeId == lastInputMethodSubtypeId)) {
3182 checkedItem = i;
3183 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003186
3187 final Context settingsContext = new ContextThemeWrapper(context,
3188 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3189
3190 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003191 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3192 @Override
3193 public void onCancel(DialogInterface dialog) {
3194 hideInputMethodMenu();
3195 }
3196 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003197
3198 final Context dialogContext = mDialogBuilder.getContext();
3199 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3200 com.android.internal.R.styleable.DialogPreference,
3201 com.android.internal.R.attr.alertDialogStyle, 0);
3202 final Drawable dialogIcon = a.getDrawable(
3203 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3204 a.recycle();
3205
3206 mDialogBuilder.setIcon(dialogIcon);
3207
Yohei Yukawad34e1482016-02-11 08:03:52 -08003208 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003209 final View tv = inflater.inflate(
3210 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3211 mDialogBuilder.setCustomTitle(tv);
3212
3213 // Setup layout for a toggle switch of the hardware keyboard
3214 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003215 mSwitchingDialogTitleView
3216 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003217 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003218 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003219 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003220 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003221 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003222 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3223 @Override
3224 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003225 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226 // Ensure that the input method dialog is dismissed when changing
3227 // the hardware keyboard state.
3228 hideInputMethodMenu();
3229 }
3230 });
3231
Alan Viverette505e3ab2014-11-24 15:22:11 -08003232 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003233 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3234 final OnClickListener choiceListener = new OnClickListener() {
3235 @Override
3236 public void onClick(final DialogInterface dialog, final int which) {
3237 synchronized (mMethodMap) {
3238 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3239 || mSubtypeIds.length <= which) {
3240 return;
satok01038492012-04-09 21:08:27 +09003241 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003242 final InputMethodInfo im = mIms[which];
3243 int subtypeId = mSubtypeIds[which];
3244 adapter.mCheckedItem = which;
3245 adapter.notifyDataSetChanged();
3246 hideInputMethodMenu();
3247 if (im != null) {
3248 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3249 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003250 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003251 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 }
3254 }
3255 };
3256 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003259 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 mSwitchingDialog.getWindow().setType(
3261 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003262 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3263 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003264 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003265 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 mSwitchingDialog.show();
3267 }
3268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003269
Ken Wakasa05dbb652011-08-22 15:22:43 +09003270 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3271 private final LayoutInflater mInflater;
3272 private final int mTextViewResourceId;
3273 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003274 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003275 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3276 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3277 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003278
Ken Wakasa05dbb652011-08-22 15:22:43 +09003279 mTextViewResourceId = textViewResourceId;
3280 mItemsList = itemsList;
3281 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003282 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003283 }
3284
3285 @Override
3286 public View getView(int position, View convertView, ViewGroup parent) {
3287 final View view = convertView != null ? convertView
3288 : mInflater.inflate(mTextViewResourceId, null);
3289 if (position < 0 || position >= mItemsList.size()) return view;
3290 final ImeSubtypeListItem item = mItemsList.get(position);
3291 final CharSequence imeName = item.mImeName;
3292 final CharSequence subtypeName = item.mSubtypeName;
3293 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3294 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3295 if (TextUtils.isEmpty(subtypeName)) {
3296 firstTextView.setText(imeName);
3297 secondTextView.setVisibility(View.GONE);
3298 } else {
3299 firstTextView.setText(subtypeName);
3300 secondTextView.setText(imeName);
3301 secondTextView.setVisibility(View.VISIBLE);
3302 }
3303 final RadioButton radioButton =
3304 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3305 radioButton.setChecked(position == mCheckedItem);
3306 return view;
3307 }
3308 }
3309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003311 synchronized (mMethodMap) {
3312 hideInputMethodMenuLocked();
3313 }
3314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003315
The Android Open Source Project10592532009-03-18 17:39:46 -07003316 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003317 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318
The Android Open Source Project10592532009-03-18 17:39:46 -07003319 if (mSwitchingDialog != null) {
3320 mSwitchingDialog.dismiss();
3321 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003323
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003324 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003325 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003326 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003330
satok42c5a162011-05-26 16:46:14 +09003331 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003333 // TODO: Make this work even for non-current users?
3334 if (!calledFromValidUser()) {
3335 return false;
3336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 synchronized (mMethodMap) {
3338 if (mContext.checkCallingOrSelfPermission(
3339 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3340 != PackageManager.PERMISSION_GRANTED) {
3341 throw new SecurityException(
3342 "Requires permission "
3343 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3344 }
Anna Galusza9b278112016-01-04 11:37:37 -08003345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 long ident = Binder.clearCallingIdentity();
3347 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003348 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 } finally {
3350 Binder.restoreCallingIdentity(ident);
3351 }
3352 }
3353 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003354
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003355 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3356 // Make sure this is a valid input method.
3357 InputMethodInfo imm = mMethodMap.get(id);
3358 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003359 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003360 }
3361
satokd87c2592010-09-29 11:52:06 +09003362 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3363 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003364
satokd87c2592010-09-29 11:52:06 +09003365 if (enabled) {
3366 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3367 if (pair.first.equals(id)) {
3368 // We are enabling this input method, but it is already enabled.
3369 // Nothing to do. The previous state was enabled.
3370 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003371 }
3372 }
satokd87c2592010-09-29 11:52:06 +09003373 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3374 // Previous state was disabled.
3375 return false;
3376 } else {
3377 StringBuilder builder = new StringBuilder();
3378 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3379 builder, enabledInputMethodsList, id)) {
3380 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003381 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003382 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3383 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3384 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003385 }
3386 // Previous state was enabled.
3387 return true;
3388 } else {
3389 // We are disabling the input method but it is already disabled.
3390 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003391 return false;
3392 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003393 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003394 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003395
satok723a27e2010-11-11 14:58:11 +09003396 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3397 boolean setSubtypeOnly) {
3398 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003399 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003400
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003401 mCurUserActionNotificationSequenceNumber =
3402 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3403 if (DEBUG) {
3404 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3405 + mCurUserActionNotificationSequenceNumber);
3406 }
3407
3408 if (mCurClient != null && mCurClient.client != null) {
3409 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3410 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003411 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003412 }
3413
satok723a27e2010-11-11 14:58:11 +09003414 // Set Subtype here
3415 if (imi == null || subtypeId < 0) {
3416 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003417 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003418 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003419 if (subtypeId < imi.getSubtypeCount()) {
3420 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3421 mSettings.putSelectedSubtype(subtype.hashCode());
3422 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003423 } else {
3424 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003425 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003426 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003427 }
satokab751aa2010-09-14 19:17:36 +09003428 }
satok723a27e2010-11-11 14:58:11 +09003429
Yohei Yukawa68645a62016-02-17 07:54:20 -08003430 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003431 // Set InputMethod here
3432 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3433 }
3434 }
3435
3436 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3437 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3438 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3439 // newDefaultIme is empty when there is no candidate for the selected IME.
3440 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3441 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3442 if (subtypeHashCode != null) {
3443 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003445 imi, Integer.valueOf(subtypeHashCode));
3446 } catch (NumberFormatException e) {
3447 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3448 }
3449 }
3450 }
3451 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003452 }
3453
satok4e4569d2010-11-19 18:45:53 +09003454 // If there are no selected shortcuts, tries finding the most applicable ones.
3455 private Pair<InputMethodInfo, InputMethodSubtype>
3456 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3457 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3458 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003459 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003460 boolean foundInSystemIME = false;
3461
3462 // Search applicable subtype for each InputMethodInfo
3463 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003464 final String imiId = imi.getId();
3465 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3466 continue;
3467 }
satokcd7cd292010-11-20 15:46:23 +09003468 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003469 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003470 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003471 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003472 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003473 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003474 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003475 }
satokdf31ae62011-01-15 06:19:44 +09003476 // 2. Search by the system locale from enabledSubtypes.
3477 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003478 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003480 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003481 }
satoka86f5e42011-09-02 17:12:42 +09003482 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003483 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003484 final ArrayList<InputMethodSubtype> subtypesForSearch =
3485 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 ? InputMethodUtils.getSubtypes(imi)
3487 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003488 // 4. Search by the current subtype's locale from all subtypes.
3489 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003490 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003491 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003492 }
3493 // 5. Search by the system locale from all subtypes.
3494 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003495 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003496 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003497 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003498 }
satokcd7cd292010-11-20 15:46:23 +09003499 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003500 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003501 // The current input method is the most applicable IME.
3502 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003503 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003504 break;
satok7599a7f2010-12-22 13:45:23 +09003505 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003506 // The system input method is 2nd applicable IME.
3507 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003508 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003509 if ((imi.getServiceInfo().applicationInfo.flags
3510 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3511 foundInSystemIME = true;
3512 }
satok4e4569d2010-11-19 18:45:53 +09003513 }
3514 }
3515 }
3516 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003517 if (mostApplicableIMI != null) {
3518 Slog.w(TAG, "Most applicable shortcut input method was:"
3519 + mostApplicableIMI.getId());
3520 if (mostApplicableSubtype != null) {
3521 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3522 + "," + mostApplicableSubtype.getMode() + ","
3523 + mostApplicableSubtype.getLocale());
3524 }
3525 }
satok4e4569d2010-11-19 18:45:53 +09003526 }
satokcd7cd292010-11-20 15:46:23 +09003527 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003528 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003529 } else {
3530 return null;
3531 }
3532 }
3533
satokab751aa2010-09-14 19:17:36 +09003534 /**
3535 * @return Return the current subtype of this input method.
3536 */
satok42c5a162011-05-26 16:46:14 +09003537 @Override
satokab751aa2010-09-14 19:17:36 +09003538 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003539 // TODO: Make this work even for non-current users?
3540 if (!calledFromValidUser()) {
3541 return null;
3542 }
3543 synchronized (mMethodMap) {
3544 return getCurrentInputMethodSubtypeLocked();
3545 }
3546 }
3547
3548 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003549 if (mCurMethodId == null) {
3550 return null;
3551 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003552 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003553 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3554 if (imi == null || imi.getSubtypeCount() == 0) {
3555 return null;
satok4e4569d2010-11-19 18:45:53 +09003556 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003557 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003558 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3559 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003560 if (subtypeId == NOT_A_SUBTYPE_ID) {
3561 // If there are no selected subtypes, the framework will try to find
3562 // the most applicable subtype from explicitly or implicitly enabled
3563 // subtypes.
3564 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003565 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003566 // If there is only one explicitly or implicitly enabled subtype,
3567 // just returns it.
3568 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3569 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3570 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003571 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003572 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003573 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003574 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003575 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003576 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3577 true);
satok4e4569d2010-11-19 18:45:53 +09003578 }
satok3ef8b292010-11-23 06:06:29 +09003579 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003580 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003581 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003582 }
3583 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003584 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003585 }
3586
satok4e4569d2010-11-19 18:45:53 +09003587 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003588 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003589 @Override
satok4e4569d2010-11-19 18:45:53 +09003590 public List getShortcutInputMethodsAndSubtypes() {
3591 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003592 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003593 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003594 // If there are no selected shortcut subtypes, the framework will try to find
3595 // the most applicable subtype from all subtypes whose mode is
3596 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003597 Pair<InputMethodInfo, InputMethodSubtype> info =
3598 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003599 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003600 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003601 ret.add(info.first);
3602 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003603 }
satok3da92232011-01-11 22:46:30 +09003604 return ret;
satokf3db1af2010-11-23 13:34:33 +09003605 }
satokf3db1af2010-11-23 13:34:33 +09003606 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3607 ret.add(imi);
3608 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3609 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003610 }
3611 }
satokf3db1af2010-11-23 13:34:33 +09003612 return ret;
satok4e4569d2010-11-19 18:45:53 +09003613 }
3614 }
3615
satok42c5a162011-05-26 16:46:14 +09003616 @Override
satokb66d2872010-11-10 01:04:04 +09003617 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003618 // TODO: Make this work even for non-current users?
3619 if (!calledFromValidUser()) {
3620 return false;
3621 }
satokb66d2872010-11-10 01:04:04 +09003622 synchronized (mMethodMap) {
3623 if (subtype != null && mCurMethodId != null) {
3624 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003625 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003626 if (subtypeId != NOT_A_SUBTYPE_ID) {
3627 setInputMethodLocked(mCurMethodId, subtypeId);
3628 return true;
3629 }
3630 }
3631 return false;
3632 }
3633 }
3634
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003635 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003636 private static class InputMethodFileManager {
3637 private static final String SYSTEM_PATH = "system";
3638 private static final String INPUT_METHOD_PATH = "inputmethod";
3639 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3640 private static final String NODE_SUBTYPES = "subtypes";
3641 private static final String NODE_SUBTYPE = "subtype";
3642 private static final String NODE_IMI = "imi";
3643 private static final String ATTR_ID = "id";
3644 private static final String ATTR_LABEL = "label";
3645 private static final String ATTR_ICON = "icon";
3646 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003647 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003648 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3649 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3650 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003651 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003652 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3653 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003654 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003655 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003656 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003657 if (methodMap == null) {
3658 throw new NullPointerException("methodMap is null");
3659 }
3660 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003661 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003662 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3663 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003664 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003665 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003666 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3667 }
3668 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3669 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3670 if (!subtypeFile.exists()) {
3671 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003672 writeAdditionalInputMethodSubtypes(
3673 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003674 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003675 readAdditionalInputMethodSubtypes(
3676 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003677 }
3678 }
3679
3680 private void deleteAllInputMethodSubtypes(String imiId) {
3681 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003682 mAdditionalSubtypesMap.remove(imiId);
3683 writeAdditionalInputMethodSubtypes(
3684 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003685 }
3686 }
3687
3688 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003689 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003690 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003691 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003692 final int N = additionalSubtypes.length;
3693 for (int i = 0; i < N; ++i) {
3694 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003695 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003696 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003697 } else {
3698 Slog.w(TAG, "Duplicated subtype definition found: "
3699 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003700 }
3701 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003702 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3703 writeAdditionalInputMethodSubtypes(
3704 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003705 }
3706 }
3707
3708 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3709 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003710 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003711 }
3712 }
3713
3714 private static void writeAdditionalInputMethodSubtypes(
3715 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3716 HashMap<String, InputMethodInfo> methodMap) {
3717 // Safety net for the case that this function is called before methodMap is set.
3718 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3719 FileOutputStream fos = null;
3720 try {
3721 fos = subtypesFile.startWrite();
3722 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003723 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003724 out.startDocument(null, true);
3725 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3726 out.startTag(null, NODE_SUBTYPES);
3727 for (String imiId : allSubtypes.keySet()) {
3728 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3729 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3730 continue;
3731 }
3732 out.startTag(null, NODE_IMI);
3733 out.attribute(null, ATTR_ID, imiId);
3734 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3735 final int N = subtypesList.size();
3736 for (int i = 0; i < N; ++i) {
3737 final InputMethodSubtype subtype = subtypesList.get(i);
3738 out.startTag(null, NODE_SUBTYPE);
3739 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3740 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3741 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003742 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3743 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003744 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3745 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3746 out.attribute(null, ATTR_IS_AUXILIARY,
3747 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003748 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3749 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003750 out.endTag(null, NODE_SUBTYPE);
3751 }
3752 out.endTag(null, NODE_IMI);
3753 }
3754 out.endTag(null, NODE_SUBTYPES);
3755 out.endDocument();
3756 subtypesFile.finishWrite(fos);
3757 } catch (java.io.IOException e) {
3758 Slog.w(TAG, "Error writing subtypes", e);
3759 if (fos != null) {
3760 subtypesFile.failWrite(fos);
3761 }
3762 }
3763 }
3764
3765 private static void readAdditionalInputMethodSubtypes(
3766 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3767 if (allSubtypes == null || subtypesFile == null) return;
3768 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003769 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003770 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003771 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003772 int type = parser.getEventType();
3773 // Skip parsing until START_TAG
3774 while ((type = parser.next()) != XmlPullParser.START_TAG
3775 && type != XmlPullParser.END_DOCUMENT) {}
3776 String firstNodeName = parser.getName();
3777 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3778 throw new XmlPullParserException("Xml doesn't start with subtypes");
3779 }
3780 final int depth =parser.getDepth();
3781 String currentImiId = null;
3782 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3783 while (((type = parser.next()) != XmlPullParser.END_TAG
3784 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3785 if (type != XmlPullParser.START_TAG)
3786 continue;
3787 final String nodeName = parser.getName();
3788 if (NODE_IMI.equals(nodeName)) {
3789 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3790 if (TextUtils.isEmpty(currentImiId)) {
3791 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3792 continue;
3793 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003794 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003795 allSubtypes.put(currentImiId, tempSubtypesArray);
3796 } else if (NODE_SUBTYPE.equals(nodeName)) {
3797 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3798 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3799 continue;
3800 }
3801 final int icon = Integer.valueOf(
3802 parser.getAttributeValue(null, ATTR_ICON));
3803 final int label = Integer.valueOf(
3804 parser.getAttributeValue(null, ATTR_LABEL));
3805 final String imeSubtypeLocale =
3806 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003807 final String languageTag =
3808 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003809 final String imeSubtypeMode =
3810 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3811 final String imeSubtypeExtraValue =
3812 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003813 final boolean isAuxiliary = "1".equals(String.valueOf(
3814 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003815 final boolean isAsciiCapable = "1".equals(String.valueOf(
3816 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003817 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3818 .setSubtypeNameResId(label)
3819 .setSubtypeIconResId(icon)
3820 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003821 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003822 .setSubtypeMode(imeSubtypeMode)
3823 .setSubtypeExtraValue(imeSubtypeExtraValue)
3824 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003825 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003826 .build();
satoke7c6998e2011-06-03 17:57:59 +09003827 tempSubtypesArray.add(subtype);
3828 }
3829 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003830 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3831 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003832 return;
satoke7c6998e2011-06-03 17:57:59 +09003833 }
3834 }
3835 }
3836
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003837 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3838 @NonNull
3839 private final Handler mHandler;
3840
3841 LocalServiceImpl(@NonNull final Handler handler) {
3842 mHandler = handler;
3843 }
3844
3845 @Override
3846 public void setInteractive(boolean interactive) {
3847 // Do everything in handler so as not to block the caller.
3848 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3849 interactive ? 1 : 0, 0));
3850 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003851
3852 @Override
3853 public void switchInputMethod(boolean forwardDirection) {
3854 // Do everything in handler so as not to block the caller.
3855 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3856 forwardDirection ? 1 : 0, 0));
3857 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003858 }
3859
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003860 private static String imeWindowStatusToString(final int imeWindowVis) {
3861 final StringBuilder sb = new StringBuilder();
3862 boolean first = true;
3863 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3864 sb.append("Active");
3865 first = false;
3866 }
3867 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3868 if (!first) {
3869 sb.append("|");
3870 }
3871 sb.append("Visible");
3872 }
3873 return sb.toString();
3874 }
3875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 @Override
3877 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3878 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3879 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3882 + Binder.getCallingPid()
3883 + ", uid=" + Binder.getCallingUid());
3884 return;
3885 }
3886
3887 IInputMethod method;
3888 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003889 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 synchronized (mMethodMap) {
3894 p.println("Current Input Method Manager state:");
3895 int N = mMethodList.size();
3896 p.println(" Input Methods:");
3897 for (int i=0; i<N; i++) {
3898 InputMethodInfo info = mMethodList.get(i);
3899 p.println(" InputMethod #" + i + ":");
3900 info.dump(p, " ");
3901 }
3902 p.println(" Clients:");
3903 for (ClientState ci : mClients.values()) {
3904 p.println(" Client " + ci + ":");
3905 p.println(" client=" + ci.client);
3906 p.println(" inputContext=" + ci.inputContext);
3907 p.println(" sessionRequested=" + ci.sessionRequested);
3908 p.println(" curSession=" + ci.curSession);
3909 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003910 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003912 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3913 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003914 focusedWindowClient = mCurFocusedWindowClient;
3915 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3917 + " mBoundToMethod=" + mBoundToMethod);
3918 p.println(" mCurToken=" + mCurToken);
3919 p.println(" mCurIntent=" + mCurIntent);
3920 method = mCurMethod;
3921 p.println(" mCurMethod=" + mCurMethod);
3922 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003923 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 p.println(" mShowRequested=" + mShowRequested
3925 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3926 + " mShowForced=" + mShowForced
3927 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003928 p.println(" mCurUserActionNotificationSequenceNumber="
3929 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003930 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003931 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003932 p.println(" mSwitchingController:");
3933 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003934 p.println(" mSettings:");
3935 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003937
Jeff Brownb88102f2010-09-08 11:49:43 -07003938 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 pw.flush();
3941 try {
3942 client.client.asBinder().dump(fd, args);
3943 } catch (RemoteException e) {
3944 p.println("Input method client dead: " + e);
3945 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003946 } else {
3947 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003949
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003950 if (focusedWindowClient != null && client != focusedWindowClient) {
3951 p.println(" ");
3952 p.println("Warning: Current input method client doesn't match the last focused. "
3953 + "window.");
3954 p.println("Dumping input method client in the last focused window just in case.");
3955 p.println(" ");
3956 pw.flush();
3957 try {
3958 focusedWindowClient.client.asBinder().dump(fd, args);
3959 } catch (RemoteException e) {
3960 p.println("Input method client in focused window dead: " + e);
3961 }
3962 }
3963
Jeff Brownb88102f2010-09-08 11:49:43 -07003964 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 pw.flush();
3967 try {
3968 method.asBinder().dump(fd, args);
3969 } catch (RemoteException e) {
3970 p.println("Input method service dead: " + e);
3971 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003972 } else {
3973 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 }
3975 }
3976}