blob: 2c4cfb9cee1c2be426a9165e889cef2c66ef9c84 [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
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080024import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070025import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090026import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090028import com.android.internal.inputmethod.InputMethodUtils;
29import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070031import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090032import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.view.IInputContext;
34import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.IInputMethodClient;
36import com.android.internal.view.IInputMethodManager;
37import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070038import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080040import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080041import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
satoke7c6998e2011-06-03 17:57:59 +090043import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090045import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070047import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070048import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070049import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080050import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.app.ActivityManagerNative;
52import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070053import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090054import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090055import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090056import android.app.Notification;
57import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070058import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090059import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ComponentName;
61import android.content.ContentResolver;
62import android.content.Context;
63import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090065import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090067import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070069import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090070import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.pm.PackageManager;
72import android.content.pm.ResolveInfo;
73import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070074import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.res.Resources;
76import android.content.res.TypedArray;
77import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080078import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080079import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080080import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070081import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040083import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070084import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090085import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Handler;
87import android.os.IBinder;
88import android.os.IInterface;
89import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070090import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070092import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080094import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.ServiceManager;
96import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090097import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080098import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.provider.Settings;
100import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900101import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700102import android.util.ArrayMap;
103import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700104import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900106import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900107import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.util.PrintWriterPrinter;
109import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900110import android.util.Slog;
111import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900112import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700114import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900115import android.view.LayoutInflater;
116import android.view.View;
117import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700118import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700120import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900121import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700123import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.view.inputmethod.InputMethod;
125import android.view.inputmethod.InputMethodInfo;
126import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700127import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900128import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900129import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900130import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900131import android.widget.CompoundButton;
132import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900133import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900134import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900135import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700136import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
satoke7c6998e2011-06-03 17:57:59 +0900138import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900140import java.io.FileInputStream;
141import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.IOException;
143import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700144import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100145import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700146import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900148import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.util.HashMap;
150import java.util.List;
151
152/**
153 * This class provides a system service that manages input methods.
154 */
155public class InputMethodManagerService extends IInputMethodManager.Stub
156 implements ServiceConnection, Handler.Callback {
157 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700158 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700159 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700161 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
162 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
163 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 static final int MSG_UNBIND_INPUT = 1000;
166 static final int MSG_BIND_INPUT = 1010;
167 static final int MSG_SHOW_SOFT_INPUT = 1020;
168 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700169 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 static final int MSG_ATTACH_TOKEN = 1040;
171 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final int MSG_START_INPUT = 2000;
174 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
Yohei Yukawa33e81792015-11-17 21:14:42 -0800176 static final int MSG_UNBIND_CLIENT = 3000;
177 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700178 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700179 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900180 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800181 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800182
satok01038492012-04-09 21:08:27 +0900183 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
184
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700185 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
186
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700187 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
satokf9f01002011-05-19 21:31:50 +0900189 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
190
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900191 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900192 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900193
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700194 @Retention(SOURCE)
195 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
196 private @interface HardKeyboardBehavior {
197 int WIRELESS_AFFORDANCE = 0;
198 int WIRED_AFFORDANCE = 1;
199 }
satok4e4569d2010-11-19 18:45:53 +0900200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800202 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900204 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700207 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700209 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900210 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900211 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900212 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800213 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900215 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 // All known input methods. mMethodMap also serves as the global
218 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700219 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
220 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900221 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700222 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900223 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700225 // Used to bring IME service up to visible adjustment while it is being shown.
226 final ServiceConnection mVisibleConnection = new ServiceConnection() {
227 @Override public void onServiceConnected(ComponentName name, IBinder service) {
228 }
229
230 @Override public void onServiceDisconnected(ComponentName name) {
231 }
232 };
233 boolean mVisibleBound = false;
234
satok7cfc0ed2011-06-20 21:29:36 +0900235 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700236 private NotificationManager mNotificationManager;
237 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700238 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400239 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700240 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900241 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900242 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900243 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900244
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900245 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 final ClientState client;
247 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700248
249 IInputMethodSession session;
250 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 @Override
253 public String toString() {
254 return "SessionState{uid " + client.uid + " pid " + client.pid
255 + " method " + Integer.toHexString(
256 System.identityHashCode(method))
257 + " session " + Integer.toHexString(
258 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700259 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 + "}";
261 }
262
263 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700264 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 client = _client;
266 method = _method;
267 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700268 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
Jeff Brownc28867a2013-03-26 15:42:39 -0700272 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 final IInputMethodClient client;
274 final IInputContext inputContext;
275 final int uid;
276 final int pid;
277 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 boolean sessionRequested;
280 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 @Override
283 public String toString() {
284 return "ClientState{" + Integer.toHexString(
285 System.identityHashCode(this)) + " uid " + uid
286 + " pid " + pid + "}";
287 }
288
289 ClientState(IInputMethodClient _client, IInputContext _inputContext,
290 int _uid, int _pid) {
291 client = _client;
292 inputContext = _inputContext;
293 uid = _uid;
294 pid = _pid;
295 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
296 }
297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700299 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700302 * Set once the system is ready to run third party code.
303 */
304 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700306 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700307 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
308 * method. This is to be synchronized with the secure settings keyed with
309 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
310 *
311 * <p>This can be transiently {@code null} when the system is re-initializing input method
312 * settings, e.g., the system locale is just changed.</p>
313 *
314 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
315 * {@link InputMethodManagerService}.</p>
316 *
317 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700319 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * The current binding sequence number, incremented every time there is
324 * a new bind performed.
325 */
326 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * The client that is currently bound to an input method.
330 */
331 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800334 * The last window token that we confirmed to be focused. This is always updated upon reports
335 * from the input method client. If the window state is already changed before the report is
336 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700337 */
338 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700340 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800341 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
342 */
343 ClientState mCurFocusedWindowClient;
344
345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * The input context last provided by the current client.
347 */
348 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700351 * The missing method flags for the input context last provided by the current client.
352 *
353 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
354 */
355 int mCurInputContextMissingMethods;
356
357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * The attributes last provided by the current client.
359 */
360 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700363 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700365 *
366 * <p>This can be {@code null} when no input method is connected.</p>
367 *
368 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700370 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
satokab751aa2010-09-14 19:17:36 +0900374 * The current subtype of the current input method.
375 */
376 private InputMethodSubtype mCurrentSubtype;
377
satok4e4569d2010-11-19 18:45:53 +0900378 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900379 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700380 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900381
John Spurlocke0980502013-10-25 11:59:29 -0400382 // Was the keyguard locked when this client became current?
383 private boolean mCurClientInKeyguard;
384
satokab751aa2010-09-14 19:17:36 +0900385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * Set to true if our ServiceConnection is currently actively bound to
387 * a service (whether or not we have gotten its IBinder back yet).
388 */
389 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * Set if the client has asked for the input method to be shown.
393 */
394 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * Set if we were explicitly told to show the input method.
398 */
399 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 /**
402 * Set if we were forced to be shown.
403 */
404 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * Set if we last told the input method to show itself.
408 */
409 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * The Intent used to connect to the current input method.
413 */
414 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /**
417 * The token we have made for the currently active input method, to
418 * identify it in the future.
419 */
420 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 /**
423 * If non-null, this is the input method service we are currently connected
424 * to.
425 */
426 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
429 * Time that we last initiated a bind to the input method, to determine
430 * if we should try to disconnect and reconnect to it.
431 */
432 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
435 * Have we called mCurMethod.bindInput()?
436 */
437 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
440 * Currently enabled session. Only touched by service thread, not
441 * protected by a lock.
442 */
443 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700446 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700448 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900450 int mCurUserActionNotificationSequenceNumber = 0;
451
Joe Onorato857fd9b2011-01-27 15:08:35 -0800452 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900453
454 /**
455 * A set of status bits regarding the active IME.
456 *
457 * <p>This value is a combination of following two bits:</p>
458 * <dl>
459 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
460 * <dd>
461 * If this bit is ON, connected IME is ready to accept touch/key events.
462 * </dd>
463 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
464 * <dd>
465 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
466 * </dd>
467 * </dl>
468 * <em>Do not update this value outside of setImeWindowStatus.</em>
469 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800470 int mImeWindowVis;
471
Ken Wakasa05dbb652011-08-22 15:22:43 +0900472 private AlertDialog.Builder mDialogBuilder;
473 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700474 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900475 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700476 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900477 private InputMethodInfo[] mIms;
478 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800479 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700480 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800481 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900482 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
483 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500484 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700485 @HardKeyboardBehavior
486 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700489 int mUserId;
490 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700491 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800492 String mLastEnabled = "";
493
Yohei Yukawa81482972015-06-04 00:58:59 -0700494 /**
495 * <em>This constructor must be called within the lock.</em>
496 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 SettingsObserver(Handler handler) {
498 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700499 }
500
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800501 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 if (mRegistered && mUserId == userId) {
503 return;
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700506 if (mRegistered) {
507 mContext.getContentResolver().unregisterContentObserver(this);
508 mRegistered = false;
509 }
510 if (mUserId != userId) {
511 mLastEnabled = "";
512 mUserId = userId;
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700515 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900516 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700517 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900518 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700519 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700520 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700521 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800522 resolver.registerContentObserver(Settings.Secure.getUriFor(
523 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700524 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800526
Michael Wright7b5a96b2014-08-09 19:28:42 -0700527 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800528 final Uri showImeUri = Settings.Secure.getUriFor(
529 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
530 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
531 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700533 if (showImeUri.equals(uri)) {
534 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800535 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
536 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
537 mContext.getContentResolver(),
538 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
539 0, mUserId) == 1;
540 if (mAccessibilityRequestingNoSoftKeyboard) {
541 final boolean showRequested = mShowRequested;
542 hideCurrentInputLocked(0, null);
543 mShowRequested = showRequested;
544 } else if (mShowRequested) {
545 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
546 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700547 } else {
548 boolean enabledChanged = false;
549 String newEnabled = mSettings.getEnabledInputMethodsStr();
550 if (!mLastEnabled.equals(newEnabled)) {
551 mLastEnabled = newEnabled;
552 enabledChanged = true;
553 }
554 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700558
559 @Override
560 public String toString() {
561 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
562 + " mLastEnabled=" + mLastEnabled + "}";
563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900566 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900567 @Override
568 public void onReceive(Context context, Intent intent) {
569 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700570 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900571 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700572 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900573 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800574 } else if (Intent.ACTION_USER_ADDED.equals(action)
575 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100576 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800577 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700578 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
579 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
580 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
581 final String prevValue = intent.getStringExtra(
582 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
583 final String newValue = intent.getStringExtra(
584 Intent.EXTRA_SETTING_NEW_VALUE);
585 restoreEnabledInputMethods(mContext, prevValue, newValue);
586 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900587 } else {
588 Slog.w(TAG, "Unexpected intent " + intent);
589 }
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800592
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700593 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
594 // does not attempt to validate on the fly with any installed device policy, so must only
595 // be run in the context of initial device setup.
596 //
597 // TODO: Move this method to InputMethodUtils with adding unit tests.
598 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
599 if (DEBUG_RESTORE) {
600 Slog.i(TAG, "Restoring enabled input methods:");
601 Slog.i(TAG, "prev=" + prevValue);
602 Slog.i(TAG, " new=" + newValue);
603 }
604 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900605 ArrayMap<String, ArraySet<String>> prevMap =
606 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
607 ArrayMap<String, ArraySet<String>> newMap =
608 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700609
610 // Merge the restored ime+subtype enabled states into the live state
611 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
612 final String imeId = entry.getKey();
613 ArraySet<String> prevSubtypes = prevMap.get(imeId);
614 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700615 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700616 prevMap.put(imeId, prevSubtypes);
617 }
618 prevSubtypes.addAll(entry.getValue());
619 }
620
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700621 final String mergedImesAndSubtypesString =
622 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700623 if (DEBUG_RESTORE) {
624 Slog.i(TAG, "Merged IME string:");
625 Slog.i(TAG, " " + mergedImesAndSubtypesString);
626 }
627 Settings.Secure.putString(context.getContentResolver(),
628 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
629 }
630
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800631 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900632 private boolean isChangingPackagesOfCurrentUser() {
633 final int userId = getChangingUserId();
634 final boolean retval = userId == mSettings.getCurrentUserId();
635 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900636 if (!retval) {
637 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
638 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 }
640 return retval;
641 }
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 if (!isChangingPackagesOfCurrentUser()) {
646 return false;
647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900649 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 final int N = mMethodList.size();
651 if (curInputMethodId != null) {
652 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 InputMethodInfo imi = mMethodList.get(i);
654 if (imi.getId().equals(curInputMethodId)) {
655 for (String pkg : packages) {
656 if (imi.getPackageName().equals(pkg)) {
657 if (!doit) {
658 return true;
659 }
satok723a27e2010-11-11 14:58:11 +0900660 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800661 chooseNewDefaultIMELocked();
662 return true;
663 }
664 }
665 }
666 }
667 }
668 }
669 return false;
670 }
671
672 @Override
673 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 if (!isChangingPackagesOfCurrentUser()) {
675 return;
676 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800677 synchronized (mMethodMap) {
678 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 final int N = mMethodList.size();
681 if (curInputMethodId != null) {
682 for (int i=0; i<N; i++) {
683 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900684 final String imiId = imi.getId();
685 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 curIm = imi;
687 }
satoke7c6998e2011-06-03 17:57:59 +0900688
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900690 if (isPackageModified(imi.getPackageName())) {
691 mFileManager.deleteAllInputMethodSubtypes(imiId);
692 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800693 if (change == PACKAGE_TEMPORARY_CHANGE
694 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800695 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 + imi.getComponent());
697 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 }
700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
Yohei Yukawa94e33302016-02-12 19:37:03 -0800702 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800706 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800707 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 if (change == PACKAGE_TEMPORARY_CHANGE
709 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800710 ServiceInfo si = null;
711 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900712 si = mIPackageManager.getServiceInfo(
713 curIm.getComponent(), 0, mSettings.getCurrentUserId());
714 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800715 }
716 if (si == null) {
717 // Uh oh, current input method is no longer around!
718 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800719 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900720 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800721 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800722 changed = true;
723 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800724 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900725 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800726 }
727 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800728 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800729 }
satokab751aa2010-09-14 19:17:36 +0900730
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800731 if (curIm == null) {
732 // We currently don't have a default input method... is
733 // one now available?
734 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700735 } else if (!changed && isPackageModified(curIm.getPackageName())) {
736 // Even if the current input method is still available, mCurrentSubtype could
737 // be obsolete when the package is modified in practice.
738 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800740
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800741 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800742 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 }
745 }
746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Jeff Brownc28867a2013-03-26 15:42:39 -0700748 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900749 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700750 private final IInputMethod mMethod;
751 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800752
Jeff Brownc28867a2013-03-26 15:42:39 -0700753 MethodCallback(InputMethodManagerService imms, IInputMethod method,
754 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900755 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 mMethod = method;
757 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
satoke7c6998e2011-06-03 17:57:59 +0900760 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700761 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700762 long ident = Binder.clearCallingIdentity();
763 try {
764 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
765 } finally {
766 Binder.restoreCallingIdentity(ident);
767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800770
satok01038492012-04-09 21:08:27 +0900771 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700772 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900773 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700774 public void onHardKeyboardStatusChange(boolean available) {
775 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
776 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900777 }
778
Michael Wright7b5a96b2014-08-09 19:28:42 -0700779 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900780 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700781 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900782 }
783 synchronized(mMethodMap) {
784 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
785 && mSwitchingDialog.isShowing()) {
786 mSwitchingDialogTitleView.findViewById(
787 com.android.internal.R.id.hard_keyboard_section).setVisibility(
788 available ? View.VISIBLE : View.GONE);
789 }
790 }
791 }
792 }
793
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800794 public static final class Lifecycle extends SystemService {
795 private InputMethodManagerService mService;
796
797 public Lifecycle(Context context) {
798 super(context);
799 mService = new InputMethodManagerService(context);
800 }
801
802 @Override
803 public void onStart() {
804 LocalServices.addService(InputMethodManagerInternal.class,
805 new LocalServiceImpl(mService.mHandler));
806 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
807 }
808
809 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800810 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700811 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800812 // TODO: Dispatch this to a worker thread as needed.
813 mService.onSwitchUser(userHandle);
814 }
815
816 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800817 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700818 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800819 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800820 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
821 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
822 .getService(Context.STATUS_BAR_SERVICE);
823 mService.systemRunning(statusBarService);
824 }
825 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800826
827 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700828 public void onUnlockUser(final @UserIdInt int userHandle) {
829 // Called on ActivityManager thread.
830 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700831 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800832 }
833 }
834
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800835 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800836 synchronized(mMethodMap) {
837 final int currentUserId = mSettings.getCurrentUserId();
838 if (DEBUG) {
839 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
840 }
841 if (userId != currentUserId) {
842 return;
843 }
844 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
845 // We need to rebuild IMEs.
846 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
847 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
848 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800849 }
850
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800851 void onSwitchUser(@UserIdInt int userId) {
852 synchronized (mMethodMap) {
853 switchUserLocked(userId);
854 }
855 }
856
Seigo Nonaka7309b122015-08-17 18:34:13 -0700857 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900858 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800860 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700862 // Note: SettingsObserver doesn't register observers in its constructor.
863 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 mIWindowManager = IWindowManager.Stub.asInterface(
865 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700866 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800867 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900868 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 public void executeMessage(Message msg) {
870 handleMessage(msg);
871 }
Mita Yuned218c72012-12-06 17:18:25 -0800872 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800873 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800874 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900875 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700876 mHasFeature = context.getPackageManager().hasSystemFeature(
877 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500878 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700879 mHardKeyboardBehavior = mContext.getResources().getInteger(
880 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900881
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400882 Bundle extras = new Bundle();
883 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
884 mImeSwitcherNotification = new Notification.Builder(mContext)
885 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
886 .setWhen(0)
887 .setOngoing(true)
888 .addExtras(extras)
889 .setCategory(Notification.CATEGORY_SYSTEM)
890 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400891
satok7cfc0ed2011-06-20 21:29:36 +0900892 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900893 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900894
895 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900896
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900898 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800899 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
900 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700901 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900902 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
satok7cfc0ed2011-06-20 21:29:36 +0900904 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900905 int userId = 0;
906 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
908 } catch (RemoteException e) {
909 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
910 }
satok81f8b7c2012-12-04 20:42:56 +0900911 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900912
satokd87c2592010-09-29 11:52:06 +0900913 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900914 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800915 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700916
Kenny Guy2a764942014-04-02 13:29:20 +0100917 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900918 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900919 synchronized (mMethodMap) {
920 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
921 mSettings, context);
922 }
satok0a1bcf42012-05-16 19:26:31 +0900923
924 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900925 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900926 if (DEBUG) {
927 Slog.d(TAG, "Initial default ime = " + defaultImiId);
928 }
satok0a1bcf42012-05-16 19:26:31 +0900929 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
930
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900931 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800932 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900933 }
satokd87c2592010-09-29 11:52:06 +0900934 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935
satok0a1bcf42012-05-16 19:26:31 +0900936 if (!mImeSelectedOnBoot) {
937 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900938 synchronized (mMethodMap) {
939 resetDefaultImeLocked(context);
940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900943 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700944 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900945 updateFromSettingsLocked(true);
946 }
satok5b927c432012-05-01 20:09:34 +0900947
948 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
949 // according to the new system locale.
950 final IntentFilter filter = new IntentFilter();
951 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
952 mContext.registerReceiver(
953 new BroadcastReceiver() {
954 @Override
955 public void onReceive(Context context, Intent intent) {
956 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900957 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900958 }
959 }
960 }, filter);
961 }
962
satok5b927c432012-05-01 20:09:34 +0900963 private void resetDefaultImeLocked(Context context) {
964 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800965 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900966 return;
967 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800968 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
969 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
970 if (suitableImes.isEmpty()) {
971 Slog.i(TAG, "No default found");
972 return;
satok5b927c432012-05-01 20:09:34 +0900973 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800974 final InputMethodInfo defIm = suitableImes.get(0);
975 Slog.i(TAG, "Default found, using " + defIm.getId());
976 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900977 }
978
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900979 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
980 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 if (!mSystemReady) {
982 // not system ready
983 return;
984 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800985 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800987 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 if (!updateOnlyWhenLocaleChanged) {
989 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800990 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 }
992 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800993 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800995 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 if (!updateOnlyWhenLocaleChanged) {
997 final String selectedImiId = mSettings.getSelectedInputMethod();
998 if (TextUtils.isEmpty(selectedImiId)) {
999 // This is the first time of the user switch and
1000 // set the current ime to the proper one.
1001 resetDefaultImeLocked(mContext);
1002 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001003 } else {
1004 // If the locale is changed, needs to reset the default ime
1005 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001006 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001007 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001008 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001009 if (!updateOnlyWhenLocaleChanged) {
1010 try {
1011 startInputInnerLocked();
1012 } catch (RuntimeException e) {
1013 Slog.w(TAG, "Unexpected exception", e);
1014 }
1015 }
1016 }
1017 }
1018
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001019 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001020 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1021 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001022 }
1023
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001024 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001025 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1026 + " currentUserId=" + mSettings.getCurrentUserId());
1027
Yohei Yukawa81482972015-06-04 00:58:59 -07001028 // ContentObserver should be registered again when the user is changed
1029 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001030
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001031 // If the system is not ready or the device is not yed unlocked by the user, then we use
1032 // copy-on-write settings.
1033 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001034 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001035 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001036 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001037 // InputMethodFileManager should be reset when the user is changed
1038 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001039 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001040
1041 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1042 + " defaultImiId=" + defaultImiId);
1043
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001044 // For secondary users, the list of enabled IMEs may not have been updated since the
1045 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1046 // not be empty even if the IME has been uninstalled by the primary user.
1047 // Even in such cases, IMMS works fine because it will find the most applicable
1048 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001049 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001050 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001051 initialUserSwitch /* needsToResetDefaultIme */);
1052 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001053 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1054 mSettings.getEnabledInputMethodListLocked(), newUserId,
1055 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001056 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001057
1058 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1059 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001060 }
1061
Kenny Guy2a764942014-04-02 13:29:20 +01001062 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001063 mSettings.setCurrentProfileIds(
1064 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001065 }
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 @Override
1068 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1069 throws RemoteException {
1070 try {
1071 return super.onTransact(code, data, reply, flags);
1072 } catch (RuntimeException e) {
1073 // The input method manager only throws security exceptions, so let's
1074 // log all others.
1075 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001076 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078 throw e;
1079 }
1080 }
1081
Svetoslav Ganova0027152013-06-25 14:59:53 -07001082 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001083 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001084 if (DEBUG) {
1085 Slog.d(TAG, "--- systemReady");
1086 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001087 if (!mSystemReady) {
1088 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001089 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001090 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001091 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001092 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1093 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001094 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001095 if (mStatusBar != null) {
1096 mStatusBar.setIconVisibility(mSlotIme, false);
1097 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001098 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001099 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1100 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001101 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001102 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001103 mHardKeyboardListener);
1104 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001105 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001106 if (!mImeSelectedOnBoot) {
1107 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001108 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001109 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1110 mSettings.getEnabledInputMethodListLocked(),
1111 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001112 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001113 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001114 try {
1115 startInputInnerLocked();
1116 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001118 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001119 }
1120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001123 // ---------------------------------------------------------------------------------------
1124 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1125 // 1) it comes from the system process
1126 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1127 private boolean calledFromValidUser() {
1128 final int uid = Binder.getCallingUid();
1129 final int userId = UserHandle.getUserId(uid);
1130 if (DEBUG) {
1131 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1132 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1133 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001134 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1135 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001136 }
Kenny Guy2a764942014-04-02 13:29:20 +01001137 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001139 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001140
1141 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1142 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1143 // must not manage background users' states in any functions.
1144 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1145 // by a token.
1146 if (mContext.checkCallingOrSelfPermission(
1147 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1148 == PackageManager.PERMISSION_GRANTED) {
1149 if (DEBUG) {
1150 Slog.d(TAG, "--- Access granted because the calling process has "
1151 + "the INTERACT_ACROSS_USERS_FULL permission");
1152 }
1153 return true;
1154 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001155 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1156 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001157 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001158 }
1159
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001160
1161 /**
1162 * Returns true iff the caller is identified to be the current input method with the token.
1163 * @param token The window token given to the input method when it was started.
1164 * @return true if and only if non-null valid token is specified.
1165 */
1166 private boolean calledWithValidToken(IBinder token) {
1167 if (token == null || mCurToken != token) {
1168 return false;
1169 }
1170 return true;
1171 }
1172
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001173 private boolean bindCurrentInputMethodService(
1174 Intent service, ServiceConnection conn, int flags) {
1175 if (service == null || conn == null) {
1176 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1177 return false;
1178 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001179 return mContext.bindServiceAsUser(service, conn, flags,
1180 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 }
1182
satoke7c6998e2011-06-03 17:57:59 +09001183 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 public List<InputMethodInfo> getInputMethodList() {
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) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001190 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
satoke7c6998e2011-06-03 17:57:59 +09001194 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001196 // TODO: Make this work even for non-current users?
1197 if (!calledFromValidUser()) {
1198 return Collections.emptyList();
1199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001201 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 }
1204
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001205 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001206 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001207 * @return enabled subtypes of the specified imi
1208 */
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001210 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001211 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 // TODO: Make this work even for non-current users?
1213 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001214 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001215 }
satok67ddf9c2010-11-17 09:45:54 +09001216 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001217 final InputMethodInfo imi;
1218 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001219 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001220 } else {
1221 imi = mMethodMap.get(imiId);
1222 }
1223 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001224 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001225 }
1226 return mSettings.getEnabledInputMethodSubtypeListLocked(
1227 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001228 }
1229 }
1230
satoke7c6998e2011-06-03 17:57:59 +09001231 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 public void addClient(IInputMethodClient client,
1233 IInputContext inputContext, int uid, int pid) {
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) {
1238 mClients.put(client.asBinder(), new ClientState(client,
1239 inputContext, uid, pid));
1240 }
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
satoke7c6998e2011-06-03 17:57:59 +09001243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001245 if (!calledFromValidUser()) {
1246 return;
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001249 ClientState cs = mClients.remove(client.asBinder());
1250 if (cs != null) {
1251 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001252 if (mCurClient == cs) {
1253 mCurClient = null;
1254 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001255 if (mCurFocusedWindowClient == cs) {
1256 mCurFocusedWindowClient = null;
1257 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 void executeOrSendMessage(IInterface target, Message msg) {
1263 if (target.asBinder() instanceof Binder) {
1264 mCaller.sendMessage(msg);
1265 } else {
1266 handleMessage(msg);
1267 msg.recycle();
1268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
Yohei Yukawa33e81792015-11-17 21:14:42 -08001271 void unbindCurrentClientLocked(
1272 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001274 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 + mCurClient.client.asBinder());
1276 if (mBoundToMethod) {
1277 mBoundToMethod = false;
1278 if (mCurMethod != null) {
1279 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1280 MSG_UNBIND_INPUT, mCurMethod));
1281 }
1282 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001283
1284 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1285 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001286 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1287 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project10592532009-03-18 17:39:46 -07001291 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 private int getImeShowFlags() {
1296 int flags = 0;
1297 if (mShowForced) {
1298 flags |= InputMethod.SHOW_FORCED
1299 | InputMethod.SHOW_EXPLICIT;
1300 } else if (mShowExplicitlyRequested) {
1301 flags |= InputMethod.SHOW_EXPLICIT;
1302 }
1303 return flags;
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 private int getAppShowFlags() {
1307 int flags = 0;
1308 if (mShowForced) {
1309 flags |= InputMethodManager.SHOW_FORCED;
1310 } else if (!mShowExplicitlyRequested) {
1311 flags |= InputMethodManager.SHOW_IMPLICIT;
1312 }
1313 return flags;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
Dianne Hackborn7663d802012-02-24 13:08:49 -08001316 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (!mBoundToMethod) {
1318 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1319 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1320 mBoundToMethod = true;
1321 }
1322 final SessionState session = mCurClient.curSession;
1323 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001324 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1325 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1326 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001328 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1329 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1330 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001333 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001334 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001336 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001337 (session.channel != null ? session.channel.dup() : null),
1338 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001341 InputBindResult startInputLocked(
1342 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001343 IInputMethodClient client, IInputContext inputContext,
1344 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001345 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // If no method is currently selected, do nothing.
1347 if (mCurMethodId == null) {
1348 return mNoBinding;
1349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 ClientState cs = mClients.get(client.asBinder());
1352 if (cs == null) {
1353 throw new IllegalArgumentException("unknown client "
1354 + client.asBinder());
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
Yohei Yukawa74750f22016-03-22 12:54:22 -07001357 if (attribute == null) {
1358 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1359 + " uid=" + cs.uid + " pid=" + cs.pid);
1360 return null;
1361 }
1362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 try {
1364 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1365 // Check with the window manager to make sure this client actually
1366 // has a window with focus. If not, reject. This is thread safe
1367 // because if the focus changes some time before or after, the
1368 // next client receiving focus that has any interest in input will
1369 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1372 return null;
1373 }
1374 } catch (RemoteException e) {
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001377 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1378 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001379 }
1380
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001381 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001382 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001383 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001384 // If no method is currently selected, do nothing.
1385 if (mCurMethodId == null) {
1386 return mNoBinding;
1387 }
1388
Yohei Yukawad57ba672015-06-08 16:39:46 -07001389 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1390 attribute.packageName)) {
1391 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1392 + " uid=" + cs.uid + " package=" + attribute.packageName);
1393 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001394 }
1395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001397 // Was the keyguard locked when switching over to the new client?
1398 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 // If the client is changing, we need to switch over to the new
1400 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001401 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001402 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001403 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404
1405 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001406 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001407 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001408 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 // Bump up the sequence for this client and attach it.
1413 mCurSeq++;
1414 if (mCurSeq <= 0) mCurSeq = 1;
1415 mCurClient = cs;
1416 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001417 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 // Check if the input method is changing.
1421 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1422 if (cs.curSession != null) {
1423 // Fast case: if we are already connected to the input method,
1424 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001425 return attachNewInputLocked(
1426 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428 if (mHaveConnection) {
1429 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 // Return to client, and we will get back with it when
1431 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001432 requestClientSessionLocked(cs);
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 if (SystemClock.uptimeMillis()
1436 < (mLastBindTime+TIME_TO_RECONNECT)) {
1437 // In this case we have connected to the service, but
1438 // don't yet have its interface. If it hasn't been too
1439 // long since we did the connection, we'll return to
1440 // the client and wait to get the service interface so
1441 // we can report back. If it has been too long, we want
1442 // to fall through so we can try a disconnect/reconnect
1443 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001444 return new InputBindResult(null, null, mCurId, mCurSeq,
1445 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1448 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450 }
1451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001453 return startInputInnerLocked();
1454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001456 InputBindResult startInputInnerLocked() {
1457 if (mCurMethodId == null) {
1458 return mNoBinding;
1459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001461 if (!mSystemReady) {
1462 // If the system is not yet ready, we shouldn't be running third
1463 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001464 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1465 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1469 if (info == null) {
1470 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001472
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001473 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1476 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001477 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1478 com.android.internal.R.string.input_method_binding_label);
1479 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1480 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001482 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1483 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mLastBindTime = SystemClock.uptimeMillis();
1485 mHaveConnection = true;
1486 mCurId = info.getId();
1487 mCurToken = new Binder();
1488 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001489 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001490 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 } catch (RemoteException e) {
1492 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001493 return new InputBindResult(null, null, mCurId, mCurSeq,
1494 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 } else {
1496 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001497 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 + mCurIntent);
1499 }
1500 return null;
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001503 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001504 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001505 IInputMethodClient client, IInputContext inputContext,
1506 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001507 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 if (!calledFromValidUser()) {
1509 return null;
1510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001512 if (DEBUG) {
1513 Slog.v(TAG, "startInput: reason="
1514 + InputMethodClient.getStartInputReason(startInputReason)
1515 + " client = " + client.asBinder()
1516 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001517 + " missingMethods="
1518 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001519 + " attribute=" + attribute
1520 + " controlFlags=#" + Integer.toHexString(controlFlags));
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 final long ident = Binder.clearCallingIdentity();
1523 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001524 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1525 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 } finally {
1527 Binder.restoreCallingIdentity(ident);
1528 }
1529 }
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
satoke7c6998e2011-06-03 17:57:59 +09001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public void finishInput(IInputMethodClient client) {
1534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
satoke7c6998e2011-06-03 17:57:59 +09001536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public void onServiceConnected(ComponentName name, IBinder service) {
1538 synchronized (mMethodMap) {
1539 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1540 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001541 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001542 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001543 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001544 return;
1545 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001546 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001547 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1548 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001550 clearClientSessionLocked(mCurClient);
1551 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
1553 }
1554 }
1555 }
1556
Jeff Brownc28867a2013-03-26 15:42:39 -07001557 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1558 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 synchronized (mMethodMap) {
1560 if (mCurMethod != null && method != null
1561 && mCurMethod.asBinder() == method.asBinder()) {
1562 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001563 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001566 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 if (res.method != null) {
1568 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001569 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001571 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 }
1574 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001575
1576 // Session abandoned. Close its associated input channel.
1577 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001579
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001580 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001581 if (mVisibleBound) {
1582 mContext.unbindService(mVisibleConnection);
1583 mVisibleBound = false;
1584 }
1585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001586 if (mHaveConnection) {
1587 mContext.unbindService(this);
1588 mHaveConnection = false;
1589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001591 if (mCurToken != null) {
1592 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001593 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001594 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001595 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001596 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001597 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001598 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001599 } catch (RemoteException e) {
1600 }
1601 mCurToken = null;
1602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001603
The Android Open Source Project10592532009-03-18 17:39:46 -07001604 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001605 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
Yohei Yukawa33e81792015-11-17 21:14:42 -08001608 void resetCurrentMethodAndClient(
1609 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001610 mCurMethodId = null;
1611 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001612 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001613 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001614
1615 void requestClientSessionLocked(ClientState cs) {
1616 if (!cs.sessionRequested) {
1617 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1618 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1619 cs.sessionRequested = true;
1620 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1621 MSG_CREATE_SESSION, mCurMethod, channels[1],
1622 new MethodCallback(this, mCurMethod, channels[0])));
1623 }
1624 }
1625
1626 void clearClientSessionLocked(ClientState cs) {
1627 finishSessionLocked(cs.curSession);
1628 cs.curSession = null;
1629 cs.sessionRequested = false;
1630 }
1631
1632 private void finishSessionLocked(SessionState sessionState) {
1633 if (sessionState != null) {
1634 if (sessionState.session != null) {
1635 try {
1636 sessionState.session.finishSession();
1637 } catch (RemoteException e) {
1638 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001639 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001640 }
1641 sessionState.session = null;
1642 }
1643 if (sessionState.channel != null) {
1644 sessionState.channel.dispose();
1645 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001646 }
1647 }
1648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001649
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001650 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if (mCurMethod != null) {
1652 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001653 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001655
Jeff Brownc28867a2013-03-26 15:42:39 -07001656 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001657 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 mCurMethod = null;
1659 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001660 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001661 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
satoke7c6998e2011-06-03 17:57:59 +09001665 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 public void onServiceDisconnected(ComponentName name) {
1667 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001668 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 + " mCurIntent=" + mCurIntent);
1670 if (mCurMethod != null && mCurIntent != null
1671 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001672 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 // We consider this to be a new bind attempt, since the system
1674 // should now try to restart the service for us.
1675 mLastBindTime = SystemClock.uptimeMillis();
1676 mShowRequested = mInputShown;
1677 mInputShown = false;
1678 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001679 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1680 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1681 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
1684 }
1685 }
1686
satokf9f01002011-05-19 21:31:50 +09001687 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1689 long ident = Binder.clearCallingIdentity();
1690 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001692 if (!calledWithValidToken(token)) {
1693 final int uid = Binder.getCallingUid();
1694 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1695 + " token:" + token);
1696 return;
1697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001699 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001700 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001701 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001704 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001705 CharSequence contentDescription = null;
1706 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001707 // Use PackageManager to load label
1708 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001709 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001710 mIPackageManager.getApplicationInfo(packageName, 0,
1711 mSettings.getCurrentUserId()));
1712 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001713 /* ignore */
1714 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001715 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001716 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001717 contentDescription != null
1718 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001719 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722 }
1723 } finally {
1724 Binder.restoreCallingIdentity(ident);
1725 }
1726 }
1727
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001728 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001729 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001730 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001731 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001732 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001733 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001734 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1735 // When physical keyboard is attached, we show the ime switcher (or notification if
1736 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1737 // exists in the IME switcher dialog. Might be OK to remove this condition once
1738 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1739 return true;
1740 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001741 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1742 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001743 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001744
1745 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1746 final int N = imis.size();
1747 if (N > 2) return true;
1748 if (N < 1) return false;
1749 int nonAuxCount = 0;
1750 int auxCount = 0;
1751 InputMethodSubtype nonAuxSubtype = null;
1752 InputMethodSubtype auxSubtype = null;
1753 for(int i = 0; i < N; ++i) {
1754 final InputMethodInfo imi = imis.get(i);
1755 final List<InputMethodSubtype> subtypes =
1756 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1757 final int subtypeCount = subtypes.size();
1758 if (subtypeCount == 0) {
1759 ++nonAuxCount;
1760 } else {
1761 for (int j = 0; j < subtypeCount; ++j) {
1762 final InputMethodSubtype subtype = subtypes.get(j);
1763 if (!subtype.isAuxiliary()) {
1764 ++nonAuxCount;
1765 nonAuxSubtype = subtype;
1766 } else {
1767 ++auxCount;
1768 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001769 }
1770 }
satok7cfc0ed2011-06-20 21:29:36 +09001771 }
1772 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001773 if (nonAuxCount > 1 || auxCount > 1) {
1774 return true;
1775 } else if (nonAuxCount == 1 && auxCount == 1) {
1776 if (nonAuxSubtype != null && auxSubtype != null
1777 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1778 || auxSubtype.overridesImplicitlyEnabledSubtype()
1779 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1780 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1781 return false;
1782 }
1783 return true;
1784 }
1785 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001786 }
1787
John Spurlocke0980502013-10-25 11:59:29 -04001788 private boolean isKeyguardLocked() {
1789 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1790 }
1791
satokdbf29502011-08-25 15:28:23 +09001792 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001793 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001794 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001795 if (!calledWithValidToken(token)) {
1796 final int uid = Binder.getCallingUid();
1797 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1798 + " token:" + token);
1799 return;
1800 }
1801
1802 synchronized (mMethodMap) {
1803 mImeWindowVis = vis;
1804 mBackDisposition = backDisposition;
1805 updateSystemUiLocked(token, vis, backDisposition);
1806 }
1807 }
1808
1809 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1810 synchronized (mMethodMap) {
1811 updateSystemUiLocked(token, vis, backDisposition);
1812 }
1813 }
1814
1815 // Caution! This method is called in this class. Handle multi-user carefully
1816 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1817 if (!calledWithValidToken(token)) {
1818 final int uid = Binder.getCallingUid();
1819 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1820 + " token:" + token);
1821 return;
1822 }
1823
1824 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1825 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001826 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001827 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 // apply policy for binder calls
1829 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1830 vis = 0;
satok06487a52010-10-29 11:37:18 +09001831 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001832 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1833 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1834 if (mStatusBar != null) {
1835 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1836 needsToShowImeSwitcher);
1837 }
1838 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1839 if (imi != null && needsToShowImeSwitcher) {
1840 // Used to load label
1841 final CharSequence title = mRes.getText(
1842 com.android.internal.R.string.select_input_method);
1843 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1844 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001845 mImeSwitcherNotification.setContentTitle(title)
1846 .setContentText(summary)
1847 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001848 try {
1849 if ((mNotificationManager != null)
1850 && !mIWindowManager.hasNavigationBar()) {
1851 if (DEBUG) {
1852 Slog.d(TAG, "--- show notification: label = " + summary);
1853 }
1854 mNotificationManager.notifyAsUser(null,
1855 com.android.internal.R.string.select_input_method,
1856 mImeSwitcherNotification.build(), UserHandle.ALL);
1857 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001858 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001859 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001860 }
1861 } else {
1862 if (mNotificationShown && mNotificationManager != null) {
1863 if (DEBUG) {
1864 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001865 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001866 mNotificationManager.cancelAsUser(null,
1867 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1868 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001869 }
satok06487a52010-10-29 11:37:18 +09001870 }
1871 } finally {
1872 Binder.restoreCallingIdentity(ident);
1873 }
1874 }
1875
satoke7c6998e2011-06-03 17:57:59 +09001876 @Override
satokf9f01002011-05-19 21:31:50 +09001877 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001878 if (!calledFromValidUser()) {
1879 return;
1880 }
satokf9f01002011-05-19 21:31:50 +09001881 synchronized (mMethodMap) {
1882 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1883 for (int i = 0; i < spans.length; ++i) {
1884 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001885 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001886 mSecureSuggestionSpans.put(ss, currentImi);
1887 }
1888 }
1889 }
1890 }
1891
satoke7c6998e2011-06-03 17:57:59 +09001892 @Override
satokf9f01002011-05-19 21:31:50 +09001893 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001894 if (!calledFromValidUser()) {
1895 return false;
1896 }
satokf9f01002011-05-19 21:31:50 +09001897 synchronized (mMethodMap) {
1898 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1899 // TODO: Do not send the intent if the process of the targetImi is already dead.
1900 if (targetImi != null) {
1901 final String[] suggestions = span.getSuggestions();
1902 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001903 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001904 final Intent intent = new Intent();
1905 // Ensures that only a class in the original IME package will receive the
1906 // notification.
satok42c5a162011-05-26 16:46:14 +09001907 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001908 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1909 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1910 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1911 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001912 final long ident = Binder.clearCallingIdentity();
1913 try {
1914 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1915 } finally {
1916 Binder.restoreCallingIdentity(ident);
1917 }
satokf9f01002011-05-19 21:31:50 +09001918 return true;
1919 }
1920 }
1921 return false;
1922 }
1923
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001924 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001925 updateInputMethodsFromSettingsLocked(enabledMayChange);
1926 updateKeyboardFromSettingsLocked();
1927 }
1928
1929 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001930 if (enabledMayChange) {
1931 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1932 for (int i=0; i<enabled.size(); i++) {
1933 // We allow the user to select "disabled until used" apps, so if they
1934 // are enabling one of those here we now need to make it enabled.
1935 InputMethodInfo imm = enabled.get(i);
1936 try {
1937 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1938 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1939 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001940 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001941 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001942 if (DEBUG) {
1943 Slog.d(TAG, "Update state(" + imm.getId()
1944 + "): DISABLED_UNTIL_USED -> DEFAULT");
1945 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001946 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1947 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001948 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1949 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001950 }
1951 } catch (RemoteException e) {
1952 }
1953 }
1954 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001955 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1956 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1957 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1958 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001959 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001960 // There is no input method selected, try to choose new applicable input method.
1961 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001962 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001963 }
1964 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001966 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001968 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001969 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
satokf3db1af2010-11-23 13:34:33 +09001971 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001972 } else {
1973 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001974 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001976 // Here is not the perfect place to reset the switching controller. Ideally
1977 // mSwitchingController and mSettings should be able to share the same state.
1978 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1979 // the same enabled IMEs list.
1980 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001981
1982 }
1983
1984 public void updateKeyboardFromSettingsLocked() {
1985 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1986 if (mSwitchingDialog != null
1987 && mSwitchingDialogTitleView != null
1988 && mSwitchingDialog.isShowing()) {
1989 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1990 com.android.internal.R.id.hard_keyboard_switch);
1991 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001994
Yohei Yukawab097b822015-12-01 10:43:08 -08001995 private void notifyInputMethodSubtypeChanged(final int userId,
1996 @Nullable final InputMethodInfo inputMethodInfo,
1997 @Nullable final InputMethodSubtype subtype) {
1998 final InputManagerInternal inputManagerInternal =
1999 LocalServices.getService(InputManagerInternal.class);
2000 if (inputManagerInternal != null) {
2001 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2002 }
2003 }
2004
satokab751aa2010-09-14 19:17:36 +09002005 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 InputMethodInfo info = mMethodMap.get(id);
2007 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002008 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002010
satokd81e9502012-05-21 12:58:45 +09002011 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002013 final int subtypeCount = info.getSubtypeCount();
2014 if (subtypeCount <= 0) {
2015 return;
satokcd7cd292010-11-20 15:46:23 +09002016 }
satokd81e9502012-05-21 12:58:45 +09002017 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2018 final InputMethodSubtype newSubtype;
2019 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2020 newSubtype = info.getSubtypeAt(subtypeId);
2021 } else {
2022 // If subtype is null, try to find the most applicable one from
2023 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002024 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002025 }
2026 if (newSubtype == null || oldSubtype == null) {
2027 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2028 + ", new subtype = " + newSubtype);
2029 return;
2030 }
2031 if (newSubtype != oldSubtype) {
2032 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2033 if (mCurMethod != null) {
2034 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002035 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002036 mCurMethod.changeInputMethodSubtype(newSubtype);
2037 } catch (RemoteException e) {
2038 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002039 return;
satokab751aa2010-09-14 19:17:36 +09002040 }
2041 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002042 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 return;
2045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002046
satokd81e9502012-05-21 12:58:45 +09002047 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 final long ident = Binder.clearCallingIdentity();
2049 try {
satokab751aa2010-09-14 19:17:36 +09002050 // Set a subtype to this input method.
2051 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002052 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2053 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2054 // because mCurMethodId is stored as a history in
2055 // setSelectedInputMethodAndSubtypeLocked().
2056 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
2058 if (ActivityManagerNative.isSystemReady()) {
2059 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002060 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002062 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002064 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 } finally {
2066 Binder.restoreCallingIdentity(ident);
2067 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002068
2069 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2070 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002072
satok42c5a162011-05-26 16:46:14 +09002073 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002074 public boolean showSoftInput(IInputMethodClient client, int flags,
2075 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002076 if (!calledFromValidUser()) {
2077 return false;
2078 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002079 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 long ident = Binder.clearCallingIdentity();
2081 try {
2082 synchronized (mMethodMap) {
2083 if (mCurClient == null || client == null
2084 || mCurClient.client.asBinder() != client.asBinder()) {
2085 try {
2086 // We need to check if this is the current client with
2087 // focus in the window manager, to allow this call to
2088 // be made before input is started in it.
2089 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002090 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002091 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002097
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002098 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002099 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101 } finally {
2102 Binder.restoreCallingIdentity(ident);
2103 }
2104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002105
The Android Open Source Project4df24232009-03-05 14:34:35 -08002106 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002108 if (mAccessibilityRequestingNoSoftKeyboard) {
2109 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Anna Galusza9b278112016-01-04 11:37:37 -08002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2113 mShowExplicitlyRequested = true;
2114 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002115 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2116 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
Dianne Hackborncc278702009-09-02 23:07:23 -07002119 if (!mSystemReady) {
2120 return false;
2121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002122
The Android Open Source Project4df24232009-03-05 14:34:35 -08002123 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002125 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2127 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2128 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002130 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002131 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002132 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002133 | Context.BIND_TREAT_LIKE_ACTIVITY
2134 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002135 mVisibleBound = true;
2136 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002137 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002139 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 // The client has asked to have the input method shown, but
2141 // we have been sitting here too long with a connection to the
2142 // service and no interface received, so let's disconnect/connect
2143 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002146 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002148 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002149 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002150 } else {
2151 if (DEBUG) {
2152 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2153 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002156
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
satok42c5a162011-05-26 16:46:14 +09002160 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161 public boolean hideSoftInput(IInputMethodClient client, int flags,
2162 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002163 if (!calledFromValidUser()) {
2164 return false;
2165 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002166 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 long ident = Binder.clearCallingIdentity();
2168 try {
2169 synchronized (mMethodMap) {
2170 if (mCurClient == null || client == null
2171 || mCurClient.client.asBinder() != client.asBinder()) {
2172 try {
2173 // We need to check if this is the current client with
2174 // focus in the window manager, to allow this call to
2175 // be made before input is started in it.
2176 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002177 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2178 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002179 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002182 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002185
Joe Onorato8a9b2202010-02-26 18:56:32 -08002186 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189 } finally {
2190 Binder.restoreCallingIdentity(ident);
2191 }
2192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002193
The Android Open Source Project4df24232009-03-05 14:34:35 -08002194 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2196 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002197 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 -08002198 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002201 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 -08002202 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002204
2205 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2206 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2207 // to be updated with the new value sent from IME process. Even in such a transient state
2208 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2209 // application process as a valid request, and have even promised such a behavior with CTS
2210 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2211 // IMMS#InputShown indicates that the software keyboard is shown.
2212 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2213 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2214 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002215 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002216 if (shouldHideSoftInput) {
2217 // The IME will report its visible state again after the following message finally
2218 // delivered to the IME process as an IPC. Hence the inconsistency between
2219 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2220 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002221 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2222 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2223 res = true;
2224 } else {
2225 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002227 if (mHaveConnection && mVisibleBound) {
2228 mContext.unbindService(mVisibleConnection);
2229 mVisibleBound = false;
2230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 mInputShown = false;
2232 mShowRequested = false;
2233 mShowExplicitlyRequested = false;
2234 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002237
satok42c5a162011-05-26 16:46:14 +09002238 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002239 public InputBindResult startInputOrWindowGainedFocus(
2240 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2241 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002242 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002243 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002244 if (windowToken != null) {
2245 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002246 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002247 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002248 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2249 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002250 }
2251 }
2252
2253 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002254 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2255 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002256 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2257 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002258 // Needs to check the validity before clearing calling identity
2259 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002260 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 long ident = Binder.clearCallingIdentity();
2262 try {
2263 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002264 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2265 + InputMethodClient.getStartInputReason(startInputReason)
2266 + " client=" + client.asBinder()
2267 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002268 + " missingMethods="
2269 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002270 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002271 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002273 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002274
Dianne Hackborn7663d802012-02-24 13:08:49 -08002275 ClientState cs = mClients.get(client.asBinder());
2276 if (cs == null) {
2277 throw new IllegalArgumentException("unknown client "
2278 + client.asBinder());
2279 }
2280
2281 try {
2282 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2283 // Check with the window manager to make sure this client actually
2284 // has a window with focus. If not, reject. This is thread safe
2285 // because if the focus changes some time before or after, the
2286 // next client receiving focus that has any interest in input will
2287 // be calling through here after that change happens.
2288 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2289 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2290 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002292 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002294
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002295 if (!calledFromValidUser) {
2296 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2297 Slog.w(TAG, "If you want to interect with IME, you need "
2298 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2299 hideCurrentInputLocked(0, null);
2300 return null;
2301 }
2302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002303 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002304 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002305 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002306 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002307 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2308 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002309 }
2310 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002311 }
2312 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002313 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002314
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002315 // Should we auto-show the IME even if the caller has not
2316 // specified what should be done with it?
2317 // We only do this automatically if the window can resize
2318 // to accommodate the IME (so what the user sees will give
2319 // them good context without input information being obscured
2320 // by the IME) or if running on a large screen where there
2321 // is more room for the target window + IME.
2322 final boolean doAutoShow =
2323 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2324 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2325 || mRes.getConfiguration().isLayoutSizeAtLeast(
2326 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002327 final boolean isTextEditor =
2328 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2329
2330 // We want to start input before showing the IME, but after closing
2331 // it. We want to do this after closing it to help the IME disappear
2332 // more quickly (not get stuck behind it initializing itself for the
2333 // new focused input, even if its window wants to hide the IME).
2334 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2337 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002338 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2340 // There is no focus view, and this window will
2341 // be behind any soft input window, so hide the
2342 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002343 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002344 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002346 } else if (isTextEditor && doAutoShow && (softInputMode &
2347 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 // There is a focus view, and we are navigating forward
2349 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002350 // We only do this automatically if the window can resize
2351 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002352 // them good context without input information being obscured
2353 // by the IME) or if running on a large screen where there
2354 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002355 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002356 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002357 res = startInputUncheckedLocked(cs, inputContext,
2358 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 didStart = true;
2360 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002361 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
2363 break;
2364 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2365 // Do nothing.
2366 break;
2367 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2368 if ((softInputMode &
2369 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373 break;
2374 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002375 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002376 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 break;
2378 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2379 if ((softInputMode &
2380 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002381 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002383 res = startInputUncheckedLocked(cs, inputContext,
2384 missingMethods, 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 }
2389 break;
2390 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002391 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002392 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002393 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2394 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002395 didStart = true;
2396 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002397 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 break;
2399 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002400
2401 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002402 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002403 controlFlags);
2404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406 } finally {
2407 Binder.restoreCallingIdentity(ident);
2408 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002409
2410 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
satok42c5a162011-05-26 16:46:14 +09002413 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002414 public void showInputMethodPickerFromClient(
2415 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 if (!calledFromValidUser()) {
2417 return;
2418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 synchronized (mMethodMap) {
2420 if (mCurClient == null || client == null
2421 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002422 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002423 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 }
2425
satok440aab52010-11-25 09:43:11 +09002426 // Always call subtype picker, because subtype picker is a superset of input method
2427 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002428 mHandler.sendMessage(mCaller.obtainMessageI(
2429 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002430 }
2431 }
2432
satok42c5a162011-05-26 16:46:14 +09002433 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002435 if (!calledFromValidUser()) {
2436 return;
2437 }
satok28203512010-11-24 11:06:49 +09002438 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2439 }
2440
satok42c5a162011-05-26 16:46:14 +09002441 @Override
satok28203512010-11-24 11:06:49 +09002442 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002443 if (!calledFromValidUser()) {
2444 return;
2445 }
satok28203512010-11-24 11:06:49 +09002446 synchronized (mMethodMap) {
2447 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002448 setInputMethodWithSubtypeIdLocked(token, id,
2449 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2450 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002451 } else {
2452 setInputMethod(token, id);
2453 }
2454 }
satokab751aa2010-09-14 19:17:36 +09002455 }
2456
satok42c5a162011-05-26 16:46:14 +09002457 @Override
satokb416a712010-11-25 20:42:14 +09002458 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002459 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002460 if (!calledFromValidUser()) {
2461 return;
2462 }
satokb416a712010-11-25 20:42:14 +09002463 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002464 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2465 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002466 }
2467 }
2468
satok4fc87d62011-05-20 16:13:43 +09002469 @Override
satok735cf382010-11-11 20:40:09 +09002470 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002471 if (!calledFromValidUser()) {
2472 return false;
2473 }
satok735cf382010-11-11 20:40:09 +09002474 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002475 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002476 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002477 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002478 lastImi = mMethodMap.get(lastIme.first);
2479 } else {
2480 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002481 }
satok4fc87d62011-05-20 16:13:43 +09002482 String targetLastImiId = null;
2483 int subtypeId = NOT_A_SUBTYPE_ID;
2484 if (lastIme != null && lastImi != null) {
2485 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002486 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002487 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2488 : mCurrentSubtype.hashCode();
2489 // If the last IME is the same as the current IME and the last subtype is not
2490 // defined, there is no need to switch to the last IME.
2491 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2492 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002493 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002494 }
2495 }
2496
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002497 if (TextUtils.isEmpty(targetLastImiId)
2498 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002499 // This is a safety net. If the currentSubtype can't be added to the history
2500 // and the framework couldn't find the last ime, we will make the last ime be
2501 // the most applicable enabled keyboard subtype of the system imes.
2502 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2503 if (enabled != null) {
2504 final int N = enabled.size();
2505 final String locale = mCurrentSubtype == null
2506 ? mRes.getConfiguration().locale.toString()
2507 : mCurrentSubtype.getLocale();
2508 for (int i = 0; i < N; ++i) {
2509 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002510 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002511 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002512 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2513 InputMethodUtils.getSubtypes(imi),
2514 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002515 if (keyboardSubtype != null) {
2516 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002517 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002518 imi, keyboardSubtype.hashCode());
2519 if(keyboardSubtype.getLocale().equals(locale)) {
2520 break;
2521 }
2522 }
2523 }
2524 }
2525 }
2526 }
2527
2528 if (!TextUtils.isEmpty(targetLastImiId)) {
2529 if (DEBUG) {
2530 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2531 + ", from: " + mCurMethodId + ", " + subtypeId);
2532 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002533 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002534 return true;
2535 } else {
2536 return false;
2537 }
satok735cf382010-11-11 20:40:09 +09002538 }
2539 }
2540
satoke7c6998e2011-06-03 17:57:59 +09002541 @Override
satok688bd472012-02-09 20:09:17 +09002542 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002543 if (!calledFromValidUser()) {
2544 return false;
2545 }
satok688bd472012-02-09 20:09:17 +09002546 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002547 if (!calledWithValidToken(token)) {
2548 final int uid = Binder.getCallingUid();
2549 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2550 + " token:" + token);
2551 return false;
2552 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002553 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002554 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2555 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002556 if (nextSubtype == null) {
2557 return false;
2558 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002559 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2560 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002561 return true;
2562 }
2563 }
2564
2565 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002566 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2567 if (!calledFromValidUser()) {
2568 return false;
2569 }
2570 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002571 if (!calledWithValidToken(token)) {
2572 final int uid = Binder.getCallingUid();
2573 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2574 + "token. uid:" + uid + " token:" + token);
2575 return false;
2576 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002577 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002578 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2579 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002580 if (nextSubtype == null) {
2581 return false;
2582 }
2583 return true;
2584 }
2585 }
2586
2587 @Override
satok68f1b782011-04-11 14:26:04 +09002588 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002589 if (!calledFromValidUser()) {
2590 return null;
2591 }
satok68f1b782011-04-11 14:26:04 +09002592 synchronized (mMethodMap) {
2593 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2594 // TODO: Handle the case of the last IME with no subtypes
2595 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2596 || TextUtils.isEmpty(lastIme.second)) return null;
2597 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2598 if (lastImi == null) return null;
2599 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002600 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002601 final int lastSubtypeId =
2602 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002603 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2604 return null;
2605 }
2606 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002607 } catch (NumberFormatException e) {
2608 return null;
2609 }
2610 }
2611 }
2612
satoke7c6998e2011-06-03 17:57:59 +09002613 @Override
satokee5e77c2011-09-02 18:50:15 +09002614 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002615 if (!calledFromValidUser()) {
2616 return;
2617 }
satok91e88122011-07-18 11:11:42 +09002618 // By this IPC call, only a process which shares the same uid with the IME can add
2619 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002620 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002621 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002622 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002623 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002624 final String[] packageInfos;
2625 try {
2626 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2627 } catch (RemoteException e) {
2628 Slog.e(TAG, "Failed to get package infos");
2629 return;
2630 }
satok91e88122011-07-18 11:11:42 +09002631 if (packageInfos != null) {
2632 final int packageNum = packageInfos.length;
2633 for (int i = 0; i < packageNum; ++i) {
2634 if (packageInfos[i].equals(imi.getPackageName())) {
2635 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002636 final long ident = Binder.clearCallingIdentity();
2637 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002638 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002639 } finally {
2640 Binder.restoreCallingIdentity(ident);
2641 }
satokee5e77c2011-09-02 18:50:15 +09002642 return;
satok91e88122011-07-18 11:11:42 +09002643 }
2644 }
2645 }
satoke7c6998e2011-06-03 17:57:59 +09002646 }
satokee5e77c2011-09-02 18:50:15 +09002647 return;
satoke7c6998e2011-06-03 17:57:59 +09002648 }
2649
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002650 @Override
2651 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002652 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002653 }
2654
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002655 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002656 public void clearLastInputMethodWindowForTransition(IBinder token) {
2657 if (!calledFromValidUser()) {
2658 return;
2659 }
2660 final long ident = Binder.clearCallingIdentity();
2661 try {
2662 synchronized (mMethodMap) {
2663 if (!calledWithValidToken(token)) {
2664 final int uid = Binder.getCallingUid();
2665 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2666 + "invalid token. uid:" + uid + " token:" + token);
2667 return;
2668 }
2669 }
2670 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2671 } finally {
2672 Binder.restoreCallingIdentity(ident);
2673 }
2674 }
2675
2676 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002677 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002678 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002679 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002680 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002681 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002682 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2683 if (DEBUG) {
2684 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2685 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2686 + " actual: " + sequenceNumber);
2687 }
2688 return;
2689 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002690 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2691 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002692 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002693 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002694 }
2695 }
2696
satok28203512010-11-24 11:06:49 +09002697 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002699 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2700 }
2701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002703 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2704 if (token == null) {
2705 if (mContext.checkCallingOrSelfPermission(
2706 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2707 != PackageManager.PERMISSION_GRANTED) {
2708 throw new SecurityException(
2709 "Using null token requires permission "
2710 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002712 } else if (mCurToken != token) {
2713 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2714 + " token: " + token);
2715 return;
2716 }
2717
2718 final long ident = Binder.clearCallingIdentity();
2719 try {
2720 setInputMethodLocked(id, subtypeId);
2721 } finally {
2722 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
2724 }
2725
satok42c5a162011-05-26 16:46:14 +09002726 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002728 if (!calledFromValidUser()) {
2729 return;
2730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002732 if (!calledWithValidToken(token)) {
2733 final int uid = Binder.getCallingUid();
2734 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2735 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 return;
2737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 long ident = Binder.clearCallingIdentity();
2739 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002740 hideCurrentInputLocked(flags, null);
2741 } finally {
2742 Binder.restoreCallingIdentity(ident);
2743 }
2744 }
2745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002746
satok42c5a162011-05-26 16:46:14 +09002747 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002749 if (!calledFromValidUser()) {
2750 return;
2751 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002752 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002753 if (!calledWithValidToken(token)) {
2754 final int uid = Binder.getCallingUid();
2755 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2756 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002757 return;
2758 }
2759 long ident = Binder.clearCallingIdentity();
2760 try {
2761 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 } finally {
2763 Binder.restoreCallingIdentity(ident);
2764 }
2765 }
2766 }
2767
2768 void setEnabledSessionInMainThread(SessionState session) {
2769 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002770 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002772 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002773 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 } catch (RemoteException e) {
2775 }
2776 }
2777 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002778 if (mEnabledSession != null && mEnabledSession.session != null) {
2779 try {
2780 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2781 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2782 } catch (RemoteException e) {
2783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 }
2786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002787
satok42c5a162011-05-26 16:46:14 +09002788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002790 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002792 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002793 final boolean showAuxSubtypes;
2794 switch (msg.arg1) {
2795 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2796 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2797 // implemented so that auxiliary subtypes will be excluded when the soft
2798 // keyboard is invisible.
2799 showAuxSubtypes = mInputShown;
2800 break;
2801 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2802 showAuxSubtypes = true;
2803 break;
2804 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2805 showAuxSubtypes = false;
2806 break;
2807 default:
2808 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2809 return false;
2810 }
2811 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002812 return true;
2813
satok47a44912010-10-06 16:03:58 +09002814 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002815 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002816 return true;
2817
2818 case MSG_SHOW_IM_CONFIG:
2819 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002820 return true;
2821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 case MSG_UNBIND_INPUT:
2825 try {
2826 ((IInputMethod)msg.obj).unbindInput();
2827 } catch (RemoteException e) {
2828 // There is nothing interesting about the method dying.
2829 }
2830 return true;
2831 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002832 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 try {
2834 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2835 } catch (RemoteException e) {
2836 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002837 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 return true;
2839 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002840 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002842 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002843 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002844 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 } catch (RemoteException e) {
2846 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002847 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 return true;
2849 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002852 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002853 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002854 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 } catch (RemoteException e) {
2856 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002857 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002859 case MSG_HIDE_CURRENT_INPUT_METHOD:
2860 synchronized (mMethodMap) {
2861 hideCurrentInputLocked(0, null);
2862 }
2863 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002865 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002867 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2869 } catch (RemoteException e) {
2870 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002871 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002873 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002874 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002875 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002876 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002878 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002880 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002881 // Dispose the channel if the input method is not local to this process
2882 // because the remote proxy will get its own copy when unparceled.
2883 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002884 channel.dispose();
2885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002887 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002891
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002892 case MSG_START_INPUT: {
2893 int missingMethods = msg.arg1;
2894 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002896 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002898 session.method.startInput((IInputContext) args.arg2, missingMethods,
2899 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 } catch (RemoteException e) {
2901 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002902 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002904 }
2905 case MSG_RESTART_INPUT: {
2906 int missingMethods = msg.arg1;
2907 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002909 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002911 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2912 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } catch (RemoteException e) {
2914 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002915 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002920
Yohei Yukawa33e81792015-11-17 21:14:42 -08002921 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002923 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 } catch (RemoteException e) {
2925 // There is nothing interesting about the last client dying.
2926 }
2927 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002928 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002929 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002930 IInputMethodClient client = (IInputMethodClient)args.arg1;
2931 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002933 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002935 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002936 } finally {
2937 // Dispose the channel if the input method is not local to this process
2938 // because the remote proxy will get its own copy when unparceled.
2939 if (res.channel != null && Binder.isProxy(client)) {
2940 res.channel.dispose();
2941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002943 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002945 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002946 case MSG_SET_ACTIVE:
2947 try {
2948 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2949 } catch (RemoteException e) {
2950 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2951 + ((ClientState)msg.obj).pid + " uid "
2952 + ((ClientState)msg.obj).uid);
2953 }
2954 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002955 case MSG_SET_INTERACTIVE:
2956 handleSetInteractive(msg.arg1 != 0);
2957 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002958 case MSG_SWITCH_IME:
2959 handleSwitchInputMethod(msg.arg1 != 0);
2960 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002961 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2962 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002963 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002964 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002965 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002966 } catch (RemoteException e) {
2967 Slog.w(TAG, "Got RemoteException sending "
2968 + "setUserActionNotificationSequenceNumber("
2969 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002970 + clientState.pid + " uid "
2971 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002972 }
2973 return true;
2974 }
satok01038492012-04-09 21:08:27 +09002975
2976 // --------------------------------------------------------------
2977 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002978 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002979 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002980 case MSG_SYSTEM_UNLOCK_USER:
2981 final int userId = msg.arg1;
2982 onUnlockUser(userId);
2983 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 }
2985 return false;
2986 }
2987
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002988 private void handleSetInteractive(final boolean interactive) {
2989 synchronized (mMethodMap) {
2990 mIsInteractive = interactive;
2991 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2992
2993 // Inform the current client of the change in active status
2994 if (mCurClient != null && mCurClient.client != null) {
2995 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2996 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2997 }
2998 }
2999 }
3000
Yohei Yukawaae61f712015-12-09 13:00:10 -08003001 private void handleSwitchInputMethod(final boolean forwardDirection) {
3002 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003003 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003004 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003005 if (nextSubtype == null) {
3006 return;
3007 }
3008 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003009 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3010 if (newInputMethodInfo == null) {
3011 return;
3012 }
3013 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3014 newInputMethodInfo, mCurrentSubtype);
3015 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003016 if (mSubtypeSwitchedByShortCutToast == null) {
3017 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3018 Toast.LENGTH_SHORT);
3019 } else {
3020 mSubtypeSwitchedByShortCutToast.setText(toastText);
3021 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003022 mSubtypeSwitchedByShortCutToast.show();
3023 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003024 }
3025 }
3026
satokdc9ddae2011-10-06 12:22:36 +09003027 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003028 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3029 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003030 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003031 if (DEBUG) {
3032 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3033 }
satok723a27e2010-11-11 14:58:11 +09003034 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003035 return true;
3036 }
3037
3038 return false;
3039 }
3040
Yohei Yukawa94e33302016-02-12 19:37:03 -08003041 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003043 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003044 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003045 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003046 mMethodList.clear();
3047 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003048
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003049 // Use for queryIntentServicesAsUser
3050 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003052 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3053 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3054 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003055 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003057 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3058 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003059
satoke7c6998e2011-06-03 17:57:59 +09003060 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3061 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 for (int i = 0; i < services.size(); ++i) {
3063 ResolveInfo ri = services.get(i);
3064 ServiceInfo si = ri.serviceInfo;
3065 ComponentName compName = new ComponentName(si.packageName, si.name);
3066 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3067 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003068 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 + ": it does not require the permission "
3070 + android.Manifest.permission.BIND_INPUT_METHOD);
3071 continue;
3072 }
3073
Joe Onorato8a9b2202010-02-26 18:56:32 -08003074 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075
3076 try {
satoke7c6998e2011-06-03 17:57:59 +09003077 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003078 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003079 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003080 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081
3082 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003083 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003085 } catch (Exception e) {
3086 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003089
Yohei Yukawa859df052016-02-17 07:56:46 -08003090 // TODO: The following code should find better place to live.
3091 if (!resetDefaultEnabledIme) {
3092 boolean enabledImeFound = false;
3093 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3094 final int N = enabledImes.size();
3095 for (int i = 0; i < N; ++i) {
3096 final InputMethodInfo imi = enabledImes.get(i);
3097 if (mMethodList.contains(imi)) {
3098 enabledImeFound = true;
3099 break;
3100 }
3101 }
3102 if (!enabledImeFound) {
3103 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3104 resetDefaultEnabledIme = true;
3105 resetSelectedInputMethodAndSubtypeLocked("");
3106 }
3107 }
3108
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003109 if (resetDefaultEnabledIme) {
3110 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003111 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003112 final int N = defaultEnabledIme.size();
3113 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003114 final InputMethodInfo imi = defaultEnabledIme.get(i);
3115 if (DEBUG) {
3116 Slog.d(TAG, "--- enable ime = " + imi);
3117 }
3118 setInputMethodEnabledLocked(imi.getId(), true);
3119 }
3120 }
3121
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003122 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003123 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003124 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003125 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3126 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003127 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003128 }
3129 } else {
3130 // Double check that the default IME is certainly enabled.
3131 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003132 }
3133 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003134 // Here is not the perfect place to reset the switching controller. Ideally
3135 // mSwitchingController and mSettings should be able to share the same state.
3136 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3137 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003138 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003142
satok217f5482010-12-15 05:19:19 +09003143 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003144 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003145 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003146 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3147 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003148 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003149 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003150 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003151 final int userId;
3152 synchronized (mMethodMap) {
3153 userId = mSettings.getCurrentUserId();
3154 }
3155 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003156 }
3157
3158 private void showConfigureInputMethods() {
3159 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3161 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3162 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003163 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003164 }
3165
satok2c93efc2012-04-02 19:33:47 +09003166 private boolean isScreenLocked() {
3167 return mKeyguardManager != null
3168 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3169 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003170
Seigo Nonaka14e13912015-05-06 21:04:13 -07003171 private void showInputMethodMenu(boolean showAuxSubtypes) {
3172 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003175 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003176
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003177 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003178 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003179 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003180
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003181 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003182 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003183 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3184 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003185 if (immis == null || immis.size() == 0) {
3186 return;
3187 }
3188
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003189 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190
satok688bd472012-02-09 20:09:17 +09003191 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003192 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003193 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003194
satokc3690562012-01-10 20:14:43 +09003195 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003196 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003197 if (currentSubtype != null) {
3198 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003199 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3200 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003201 }
3202 }
3203
Ken Wakasa761eb372011-03-04 19:06:18 +09003204 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003205 mIms = new InputMethodInfo[N];
3206 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003207 int checkedItem = 0;
3208 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003209 final ImeSubtypeListItem item = imList.get(i);
3210 mIms[i] = item.mImi;
3211 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003212 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003213 int subtypeId = mSubtypeIds[i];
3214 if ((subtypeId == NOT_A_SUBTYPE_ID)
3215 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3216 || (subtypeId == lastInputMethodSubtypeId)) {
3217 checkedItem = i;
3218 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003221
3222 final Context settingsContext = new ContextThemeWrapper(context,
3223 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3224
3225 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3227 @Override
3228 public void onCancel(DialogInterface dialog) {
3229 hideInputMethodMenu();
3230 }
3231 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003232
3233 final Context dialogContext = mDialogBuilder.getContext();
3234 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3235 com.android.internal.R.styleable.DialogPreference,
3236 com.android.internal.R.attr.alertDialogStyle, 0);
3237 final Drawable dialogIcon = a.getDrawable(
3238 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3239 a.recycle();
3240
3241 mDialogBuilder.setIcon(dialogIcon);
3242
Yohei Yukawad34e1482016-02-11 08:03:52 -08003243 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003244 final View tv = inflater.inflate(
3245 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3246 mDialogBuilder.setCustomTitle(tv);
3247
3248 // Setup layout for a toggle switch of the hardware keyboard
3249 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250 mSwitchingDialogTitleView
3251 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003252 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003254 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003255 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003256 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3258 @Override
3259 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003260 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003261 // Ensure that the input method dialog is dismissed when changing
3262 // the hardware keyboard state.
3263 hideInputMethodMenu();
3264 }
3265 });
3266
Alan Viverette505e3ab2014-11-24 15:22:11 -08003267 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003268 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3269 final OnClickListener choiceListener = new OnClickListener() {
3270 @Override
3271 public void onClick(final DialogInterface dialog, final int which) {
3272 synchronized (mMethodMap) {
3273 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3274 || mSubtypeIds.length <= which) {
3275 return;
satok01038492012-04-09 21:08:27 +09003276 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003277 final InputMethodInfo im = mIms[which];
3278 int subtypeId = mSubtypeIds[which];
3279 adapter.mCheckedItem = which;
3280 adapter.notifyDataSetChanged();
3281 hideInputMethodMenu();
3282 if (im != null) {
3283 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3284 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003285 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003286 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003288 }
3289 }
3290 };
3291 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003294 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003295 final Window w = mSwitchingDialog.getWindow();
3296 final WindowManager.LayoutParams attrs = w.getAttributes();
3297 w.setType(TYPE_INPUT_METHOD_DIALOG);
3298 // Use an alternate token for the dialog for that window manager can group the token
3299 // with other IME windows based on type vs. grouping based on whichever token happens
3300 // to get selected by the system later on.
3301 attrs.token = mSwitchingDialogToken;
3302 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3303 attrs.setTitle("Select input method");
3304 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003305 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 mSwitchingDialog.show();
3307 }
3308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003309
Ken Wakasa05dbb652011-08-22 15:22:43 +09003310 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3311 private final LayoutInflater mInflater;
3312 private final int mTextViewResourceId;
3313 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003314 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003315 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3316 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3317 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003318
Ken Wakasa05dbb652011-08-22 15:22:43 +09003319 mTextViewResourceId = textViewResourceId;
3320 mItemsList = itemsList;
3321 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003322 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003323 }
3324
3325 @Override
3326 public View getView(int position, View convertView, ViewGroup parent) {
3327 final View view = convertView != null ? convertView
3328 : mInflater.inflate(mTextViewResourceId, null);
3329 if (position < 0 || position >= mItemsList.size()) return view;
3330 final ImeSubtypeListItem item = mItemsList.get(position);
3331 final CharSequence imeName = item.mImeName;
3332 final CharSequence subtypeName = item.mSubtypeName;
3333 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3334 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3335 if (TextUtils.isEmpty(subtypeName)) {
3336 firstTextView.setText(imeName);
3337 secondTextView.setVisibility(View.GONE);
3338 } else {
3339 firstTextView.setText(subtypeName);
3340 secondTextView.setText(imeName);
3341 secondTextView.setVisibility(View.VISIBLE);
3342 }
3343 final RadioButton radioButton =
3344 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3345 radioButton.setChecked(position == mCheckedItem);
3346 return view;
3347 }
3348 }
3349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003351 synchronized (mMethodMap) {
3352 hideInputMethodMenuLocked();
3353 }
3354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003355
The Android Open Source Project10592532009-03-18 17:39:46 -07003356 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003357 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358
The Android Open Source Project10592532009-03-18 17:39:46 -07003359 if (mSwitchingDialog != null) {
3360 mSwitchingDialog.dismiss();
3361 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003364 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003365 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003366 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003370
satok42c5a162011-05-26 16:46:14 +09003371 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003373 // TODO: Make this work even for non-current users?
3374 if (!calledFromValidUser()) {
3375 return false;
3376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 synchronized (mMethodMap) {
3378 if (mContext.checkCallingOrSelfPermission(
3379 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3380 != PackageManager.PERMISSION_GRANTED) {
3381 throw new SecurityException(
3382 "Requires permission "
3383 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3384 }
Anna Galusza9b278112016-01-04 11:37:37 -08003385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 long ident = Binder.clearCallingIdentity();
3387 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003388 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 } finally {
3390 Binder.restoreCallingIdentity(ident);
3391 }
3392 }
3393 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003395 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3396 // Make sure this is a valid input method.
3397 InputMethodInfo imm = mMethodMap.get(id);
3398 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003399 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003400 }
3401
satokd87c2592010-09-29 11:52:06 +09003402 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3403 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003404
satokd87c2592010-09-29 11:52:06 +09003405 if (enabled) {
3406 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3407 if (pair.first.equals(id)) {
3408 // We are enabling this input method, but it is already enabled.
3409 // Nothing to do. The previous state was enabled.
3410 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003411 }
3412 }
satokd87c2592010-09-29 11:52:06 +09003413 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3414 // Previous state was disabled.
3415 return false;
3416 } else {
3417 StringBuilder builder = new StringBuilder();
3418 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3419 builder, enabledInputMethodsList, id)) {
3420 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003422 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3423 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3424 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003425 }
3426 // Previous state was enabled.
3427 return true;
3428 } else {
3429 // We are disabling the input method but it is already disabled.
3430 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003431 return false;
3432 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003433 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003434 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003435
satok723a27e2010-11-11 14:58:11 +09003436 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3437 boolean setSubtypeOnly) {
3438 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003439 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003440
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003441 mCurUserActionNotificationSequenceNumber =
3442 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3443 if (DEBUG) {
3444 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3445 + mCurUserActionNotificationSequenceNumber);
3446 }
3447
3448 if (mCurClient != null && mCurClient.client != null) {
3449 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3450 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003451 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003452 }
3453
satok723a27e2010-11-11 14:58:11 +09003454 // Set Subtype here
3455 if (imi == null || subtypeId < 0) {
3456 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003457 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003458 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003459 if (subtypeId < imi.getSubtypeCount()) {
3460 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3461 mSettings.putSelectedSubtype(subtype.hashCode());
3462 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003463 } else {
3464 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003465 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003466 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003467 }
satokab751aa2010-09-14 19:17:36 +09003468 }
satok723a27e2010-11-11 14:58:11 +09003469
Yohei Yukawa68645a62016-02-17 07:54:20 -08003470 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003471 // Set InputMethod here
3472 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3473 }
3474 }
3475
3476 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3477 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3478 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3479 // newDefaultIme is empty when there is no candidate for the selected IME.
3480 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3481 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3482 if (subtypeHashCode != null) {
3483 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003484 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003485 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003486 } catch (NumberFormatException e) {
3487 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3488 }
3489 }
3490 }
3491 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003492 }
3493
satok4e4569d2010-11-19 18:45:53 +09003494 // If there are no selected shortcuts, tries finding the most applicable ones.
3495 private Pair<InputMethodInfo, InputMethodSubtype>
3496 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3497 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3498 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003499 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003500 boolean foundInSystemIME = false;
3501
3502 // Search applicable subtype for each InputMethodInfo
3503 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003504 final String imiId = imi.getId();
3505 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3506 continue;
3507 }
satokcd7cd292010-11-20 15:46:23 +09003508 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003509 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003510 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003511 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003512 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003513 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003514 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003515 }
satokdf31ae62011-01-15 06:19:44 +09003516 // 2. Search by the system locale from enabledSubtypes.
3517 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003518 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003519 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003520 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003521 }
satoka86f5e42011-09-02 17:12:42 +09003522 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003523 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003524 final ArrayList<InputMethodSubtype> subtypesForSearch =
3525 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003526 ? InputMethodUtils.getSubtypes(imi)
3527 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003528 // 4. Search by the current subtype's locale from all subtypes.
3529 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003530 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003531 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003532 }
3533 // 5. Search by the system locale from all subtypes.
3534 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003535 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003536 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003537 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003538 }
satokcd7cd292010-11-20 15:46:23 +09003539 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003540 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003541 // The current input method is the most applicable IME.
3542 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003543 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003544 break;
satok7599a7f2010-12-22 13:45:23 +09003545 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003546 // The system input method is 2nd applicable IME.
3547 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003548 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003549 if ((imi.getServiceInfo().applicationInfo.flags
3550 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3551 foundInSystemIME = true;
3552 }
satok4e4569d2010-11-19 18:45:53 +09003553 }
3554 }
3555 }
3556 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003557 if (mostApplicableIMI != null) {
3558 Slog.w(TAG, "Most applicable shortcut input method was:"
3559 + mostApplicableIMI.getId());
3560 if (mostApplicableSubtype != null) {
3561 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3562 + "," + mostApplicableSubtype.getMode() + ","
3563 + mostApplicableSubtype.getLocale());
3564 }
3565 }
satok4e4569d2010-11-19 18:45:53 +09003566 }
satokcd7cd292010-11-20 15:46:23 +09003567 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003568 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003569 } else {
3570 return null;
3571 }
3572 }
3573
satokab751aa2010-09-14 19:17:36 +09003574 /**
3575 * @return Return the current subtype of this input method.
3576 */
satok42c5a162011-05-26 16:46:14 +09003577 @Override
satokab751aa2010-09-14 19:17:36 +09003578 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003579 // TODO: Make this work even for non-current users?
3580 if (!calledFromValidUser()) {
3581 return null;
3582 }
3583 synchronized (mMethodMap) {
3584 return getCurrentInputMethodSubtypeLocked();
3585 }
3586 }
3587
3588 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003589 if (mCurMethodId == null) {
3590 return null;
3591 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003592 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003593 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3594 if (imi == null || imi.getSubtypeCount() == 0) {
3595 return null;
satok4e4569d2010-11-19 18:45:53 +09003596 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003597 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003598 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3599 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003600 if (subtypeId == NOT_A_SUBTYPE_ID) {
3601 // If there are no selected subtypes, the framework will try to find
3602 // the most applicable subtype from explicitly or implicitly enabled
3603 // subtypes.
3604 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003605 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003606 // If there is only one explicitly or implicitly enabled subtype,
3607 // just returns it.
3608 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3609 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3610 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003611 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003612 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003613 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003614 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003615 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003616 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3617 true);
satok4e4569d2010-11-19 18:45:53 +09003618 }
satok3ef8b292010-11-23 06:06:29 +09003619 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003620 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003621 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003622 }
3623 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003624 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003625 }
3626
satok4e4569d2010-11-19 18:45:53 +09003627 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003628 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003629 @Override
satok4e4569d2010-11-19 18:45:53 +09003630 public List getShortcutInputMethodsAndSubtypes() {
3631 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003632 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003633 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003634 // If there are no selected shortcut subtypes, the framework will try to find
3635 // the most applicable subtype from all subtypes whose mode is
3636 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003637 Pair<InputMethodInfo, InputMethodSubtype> info =
3638 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003639 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003640 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003641 ret.add(info.first);
3642 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003643 }
satok3da92232011-01-11 22:46:30 +09003644 return ret;
satokf3db1af2010-11-23 13:34:33 +09003645 }
satokf3db1af2010-11-23 13:34:33 +09003646 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3647 ret.add(imi);
3648 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3649 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003650 }
3651 }
satokf3db1af2010-11-23 13:34:33 +09003652 return ret;
satok4e4569d2010-11-19 18:45:53 +09003653 }
3654 }
3655
satok42c5a162011-05-26 16:46:14 +09003656 @Override
satokb66d2872010-11-10 01:04:04 +09003657 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003658 // TODO: Make this work even for non-current users?
3659 if (!calledFromValidUser()) {
3660 return false;
3661 }
satokb66d2872010-11-10 01:04:04 +09003662 synchronized (mMethodMap) {
3663 if (subtype != null && mCurMethodId != null) {
3664 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003665 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003666 if (subtypeId != NOT_A_SUBTYPE_ID) {
3667 setInputMethodLocked(mCurMethodId, subtypeId);
3668 return true;
3669 }
3670 }
3671 return false;
3672 }
3673 }
3674
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003675 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003676 private static class InputMethodFileManager {
3677 private static final String SYSTEM_PATH = "system";
3678 private static final String INPUT_METHOD_PATH = "inputmethod";
3679 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3680 private static final String NODE_SUBTYPES = "subtypes";
3681 private static final String NODE_SUBTYPE = "subtype";
3682 private static final String NODE_IMI = "imi";
3683 private static final String ATTR_ID = "id";
3684 private static final String ATTR_LABEL = "label";
3685 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003686 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003687 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003688 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003689 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3690 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3691 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003692 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003693 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3694 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003695 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003696 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003697 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003698 if (methodMap == null) {
3699 throw new NullPointerException("methodMap is null");
3700 }
3701 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003702 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003703 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3704 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003705 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003706 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003707 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3708 }
3709 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3710 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3711 if (!subtypeFile.exists()) {
3712 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003713 writeAdditionalInputMethodSubtypes(
3714 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003715 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003716 readAdditionalInputMethodSubtypes(
3717 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003718 }
3719 }
3720
3721 private void deleteAllInputMethodSubtypes(String imiId) {
3722 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003723 mAdditionalSubtypesMap.remove(imiId);
3724 writeAdditionalInputMethodSubtypes(
3725 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003726 }
3727 }
3728
3729 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003730 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003731 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003732 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003733 final int N = additionalSubtypes.length;
3734 for (int i = 0; i < N; ++i) {
3735 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003736 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003737 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003738 } else {
3739 Slog.w(TAG, "Duplicated subtype definition found: "
3740 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003741 }
3742 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003743 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3744 writeAdditionalInputMethodSubtypes(
3745 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003746 }
3747 }
3748
3749 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3750 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003751 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003752 }
3753 }
3754
3755 private static void writeAdditionalInputMethodSubtypes(
3756 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3757 HashMap<String, InputMethodInfo> methodMap) {
3758 // Safety net for the case that this function is called before methodMap is set.
3759 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3760 FileOutputStream fos = null;
3761 try {
3762 fos = subtypesFile.startWrite();
3763 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003764 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003765 out.startDocument(null, true);
3766 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3767 out.startTag(null, NODE_SUBTYPES);
3768 for (String imiId : allSubtypes.keySet()) {
3769 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3770 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3771 continue;
3772 }
3773 out.startTag(null, NODE_IMI);
3774 out.attribute(null, ATTR_ID, imiId);
3775 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3776 final int N = subtypesList.size();
3777 for (int i = 0; i < N; ++i) {
3778 final InputMethodSubtype subtype = subtypesList.get(i);
3779 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003780 if (subtype.hasSubtypeId()) {
3781 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3782 String.valueOf(subtype.getSubtypeId()));
3783 }
satoke7c6998e2011-06-03 17:57:59 +09003784 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3785 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3786 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003787 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3788 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003789 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3790 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3791 out.attribute(null, ATTR_IS_AUXILIARY,
3792 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003793 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3794 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003795 out.endTag(null, NODE_SUBTYPE);
3796 }
3797 out.endTag(null, NODE_IMI);
3798 }
3799 out.endTag(null, NODE_SUBTYPES);
3800 out.endDocument();
3801 subtypesFile.finishWrite(fos);
3802 } catch (java.io.IOException e) {
3803 Slog.w(TAG, "Error writing subtypes", e);
3804 if (fos != null) {
3805 subtypesFile.failWrite(fos);
3806 }
3807 }
3808 }
3809
3810 private static void readAdditionalInputMethodSubtypes(
3811 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3812 if (allSubtypes == null || subtypesFile == null) return;
3813 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003814 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003815 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003816 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003817 int type = parser.getEventType();
3818 // Skip parsing until START_TAG
3819 while ((type = parser.next()) != XmlPullParser.START_TAG
3820 && type != XmlPullParser.END_DOCUMENT) {}
3821 String firstNodeName = parser.getName();
3822 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3823 throw new XmlPullParserException("Xml doesn't start with subtypes");
3824 }
3825 final int depth =parser.getDepth();
3826 String currentImiId = null;
3827 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3828 while (((type = parser.next()) != XmlPullParser.END_TAG
3829 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3830 if (type != XmlPullParser.START_TAG)
3831 continue;
3832 final String nodeName = parser.getName();
3833 if (NODE_IMI.equals(nodeName)) {
3834 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3835 if (TextUtils.isEmpty(currentImiId)) {
3836 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3837 continue;
3838 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003839 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003840 allSubtypes.put(currentImiId, tempSubtypesArray);
3841 } else if (NODE_SUBTYPE.equals(nodeName)) {
3842 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3843 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3844 continue;
3845 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003846 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003847 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003848 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003849 parser.getAttributeValue(null, ATTR_LABEL));
3850 final String imeSubtypeLocale =
3851 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003852 final String languageTag =
3853 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003854 final String imeSubtypeMode =
3855 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3856 final String imeSubtypeExtraValue =
3857 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003858 final boolean isAuxiliary = "1".equals(String.valueOf(
3859 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003860 final boolean isAsciiCapable = "1".equals(String.valueOf(
3861 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003862 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003863 .setSubtypeNameResId(label)
3864 .setSubtypeIconResId(icon)
3865 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003866 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003867 .setSubtypeMode(imeSubtypeMode)
3868 .setSubtypeExtraValue(imeSubtypeExtraValue)
3869 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003870 .setIsAsciiCapable(isAsciiCapable);
3871 final String subtypeIdString =
3872 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3873 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003874 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003875 }
3876 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003877 }
3878 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003879 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3880 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003881 return;
satoke7c6998e2011-06-03 17:57:59 +09003882 }
3883 }
3884 }
3885
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003886 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3887 @NonNull
3888 private final Handler mHandler;
3889
3890 LocalServiceImpl(@NonNull final Handler handler) {
3891 mHandler = handler;
3892 }
3893
3894 @Override
3895 public void setInteractive(boolean interactive) {
3896 // Do everything in handler so as not to block the caller.
3897 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3898 interactive ? 1 : 0, 0));
3899 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003900
3901 @Override
3902 public void switchInputMethod(boolean forwardDirection) {
3903 // Do everything in handler so as not to block the caller.
3904 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3905 forwardDirection ? 1 : 0, 0));
3906 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003907
3908 @Override
3909 public void hideCurrentInputMethod() {
3910 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3911 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3912 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003913 }
3914
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003915 private static String imeWindowStatusToString(final int imeWindowVis) {
3916 final StringBuilder sb = new StringBuilder();
3917 boolean first = true;
3918 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3919 sb.append("Active");
3920 first = false;
3921 }
3922 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3923 if (!first) {
3924 sb.append("|");
3925 }
3926 sb.append("Visible");
3927 }
3928 return sb.toString();
3929 }
3930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003932 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3933 @Nullable Uri contentUri, @Nullable String packageName) {
3934 if (!calledFromValidUser()) {
3935 return null;
3936 }
3937
3938 if (token == null) {
3939 throw new NullPointerException("token");
3940 }
3941 if (packageName == null) {
3942 throw new NullPointerException("packageName");
3943 }
3944 if (contentUri == null) {
3945 throw new NullPointerException("contentUri");
3946 }
3947 final String contentUriScheme = contentUri.getScheme();
3948 if (!"content".equals(contentUriScheme)) {
3949 throw new InvalidParameterException("contentUri must have content scheme");
3950 }
3951
3952 synchronized (mMethodMap) {
3953 final int uid = Binder.getCallingUid();
3954 if (mCurMethodId == null) {
3955 return null;
3956 }
3957 if (mCurToken != token) {
3958 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3959 + " token=" + token);
3960 return null;
3961 }
3962 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3963 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3964 // nature of our system. Let's compare it with our internal record.
3965 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3966 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3967 + mCurAttribute.packageName + " packageName=" + packageName);
3968 return null;
3969 }
3970 final int imeUserId = UserHandle.getUserId(uid);
3971 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3972 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3973 appUserId);
3974 }
3975 }
3976
3977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3979 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3980 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3983 + Binder.getCallingPid()
3984 + ", uid=" + Binder.getCallingUid());
3985 return;
3986 }
3987
3988 IInputMethod method;
3989 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003990 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 synchronized (mMethodMap) {
3995 p.println("Current Input Method Manager state:");
3996 int N = mMethodList.size();
3997 p.println(" Input Methods:");
3998 for (int i=0; i<N; i++) {
3999 InputMethodInfo info = mMethodList.get(i);
4000 p.println(" InputMethod #" + i + ":");
4001 info.dump(p, " ");
4002 }
4003 p.println(" Clients:");
4004 for (ClientState ci : mClients.values()) {
4005 p.println(" Client " + ci + ":");
4006 p.println(" client=" + ci.client);
4007 p.println(" inputContext=" + ci.inputContext);
4008 p.println(" sessionRequested=" + ci.sessionRequested);
4009 p.println(" curSession=" + ci.curSession);
4010 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004011 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004013 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4014 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004015 focusedWindowClient = mCurFocusedWindowClient;
4016 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4018 + " mBoundToMethod=" + mBoundToMethod);
4019 p.println(" mCurToken=" + mCurToken);
4020 p.println(" mCurIntent=" + mCurIntent);
4021 method = mCurMethod;
4022 p.println(" mCurMethod=" + mCurMethod);
4023 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004024 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 p.println(" mShowRequested=" + mShowRequested
4026 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4027 + " mShowForced=" + mShowForced
4028 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004029 p.println(" mCurUserActionNotificationSequenceNumber="
4030 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004031 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004032 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004033 p.println(" mSwitchingController:");
4034 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004035 p.println(" mSettings:");
4036 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004038
Jeff Brownb88102f2010-09-08 11:49:43 -07004039 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 pw.flush();
4042 try {
4043 client.client.asBinder().dump(fd, args);
4044 } catch (RemoteException e) {
4045 p.println("Input method client dead: " + e);
4046 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004047 } else {
4048 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004050
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004051 if (focusedWindowClient != null && client != focusedWindowClient) {
4052 p.println(" ");
4053 p.println("Warning: Current input method client doesn't match the last focused. "
4054 + "window.");
4055 p.println("Dumping input method client in the last focused window just in case.");
4056 p.println(" ");
4057 pw.flush();
4058 try {
4059 focusedWindowClient.client.asBinder().dump(fd, args);
4060 } catch (RemoteException e) {
4061 p.println("Input method client in focused window dead: " + e);
4062 }
4063 }
4064
Jeff Brownb88102f2010-09-08 11:49:43 -07004065 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 pw.flush();
4068 try {
4069 method.asBinder().dump(fd, args);
4070 } catch (RemoteException e) {
4071 p.println("Input method service dead: " + e);
4072 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004073 } else {
4074 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 }
4076 }
4077}