blob: 79d16da00ee78649fc86c2d66670b60ca995c39f [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);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002476 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002477 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(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002544 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2545 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002546 if (nextSubtype == null) {
2547 return false;
2548 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002549 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2550 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002551 return true;
2552 }
2553 }
2554
2555 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002556 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2557 if (!calledFromValidUser()) {
2558 return false;
2559 }
2560 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002561 if (!calledWithValidToken(token)) {
2562 final int uid = Binder.getCallingUid();
2563 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2564 + "token. uid:" + uid + " token:" + token);
2565 return false;
2566 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002567 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002568 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2569 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002570 if (nextSubtype == null) {
2571 return false;
2572 }
2573 return true;
2574 }
2575 }
2576
2577 @Override
satok68f1b782011-04-11 14:26:04 +09002578 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002579 if (!calledFromValidUser()) {
2580 return null;
2581 }
satok68f1b782011-04-11 14:26:04 +09002582 synchronized (mMethodMap) {
2583 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2584 // TODO: Handle the case of the last IME with no subtypes
2585 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2586 || TextUtils.isEmpty(lastIme.second)) return null;
2587 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2588 if (lastImi == null) return null;
2589 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002590 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002591 final int lastSubtypeId =
2592 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002593 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2594 return null;
2595 }
2596 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002597 } catch (NumberFormatException e) {
2598 return null;
2599 }
2600 }
2601 }
2602
satoke7c6998e2011-06-03 17:57:59 +09002603 @Override
satokee5e77c2011-09-02 18:50:15 +09002604 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002605 if (!calledFromValidUser()) {
2606 return;
2607 }
satok91e88122011-07-18 11:11:42 +09002608 // By this IPC call, only a process which shares the same uid with the IME can add
2609 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002610 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002611 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002612 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002613 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002614 final String[] packageInfos;
2615 try {
2616 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2617 } catch (RemoteException e) {
2618 Slog.e(TAG, "Failed to get package infos");
2619 return;
2620 }
satok91e88122011-07-18 11:11:42 +09002621 if (packageInfos != null) {
2622 final int packageNum = packageInfos.length;
2623 for (int i = 0; i < packageNum; ++i) {
2624 if (packageInfos[i].equals(imi.getPackageName())) {
2625 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002626 final long ident = Binder.clearCallingIdentity();
2627 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002628 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002629 } finally {
2630 Binder.restoreCallingIdentity(ident);
2631 }
satokee5e77c2011-09-02 18:50:15 +09002632 return;
satok91e88122011-07-18 11:11:42 +09002633 }
2634 }
2635 }
satoke7c6998e2011-06-03 17:57:59 +09002636 }
satokee5e77c2011-09-02 18:50:15 +09002637 return;
satoke7c6998e2011-06-03 17:57:59 +09002638 }
2639
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002640 @Override
2641 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002642 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002643 }
2644
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002645 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002646 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002647 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002648 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002649 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002650 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002651 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2652 if (DEBUG) {
2653 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2654 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2655 + " actual: " + sequenceNumber);
2656 }
2657 return;
2658 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002659 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2660 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002661 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002662 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002663 }
2664 }
2665
satok28203512010-11-24 11:06:49 +09002666 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002668 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2669 }
2670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002672 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2673 if (token == null) {
2674 if (mContext.checkCallingOrSelfPermission(
2675 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2676 != PackageManager.PERMISSION_GRANTED) {
2677 throw new SecurityException(
2678 "Using null token requires permission "
2679 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002681 } else if (mCurToken != token) {
2682 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2683 + " token: " + token);
2684 return;
2685 }
2686
2687 final long ident = Binder.clearCallingIdentity();
2688 try {
2689 setInputMethodLocked(id, subtypeId);
2690 } finally {
2691 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
2693 }
2694
satok42c5a162011-05-26 16:46:14 +09002695 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002697 if (!calledFromValidUser()) {
2698 return;
2699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002701 if (!calledWithValidToken(token)) {
2702 final int uid = Binder.getCallingUid();
2703 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2704 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 return;
2706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 long ident = Binder.clearCallingIdentity();
2708 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002709 hideCurrentInputLocked(flags, null);
2710 } finally {
2711 Binder.restoreCallingIdentity(ident);
2712 }
2713 }
2714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002715
satok42c5a162011-05-26 16:46:14 +09002716 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002717 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002718 if (!calledFromValidUser()) {
2719 return;
2720 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002721 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002722 if (!calledWithValidToken(token)) {
2723 final int uid = Binder.getCallingUid();
2724 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2725 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002726 return;
2727 }
2728 long ident = Binder.clearCallingIdentity();
2729 try {
2730 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 } finally {
2732 Binder.restoreCallingIdentity(ident);
2733 }
2734 }
2735 }
2736
2737 void setEnabledSessionInMainThread(SessionState session) {
2738 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002739 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002741 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002742 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 } catch (RemoteException e) {
2744 }
2745 }
2746 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002747 if (mEnabledSession != null && mEnabledSession.session != null) {
2748 try {
2749 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2750 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2751 } catch (RemoteException e) {
2752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
2754 }
2755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002756
satok42c5a162011-05-26 16:46:14 +09002757 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002759 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002761 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002762 final boolean showAuxSubtypes;
2763 switch (msg.arg1) {
2764 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2765 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2766 // implemented so that auxiliary subtypes will be excluded when the soft
2767 // keyboard is invisible.
2768 showAuxSubtypes = mInputShown;
2769 break;
2770 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2771 showAuxSubtypes = true;
2772 break;
2773 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2774 showAuxSubtypes = false;
2775 break;
2776 default:
2777 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2778 return false;
2779 }
2780 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002781 return true;
2782
satok47a44912010-10-06 16:03:58 +09002783 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002784 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002785 return true;
2786
2787 case MSG_SHOW_IM_CONFIG:
2788 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002789 return true;
2790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 case MSG_UNBIND_INPUT:
2794 try {
2795 ((IInputMethod)msg.obj).unbindInput();
2796 } catch (RemoteException e) {
2797 // There is nothing interesting about the method dying.
2798 }
2799 return true;
2800 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002801 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 try {
2803 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2804 } catch (RemoteException e) {
2805 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002806 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 return true;
2808 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002809 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002811 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002812 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002813 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 } catch (RemoteException e) {
2815 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002816 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 return true;
2818 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002819 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002821 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002822 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002823 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 } catch (RemoteException e) {
2825 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002826 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 return true;
2828 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002829 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002831 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2833 } catch (RemoteException e) {
2834 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002837 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002839 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002840 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002842 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002844 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002845 // Dispose the channel if the input method is not local to this process
2846 // because the remote proxy will get its own copy when unparceled.
2847 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002848 channel.dispose();
2849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002851 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002855
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002856 case MSG_START_INPUT: {
2857 int missingMethods = msg.arg1;
2858 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002860 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002862 session.method.startInput((IInputContext) args.arg2, missingMethods,
2863 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 } catch (RemoteException e) {
2865 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002866 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002868 }
2869 case MSG_RESTART_INPUT: {
2870 int missingMethods = msg.arg1;
2871 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002873 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002875 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2876 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 } catch (RemoteException e) {
2878 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002879 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002884
Yohei Yukawa33e81792015-11-17 21:14:42 -08002885 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002887 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 } catch (RemoteException e) {
2889 // There is nothing interesting about the last client dying.
2890 }
2891 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002892 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002893 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002894 IInputMethodClient client = (IInputMethodClient)args.arg1;
2895 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002897 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002899 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002900 } finally {
2901 // Dispose the channel if the input method is not local to this process
2902 // because the remote proxy will get its own copy when unparceled.
2903 if (res.channel != null && Binder.isProxy(client)) {
2904 res.channel.dispose();
2905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002907 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002909 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002910 case MSG_SET_ACTIVE:
2911 try {
2912 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2913 } catch (RemoteException e) {
2914 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2915 + ((ClientState)msg.obj).pid + " uid "
2916 + ((ClientState)msg.obj).uid);
2917 }
2918 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002919 case MSG_SET_INTERACTIVE:
2920 handleSetInteractive(msg.arg1 != 0);
2921 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002922 case MSG_SWITCH_IME:
2923 handleSwitchInputMethod(msg.arg1 != 0);
2924 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002925 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2926 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002927 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002928 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002929 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002930 } catch (RemoteException e) {
2931 Slog.w(TAG, "Got RemoteException sending "
2932 + "setUserActionNotificationSequenceNumber("
2933 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002934 + clientState.pid + " uid "
2935 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002936 }
2937 return true;
2938 }
satok01038492012-04-09 21:08:27 +09002939
2940 // --------------------------------------------------------------
2941 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002942 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002943 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
2945 return false;
2946 }
2947
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002948 private void handleSetInteractive(final boolean interactive) {
2949 synchronized (mMethodMap) {
2950 mIsInteractive = interactive;
2951 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2952
2953 // Inform the current client of the change in active status
2954 if (mCurClient != null && mCurClient.client != null) {
2955 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2956 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2957 }
2958 }
2959 }
2960
Yohei Yukawaae61f712015-12-09 13:00:10 -08002961 private void handleSwitchInputMethod(final boolean forwardDirection) {
2962 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002963 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002964 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002965 if (nextSubtype == null) {
2966 return;
2967 }
2968 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002969 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2970 if (newInputMethodInfo == null) {
2971 return;
2972 }
2973 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2974 newInputMethodInfo, mCurrentSubtype);
2975 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07002976 if (mSubtypeSwitchedByShortCutToast == null) {
2977 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
2978 Toast.LENGTH_SHORT);
2979 } else {
2980 mSubtypeSwitchedByShortCutToast.setText(toastText);
2981 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002982 mSubtypeSwitchedByShortCutToast.show();
2983 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002984 }
2985 }
2986
satokdc9ddae2011-10-06 12:22:36 +09002987 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002988 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2989 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002990 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002991 if (DEBUG) {
2992 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2993 }
satok723a27e2010-11-11 14:58:11 +09002994 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002995 return true;
2996 }
2997
2998 return false;
2999 }
3000
Yohei Yukawa94e33302016-02-12 19:37:03 -08003001 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003002 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003003 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003004 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003005 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003006 mMethodList.clear();
3007 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003008
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 // Use for queryIntentServicesAsUser
3010 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003012 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3013 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3014 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003015 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003017 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3018 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003019
satoke7c6998e2011-06-03 17:57:59 +09003020 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3021 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 for (int i = 0; i < services.size(); ++i) {
3023 ResolveInfo ri = services.get(i);
3024 ServiceInfo si = ri.serviceInfo;
3025 ComponentName compName = new ComponentName(si.packageName, si.name);
3026 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3027 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003028 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 + ": it does not require the permission "
3030 + android.Manifest.permission.BIND_INPUT_METHOD);
3031 continue;
3032 }
3033
Joe Onorato8a9b2202010-02-26 18:56:32 -08003034 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035
3036 try {
satoke7c6998e2011-06-03 17:57:59 +09003037 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003038 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003039 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003040 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041
3042 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003043 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003045 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003046 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 }
3048 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003049
Yohei Yukawa859df052016-02-17 07:56:46 -08003050 // TODO: The following code should find better place to live.
3051 if (!resetDefaultEnabledIme) {
3052 boolean enabledImeFound = false;
3053 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3054 final int N = enabledImes.size();
3055 for (int i = 0; i < N; ++i) {
3056 final InputMethodInfo imi = enabledImes.get(i);
3057 if (mMethodList.contains(imi)) {
3058 enabledImeFound = true;
3059 break;
3060 }
3061 }
3062 if (!enabledImeFound) {
3063 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3064 resetDefaultEnabledIme = true;
3065 resetSelectedInputMethodAndSubtypeLocked("");
3066 }
3067 }
3068
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003069 if (resetDefaultEnabledIme) {
3070 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003071 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003072 final int N = defaultEnabledIme.size();
3073 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003074 final InputMethodInfo imi = defaultEnabledIme.get(i);
3075 if (DEBUG) {
3076 Slog.d(TAG, "--- enable ime = " + imi);
3077 }
3078 setInputMethodEnabledLocked(imi.getId(), true);
3079 }
3080 }
3081
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003082 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003083 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003084 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003085 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3086 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003087 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003088 }
3089 } else {
3090 // Double check that the default IME is certainly enabled.
3091 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003092 }
3093 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003094 // Here is not the perfect place to reset the switching controller. Ideally
3095 // mSwitchingController and mSettings should be able to share the same state.
3096 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3097 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003098 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003102
satok217f5482010-12-15 05:19:19 +09003103 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003104 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003105 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003106 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3107 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003108 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003109 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003110 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003111 final int userId;
3112 synchronized (mMethodMap) {
3113 userId = mSettings.getCurrentUserId();
3114 }
3115 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003116 }
3117
3118 private void showConfigureInputMethods() {
3119 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3120 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3121 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3122 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003123 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003124 }
3125
satok2c93efc2012-04-02 19:33:47 +09003126 private boolean isScreenLocked() {
3127 return mKeyguardManager != null
3128 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3129 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003130
Seigo Nonaka14e13912015-05-06 21:04:13 -07003131 private void showInputMethodMenu(boolean showAuxSubtypes) {
3132 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003135 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003136
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003137 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003138 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003139 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003140
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003141 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003142 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003143 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3144 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003145 if (immis == null || immis.size() == 0) {
3146 return;
3147 }
3148
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003149 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150
satok688bd472012-02-09 20:09:17 +09003151 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003152 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003153 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003154
satokc3690562012-01-10 20:14:43 +09003155 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003156 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003157 if (currentSubtype != null) {
3158 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003159 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3160 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003161 }
3162 }
3163
Ken Wakasa761eb372011-03-04 19:06:18 +09003164 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003165 mIms = new InputMethodInfo[N];
3166 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003167 int checkedItem = 0;
3168 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003169 final ImeSubtypeListItem item = imList.get(i);
3170 mIms[i] = item.mImi;
3171 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003172 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003173 int subtypeId = mSubtypeIds[i];
3174 if ((subtypeId == NOT_A_SUBTYPE_ID)
3175 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3176 || (subtypeId == lastInputMethodSubtypeId)) {
3177 checkedItem = i;
3178 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003181
3182 final Context settingsContext = new ContextThemeWrapper(context,
3183 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3184
3185 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003186 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3187 @Override
3188 public void onCancel(DialogInterface dialog) {
3189 hideInputMethodMenu();
3190 }
3191 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003192
3193 final Context dialogContext = mDialogBuilder.getContext();
3194 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3195 com.android.internal.R.styleable.DialogPreference,
3196 com.android.internal.R.attr.alertDialogStyle, 0);
3197 final Drawable dialogIcon = a.getDrawable(
3198 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3199 a.recycle();
3200
3201 mDialogBuilder.setIcon(dialogIcon);
3202
Yohei Yukawad34e1482016-02-11 08:03:52 -08003203 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003204 final View tv = inflater.inflate(
3205 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3206 mDialogBuilder.setCustomTitle(tv);
3207
3208 // Setup layout for a toggle switch of the hardware keyboard
3209 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003210 mSwitchingDialogTitleView
3211 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003212 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003213 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003214 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003215 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003216 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003217 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3218 @Override
3219 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003220 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003221 // Ensure that the input method dialog is dismissed when changing
3222 // the hardware keyboard state.
3223 hideInputMethodMenu();
3224 }
3225 });
3226
Alan Viverette505e3ab2014-11-24 15:22:11 -08003227 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003228 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3229 final OnClickListener choiceListener = new OnClickListener() {
3230 @Override
3231 public void onClick(final DialogInterface dialog, final int which) {
3232 synchronized (mMethodMap) {
3233 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3234 || mSubtypeIds.length <= which) {
3235 return;
satok01038492012-04-09 21:08:27 +09003236 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003237 final InputMethodInfo im = mIms[which];
3238 int subtypeId = mSubtypeIds[which];
3239 adapter.mCheckedItem = which;
3240 adapter.notifyDataSetChanged();
3241 hideInputMethodMenu();
3242 if (im != null) {
3243 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3244 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003245 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003246 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003248 }
3249 }
3250 };
3251 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003254 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 mSwitchingDialog.getWindow().setType(
3256 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003257 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3258 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003259 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003260 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 mSwitchingDialog.show();
3262 }
3263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003264
Ken Wakasa05dbb652011-08-22 15:22:43 +09003265 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3266 private final LayoutInflater mInflater;
3267 private final int mTextViewResourceId;
3268 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003269 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003270 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3271 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3272 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003273
Ken Wakasa05dbb652011-08-22 15:22:43 +09003274 mTextViewResourceId = textViewResourceId;
3275 mItemsList = itemsList;
3276 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003277 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003278 }
3279
3280 @Override
3281 public View getView(int position, View convertView, ViewGroup parent) {
3282 final View view = convertView != null ? convertView
3283 : mInflater.inflate(mTextViewResourceId, null);
3284 if (position < 0 || position >= mItemsList.size()) return view;
3285 final ImeSubtypeListItem item = mItemsList.get(position);
3286 final CharSequence imeName = item.mImeName;
3287 final CharSequence subtypeName = item.mSubtypeName;
3288 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3289 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3290 if (TextUtils.isEmpty(subtypeName)) {
3291 firstTextView.setText(imeName);
3292 secondTextView.setVisibility(View.GONE);
3293 } else {
3294 firstTextView.setText(subtypeName);
3295 secondTextView.setText(imeName);
3296 secondTextView.setVisibility(View.VISIBLE);
3297 }
3298 final RadioButton radioButton =
3299 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3300 radioButton.setChecked(position == mCheckedItem);
3301 return view;
3302 }
3303 }
3304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003306 synchronized (mMethodMap) {
3307 hideInputMethodMenuLocked();
3308 }
3309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003310
The Android Open Source Project10592532009-03-18 17:39:46 -07003311 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003312 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313
The Android Open Source Project10592532009-03-18 17:39:46 -07003314 if (mSwitchingDialog != null) {
3315 mSwitchingDialog.dismiss();
3316 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003318
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003319 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003320 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003321 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003325
satok42c5a162011-05-26 16:46:14 +09003326 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003328 // TODO: Make this work even for non-current users?
3329 if (!calledFromValidUser()) {
3330 return false;
3331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 synchronized (mMethodMap) {
3333 if (mContext.checkCallingOrSelfPermission(
3334 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3335 != PackageManager.PERMISSION_GRANTED) {
3336 throw new SecurityException(
3337 "Requires permission "
3338 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3339 }
Anna Galusza9b278112016-01-04 11:37:37 -08003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 long ident = Binder.clearCallingIdentity();
3342 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003343 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 } finally {
3345 Binder.restoreCallingIdentity(ident);
3346 }
3347 }
3348 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003349
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003350 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3351 // Make sure this is a valid input method.
3352 InputMethodInfo imm = mMethodMap.get(id);
3353 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003354 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003355 }
3356
satokd87c2592010-09-29 11:52:06 +09003357 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3358 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003359
satokd87c2592010-09-29 11:52:06 +09003360 if (enabled) {
3361 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3362 if (pair.first.equals(id)) {
3363 // We are enabling this input method, but it is already enabled.
3364 // Nothing to do. The previous state was enabled.
3365 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003366 }
3367 }
satokd87c2592010-09-29 11:52:06 +09003368 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3369 // Previous state was disabled.
3370 return false;
3371 } else {
3372 StringBuilder builder = new StringBuilder();
3373 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3374 builder, enabledInputMethodsList, id)) {
3375 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003376 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003377 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3378 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3379 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003380 }
3381 // Previous state was enabled.
3382 return true;
3383 } else {
3384 // We are disabling the input method but it is already disabled.
3385 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003386 return false;
3387 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003388 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003389 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003390
satok723a27e2010-11-11 14:58:11 +09003391 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3392 boolean setSubtypeOnly) {
3393 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003395
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003396 mCurUserActionNotificationSequenceNumber =
3397 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3398 if (DEBUG) {
3399 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3400 + mCurUserActionNotificationSequenceNumber);
3401 }
3402
3403 if (mCurClient != null && mCurClient.client != null) {
3404 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3405 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003406 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003407 }
3408
satok723a27e2010-11-11 14:58:11 +09003409 // Set Subtype here
3410 if (imi == null || subtypeId < 0) {
3411 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003412 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003413 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003414 if (subtypeId < imi.getSubtypeCount()) {
3415 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3416 mSettings.putSelectedSubtype(subtype.hashCode());
3417 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003418 } else {
3419 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003420 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003422 }
satokab751aa2010-09-14 19:17:36 +09003423 }
satok723a27e2010-11-11 14:58:11 +09003424
Yohei Yukawa68645a62016-02-17 07:54:20 -08003425 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003426 // Set InputMethod here
3427 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3428 }
3429 }
3430
3431 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3432 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3433 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3434 // newDefaultIme is empty when there is no candidate for the selected IME.
3435 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3436 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3437 if (subtypeHashCode != null) {
3438 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003439 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003440 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003441 } catch (NumberFormatException e) {
3442 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3443 }
3444 }
3445 }
3446 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003447 }
3448
satok4e4569d2010-11-19 18:45:53 +09003449 // If there are no selected shortcuts, tries finding the most applicable ones.
3450 private Pair<InputMethodInfo, InputMethodSubtype>
3451 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3452 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3453 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003454 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003455 boolean foundInSystemIME = false;
3456
3457 // Search applicable subtype for each InputMethodInfo
3458 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003459 final String imiId = imi.getId();
3460 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3461 continue;
3462 }
satokcd7cd292010-11-20 15:46:23 +09003463 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003464 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003465 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003466 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003467 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003468 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003469 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003470 }
satokdf31ae62011-01-15 06:19:44 +09003471 // 2. Search by the system locale from enabledSubtypes.
3472 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003473 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003474 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003475 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003476 }
satoka86f5e42011-09-02 17:12:42 +09003477 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003478 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003479 final ArrayList<InputMethodSubtype> subtypesForSearch =
3480 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003481 ? InputMethodUtils.getSubtypes(imi)
3482 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003483 // 4. Search by the current subtype's locale from all subtypes.
3484 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003485 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003486 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003487 }
3488 // 5. Search by the system locale from all subtypes.
3489 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003490 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003491 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003492 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003493 }
satokcd7cd292010-11-20 15:46:23 +09003494 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003495 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003496 // The current input method is the most applicable IME.
3497 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003498 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003499 break;
satok7599a7f2010-12-22 13:45:23 +09003500 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003501 // The system input method is 2nd applicable IME.
3502 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003503 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003504 if ((imi.getServiceInfo().applicationInfo.flags
3505 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3506 foundInSystemIME = true;
3507 }
satok4e4569d2010-11-19 18:45:53 +09003508 }
3509 }
3510 }
3511 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003512 if (mostApplicableIMI != null) {
3513 Slog.w(TAG, "Most applicable shortcut input method was:"
3514 + mostApplicableIMI.getId());
3515 if (mostApplicableSubtype != null) {
3516 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3517 + "," + mostApplicableSubtype.getMode() + ","
3518 + mostApplicableSubtype.getLocale());
3519 }
3520 }
satok4e4569d2010-11-19 18:45:53 +09003521 }
satokcd7cd292010-11-20 15:46:23 +09003522 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003523 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003524 } else {
3525 return null;
3526 }
3527 }
3528
satokab751aa2010-09-14 19:17:36 +09003529 /**
3530 * @return Return the current subtype of this input method.
3531 */
satok42c5a162011-05-26 16:46:14 +09003532 @Override
satokab751aa2010-09-14 19:17:36 +09003533 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003534 // TODO: Make this work even for non-current users?
3535 if (!calledFromValidUser()) {
3536 return null;
3537 }
3538 synchronized (mMethodMap) {
3539 return getCurrentInputMethodSubtypeLocked();
3540 }
3541 }
3542
3543 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003544 if (mCurMethodId == null) {
3545 return null;
3546 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003547 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003548 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3549 if (imi == null || imi.getSubtypeCount() == 0) {
3550 return null;
satok4e4569d2010-11-19 18:45:53 +09003551 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003552 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003553 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3554 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003555 if (subtypeId == NOT_A_SUBTYPE_ID) {
3556 // If there are no selected subtypes, the framework will try to find
3557 // the most applicable subtype from explicitly or implicitly enabled
3558 // subtypes.
3559 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003560 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003561 // If there is only one explicitly or implicitly enabled subtype,
3562 // just returns it.
3563 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3564 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3565 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003566 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003567 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003568 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003569 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003571 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3572 true);
satok4e4569d2010-11-19 18:45:53 +09003573 }
satok3ef8b292010-11-23 06:06:29 +09003574 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003575 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003576 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003577 }
3578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003579 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003580 }
3581
satok4e4569d2010-11-19 18:45:53 +09003582 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003583 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003584 @Override
satok4e4569d2010-11-19 18:45:53 +09003585 public List getShortcutInputMethodsAndSubtypes() {
3586 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003587 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003588 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003589 // If there are no selected shortcut subtypes, the framework will try to find
3590 // the most applicable subtype from all subtypes whose mode is
3591 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003592 Pair<InputMethodInfo, InputMethodSubtype> info =
3593 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003594 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003595 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003596 ret.add(info.first);
3597 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003598 }
satok3da92232011-01-11 22:46:30 +09003599 return ret;
satokf3db1af2010-11-23 13:34:33 +09003600 }
satokf3db1af2010-11-23 13:34:33 +09003601 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3602 ret.add(imi);
3603 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3604 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003605 }
3606 }
satokf3db1af2010-11-23 13:34:33 +09003607 return ret;
satok4e4569d2010-11-19 18:45:53 +09003608 }
3609 }
3610
satok42c5a162011-05-26 16:46:14 +09003611 @Override
satokb66d2872010-11-10 01:04:04 +09003612 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003613 // TODO: Make this work even for non-current users?
3614 if (!calledFromValidUser()) {
3615 return false;
3616 }
satokb66d2872010-11-10 01:04:04 +09003617 synchronized (mMethodMap) {
3618 if (subtype != null && mCurMethodId != null) {
3619 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003620 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003621 if (subtypeId != NOT_A_SUBTYPE_ID) {
3622 setInputMethodLocked(mCurMethodId, subtypeId);
3623 return true;
3624 }
3625 }
3626 return false;
3627 }
3628 }
3629
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003630 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003631 private static class InputMethodFileManager {
3632 private static final String SYSTEM_PATH = "system";
3633 private static final String INPUT_METHOD_PATH = "inputmethod";
3634 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3635 private static final String NODE_SUBTYPES = "subtypes";
3636 private static final String NODE_SUBTYPE = "subtype";
3637 private static final String NODE_IMI = "imi";
3638 private static final String ATTR_ID = "id";
3639 private static final String ATTR_LABEL = "label";
3640 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003641 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003642 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003643 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003644 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3645 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3646 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003647 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003648 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3649 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003650 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003651 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003652 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003653 if (methodMap == null) {
3654 throw new NullPointerException("methodMap is null");
3655 }
3656 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003657 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003658 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3659 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003660 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003661 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003662 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3663 }
3664 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3665 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3666 if (!subtypeFile.exists()) {
3667 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003668 writeAdditionalInputMethodSubtypes(
3669 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003670 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003671 readAdditionalInputMethodSubtypes(
3672 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003673 }
3674 }
3675
3676 private void deleteAllInputMethodSubtypes(String imiId) {
3677 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003678 mAdditionalSubtypesMap.remove(imiId);
3679 writeAdditionalInputMethodSubtypes(
3680 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003681 }
3682 }
3683
3684 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003685 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003686 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003687 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003688 final int N = additionalSubtypes.length;
3689 for (int i = 0; i < N; ++i) {
3690 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003691 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003692 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003693 } else {
3694 Slog.w(TAG, "Duplicated subtype definition found: "
3695 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003696 }
3697 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003698 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3699 writeAdditionalInputMethodSubtypes(
3700 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003701 }
3702 }
3703
3704 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3705 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003706 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003707 }
3708 }
3709
3710 private static void writeAdditionalInputMethodSubtypes(
3711 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3712 HashMap<String, InputMethodInfo> methodMap) {
3713 // Safety net for the case that this function is called before methodMap is set.
3714 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3715 FileOutputStream fos = null;
3716 try {
3717 fos = subtypesFile.startWrite();
3718 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003719 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003720 out.startDocument(null, true);
3721 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3722 out.startTag(null, NODE_SUBTYPES);
3723 for (String imiId : allSubtypes.keySet()) {
3724 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3725 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3726 continue;
3727 }
3728 out.startTag(null, NODE_IMI);
3729 out.attribute(null, ATTR_ID, imiId);
3730 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3731 final int N = subtypesList.size();
3732 for (int i = 0; i < N; ++i) {
3733 final InputMethodSubtype subtype = subtypesList.get(i);
3734 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003735 if (subtype.hasSubtypeId()) {
3736 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3737 String.valueOf(subtype.getSubtypeId()));
3738 }
satoke7c6998e2011-06-03 17:57:59 +09003739 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 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003801 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003802 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003803 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003804 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 Yukawa66baf692016-04-11 02:29:35 -07003817 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003818 .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 Yukawa66baf692016-04-11 02:29:35 -07003825 .setIsAsciiCapable(isAsciiCapable);
3826 final String subtypeIdString =
3827 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3828 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003829 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003830 }
3831 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003832 }
3833 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003834 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3835 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003836 return;
satoke7c6998e2011-06-03 17:57:59 +09003837 }
3838 }
3839 }
3840
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003841 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3842 @NonNull
3843 private final Handler mHandler;
3844
3845 LocalServiceImpl(@NonNull final Handler handler) {
3846 mHandler = handler;
3847 }
3848
3849 @Override
3850 public void setInteractive(boolean interactive) {
3851 // Do everything in handler so as not to block the caller.
3852 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3853 interactive ? 1 : 0, 0));
3854 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003855
3856 @Override
3857 public void switchInputMethod(boolean forwardDirection) {
3858 // Do everything in handler so as not to block the caller.
3859 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3860 forwardDirection ? 1 : 0, 0));
3861 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003862 }
3863
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003864 private static String imeWindowStatusToString(final int imeWindowVis) {
3865 final StringBuilder sb = new StringBuilder();
3866 boolean first = true;
3867 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3868 sb.append("Active");
3869 first = false;
3870 }
3871 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3872 if (!first) {
3873 sb.append("|");
3874 }
3875 sb.append("Visible");
3876 }
3877 return sb.toString();
3878 }
3879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 @Override
3881 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3882 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3883 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3886 + Binder.getCallingPid()
3887 + ", uid=" + Binder.getCallingUid());
3888 return;
3889 }
3890
3891 IInputMethod method;
3892 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003893 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 synchronized (mMethodMap) {
3898 p.println("Current Input Method Manager state:");
3899 int N = mMethodList.size();
3900 p.println(" Input Methods:");
3901 for (int i=0; i<N; i++) {
3902 InputMethodInfo info = mMethodList.get(i);
3903 p.println(" InputMethod #" + i + ":");
3904 info.dump(p, " ");
3905 }
3906 p.println(" Clients:");
3907 for (ClientState ci : mClients.values()) {
3908 p.println(" Client " + ci + ":");
3909 p.println(" client=" + ci.client);
3910 p.println(" inputContext=" + ci.inputContext);
3911 p.println(" sessionRequested=" + ci.sessionRequested);
3912 p.println(" curSession=" + ci.curSession);
3913 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003914 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003916 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3917 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003918 focusedWindowClient = mCurFocusedWindowClient;
3919 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3921 + " mBoundToMethod=" + mBoundToMethod);
3922 p.println(" mCurToken=" + mCurToken);
3923 p.println(" mCurIntent=" + mCurIntent);
3924 method = mCurMethod;
3925 p.println(" mCurMethod=" + mCurMethod);
3926 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003927 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 p.println(" mShowRequested=" + mShowRequested
3929 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3930 + " mShowForced=" + mShowForced
3931 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003932 p.println(" mCurUserActionNotificationSequenceNumber="
3933 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003934 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003935 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003936 p.println(" mSwitchingController:");
3937 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003938 p.println(" mSettings:");
3939 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003941
Jeff Brownb88102f2010-09-08 11:49:43 -07003942 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 pw.flush();
3945 try {
3946 client.client.asBinder().dump(fd, args);
3947 } catch (RemoteException e) {
3948 p.println("Input method client dead: " + e);
3949 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003950 } else {
3951 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003953
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003954 if (focusedWindowClient != null && client != focusedWindowClient) {
3955 p.println(" ");
3956 p.println("Warning: Current input method client doesn't match the last focused. "
3957 + "window.");
3958 p.println("Dumping input method client in the last focused window just in case.");
3959 p.println(" ");
3960 pw.flush();
3961 try {
3962 focusedWindowClient.client.asBinder().dump(fd, args);
3963 } catch (RemoteException e) {
3964 p.println("Input method client in focused window dead: " + e);
3965 }
3966 }
3967
Jeff Brownb88102f2010-09-08 11:49:43 -07003968 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 pw.flush();
3971 try {
3972 method.asBinder().dump(fd, args);
3973 } catch (RemoteException e) {
3974 p.println("Input method service dead: " + e);
3975 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003976 } else {
3977 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 }
3979 }
3980}