blob: 71ac54492cb6c74902ccb1f4f397a7177686f5c0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070018import static java.lang.annotation.RetentionPolicy.SOURCE;
19
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070021import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090023import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090024import com.android.internal.inputmethod.InputMethodUtils;
25import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070027import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090028import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputContext;
30import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070034import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080036import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080037import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090041import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070043import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070044import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070045import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080046import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.app.ActivityManagerNative;
48import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070049import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090050import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090051import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090052import android.app.Notification;
53import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070054import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090055import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ComponentName;
57import android.content.ContentResolver;
58import android.content.Context;
59import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090061import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090063import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070065import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090066import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.pm.PackageManager;
68import android.content.pm.ResolveInfo;
69import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070070import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.res.Resources;
72import android.content.res.TypedArray;
73import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080074import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080075import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080076import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070077import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040079import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070080import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090081import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Handler;
83import android.os.IBinder;
84import android.os.IInterface;
85import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070086import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070088import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080090import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.ServiceManager;
92import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090093import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080094import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.provider.Settings;
96import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090097import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070098import android.util.ArrayMap;
99import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700100import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900102import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900103import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.util.PrintWriterPrinter;
105import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900106import android.util.Slog;
107import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900108import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700110import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.view.LayoutInflater;
112import android.view.View;
113import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700115import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900116import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700118import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.view.inputmethod.InputMethod;
120import android.view.inputmethod.InputMethodInfo;
121import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700122import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900123import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900124import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900126import android.widget.CompoundButton;
127import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900128import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900129import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900130import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700131import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
satoke7c6998e2011-06-03 17:57:59 +0900133import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900135import java.io.FileInputStream;
136import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.io.IOException;
138import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700139import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100140import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700141import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900143import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.util.HashMap;
145import java.util.List;
146
147/**
148 * This class provides a system service that manages input methods.
149 */
150public class InputMethodManagerService extends IInputMethodManager.Stub
151 implements ServiceConnection, Handler.Callback {
152 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700153 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700154 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700156 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
157 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
158 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_UNBIND_INPUT = 1000;
161 static final int MSG_BIND_INPUT = 1010;
162 static final int MSG_SHOW_SOFT_INPUT = 1020;
163 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700164 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 static final int MSG_ATTACH_TOKEN = 1040;
166 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final int MSG_START_INPUT = 2000;
169 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
Yohei Yukawa33e81792015-11-17 21:14:42 -0800171 static final int MSG_UNBIND_CLIENT = 3000;
172 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700173 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700174 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900175 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800176 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
satok01038492012-04-09 21:08:27 +0900178 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
179
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700180 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
181
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700182 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
satokf9f01002011-05-19 21:31:50 +0900184 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
185
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900186 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900187 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900188
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700189 @Retention(SOURCE)
190 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
191 private @interface HardKeyboardBehavior {
192 int WIRELESS_AFFORDANCE = 0;
193 int WIRED_AFFORDANCE = 1;
194 }
satok4e4569d2010-11-19 18:45:53 +0900195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800197 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900199 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700202 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700204 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900205 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900206 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900207 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800208 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900210 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 // All known input methods. mMethodMap also serves as the global
213 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700214 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
215 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900216 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700217 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900218 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700220 // Used to bring IME service up to visible adjustment while it is being shown.
221 final ServiceConnection mVisibleConnection = new ServiceConnection() {
222 @Override public void onServiceConnected(ComponentName name, IBinder service) {
223 }
224
225 @Override public void onServiceDisconnected(ComponentName name) {
226 }
227 };
228 boolean mVisibleBound = false;
229
satok7cfc0ed2011-06-20 21:29:36 +0900230 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700231 private NotificationManager mNotificationManager;
232 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700233 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400234 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700235 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900236 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900237 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900238 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900239
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900240 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 final ClientState client;
242 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700243
244 IInputMethodSession session;
245 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 @Override
248 public String toString() {
249 return "SessionState{uid " + client.uid + " pid " + client.pid
250 + " method " + Integer.toHexString(
251 System.identityHashCode(method))
252 + " session " + Integer.toHexString(
253 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700254 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 + "}";
256 }
257
258 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700259 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 client = _client;
261 method = _method;
262 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700263 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
Jeff Brownc28867a2013-03-26 15:42:39 -0700267 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 final IInputMethodClient client;
269 final IInputContext inputContext;
270 final int uid;
271 final int pid;
272 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 boolean sessionRequested;
275 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 @Override
278 public String toString() {
279 return "ClientState{" + Integer.toHexString(
280 System.identityHashCode(this)) + " uid " + uid
281 + " pid " + pid + "}";
282 }
283
284 ClientState(IInputMethodClient _client, IInputContext _inputContext,
285 int _uid, int _pid) {
286 client = _client;
287 inputContext = _inputContext;
288 uid = _uid;
289 pid = _pid;
290 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
291 }
292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700294 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700297 * Set once the system is ready to run third party code.
298 */
299 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700301 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700302 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
303 * method. This is to be synchronized with the secure settings keyed with
304 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
305 *
306 * <p>This can be transiently {@code null} when the system is re-initializing input method
307 * settings, e.g., the system locale is just changed.</p>
308 *
309 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
310 * {@link InputMethodManagerService}.</p>
311 *
312 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700314 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * The current binding sequence number, incremented every time there is
319 * a new bind performed.
320 */
321 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * The client that is currently bound to an input method.
325 */
326 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800329 * The last window token that we confirmed to be focused. This is always updated upon reports
330 * from the input method client. If the window state is already changed before the report is
331 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700332 */
333 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700335 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800336 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
337 */
338 ClientState mCurFocusedWindowClient;
339
340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * The input context last provided by the current client.
342 */
343 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700346 * The missing method flags for the input context last provided by the current client.
347 *
348 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
349 */
350 int mCurInputContextMissingMethods;
351
352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 * The attributes last provided by the current client.
354 */
355 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700358 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700360 *
361 * <p>This can be {@code null} when no input method is connected.</p>
362 *
363 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700365 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
satokab751aa2010-09-14 19:17:36 +0900369 * The current subtype of the current input method.
370 */
371 private InputMethodSubtype mCurrentSubtype;
372
satok4e4569d2010-11-19 18:45:53 +0900373 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900374 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700375 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900376
John Spurlocke0980502013-10-25 11:59:29 -0400377 // Was the keyguard locked when this client became current?
378 private boolean mCurClientInKeyguard;
379
satokab751aa2010-09-14 19:17:36 +0900380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * Set to true if our ServiceConnection is currently actively bound to
382 * a service (whether or not we have gotten its IBinder back yet).
383 */
384 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Set if the client has asked for the input method to be shown.
388 */
389 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * Set if we were explicitly told to show the input method.
393 */
394 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * Set if we were forced to be shown.
398 */
399 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 /**
402 * Set if we last told the input method to show itself.
403 */
404 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * The Intent used to connect to the current input method.
408 */
409 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * The token we have made for the currently active input method, to
413 * identify it in the future.
414 */
415 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
418 * If non-null, this is the input method service we are currently connected
419 * to.
420 */
421 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
424 * Time that we last initiated a bind to the input method, to determine
425 * if we should try to disconnect and reconnect to it.
426 */
427 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
430 * Have we called mCurMethod.bindInput()?
431 */
432 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
435 * Currently enabled session. Only touched by service thread, not
436 * protected by a lock.
437 */
438 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700441 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700443 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900445 int mCurUserActionNotificationSequenceNumber = 0;
446
Joe Onorato857fd9b2011-01-27 15:08:35 -0800447 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900448
449 /**
450 * A set of status bits regarding the active IME.
451 *
452 * <p>This value is a combination of following two bits:</p>
453 * <dl>
454 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
455 * <dd>
456 * If this bit is ON, connected IME is ready to accept touch/key events.
457 * </dd>
458 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
459 * <dd>
460 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
461 * </dd>
462 * </dl>
463 * <em>Do not update this value outside of setImeWindowStatus.</em>
464 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800465 int mImeWindowVis;
466
Ken Wakasa05dbb652011-08-22 15:22:43 +0900467 private AlertDialog.Builder mDialogBuilder;
468 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900469 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700470 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900471 private InputMethodInfo[] mIms;
472 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800473 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700474 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800475 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900476 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
477 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500478 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700479 @HardKeyboardBehavior
480 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700483 int mUserId;
484 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700485 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800486 String mLastEnabled = "";
487
Yohei Yukawa81482972015-06-04 00:58:59 -0700488 /**
489 * <em>This constructor must be called within the lock.</em>
490 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 SettingsObserver(Handler handler) {
492 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700493 }
494
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800495 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700496 if (mRegistered && mUserId == userId) {
497 return;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700500 if (mRegistered) {
501 mContext.getContentResolver().unregisterContentObserver(this);
502 mRegistered = false;
503 }
504 if (mUserId != userId) {
505 mLastEnabled = "";
506 mUserId = userId;
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900510 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900512 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700513 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700514 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700515 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800516 resolver.registerContentObserver(Settings.Secure.getUriFor(
517 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700518 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800520
Michael Wright7b5a96b2014-08-09 19:28:42 -0700521 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800522 final Uri showImeUri = Settings.Secure.getUriFor(
523 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
524 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
525 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700527 if (showImeUri.equals(uri)) {
528 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800529 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
530 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
531 mContext.getContentResolver(),
532 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
533 0, mUserId) == 1;
534 if (mAccessibilityRequestingNoSoftKeyboard) {
535 final boolean showRequested = mShowRequested;
536 hideCurrentInputLocked(0, null);
537 mShowRequested = showRequested;
538 } else if (mShowRequested) {
539 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
540 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700541 } else {
542 boolean enabledChanged = false;
543 String newEnabled = mSettings.getEnabledInputMethodsStr();
544 if (!mLastEnabled.equals(newEnabled)) {
545 mLastEnabled = newEnabled;
546 enabledChanged = true;
547 }
548 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700552
553 @Override
554 public String toString() {
555 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
556 + " mLastEnabled=" + mLastEnabled + "}";
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800559
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900560 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900561 @Override
562 public void onReceive(Context context, Intent intent) {
563 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700564 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900565 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700566 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900567 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800568 } else if (Intent.ACTION_USER_ADDED.equals(action)
569 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100570 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800571 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700572 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
573 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
574 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
575 final String prevValue = intent.getStringExtra(
576 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
577 final String newValue = intent.getStringExtra(
578 Intent.EXTRA_SETTING_NEW_VALUE);
579 restoreEnabledInputMethods(mContext, prevValue, newValue);
580 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 } else {
582 Slog.w(TAG, "Unexpected intent " + intent);
583 }
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800586
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700587 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
588 // does not attempt to validate on the fly with any installed device policy, so must only
589 // be run in the context of initial device setup.
590 //
591 // TODO: Move this method to InputMethodUtils with adding unit tests.
592 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
593 if (DEBUG_RESTORE) {
594 Slog.i(TAG, "Restoring enabled input methods:");
595 Slog.i(TAG, "prev=" + prevValue);
596 Slog.i(TAG, " new=" + newValue);
597 }
598 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900599 ArrayMap<String, ArraySet<String>> prevMap =
600 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
601 ArrayMap<String, ArraySet<String>> newMap =
602 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700603
604 // Merge the restored ime+subtype enabled states into the live state
605 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
606 final String imeId = entry.getKey();
607 ArraySet<String> prevSubtypes = prevMap.get(imeId);
608 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700609 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700610 prevMap.put(imeId, prevSubtypes);
611 }
612 prevSubtypes.addAll(entry.getValue());
613 }
614
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700615 final String mergedImesAndSubtypesString =
616 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700617 if (DEBUG_RESTORE) {
618 Slog.i(TAG, "Merged IME string:");
619 Slog.i(TAG, " " + mergedImesAndSubtypesString);
620 }
621 Settings.Secure.putString(context.getContentResolver(),
622 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
623 }
624
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800625 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900626 private boolean isChangingPackagesOfCurrentUser() {
627 final int userId = getChangingUserId();
628 final boolean retval = userId == mSettings.getCurrentUserId();
629 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900630 if (!retval) {
631 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
632 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 }
634 return retval;
635 }
636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800638 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 if (!isChangingPackagesOfCurrentUser()) {
640 return false;
641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900643 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 final int N = mMethodList.size();
645 if (curInputMethodId != null) {
646 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 InputMethodInfo imi = mMethodList.get(i);
648 if (imi.getId().equals(curInputMethodId)) {
649 for (String pkg : packages) {
650 if (imi.getPackageName().equals(pkg)) {
651 if (!doit) {
652 return true;
653 }
satok723a27e2010-11-11 14:58:11 +0900654 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800655 chooseNewDefaultIMELocked();
656 return true;
657 }
658 }
659 }
660 }
661 }
662 }
663 return false;
664 }
665
666 @Override
667 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900668 if (!isChangingPackagesOfCurrentUser()) {
669 return;
670 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 synchronized (mMethodMap) {
672 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900673 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 final int N = mMethodList.size();
675 if (curInputMethodId != null) {
676 for (int i=0; i<N; i++) {
677 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900678 final String imiId = imi.getId();
679 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 curIm = imi;
681 }
satoke7c6998e2011-06-03 17:57:59 +0900682
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900684 if (isPackageModified(imi.getPackageName())) {
685 mFileManager.deleteAllInputMethodSubtypes(imiId);
686 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800687 if (change == PACKAGE_TEMPORARY_CHANGE
688 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800689 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800690 + imi.getComponent());
691 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
Yohei Yukawa94e33302016-02-12 19:37:03 -0800696 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800699
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800700 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800701 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800702 if (change == PACKAGE_TEMPORARY_CHANGE
703 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 ServiceInfo si = null;
705 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900706 si = mIPackageManager.getServiceInfo(
707 curIm.getComponent(), 0, mSettings.getCurrentUserId());
708 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800709 }
710 if (si == null) {
711 // Uh oh, current input method is no longer around!
712 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900714 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800715 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800716 changed = true;
717 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800718 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900719 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800720 }
721 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800722 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800723 }
satokab751aa2010-09-14 19:17:36 +0900724
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800725 if (curIm == null) {
726 // We currently don't have a default input method... is
727 // one now available?
728 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700729 } else if (!changed && isPackageModified(curIm.getPackageName())) {
730 // Even if the current input method is still available, mCurrentSubtype could
731 // be obsolete when the package is modified in practice.
732 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800736 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738 }
739 }
740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Jeff Brownc28867a2013-03-26 15:42:39 -0700742 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900743 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700744 private final IInputMethod mMethod;
745 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
Jeff Brownc28867a2013-03-26 15:42:39 -0700747 MethodCallback(InputMethodManagerService imms, IInputMethod method,
748 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900749 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700750 mMethod = method;
751 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
satoke7c6998e2011-06-03 17:57:59 +0900754 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700755 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700756 long ident = Binder.clearCallingIdentity();
757 try {
758 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
759 } finally {
760 Binder.restoreCallingIdentity(ident);
761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
satok01038492012-04-09 21:08:27 +0900765 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700766 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900767 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700768 public void onHardKeyboardStatusChange(boolean available) {
769 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
770 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900771 }
772
Michael Wright7b5a96b2014-08-09 19:28:42 -0700773 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900774 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700775 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900776 }
777 synchronized(mMethodMap) {
778 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
779 && mSwitchingDialog.isShowing()) {
780 mSwitchingDialogTitleView.findViewById(
781 com.android.internal.R.id.hard_keyboard_section).setVisibility(
782 available ? View.VISIBLE : View.GONE);
783 }
784 }
785 }
786 }
787
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800788 public static final class Lifecycle extends SystemService {
789 private InputMethodManagerService mService;
790
791 public Lifecycle(Context context) {
792 super(context);
793 mService = new InputMethodManagerService(context);
794 }
795
796 @Override
797 public void onStart() {
798 LocalServices.addService(InputMethodManagerInternal.class,
799 new LocalServiceImpl(mService.mHandler));
800 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
801 }
802
803 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800804 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700805 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800806 // TODO: Dispatch this to a worker thread as needed.
807 mService.onSwitchUser(userHandle);
808 }
809
810 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800811 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700812 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800813 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800814 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
815 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
816 .getService(Context.STATUS_BAR_SERVICE);
817 mService.systemRunning(statusBarService);
818 }
819 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800820
821 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700822 public void onUnlockUser(final @UserIdInt int userHandle) {
823 // Called on ActivityManager thread.
824 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
825 userHandle));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800826 }
827 }
828
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800829 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800830 synchronized(mMethodMap) {
831 final int currentUserId = mSettings.getCurrentUserId();
832 if (DEBUG) {
833 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
834 }
835 if (userId != currentUserId) {
836 return;
837 }
838 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
839 // We need to rebuild IMEs.
840 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
841 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
842 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800843 }
844
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800845 void onSwitchUser(@UserIdInt int userId) {
846 synchronized (mMethodMap) {
847 switchUserLocked(userId);
848 }
849 }
850
Seigo Nonaka7309b122015-08-17 18:34:13 -0700851 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900852 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800854 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700856 // Note: SettingsObserver doesn't register observers in its constructor.
857 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 mIWindowManager = IWindowManager.Stub.asInterface(
859 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700860 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800861 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900862 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 public void executeMessage(Message msg) {
864 handleMessage(msg);
865 }
Mita Yuned218c72012-12-06 17:18:25 -0800866 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800867 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800868 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900869 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700870 mHasFeature = context.getPackageManager().hasSystemFeature(
871 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500872 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700873 mHardKeyboardBehavior = mContext.getResources().getInteger(
874 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900875
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400876 Bundle extras = new Bundle();
877 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
878 mImeSwitcherNotification = new Notification.Builder(mContext)
879 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
880 .setWhen(0)
881 .setOngoing(true)
882 .addExtras(extras)
883 .setCategory(Notification.CATEGORY_SYSTEM)
884 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400885
satok7cfc0ed2011-06-20 21:29:36 +0900886 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900887 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900888
889 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900890
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900891 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800893 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
894 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700895 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900896 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
satok7cfc0ed2011-06-20 21:29:36 +0900898 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 int userId = 0;
900 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
902 } catch (RemoteException e) {
903 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
904 }
satok81f8b7c2012-12-04 20:42:56 +0900905 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900906
satokd87c2592010-09-29 11:52:06 +0900907 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900908 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800909 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700910
Kenny Guy2a764942014-04-02 13:29:20 +0100911 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900912 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900913 synchronized (mMethodMap) {
914 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
915 mSettings, context);
916 }
satok0a1bcf42012-05-16 19:26:31 +0900917
918 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900919 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900920 if (DEBUG) {
921 Slog.d(TAG, "Initial default ime = " + defaultImiId);
922 }
satok0a1bcf42012-05-16 19:26:31 +0900923 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
924
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900925 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800926 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900927 }
satokd87c2592010-09-29 11:52:06 +0900928 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929
satok0a1bcf42012-05-16 19:26:31 +0900930 if (!mImeSelectedOnBoot) {
931 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900932 synchronized (mMethodMap) {
933 resetDefaultImeLocked(context);
934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800936
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900937 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700938 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900939 updateFromSettingsLocked(true);
940 }
satok5b927c432012-05-01 20:09:34 +0900941
942 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
943 // according to the new system locale.
944 final IntentFilter filter = new IntentFilter();
945 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
946 mContext.registerReceiver(
947 new BroadcastReceiver() {
948 @Override
949 public void onReceive(Context context, Intent intent) {
950 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900951 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900952 }
953 }
954 }, filter);
955 }
956
satok5b927c432012-05-01 20:09:34 +0900957 private void resetDefaultImeLocked(Context context) {
958 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800959 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900960 return;
961 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800962 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
963 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
964 if (suitableImes.isEmpty()) {
965 Slog.i(TAG, "No default found");
966 return;
satok5b927c432012-05-01 20:09:34 +0900967 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800968 final InputMethodInfo defIm = suitableImes.get(0);
969 Slog.i(TAG, "Default found, using " + defIm.getId());
970 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900971 }
972
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900973 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
974 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 if (!mSystemReady) {
976 // not system ready
977 return;
978 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800979 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800981 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900982 if (!updateOnlyWhenLocaleChanged) {
983 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800984 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 }
986 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800987 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800989 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 if (!updateOnlyWhenLocaleChanged) {
991 final String selectedImiId = mSettings.getSelectedInputMethod();
992 if (TextUtils.isEmpty(selectedImiId)) {
993 // This is the first time of the user switch and
994 // set the current ime to the proper one.
995 resetDefaultImeLocked(mContext);
996 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900997 } else {
998 // If the locale is changed, needs to reset the default ime
999 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001001 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001002 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 if (!updateOnlyWhenLocaleChanged) {
1004 try {
1005 startInputInnerLocked();
1006 } catch (RuntimeException e) {
1007 Slog.w(TAG, "Unexpected exception", e);
1008 }
1009 }
1010 }
1011 }
1012
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001013 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001014 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1015 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001016 }
1017
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001018 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001019 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1020 + " currentUserId=" + mSettings.getCurrentUserId());
1021
Yohei Yukawa81482972015-06-04 00:58:59 -07001022 // ContentObserver should be registered again when the user is changed
1023 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001024
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001025 // If the system is not ready or the device is not yed unlocked by the user, then we use
1026 // copy-on-write settings.
1027 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001028 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001029 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001030 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001031 // InputMethodFileManager should be reset when the user is changed
1032 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001033 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001034
1035 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1036 + " defaultImiId=" + defaultImiId);
1037
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001038 // For secondary users, the list of enabled IMEs may not have been updated since the
1039 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1040 // not be empty even if the IME has been uninstalled by the primary user.
1041 // Even in such cases, IMMS works fine because it will find the most applicable
1042 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001043 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001044 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001045 initialUserSwitch /* needsToResetDefaultIme */);
1046 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001047 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1048 mSettings.getEnabledInputMethodListLocked(), newUserId,
1049 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001050 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001051
1052 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1053 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001054 }
1055
Kenny Guy2a764942014-04-02 13:29:20 +01001056 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001057 mSettings.setCurrentProfileIds(
1058 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001059 }
1060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 @Override
1062 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1063 throws RemoteException {
1064 try {
1065 return super.onTransact(code, data, reply, flags);
1066 } catch (RuntimeException e) {
1067 // The input method manager only throws security exceptions, so let's
1068 // log all others.
1069 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001070 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 throw e;
1073 }
1074 }
1075
Svetoslav Ganova0027152013-06-25 14:59:53 -07001076 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001077 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001078 if (DEBUG) {
1079 Slog.d(TAG, "--- systemReady");
1080 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001081 if (!mSystemReady) {
1082 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001083 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001084 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001085 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001086 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1087 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001088 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001089 if (mStatusBar != null) {
1090 mStatusBar.setIconVisibility(mSlotIme, false);
1091 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001092 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001093 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1094 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001095 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001096 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001097 mHardKeyboardListener);
1098 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001099 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001100 if (!mImeSelectedOnBoot) {
1101 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001102 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001103 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1104 mSettings.getEnabledInputMethodListLocked(),
1105 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001106 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001107 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001108 try {
1109 startInputInnerLocked();
1110 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001112 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001113 }
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 // ---------------------------------------------------------------------------------------
1118 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1119 // 1) it comes from the system process
1120 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1121 private boolean calledFromValidUser() {
1122 final int uid = Binder.getCallingUid();
1123 final int userId = UserHandle.getUserId(uid);
1124 if (DEBUG) {
1125 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1126 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1127 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001128 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1129 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001130 }
Kenny Guy2a764942014-04-02 13:29:20 +01001131 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001134
1135 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1136 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1137 // must not manage background users' states in any functions.
1138 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1139 // by a token.
1140 if (mContext.checkCallingOrSelfPermission(
1141 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1142 == PackageManager.PERMISSION_GRANTED) {
1143 if (DEBUG) {
1144 Slog.d(TAG, "--- Access granted because the calling process has "
1145 + "the INTERACT_ACROSS_USERS_FULL permission");
1146 }
1147 return true;
1148 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001149 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1150 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001151 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001152 }
1153
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001154
1155 /**
1156 * Returns true iff the caller is identified to be the current input method with the token.
1157 * @param token The window token given to the input method when it was started.
1158 * @return true if and only if non-null valid token is specified.
1159 */
1160 private boolean calledWithValidToken(IBinder token) {
1161 if (token == null || mCurToken != token) {
1162 return false;
1163 }
1164 return true;
1165 }
1166
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 private boolean bindCurrentInputMethodService(
1168 Intent service, ServiceConnection conn, int flags) {
1169 if (service == null || conn == null) {
1170 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1171 return false;
1172 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001173 return mContext.bindServiceAsUser(service, conn, flags,
1174 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 }
1176
satoke7c6998e2011-06-03 17:57:59 +09001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001179 // TODO: Make this work even for non-current users?
1180 if (!calledFromValidUser()) {
1181 return Collections.emptyList();
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001184 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
1187
satoke7c6998e2011-06-03 17:57:59 +09001188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 // TODO: Make this work even for non-current users?
1191 if (!calledFromValidUser()) {
1192 return Collections.emptyList();
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001195 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
1198
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001199 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001200 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001201 * @return enabled subtypes of the specified imi
1202 */
satoke7c6998e2011-06-03 17:57:59 +09001203 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001204 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001205 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001206 // TODO: Make this work even for non-current users?
1207 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001208 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001209 }
satok67ddf9c2010-11-17 09:45:54 +09001210 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001211 final InputMethodInfo imi;
1212 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001213 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001214 } else {
1215 imi = mMethodMap.get(imiId);
1216 }
1217 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001218 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001219 }
1220 return mSettings.getEnabledInputMethodSubtypeListLocked(
1221 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001222 }
1223 }
1224
satoke7c6998e2011-06-03 17:57:59 +09001225 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 public void addClient(IInputMethodClient client,
1227 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001228 if (!calledFromValidUser()) {
1229 return;
1230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 synchronized (mMethodMap) {
1232 mClients.put(client.asBinder(), new ClientState(client,
1233 inputContext, uid, pid));
1234 }
1235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236
satoke7c6998e2011-06-03 17:57:59 +09001237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001239 if (!calledFromValidUser()) {
1240 return;
1241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001243 ClientState cs = mClients.remove(client.asBinder());
1244 if (cs != null) {
1245 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001246 if (mCurClient == cs) {
1247 mCurClient = null;
1248 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001249 if (mCurFocusedWindowClient == cs) {
1250 mCurFocusedWindowClient = null;
1251 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 void executeOrSendMessage(IInterface target, Message msg) {
1257 if (target.asBinder() instanceof Binder) {
1258 mCaller.sendMessage(msg);
1259 } else {
1260 handleMessage(msg);
1261 msg.recycle();
1262 }
1263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
Yohei Yukawa33e81792015-11-17 21:14:42 -08001265 void unbindCurrentClientLocked(
1266 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001268 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 + mCurClient.client.asBinder());
1270 if (mBoundToMethod) {
1271 mBoundToMethod = false;
1272 if (mCurMethod != null) {
1273 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1274 MSG_UNBIND_INPUT, mCurMethod));
1275 }
1276 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001277
1278 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1279 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001280 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1281 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project10592532009-03-18 17:39:46 -07001285 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 private int getImeShowFlags() {
1290 int flags = 0;
1291 if (mShowForced) {
1292 flags |= InputMethod.SHOW_FORCED
1293 | InputMethod.SHOW_EXPLICIT;
1294 } else if (mShowExplicitlyRequested) {
1295 flags |= InputMethod.SHOW_EXPLICIT;
1296 }
1297 return flags;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 private int getAppShowFlags() {
1301 int flags = 0;
1302 if (mShowForced) {
1303 flags |= InputMethodManager.SHOW_FORCED;
1304 } else if (!mShowExplicitlyRequested) {
1305 flags |= InputMethodManager.SHOW_IMPLICIT;
1306 }
1307 return flags;
1308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 if (!mBoundToMethod) {
1312 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1313 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1314 mBoundToMethod = true;
1315 }
1316 final SessionState session = mCurClient.curSession;
1317 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001318 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1319 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1320 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001322 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1323 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1324 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001327 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001330 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001331 (session.channel != null ? session.channel.dup() : null),
1332 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001335 InputBindResult startInputLocked(
1336 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001337 IInputMethodClient client, IInputContext inputContext,
1338 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001339 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // If no method is currently selected, do nothing.
1341 if (mCurMethodId == null) {
1342 return mNoBinding;
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 ClientState cs = mClients.get(client.asBinder());
1346 if (cs == null) {
1347 throw new IllegalArgumentException("unknown client "
1348 + client.asBinder());
1349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001350
Yohei Yukawa74750f22016-03-22 12:54:22 -07001351 if (attribute == null) {
1352 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1353 + " uid=" + cs.uid + " pid=" + cs.pid);
1354 return null;
1355 }
1356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 try {
1358 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1359 // Check with the window manager to make sure this client actually
1360 // has a window with focus. If not, reject. This is thread safe
1361 // because if the focus changes some time before or after, the
1362 // next client receiving focus that has any interest in input will
1363 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1366 return null;
1367 }
1368 } catch (RemoteException e) {
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001371 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1372 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001373 }
1374
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001375 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001376 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001377 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001378 // If no method is currently selected, do nothing.
1379 if (mCurMethodId == null) {
1380 return mNoBinding;
1381 }
1382
Yohei Yukawad57ba672015-06-08 16:39:46 -07001383 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1384 attribute.packageName)) {
1385 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1386 + " uid=" + cs.uid + " package=" + attribute.packageName);
1387 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001391 // Was the keyguard locked when switching over to the new client?
1392 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 // If the client is changing, we need to switch over to the new
1394 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001395 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001396 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001397 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398
1399 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001400 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001401 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001402 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // Bump up the sequence for this client and attach it.
1407 mCurSeq++;
1408 if (mCurSeq <= 0) mCurSeq = 1;
1409 mCurClient = cs;
1410 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001411 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 // Check if the input method is changing.
1415 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1416 if (cs.curSession != null) {
1417 // Fast case: if we are already connected to the input method,
1418 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001419 return attachNewInputLocked(
1420 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422 if (mHaveConnection) {
1423 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 // Return to client, and we will get back with it when
1425 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001426 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001427 return new InputBindResult(null, null, mCurId, mCurSeq,
1428 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 } else if (SystemClock.uptimeMillis()
1430 < (mLastBindTime+TIME_TO_RECONNECT)) {
1431 // In this case we have connected to the service, but
1432 // don't yet have its interface. If it hasn't been too
1433 // long since we did the connection, we'll return to
1434 // the client and wait to get the service interface so
1435 // we can report back. If it has been too long, we want
1436 // to fall through so we can try a disconnect/reconnect
1437 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001438 return new InputBindResult(null, null, mCurId, mCurSeq,
1439 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1442 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 }
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001447 return startInputInnerLocked();
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001450 InputBindResult startInputInnerLocked() {
1451 if (mCurMethodId == null) {
1452 return mNoBinding;
1453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001455 if (!mSystemReady) {
1456 // If the system is not yet ready, we shouldn't be running third
1457 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001458 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1459 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1463 if (info == null) {
1464 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001467 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1470 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001471 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1472 com.android.internal.R.string.input_method_binding_label);
1473 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1474 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001476 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1477 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mLastBindTime = SystemClock.uptimeMillis();
1479 mHaveConnection = true;
1480 mCurId = info.getId();
1481 mCurToken = new Binder();
1482 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001483 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mIWindowManager.addWindowToken(mCurToken,
1485 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1486 } catch (RemoteException e) {
1487 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001488 return new InputBindResult(null, null, mCurId, mCurSeq,
1489 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 } else {
1491 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001492 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 + mCurIntent);
1494 }
1495 return null;
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001498 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001499 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001500 IInputMethodClient client, IInputContext inputContext,
1501 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001502 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001503 if (!calledFromValidUser()) {
1504 return null;
1505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001507 if (DEBUG) {
1508 Slog.v(TAG, "startInput: reason="
1509 + InputMethodClient.getStartInputReason(startInputReason)
1510 + " client = " + client.asBinder()
1511 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001512 + " missingMethods="
1513 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001514 + " attribute=" + attribute
1515 + " controlFlags=#" + Integer.toHexString(controlFlags));
1516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 final long ident = Binder.clearCallingIdentity();
1518 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001519 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1520 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } finally {
1522 Binder.restoreCallingIdentity(ident);
1523 }
1524 }
1525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
satoke7c6998e2011-06-03 17:57:59 +09001527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 public void finishInput(IInputMethodClient client) {
1529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530
satoke7c6998e2011-06-03 17:57:59 +09001531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void onServiceConnected(ComponentName name, IBinder service) {
1533 synchronized (mMethodMap) {
1534 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1535 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001536 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001537 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001538 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001539 return;
1540 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001541 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001542 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1543 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001545 clearClientSessionLocked(mCurClient);
1546 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549 }
1550 }
1551
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1553 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 synchronized (mMethodMap) {
1555 if (mCurMethod != null && method != null
1556 && mCurMethod.asBinder() == method.asBinder()) {
1557 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001560 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001561 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 if (res.method != null) {
1563 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001564 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 }
1569 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001570
1571 // Session abandoned. Close its associated input channel.
1572 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001575 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001576 if (mVisibleBound) {
1577 mContext.unbindService(mVisibleConnection);
1578 mVisibleBound = false;
1579 }
1580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001581 if (mHaveConnection) {
1582 mContext.unbindService(this);
1583 mHaveConnection = false;
1584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001586 if (mCurToken != null) {
1587 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001588 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001589 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001590 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001591 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001592 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001593 mIWindowManager.removeWindowToken(mCurToken);
1594 } catch (RemoteException e) {
1595 }
1596 mCurToken = null;
1597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
The Android Open Source Project10592532009-03-18 17:39:46 -07001599 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001600 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
Yohei Yukawa33e81792015-11-17 21:14:42 -08001603 void resetCurrentMethodAndClient(
1604 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001605 mCurMethodId = null;
1606 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001607 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001609
1610 void requestClientSessionLocked(ClientState cs) {
1611 if (!cs.sessionRequested) {
1612 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1613 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1614 cs.sessionRequested = true;
1615 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1616 MSG_CREATE_SESSION, mCurMethod, channels[1],
1617 new MethodCallback(this, mCurMethod, channels[0])));
1618 }
1619 }
1620
1621 void clearClientSessionLocked(ClientState cs) {
1622 finishSessionLocked(cs.curSession);
1623 cs.curSession = null;
1624 cs.sessionRequested = false;
1625 }
1626
1627 private void finishSessionLocked(SessionState sessionState) {
1628 if (sessionState != null) {
1629 if (sessionState.session != null) {
1630 try {
1631 sessionState.session.finishSession();
1632 } catch (RemoteException e) {
1633 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001634 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001635 }
1636 sessionState.session = null;
1637 }
1638 if (sessionState.channel != null) {
1639 sessionState.channel.dispose();
1640 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001641 }
1642 }
1643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001644
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001645 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (mCurMethod != null) {
1647 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001648 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001650
Jeff Brownc28867a2013-03-26 15:42:39 -07001651 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001652 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 mCurMethod = null;
1654 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001655 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001656 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
satoke7c6998e2011-06-03 17:57:59 +09001660 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 public void onServiceDisconnected(ComponentName name) {
1662 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 + " mCurIntent=" + mCurIntent);
1665 if (mCurMethod != null && mCurIntent != null
1666 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 // We consider this to be a new bind attempt, since the system
1669 // should now try to restart the service for us.
1670 mLastBindTime = SystemClock.uptimeMillis();
1671 mShowRequested = mInputShown;
1672 mInputShown = false;
1673 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001674 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1675 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1676 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678 }
1679 }
1680 }
1681
satokf9f01002011-05-19 21:31:50 +09001682 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1684 long ident = Binder.clearCallingIdentity();
1685 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001687 if (!calledWithValidToken(token)) {
1688 final int uid = Binder.getCallingUid();
1689 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1690 + " token:" + token);
1691 return;
1692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001694 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001695 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001696 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001699 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001700 CharSequence contentDescription = null;
1701 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001702 // Use PackageManager to load label
1703 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001704 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001705 mIPackageManager.getApplicationInfo(packageName, 0,
1706 mSettings.getCurrentUserId()));
1707 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001708 /* ignore */
1709 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001710 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001711 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001712 contentDescription != null
1713 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001714 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717 }
1718 } finally {
1719 Binder.restoreCallingIdentity(ident);
1720 }
1721 }
1722
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001723 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001724 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001725 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001726 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001727 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001728 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001729 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1730 // When physical keyboard is attached, we show the ime switcher (or notification if
1731 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1732 // exists in the IME switcher dialog. Might be OK to remove this condition once
1733 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1734 return true;
1735 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001736 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1737 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001738 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001739
1740 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1741 final int N = imis.size();
1742 if (N > 2) return true;
1743 if (N < 1) return false;
1744 int nonAuxCount = 0;
1745 int auxCount = 0;
1746 InputMethodSubtype nonAuxSubtype = null;
1747 InputMethodSubtype auxSubtype = null;
1748 for(int i = 0; i < N; ++i) {
1749 final InputMethodInfo imi = imis.get(i);
1750 final List<InputMethodSubtype> subtypes =
1751 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1752 final int subtypeCount = subtypes.size();
1753 if (subtypeCount == 0) {
1754 ++nonAuxCount;
1755 } else {
1756 for (int j = 0; j < subtypeCount; ++j) {
1757 final InputMethodSubtype subtype = subtypes.get(j);
1758 if (!subtype.isAuxiliary()) {
1759 ++nonAuxCount;
1760 nonAuxSubtype = subtype;
1761 } else {
1762 ++auxCount;
1763 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001764 }
1765 }
satok7cfc0ed2011-06-20 21:29:36 +09001766 }
1767 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001768 if (nonAuxCount > 1 || auxCount > 1) {
1769 return true;
1770 } else if (nonAuxCount == 1 && auxCount == 1) {
1771 if (nonAuxSubtype != null && auxSubtype != null
1772 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1773 || auxSubtype.overridesImplicitlyEnabledSubtype()
1774 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1775 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1776 return false;
1777 }
1778 return true;
1779 }
1780 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001781 }
1782
John Spurlocke0980502013-10-25 11:59:29 -04001783 private boolean isKeyguardLocked() {
1784 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1785 }
1786
satokdbf29502011-08-25 15:28:23 +09001787 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001788 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001789 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001790 if (!calledWithValidToken(token)) {
1791 final int uid = Binder.getCallingUid();
1792 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1793 + " token:" + token);
1794 return;
1795 }
1796
1797 synchronized (mMethodMap) {
1798 mImeWindowVis = vis;
1799 mBackDisposition = backDisposition;
1800 updateSystemUiLocked(token, vis, backDisposition);
1801 }
1802 }
1803
1804 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1805 synchronized (mMethodMap) {
1806 updateSystemUiLocked(token, vis, backDisposition);
1807 }
1808 }
1809
1810 // Caution! This method is called in this class. Handle multi-user carefully
1811 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1812 if (!calledWithValidToken(token)) {
1813 final int uid = Binder.getCallingUid();
1814 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1815 + " token:" + token);
1816 return;
1817 }
1818
1819 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1820 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001821 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001822 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001823 // apply policy for binder calls
1824 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1825 vis = 0;
satok06487a52010-10-29 11:37:18 +09001826 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001827 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1828 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1829 if (mStatusBar != null) {
1830 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1831 needsToShowImeSwitcher);
1832 }
1833 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1834 if (imi != null && needsToShowImeSwitcher) {
1835 // Used to load label
1836 final CharSequence title = mRes.getText(
1837 com.android.internal.R.string.select_input_method);
1838 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1839 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001840 mImeSwitcherNotification.setContentTitle(title)
1841 .setContentText(summary)
1842 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001843 try {
1844 if ((mNotificationManager != null)
1845 && !mIWindowManager.hasNavigationBar()) {
1846 if (DEBUG) {
1847 Slog.d(TAG, "--- show notification: label = " + summary);
1848 }
1849 mNotificationManager.notifyAsUser(null,
1850 com.android.internal.R.string.select_input_method,
1851 mImeSwitcherNotification.build(), UserHandle.ALL);
1852 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001853 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001854 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001855 }
1856 } else {
1857 if (mNotificationShown && mNotificationManager != null) {
1858 if (DEBUG) {
1859 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001860 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001861 mNotificationManager.cancelAsUser(null,
1862 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1863 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001864 }
satok06487a52010-10-29 11:37:18 +09001865 }
1866 } finally {
1867 Binder.restoreCallingIdentity(ident);
1868 }
1869 }
1870
satoke7c6998e2011-06-03 17:57:59 +09001871 @Override
satokf9f01002011-05-19 21:31:50 +09001872 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001873 if (!calledFromValidUser()) {
1874 return;
1875 }
satokf9f01002011-05-19 21:31:50 +09001876 synchronized (mMethodMap) {
1877 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1878 for (int i = 0; i < spans.length; ++i) {
1879 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001880 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001881 mSecureSuggestionSpans.put(ss, currentImi);
1882 }
1883 }
1884 }
1885 }
1886
satoke7c6998e2011-06-03 17:57:59 +09001887 @Override
satokf9f01002011-05-19 21:31:50 +09001888 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001889 if (!calledFromValidUser()) {
1890 return false;
1891 }
satokf9f01002011-05-19 21:31:50 +09001892 synchronized (mMethodMap) {
1893 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1894 // TODO: Do not send the intent if the process of the targetImi is already dead.
1895 if (targetImi != null) {
1896 final String[] suggestions = span.getSuggestions();
1897 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001898 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001899 final Intent intent = new Intent();
1900 // Ensures that only a class in the original IME package will receive the
1901 // notification.
satok42c5a162011-05-26 16:46:14 +09001902 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001903 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1904 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1905 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1906 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001907 final long ident = Binder.clearCallingIdentity();
1908 try {
1909 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1910 } finally {
1911 Binder.restoreCallingIdentity(ident);
1912 }
satokf9f01002011-05-19 21:31:50 +09001913 return true;
1914 }
1915 }
1916 return false;
1917 }
1918
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001919 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001920 updateInputMethodsFromSettingsLocked(enabledMayChange);
1921 updateKeyboardFromSettingsLocked();
1922 }
1923
1924 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001925 if (enabledMayChange) {
1926 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1927 for (int i=0; i<enabled.size(); i++) {
1928 // We allow the user to select "disabled until used" apps, so if they
1929 // are enabling one of those here we now need to make it enabled.
1930 InputMethodInfo imm = enabled.get(i);
1931 try {
1932 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1933 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1934 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001935 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001936 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001937 if (DEBUG) {
1938 Slog.d(TAG, "Update state(" + imm.getId()
1939 + "): DISABLED_UNTIL_USED -> DEFAULT");
1940 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001941 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1942 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001943 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1944 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001945 }
1946 } catch (RemoteException e) {
1947 }
1948 }
1949 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001950 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1951 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1952 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1953 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001954 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001955 // There is no input method selected, try to choose new applicable input method.
1956 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001957 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001958 }
1959 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001961 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001963 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001964 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
satokf3db1af2010-11-23 13:34:33 +09001966 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 } else {
1968 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001969 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001971 // Here is not the perfect place to reset the switching controller. Ideally
1972 // mSwitchingController and mSettings should be able to share the same state.
1973 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1974 // the same enabled IMEs list.
1975 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001976
1977 }
1978
1979 public void updateKeyboardFromSettingsLocked() {
1980 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1981 if (mSwitchingDialog != null
1982 && mSwitchingDialogTitleView != null
1983 && mSwitchingDialog.isShowing()) {
1984 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1985 com.android.internal.R.id.hard_keyboard_switch);
1986 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
Yohei Yukawab097b822015-12-01 10:43:08 -08001990 private void notifyInputMethodSubtypeChanged(final int userId,
1991 @Nullable final InputMethodInfo inputMethodInfo,
1992 @Nullable final InputMethodSubtype subtype) {
1993 final InputManagerInternal inputManagerInternal =
1994 LocalServices.getService(InputManagerInternal.class);
1995 if (inputManagerInternal != null) {
1996 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1997 }
1998 }
1999
satokab751aa2010-09-14 19:17:36 +09002000 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 InputMethodInfo info = mMethodMap.get(id);
2002 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002003 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
satokd81e9502012-05-21 12:58:45 +09002006 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002008 final int subtypeCount = info.getSubtypeCount();
2009 if (subtypeCount <= 0) {
2010 return;
satokcd7cd292010-11-20 15:46:23 +09002011 }
satokd81e9502012-05-21 12:58:45 +09002012 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2013 final InputMethodSubtype newSubtype;
2014 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2015 newSubtype = info.getSubtypeAt(subtypeId);
2016 } else {
2017 // If subtype is null, try to find the most applicable one from
2018 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002019 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002020 }
2021 if (newSubtype == null || oldSubtype == null) {
2022 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2023 + ", new subtype = " + newSubtype);
2024 return;
2025 }
2026 if (newSubtype != oldSubtype) {
2027 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2028 if (mCurMethod != null) {
2029 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002030 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002031 mCurMethod.changeInputMethodSubtype(newSubtype);
2032 } catch (RemoteException e) {
2033 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002034 return;
satokab751aa2010-09-14 19:17:36 +09002035 }
2036 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002037 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 return;
2040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041
satokd81e9502012-05-21 12:58:45 +09002042 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 final long ident = Binder.clearCallingIdentity();
2044 try {
satokab751aa2010-09-14 19:17:36 +09002045 // Set a subtype to this input method.
2046 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002047 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2048 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2049 // because mCurMethodId is stored as a history in
2050 // setSelectedInputMethodAndSubtypeLocked().
2051 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052
2053 if (ActivityManagerNative.isSystemReady()) {
2054 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002055 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002057 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002059 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 } finally {
2061 Binder.restoreCallingIdentity(ident);
2062 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002063
2064 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2065 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
satok42c5a162011-05-26 16:46:14 +09002068 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002069 public boolean showSoftInput(IInputMethodClient client, int flags,
2070 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002071 if (!calledFromValidUser()) {
2072 return false;
2073 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002074 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 long ident = Binder.clearCallingIdentity();
2076 try {
2077 synchronized (mMethodMap) {
2078 if (mCurClient == null || client == null
2079 || mCurClient.client.asBinder() != client.asBinder()) {
2080 try {
2081 // We need to check if this is the current client with
2082 // focus in the window manager, to allow this call to
2083 // be made before input is started in it.
2084 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002085 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002089 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002093 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 } finally {
2097 Binder.restoreCallingIdentity(ident);
2098 }
2099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002100
The Android Open Source Project4df24232009-03-05 14:34:35 -08002101 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002103 if (mAccessibilityRequestingNoSoftKeyboard) {
2104 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Anna Galusza9b278112016-01-04 11:37:37 -08002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2108 mShowExplicitlyRequested = true;
2109 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002110 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2111 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002113
Dianne Hackborncc278702009-09-02 23:07:23 -07002114 if (!mSystemReady) {
2115 return false;
2116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
The Android Open Source Project4df24232009-03-05 14:34:35 -08002118 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002120 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002121 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2122 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2123 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002125 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002126 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002127 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002128 | Context.BIND_TREAT_LIKE_ACTIVITY
2129 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002130 mVisibleBound = true;
2131 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002134 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 // The client has asked to have the input method shown, but
2136 // we have been sitting here too long with a connection to the
2137 // service and no interface received, so let's disconnect/connect
2138 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002141 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002144 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002145 } else {
2146 if (DEBUG) {
2147 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2148 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002151
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002154
satok42c5a162011-05-26 16:46:14 +09002155 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 public boolean hideSoftInput(IInputMethodClient client, int flags,
2157 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002158 if (!calledFromValidUser()) {
2159 return false;
2160 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002161 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 long ident = Binder.clearCallingIdentity();
2163 try {
2164 synchronized (mMethodMap) {
2165 if (mCurClient == null || client == null
2166 || mCurClient.client.asBinder() != client.asBinder()) {
2167 try {
2168 // We need to check if this is the current client with
2169 // focus in the window manager, to allow this call to
2170 // be made before input is started in it.
2171 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002172 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2173 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002174 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002177 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002180
Joe Onorato8a9b2202010-02-26 18:56:32 -08002181 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002182 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 } finally {
2185 Binder.restoreCallingIdentity(ident);
2186 }
2187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2191 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002192 if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002193 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002196 if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002197 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002199
2200 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2201 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2202 // to be updated with the new value sent from IME process. Even in such a transient state
2203 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2204 // application process as a valid request, and have even promised such a behavior with CTS
2205 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2206 // IMMS#InputShown indicates that the software keyboard is shown.
2207 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2208 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2209 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002210 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002211 if (shouldHideSoftInput) {
2212 // The IME will report its visible state again after the following message finally
2213 // delivered to the IME process as an IPC. Hence the inconsistency between
2214 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2215 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002216 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2217 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2218 res = true;
2219 } else {
2220 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002222 if (mHaveConnection && mVisibleBound) {
2223 mContext.unbindService(mVisibleConnection);
2224 mVisibleBound = false;
2225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 mInputShown = false;
2227 mShowRequested = false;
2228 mShowExplicitlyRequested = false;
2229 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002230 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002232
satok42c5a162011-05-26 16:46:14 +09002233 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002234 public InputBindResult startInputOrWindowGainedFocus(
2235 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2236 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002237 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002238 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002239 if (windowToken != null) {
2240 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002241 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002242 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002243 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2244 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002245 }
2246 }
2247
2248 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002249 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2250 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002251 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2252 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002253 // Needs to check the validity before clearing calling identity
2254 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002255 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 long ident = Binder.clearCallingIdentity();
2257 try {
2258 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002259 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2260 + InputMethodClient.getStartInputReason(startInputReason)
2261 + " client=" + client.asBinder()
2262 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002263 + " missingMethods="
2264 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002265 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002268 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002269
Dianne Hackborn7663d802012-02-24 13:08:49 -08002270 ClientState cs = mClients.get(client.asBinder());
2271 if (cs == null) {
2272 throw new IllegalArgumentException("unknown client "
2273 + client.asBinder());
2274 }
2275
2276 try {
2277 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2278 // Check with the window manager to make sure this client actually
2279 // has a window with focus. If not, reject. This is thread safe
2280 // because if the focus changes some time before or after, the
2281 // next client receiving focus that has any interest in input will
2282 // be calling through here after that change happens.
2283 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2284 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2285 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002287 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002290 if (!calledFromValidUser) {
2291 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2292 Slog.w(TAG, "If you want to interect with IME, you need "
2293 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2294 hideCurrentInputLocked(0, null);
2295 return null;
2296 }
2297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002298 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002299 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002300 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002302 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2303 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002304 }
2305 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002306 }
2307 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002308 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002309
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002310 // Should we auto-show the IME even if the caller has not
2311 // specified what should be done with it?
2312 // We only do this automatically if the window can resize
2313 // to accommodate the IME (so what the user sees will give
2314 // them good context without input information being obscured
2315 // by the IME) or if running on a large screen where there
2316 // is more room for the target window + IME.
2317 final boolean doAutoShow =
2318 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2319 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2320 || mRes.getConfiguration().isLayoutSizeAtLeast(
2321 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002322 final boolean isTextEditor =
2323 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2324
2325 // We want to start input before showing the IME, but after closing
2326 // it. We want to do this after closing it to help the IME disappear
2327 // more quickly (not get stuck behind it initializing itself for the
2328 // new focused input, even if its window wants to hide the IME).
2329 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2332 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002333 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2335 // There is no focus view, and this window will
2336 // be behind any soft input window, so hide the
2337 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002338 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002339 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002341 } else if (isTextEditor && doAutoShow && (softInputMode &
2342 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 // There is a focus view, and we are navigating forward
2344 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002345 // We only do this automatically if the window can resize
2346 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002347 // them good context without input information being obscured
2348 // by the IME) or if running on a large screen where there
2349 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002350 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002351 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002352 res = startInputUncheckedLocked(cs, inputContext,
2353 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002354 didStart = true;
2355 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002356 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
2358 break;
2359 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2360 // Do nothing.
2361 break;
2362 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2363 if ((softInputMode &
2364 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002366 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 break;
2373 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2374 if ((softInputMode &
2375 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002376 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002377 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002378 res = startInputUncheckedLocked(cs, inputContext,
2379 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 didStart = true;
2381 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002382 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
2384 break;
2385 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002386 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002388 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2389 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002390 didStart = true;
2391 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002392 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 break;
2394 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002395
2396 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002397 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002398 controlFlags);
2399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401 } finally {
2402 Binder.restoreCallingIdentity(ident);
2403 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002404
2405 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
satok42c5a162011-05-26 16:46:14 +09002408 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002409 public void showInputMethodPickerFromClient(
2410 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002411 if (!calledFromValidUser()) {
2412 return;
2413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 synchronized (mMethodMap) {
2415 if (mCurClient == null || client == null
2416 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002417 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002418 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 }
2420
satok440aab52010-11-25 09:43:11 +09002421 // Always call subtype picker, because subtype picker is a superset of input method
2422 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002423 mHandler.sendMessage(mCaller.obtainMessageI(
2424 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002425 }
2426 }
2427
satok42c5a162011-05-26 16:46:14 +09002428 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 if (!calledFromValidUser()) {
2431 return;
2432 }
satok28203512010-11-24 11:06:49 +09002433 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2434 }
2435
satok42c5a162011-05-26 16:46:14 +09002436 @Override
satok28203512010-11-24 11:06:49 +09002437 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002438 if (!calledFromValidUser()) {
2439 return;
2440 }
satok28203512010-11-24 11:06:49 +09002441 synchronized (mMethodMap) {
2442 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002443 setInputMethodWithSubtypeIdLocked(token, id,
2444 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2445 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002446 } else {
2447 setInputMethod(token, id);
2448 }
2449 }
satokab751aa2010-09-14 19:17:36 +09002450 }
2451
satok42c5a162011-05-26 16:46:14 +09002452 @Override
satokb416a712010-11-25 20:42:14 +09002453 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002454 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 if (!calledFromValidUser()) {
2456 return;
2457 }
satokb416a712010-11-25 20:42:14 +09002458 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002459 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2460 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002461 }
2462 }
2463
satok4fc87d62011-05-20 16:13:43 +09002464 @Override
satok735cf382010-11-11 20:40:09 +09002465 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002466 if (!calledFromValidUser()) {
2467 return false;
2468 }
satok735cf382010-11-11 20:40:09 +09002469 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002470 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002471 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002472 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002473 lastImi = mMethodMap.get(lastIme.first);
2474 } else {
2475 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002476 }
satok4fc87d62011-05-20 16:13:43 +09002477 String targetLastImiId = null;
2478 int subtypeId = NOT_A_SUBTYPE_ID;
2479 if (lastIme != null && lastImi != null) {
2480 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002481 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002482 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2483 : mCurrentSubtype.hashCode();
2484 // If the last IME is the same as the current IME and the last subtype is not
2485 // defined, there is no need to switch to the last IME.
2486 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2487 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002488 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002489 }
2490 }
2491
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002492 if (TextUtils.isEmpty(targetLastImiId)
2493 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002494 // This is a safety net. If the currentSubtype can't be added to the history
2495 // and the framework couldn't find the last ime, we will make the last ime be
2496 // the most applicable enabled keyboard subtype of the system imes.
2497 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2498 if (enabled != null) {
2499 final int N = enabled.size();
2500 final String locale = mCurrentSubtype == null
2501 ? mRes.getConfiguration().locale.toString()
2502 : mCurrentSubtype.getLocale();
2503 for (int i = 0; i < N; ++i) {
2504 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002505 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002506 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002507 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2508 InputMethodUtils.getSubtypes(imi),
2509 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002510 if (keyboardSubtype != null) {
2511 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002512 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002513 imi, keyboardSubtype.hashCode());
2514 if(keyboardSubtype.getLocale().equals(locale)) {
2515 break;
2516 }
2517 }
2518 }
2519 }
2520 }
2521 }
2522
2523 if (!TextUtils.isEmpty(targetLastImiId)) {
2524 if (DEBUG) {
2525 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2526 + ", from: " + mCurMethodId + ", " + subtypeId);
2527 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002528 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002529 return true;
2530 } else {
2531 return false;
2532 }
satok735cf382010-11-11 20:40:09 +09002533 }
2534 }
2535
satoke7c6998e2011-06-03 17:57:59 +09002536 @Override
satok688bd472012-02-09 20:09:17 +09002537 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002538 if (!calledFromValidUser()) {
2539 return false;
2540 }
satok688bd472012-02-09 20:09:17 +09002541 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002542 if (!calledWithValidToken(token)) {
2543 final int uid = Binder.getCallingUid();
2544 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2545 + " token:" + token);
2546 return false;
2547 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002548 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002549 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2550 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002551 if (nextSubtype == null) {
2552 return false;
2553 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002554 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2555 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002556 return true;
2557 }
2558 }
2559
2560 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002561 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2562 if (!calledFromValidUser()) {
2563 return false;
2564 }
2565 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002566 if (!calledWithValidToken(token)) {
2567 final int uid = Binder.getCallingUid();
2568 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2569 + "token. uid:" + uid + " token:" + token);
2570 return false;
2571 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002572 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002573 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2574 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002575 if (nextSubtype == null) {
2576 return false;
2577 }
2578 return true;
2579 }
2580 }
2581
2582 @Override
satok68f1b782011-04-11 14:26:04 +09002583 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002584 if (!calledFromValidUser()) {
2585 return null;
2586 }
satok68f1b782011-04-11 14:26:04 +09002587 synchronized (mMethodMap) {
2588 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2589 // TODO: Handle the case of the last IME with no subtypes
2590 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2591 || TextUtils.isEmpty(lastIme.second)) return null;
2592 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2593 if (lastImi == null) return null;
2594 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002595 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002596 final int lastSubtypeId =
2597 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002598 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2599 return null;
2600 }
2601 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002602 } catch (NumberFormatException e) {
2603 return null;
2604 }
2605 }
2606 }
2607
satoke7c6998e2011-06-03 17:57:59 +09002608 @Override
satokee5e77c2011-09-02 18:50:15 +09002609 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002610 if (!calledFromValidUser()) {
2611 return;
2612 }
satok91e88122011-07-18 11:11:42 +09002613 // By this IPC call, only a process which shares the same uid with the IME can add
2614 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002615 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002616 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002617 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002618 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002619 final String[] packageInfos;
2620 try {
2621 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2622 } catch (RemoteException e) {
2623 Slog.e(TAG, "Failed to get package infos");
2624 return;
2625 }
satok91e88122011-07-18 11:11:42 +09002626 if (packageInfos != null) {
2627 final int packageNum = packageInfos.length;
2628 for (int i = 0; i < packageNum; ++i) {
2629 if (packageInfos[i].equals(imi.getPackageName())) {
2630 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002631 final long ident = Binder.clearCallingIdentity();
2632 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002633 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002634 } finally {
2635 Binder.restoreCallingIdentity(ident);
2636 }
satokee5e77c2011-09-02 18:50:15 +09002637 return;
satok91e88122011-07-18 11:11:42 +09002638 }
2639 }
2640 }
satoke7c6998e2011-06-03 17:57:59 +09002641 }
satokee5e77c2011-09-02 18:50:15 +09002642 return;
satoke7c6998e2011-06-03 17:57:59 +09002643 }
2644
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002645 @Override
2646 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002647 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002648 }
2649
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002650 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002651 public void clearLastInputMethodWindowForTransition(IBinder token) {
2652 if (!calledFromValidUser()) {
2653 return;
2654 }
2655 final long ident = Binder.clearCallingIdentity();
2656 try {
2657 synchronized (mMethodMap) {
2658 if (!calledWithValidToken(token)) {
2659 final int uid = Binder.getCallingUid();
2660 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2661 + "invalid token. uid:" + uid + " token:" + token);
2662 return;
2663 }
2664 }
2665 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2666 } finally {
2667 Binder.restoreCallingIdentity(ident);
2668 }
2669 }
2670
2671 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002672 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002673 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002674 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002675 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002676 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002677 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2678 if (DEBUG) {
2679 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2680 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2681 + " actual: " + sequenceNumber);
2682 }
2683 return;
2684 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002685 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2686 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002687 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002688 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002689 }
2690 }
2691
satok28203512010-11-24 11:06:49 +09002692 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002694 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2695 }
2696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002698 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2699 if (token == null) {
2700 if (mContext.checkCallingOrSelfPermission(
2701 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2702 != PackageManager.PERMISSION_GRANTED) {
2703 throw new SecurityException(
2704 "Using null token requires permission "
2705 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002707 } else if (mCurToken != token) {
2708 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2709 + " token: " + token);
2710 return;
2711 }
2712
2713 final long ident = Binder.clearCallingIdentity();
2714 try {
2715 setInputMethodLocked(id, subtypeId);
2716 } finally {
2717 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 }
2720
satok42c5a162011-05-26 16:46:14 +09002721 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002723 if (!calledFromValidUser()) {
2724 return;
2725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002727 if (!calledWithValidToken(token)) {
2728 final int uid = Binder.getCallingUid();
2729 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2730 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 return;
2732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 long ident = Binder.clearCallingIdentity();
2734 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002735 hideCurrentInputLocked(flags, null);
2736 } finally {
2737 Binder.restoreCallingIdentity(ident);
2738 }
2739 }
2740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002741
satok42c5a162011-05-26 16:46:14 +09002742 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002743 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002744 if (!calledFromValidUser()) {
2745 return;
2746 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002747 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002748 if (!calledWithValidToken(token)) {
2749 final int uid = Binder.getCallingUid();
2750 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2751 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002752 return;
2753 }
2754 long ident = Binder.clearCallingIdentity();
2755 try {
2756 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 } finally {
2758 Binder.restoreCallingIdentity(ident);
2759 }
2760 }
2761 }
2762
2763 void setEnabledSessionInMainThread(SessionState session) {
2764 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002765 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002767 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002768 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 } catch (RemoteException e) {
2770 }
2771 }
2772 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002773 if (mEnabledSession != null && mEnabledSession.session != null) {
2774 try {
2775 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2776 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2777 } catch (RemoteException e) {
2778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
2780 }
2781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002782
satok42c5a162011-05-26 16:46:14 +09002783 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002785 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002787 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002788 final boolean showAuxSubtypes;
2789 switch (msg.arg1) {
2790 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2791 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2792 // implemented so that auxiliary subtypes will be excluded when the soft
2793 // keyboard is invisible.
2794 showAuxSubtypes = mInputShown;
2795 break;
2796 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2797 showAuxSubtypes = true;
2798 break;
2799 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2800 showAuxSubtypes = false;
2801 break;
2802 default:
2803 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2804 return false;
2805 }
2806 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002807 return true;
2808
satok47a44912010-10-06 16:03:58 +09002809 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002810 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002811 return true;
2812
2813 case MSG_SHOW_IM_CONFIG:
2814 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002815 return true;
2816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 case MSG_UNBIND_INPUT:
2820 try {
2821 ((IInputMethod)msg.obj).unbindInput();
2822 } catch (RemoteException e) {
2823 // There is nothing interesting about the method dying.
2824 }
2825 return true;
2826 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002827 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 try {
2829 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2830 } catch (RemoteException e) {
2831 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002832 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 return true;
2834 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002837 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002838 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002839 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 } catch (RemoteException e) {
2841 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002842 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 return true;
2844 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002845 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002847 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002848 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002849 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 } catch (RemoteException e) {
2851 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002852 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002854 case MSG_HIDE_CURRENT_INPUT_METHOD:
2855 synchronized (mMethodMap) {
2856 hideCurrentInputLocked(0, null);
2857 }
2858 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002860 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002862 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2864 } catch (RemoteException e) {
2865 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002866 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002868 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002869 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002870 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002871 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002873 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002875 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002876 // Dispose the channel if the input method is not local to this process
2877 // because the remote proxy will get its own copy when unparceled.
2878 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002879 channel.dispose();
2880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002882 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002886
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002887 case MSG_START_INPUT: {
2888 int missingMethods = msg.arg1;
2889 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002891 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002893 session.method.startInput((IInputContext) args.arg2, missingMethods,
2894 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 } catch (RemoteException e) {
2896 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002897 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002899 }
2900 case MSG_RESTART_INPUT: {
2901 int missingMethods = msg.arg1;
2902 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002904 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002906 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2907 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 } catch (RemoteException e) {
2909 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002910 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002915
Yohei Yukawa33e81792015-11-17 21:14:42 -08002916 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002918 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 } catch (RemoteException e) {
2920 // There is nothing interesting about the last client dying.
2921 }
2922 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002923 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002924 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002925 IInputMethodClient client = (IInputMethodClient)args.arg1;
2926 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002928 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002930 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002931 } finally {
2932 // Dispose the channel if the input method is not local to this process
2933 // because the remote proxy will get its own copy when unparceled.
2934 if (res.channel != null && Binder.isProxy(client)) {
2935 res.channel.dispose();
2936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002938 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002940 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002941 case MSG_SET_ACTIVE:
2942 try {
2943 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2944 } catch (RemoteException e) {
2945 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2946 + ((ClientState)msg.obj).pid + " uid "
2947 + ((ClientState)msg.obj).uid);
2948 }
2949 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002950 case MSG_SET_INTERACTIVE:
2951 handleSetInteractive(msg.arg1 != 0);
2952 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002953 case MSG_SWITCH_IME:
2954 handleSwitchInputMethod(msg.arg1 != 0);
2955 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002956 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2957 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002958 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002959 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002960 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002961 } catch (RemoteException e) {
2962 Slog.w(TAG, "Got RemoteException sending "
2963 + "setUserActionNotificationSequenceNumber("
2964 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002965 + clientState.pid + " uid "
2966 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002967 }
2968 return true;
2969 }
satok01038492012-04-09 21:08:27 +09002970
2971 // --------------------------------------------------------------
2972 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002973 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002974 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002975 case MSG_SYSTEM_UNLOCK_USER:
2976 final int userId = msg.arg1;
2977 onUnlockUser(userId);
2978 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 }
2980 return false;
2981 }
2982
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002983 private void handleSetInteractive(final boolean interactive) {
2984 synchronized (mMethodMap) {
2985 mIsInteractive = interactive;
2986 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2987
2988 // Inform the current client of the change in active status
2989 if (mCurClient != null && mCurClient.client != null) {
2990 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2991 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2992 }
2993 }
2994 }
2995
Yohei Yukawaae61f712015-12-09 13:00:10 -08002996 private void handleSwitchInputMethod(final boolean forwardDirection) {
2997 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002998 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002999 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003000 if (nextSubtype == null) {
3001 return;
3002 }
3003 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003004 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3005 if (newInputMethodInfo == null) {
3006 return;
3007 }
3008 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3009 newInputMethodInfo, mCurrentSubtype);
3010 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003011 if (mSubtypeSwitchedByShortCutToast == null) {
3012 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3013 Toast.LENGTH_SHORT);
3014 } else {
3015 mSubtypeSwitchedByShortCutToast.setText(toastText);
3016 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003017 mSubtypeSwitchedByShortCutToast.show();
3018 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003019 }
3020 }
3021
satokdc9ddae2011-10-06 12:22:36 +09003022 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003023 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3024 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003025 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003026 if (DEBUG) {
3027 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3028 }
satok723a27e2010-11-11 14:58:11 +09003029 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003030 return true;
3031 }
3032
3033 return false;
3034 }
3035
Yohei Yukawa94e33302016-02-12 19:37:03 -08003036 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003037 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003038 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003039 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003041 mMethodList.clear();
3042 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003043
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003044 // Use for queryIntentServicesAsUser
3045 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003047 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3048 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3049 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003050 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003052 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3053 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003054
satoke7c6998e2011-06-03 17:57:59 +09003055 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3056 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 for (int i = 0; i < services.size(); ++i) {
3058 ResolveInfo ri = services.get(i);
3059 ServiceInfo si = ri.serviceInfo;
3060 ComponentName compName = new ComponentName(si.packageName, si.name);
3061 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3062 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003063 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 + ": it does not require the permission "
3065 + android.Manifest.permission.BIND_INPUT_METHOD);
3066 continue;
3067 }
3068
Joe Onorato8a9b2202010-02-26 18:56:32 -08003069 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070
3071 try {
satoke7c6998e2011-06-03 17:57:59 +09003072 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003073 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003074 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003075 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
3077 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003078 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003080 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003081 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 }
3083 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003084
Yohei Yukawa859df052016-02-17 07:56:46 -08003085 // TODO: The following code should find better place to live.
3086 if (!resetDefaultEnabledIme) {
3087 boolean enabledImeFound = false;
3088 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3089 final int N = enabledImes.size();
3090 for (int i = 0; i < N; ++i) {
3091 final InputMethodInfo imi = enabledImes.get(i);
3092 if (mMethodList.contains(imi)) {
3093 enabledImeFound = true;
3094 break;
3095 }
3096 }
3097 if (!enabledImeFound) {
3098 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3099 resetDefaultEnabledIme = true;
3100 resetSelectedInputMethodAndSubtypeLocked("");
3101 }
3102 }
3103
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003104 if (resetDefaultEnabledIme) {
3105 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003106 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003107 final int N = defaultEnabledIme.size();
3108 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003109 final InputMethodInfo imi = defaultEnabledIme.get(i);
3110 if (DEBUG) {
3111 Slog.d(TAG, "--- enable ime = " + imi);
3112 }
3113 setInputMethodEnabledLocked(imi.getId(), true);
3114 }
3115 }
3116
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003117 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003118 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003119 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003120 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3121 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003122 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003123 }
3124 } else {
3125 // Double check that the default IME is certainly enabled.
3126 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003127 }
3128 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003129 // Here is not the perfect place to reset the switching controller. Ideally
3130 // mSwitchingController and mSettings should be able to share the same state.
3131 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3132 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003133 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003137
satok217f5482010-12-15 05:19:19 +09003138 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003139 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003140 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003141 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3142 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003143 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003144 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003145 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003146 final int userId;
3147 synchronized (mMethodMap) {
3148 userId = mSettings.getCurrentUserId();
3149 }
3150 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003151 }
3152
3153 private void showConfigureInputMethods() {
3154 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3155 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3156 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3157 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003158 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003159 }
3160
satok2c93efc2012-04-02 19:33:47 +09003161 private boolean isScreenLocked() {
3162 return mKeyguardManager != null
3163 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3164 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003165
Seigo Nonaka14e13912015-05-06 21:04:13 -07003166 private void showInputMethodMenu(boolean showAuxSubtypes) {
3167 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003170 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003171
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003172 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003173 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003174 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003175
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003176 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003177 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003178 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3179 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003180 if (immis == null || immis.size() == 0) {
3181 return;
3182 }
3183
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003184 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185
satok688bd472012-02-09 20:09:17 +09003186 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003187 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003188 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003189
satokc3690562012-01-10 20:14:43 +09003190 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003191 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003192 if (currentSubtype != null) {
3193 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003194 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3195 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003196 }
3197 }
3198
Ken Wakasa761eb372011-03-04 19:06:18 +09003199 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003200 mIms = new InputMethodInfo[N];
3201 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003202 int checkedItem = 0;
3203 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003204 final ImeSubtypeListItem item = imList.get(i);
3205 mIms[i] = item.mImi;
3206 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003207 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003208 int subtypeId = mSubtypeIds[i];
3209 if ((subtypeId == NOT_A_SUBTYPE_ID)
3210 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3211 || (subtypeId == lastInputMethodSubtypeId)) {
3212 checkedItem = i;
3213 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003216
3217 final Context settingsContext = new ContextThemeWrapper(context,
3218 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3219
3220 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003221 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3222 @Override
3223 public void onCancel(DialogInterface dialog) {
3224 hideInputMethodMenu();
3225 }
3226 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003227
3228 final Context dialogContext = mDialogBuilder.getContext();
3229 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3230 com.android.internal.R.styleable.DialogPreference,
3231 com.android.internal.R.attr.alertDialogStyle, 0);
3232 final Drawable dialogIcon = a.getDrawable(
3233 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3234 a.recycle();
3235
3236 mDialogBuilder.setIcon(dialogIcon);
3237
Yohei Yukawad34e1482016-02-11 08:03:52 -08003238 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003239 final View tv = inflater.inflate(
3240 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3241 mDialogBuilder.setCustomTitle(tv);
3242
3243 // Setup layout for a toggle switch of the hardware keyboard
3244 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003245 mSwitchingDialogTitleView
3246 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003247 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003248 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003249 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003251 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003252 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3253 @Override
3254 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003255 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003256 // Ensure that the input method dialog is dismissed when changing
3257 // the hardware keyboard state.
3258 hideInputMethodMenu();
3259 }
3260 });
3261
Alan Viverette505e3ab2014-11-24 15:22:11 -08003262 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003263 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3264 final OnClickListener choiceListener = new OnClickListener() {
3265 @Override
3266 public void onClick(final DialogInterface dialog, final int which) {
3267 synchronized (mMethodMap) {
3268 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3269 || mSubtypeIds.length <= which) {
3270 return;
satok01038492012-04-09 21:08:27 +09003271 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003272 final InputMethodInfo im = mIms[which];
3273 int subtypeId = mSubtypeIds[which];
3274 adapter.mCheckedItem = which;
3275 adapter.notifyDataSetChanged();
3276 hideInputMethodMenu();
3277 if (im != null) {
3278 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3279 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003280 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003281 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003283 }
3284 }
3285 };
3286 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003289 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 mSwitchingDialog.getWindow().setType(
3291 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003292 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3293 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003294 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003295 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 mSwitchingDialog.show();
3297 }
3298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003299
Ken Wakasa05dbb652011-08-22 15:22:43 +09003300 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3301 private final LayoutInflater mInflater;
3302 private final int mTextViewResourceId;
3303 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003304 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003305 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3306 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3307 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003308
Ken Wakasa05dbb652011-08-22 15:22:43 +09003309 mTextViewResourceId = textViewResourceId;
3310 mItemsList = itemsList;
3311 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003312 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003313 }
3314
3315 @Override
3316 public View getView(int position, View convertView, ViewGroup parent) {
3317 final View view = convertView != null ? convertView
3318 : mInflater.inflate(mTextViewResourceId, null);
3319 if (position < 0 || position >= mItemsList.size()) return view;
3320 final ImeSubtypeListItem item = mItemsList.get(position);
3321 final CharSequence imeName = item.mImeName;
3322 final CharSequence subtypeName = item.mSubtypeName;
3323 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3324 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3325 if (TextUtils.isEmpty(subtypeName)) {
3326 firstTextView.setText(imeName);
3327 secondTextView.setVisibility(View.GONE);
3328 } else {
3329 firstTextView.setText(subtypeName);
3330 secondTextView.setText(imeName);
3331 secondTextView.setVisibility(View.VISIBLE);
3332 }
3333 final RadioButton radioButton =
3334 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3335 radioButton.setChecked(position == mCheckedItem);
3336 return view;
3337 }
3338 }
3339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003341 synchronized (mMethodMap) {
3342 hideInputMethodMenuLocked();
3343 }
3344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003345
The Android Open Source Project10592532009-03-18 17:39:46 -07003346 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003347 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348
The Android Open Source Project10592532009-03-18 17:39:46 -07003349 if (mSwitchingDialog != null) {
3350 mSwitchingDialog.dismiss();
3351 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003353
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003354 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003355 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003356 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003360
satok42c5a162011-05-26 16:46:14 +09003361 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003363 // TODO: Make this work even for non-current users?
3364 if (!calledFromValidUser()) {
3365 return false;
3366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 synchronized (mMethodMap) {
3368 if (mContext.checkCallingOrSelfPermission(
3369 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3370 != PackageManager.PERMISSION_GRANTED) {
3371 throw new SecurityException(
3372 "Requires permission "
3373 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3374 }
Anna Galusza9b278112016-01-04 11:37:37 -08003375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 long ident = Binder.clearCallingIdentity();
3377 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003378 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 } finally {
3380 Binder.restoreCallingIdentity(ident);
3381 }
3382 }
3383 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003384
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003385 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3386 // Make sure this is a valid input method.
3387 InputMethodInfo imm = mMethodMap.get(id);
3388 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003389 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003390 }
3391
satokd87c2592010-09-29 11:52:06 +09003392 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3393 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003394
satokd87c2592010-09-29 11:52:06 +09003395 if (enabled) {
3396 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3397 if (pair.first.equals(id)) {
3398 // We are enabling this input method, but it is already enabled.
3399 // Nothing to do. The previous state was enabled.
3400 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003401 }
3402 }
satokd87c2592010-09-29 11:52:06 +09003403 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3404 // Previous state was disabled.
3405 return false;
3406 } else {
3407 StringBuilder builder = new StringBuilder();
3408 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3409 builder, enabledInputMethodsList, id)) {
3410 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003412 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3413 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3414 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003415 }
3416 // Previous state was enabled.
3417 return true;
3418 } else {
3419 // We are disabling the input method but it is already disabled.
3420 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003421 return false;
3422 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003423 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003424 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003425
satok723a27e2010-11-11 14:58:11 +09003426 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3427 boolean setSubtypeOnly) {
3428 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003429 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003430
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003431 mCurUserActionNotificationSequenceNumber =
3432 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3433 if (DEBUG) {
3434 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3435 + mCurUserActionNotificationSequenceNumber);
3436 }
3437
3438 if (mCurClient != null && mCurClient.client != null) {
3439 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3440 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003441 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003442 }
3443
satok723a27e2010-11-11 14:58:11 +09003444 // Set Subtype here
3445 if (imi == null || subtypeId < 0) {
3446 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003447 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003448 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003449 if (subtypeId < imi.getSubtypeCount()) {
3450 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3451 mSettings.putSelectedSubtype(subtype.hashCode());
3452 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003453 } else {
3454 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003455 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003456 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003457 }
satokab751aa2010-09-14 19:17:36 +09003458 }
satok723a27e2010-11-11 14:58:11 +09003459
Yohei Yukawa68645a62016-02-17 07:54:20 -08003460 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003461 // Set InputMethod here
3462 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3463 }
3464 }
3465
3466 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3467 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3468 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3469 // newDefaultIme is empty when there is no candidate for the selected IME.
3470 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3471 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3472 if (subtypeHashCode != null) {
3473 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003474 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003475 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003476 } catch (NumberFormatException e) {
3477 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3478 }
3479 }
3480 }
3481 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003482 }
3483
satok4e4569d2010-11-19 18:45:53 +09003484 // If there are no selected shortcuts, tries finding the most applicable ones.
3485 private Pair<InputMethodInfo, InputMethodSubtype>
3486 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3487 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3488 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003489 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003490 boolean foundInSystemIME = false;
3491
3492 // Search applicable subtype for each InputMethodInfo
3493 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003494 final String imiId = imi.getId();
3495 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3496 continue;
3497 }
satokcd7cd292010-11-20 15:46:23 +09003498 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003499 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003500 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003501 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003502 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003503 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003504 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003505 }
satokdf31ae62011-01-15 06:19:44 +09003506 // 2. Search by the system locale from enabledSubtypes.
3507 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003508 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003509 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003510 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003511 }
satoka86f5e42011-09-02 17:12:42 +09003512 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003513 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003514 final ArrayList<InputMethodSubtype> subtypesForSearch =
3515 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003516 ? InputMethodUtils.getSubtypes(imi)
3517 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003518 // 4. Search by the current subtype's locale from all subtypes.
3519 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003520 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003521 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003522 }
3523 // 5. Search by the system locale from all subtypes.
3524 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003525 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003526 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003527 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003528 }
satokcd7cd292010-11-20 15:46:23 +09003529 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003530 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003531 // The current input method is the most applicable IME.
3532 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003533 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003534 break;
satok7599a7f2010-12-22 13:45:23 +09003535 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003536 // The system input method is 2nd applicable IME.
3537 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003538 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003539 if ((imi.getServiceInfo().applicationInfo.flags
3540 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3541 foundInSystemIME = true;
3542 }
satok4e4569d2010-11-19 18:45:53 +09003543 }
3544 }
3545 }
3546 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003547 if (mostApplicableIMI != null) {
3548 Slog.w(TAG, "Most applicable shortcut input method was:"
3549 + mostApplicableIMI.getId());
3550 if (mostApplicableSubtype != null) {
3551 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3552 + "," + mostApplicableSubtype.getMode() + ","
3553 + mostApplicableSubtype.getLocale());
3554 }
3555 }
satok4e4569d2010-11-19 18:45:53 +09003556 }
satokcd7cd292010-11-20 15:46:23 +09003557 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003558 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003559 } else {
3560 return null;
3561 }
3562 }
3563
satokab751aa2010-09-14 19:17:36 +09003564 /**
3565 * @return Return the current subtype of this input method.
3566 */
satok42c5a162011-05-26 16:46:14 +09003567 @Override
satokab751aa2010-09-14 19:17:36 +09003568 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003569 // TODO: Make this work even for non-current users?
3570 if (!calledFromValidUser()) {
3571 return null;
3572 }
3573 synchronized (mMethodMap) {
3574 return getCurrentInputMethodSubtypeLocked();
3575 }
3576 }
3577
3578 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003579 if (mCurMethodId == null) {
3580 return null;
3581 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003582 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003583 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3584 if (imi == null || imi.getSubtypeCount() == 0) {
3585 return null;
satok4e4569d2010-11-19 18:45:53 +09003586 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003587 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003588 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3589 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003590 if (subtypeId == NOT_A_SUBTYPE_ID) {
3591 // If there are no selected subtypes, the framework will try to find
3592 // the most applicable subtype from explicitly or implicitly enabled
3593 // subtypes.
3594 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003595 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003596 // If there is only one explicitly or implicitly enabled subtype,
3597 // just returns it.
3598 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3599 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3600 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003601 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003602 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003604 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003605 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003606 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3607 true);
satok4e4569d2010-11-19 18:45:53 +09003608 }
satok3ef8b292010-11-23 06:06:29 +09003609 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003610 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003611 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003612 }
3613 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003614 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003615 }
3616
satok4e4569d2010-11-19 18:45:53 +09003617 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003618 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003619 @Override
satok4e4569d2010-11-19 18:45:53 +09003620 public List getShortcutInputMethodsAndSubtypes() {
3621 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003622 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003623 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003624 // If there are no selected shortcut subtypes, the framework will try to find
3625 // the most applicable subtype from all subtypes whose mode is
3626 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003627 Pair<InputMethodInfo, InputMethodSubtype> info =
3628 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003629 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003630 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003631 ret.add(info.first);
3632 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003633 }
satok3da92232011-01-11 22:46:30 +09003634 return ret;
satokf3db1af2010-11-23 13:34:33 +09003635 }
satokf3db1af2010-11-23 13:34:33 +09003636 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3637 ret.add(imi);
3638 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3639 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003640 }
3641 }
satokf3db1af2010-11-23 13:34:33 +09003642 return ret;
satok4e4569d2010-11-19 18:45:53 +09003643 }
3644 }
3645
satok42c5a162011-05-26 16:46:14 +09003646 @Override
satokb66d2872010-11-10 01:04:04 +09003647 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003648 // TODO: Make this work even for non-current users?
3649 if (!calledFromValidUser()) {
3650 return false;
3651 }
satokb66d2872010-11-10 01:04:04 +09003652 synchronized (mMethodMap) {
3653 if (subtype != null && mCurMethodId != null) {
3654 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003655 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003656 if (subtypeId != NOT_A_SUBTYPE_ID) {
3657 setInputMethodLocked(mCurMethodId, subtypeId);
3658 return true;
3659 }
3660 }
3661 return false;
3662 }
3663 }
3664
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003665 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003666 private static class InputMethodFileManager {
3667 private static final String SYSTEM_PATH = "system";
3668 private static final String INPUT_METHOD_PATH = "inputmethod";
3669 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3670 private static final String NODE_SUBTYPES = "subtypes";
3671 private static final String NODE_SUBTYPE = "subtype";
3672 private static final String NODE_IMI = "imi";
3673 private static final String ATTR_ID = "id";
3674 private static final String ATTR_LABEL = "label";
3675 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003676 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003677 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003678 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003679 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3680 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3681 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003682 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003683 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3684 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003685 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003686 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003687 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003688 if (methodMap == null) {
3689 throw new NullPointerException("methodMap is null");
3690 }
3691 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003692 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003693 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3694 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003695 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003696 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003697 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3698 }
3699 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3700 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3701 if (!subtypeFile.exists()) {
3702 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003703 writeAdditionalInputMethodSubtypes(
3704 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003705 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003706 readAdditionalInputMethodSubtypes(
3707 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003708 }
3709 }
3710
3711 private void deleteAllInputMethodSubtypes(String imiId) {
3712 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003713 mAdditionalSubtypesMap.remove(imiId);
3714 writeAdditionalInputMethodSubtypes(
3715 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003716 }
3717 }
3718
3719 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003720 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003721 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003722 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003723 final int N = additionalSubtypes.length;
3724 for (int i = 0; i < N; ++i) {
3725 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003726 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003727 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003728 } else {
3729 Slog.w(TAG, "Duplicated subtype definition found: "
3730 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003731 }
3732 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003733 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3734 writeAdditionalInputMethodSubtypes(
3735 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003736 }
3737 }
3738
3739 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3740 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003741 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003742 }
3743 }
3744
3745 private static void writeAdditionalInputMethodSubtypes(
3746 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3747 HashMap<String, InputMethodInfo> methodMap) {
3748 // Safety net for the case that this function is called before methodMap is set.
3749 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3750 FileOutputStream fos = null;
3751 try {
3752 fos = subtypesFile.startWrite();
3753 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003754 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003755 out.startDocument(null, true);
3756 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3757 out.startTag(null, NODE_SUBTYPES);
3758 for (String imiId : allSubtypes.keySet()) {
3759 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3760 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3761 continue;
3762 }
3763 out.startTag(null, NODE_IMI);
3764 out.attribute(null, ATTR_ID, imiId);
3765 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3766 final int N = subtypesList.size();
3767 for (int i = 0; i < N; ++i) {
3768 final InputMethodSubtype subtype = subtypesList.get(i);
3769 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003770 if (subtype.hasSubtypeId()) {
3771 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3772 String.valueOf(subtype.getSubtypeId()));
3773 }
satoke7c6998e2011-06-03 17:57:59 +09003774 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3775 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3776 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003777 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3778 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003779 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3780 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3781 out.attribute(null, ATTR_IS_AUXILIARY,
3782 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003783 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3784 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003785 out.endTag(null, NODE_SUBTYPE);
3786 }
3787 out.endTag(null, NODE_IMI);
3788 }
3789 out.endTag(null, NODE_SUBTYPES);
3790 out.endDocument();
3791 subtypesFile.finishWrite(fos);
3792 } catch (java.io.IOException e) {
3793 Slog.w(TAG, "Error writing subtypes", e);
3794 if (fos != null) {
3795 subtypesFile.failWrite(fos);
3796 }
3797 }
3798 }
3799
3800 private static void readAdditionalInputMethodSubtypes(
3801 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3802 if (allSubtypes == null || subtypesFile == null) return;
3803 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003804 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003805 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003806 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003807 int type = parser.getEventType();
3808 // Skip parsing until START_TAG
3809 while ((type = parser.next()) != XmlPullParser.START_TAG
3810 && type != XmlPullParser.END_DOCUMENT) {}
3811 String firstNodeName = parser.getName();
3812 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3813 throw new XmlPullParserException("Xml doesn't start with subtypes");
3814 }
3815 final int depth =parser.getDepth();
3816 String currentImiId = null;
3817 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3818 while (((type = parser.next()) != XmlPullParser.END_TAG
3819 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3820 if (type != XmlPullParser.START_TAG)
3821 continue;
3822 final String nodeName = parser.getName();
3823 if (NODE_IMI.equals(nodeName)) {
3824 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3825 if (TextUtils.isEmpty(currentImiId)) {
3826 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3827 continue;
3828 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003829 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003830 allSubtypes.put(currentImiId, tempSubtypesArray);
3831 } else if (NODE_SUBTYPE.equals(nodeName)) {
3832 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3833 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3834 continue;
3835 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003836 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003837 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003838 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003839 parser.getAttributeValue(null, ATTR_LABEL));
3840 final String imeSubtypeLocale =
3841 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003842 final String languageTag =
3843 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003844 final String imeSubtypeMode =
3845 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3846 final String imeSubtypeExtraValue =
3847 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003848 final boolean isAuxiliary = "1".equals(String.valueOf(
3849 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003850 final boolean isAsciiCapable = "1".equals(String.valueOf(
3851 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003852 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003853 .setSubtypeNameResId(label)
3854 .setSubtypeIconResId(icon)
3855 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003856 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003857 .setSubtypeMode(imeSubtypeMode)
3858 .setSubtypeExtraValue(imeSubtypeExtraValue)
3859 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003860 .setIsAsciiCapable(isAsciiCapable);
3861 final String subtypeIdString =
3862 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3863 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003864 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003865 }
3866 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003867 }
3868 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003869 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3870 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003871 return;
satoke7c6998e2011-06-03 17:57:59 +09003872 }
3873 }
3874 }
3875
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003876 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3877 @NonNull
3878 private final Handler mHandler;
3879
3880 LocalServiceImpl(@NonNull final Handler handler) {
3881 mHandler = handler;
3882 }
3883
3884 @Override
3885 public void setInteractive(boolean interactive) {
3886 // Do everything in handler so as not to block the caller.
3887 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3888 interactive ? 1 : 0, 0));
3889 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003890
3891 @Override
3892 public void switchInputMethod(boolean forwardDirection) {
3893 // Do everything in handler so as not to block the caller.
3894 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3895 forwardDirection ? 1 : 0, 0));
3896 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003897
3898 @Override
3899 public void hideCurrentInputMethod() {
3900 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3901 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3902 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003903 }
3904
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003905 private static String imeWindowStatusToString(final int imeWindowVis) {
3906 final StringBuilder sb = new StringBuilder();
3907 boolean first = true;
3908 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3909 sb.append("Active");
3910 first = false;
3911 }
3912 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3913 if (!first) {
3914 sb.append("|");
3915 }
3916 sb.append("Visible");
3917 }
3918 return sb.toString();
3919 }
3920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003922 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3923 @Nullable Uri contentUri, @Nullable String packageName) {
3924 if (!calledFromValidUser()) {
3925 return null;
3926 }
3927
3928 if (token == null) {
3929 throw new NullPointerException("token");
3930 }
3931 if (packageName == null) {
3932 throw new NullPointerException("packageName");
3933 }
3934 if (contentUri == null) {
3935 throw new NullPointerException("contentUri");
3936 }
3937 final String contentUriScheme = contentUri.getScheme();
3938 if (!"content".equals(contentUriScheme)) {
3939 throw new InvalidParameterException("contentUri must have content scheme");
3940 }
3941
3942 synchronized (mMethodMap) {
3943 final int uid = Binder.getCallingUid();
3944 if (mCurMethodId == null) {
3945 return null;
3946 }
3947 if (mCurToken != token) {
3948 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3949 + " token=" + token);
3950 return null;
3951 }
3952 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3953 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3954 // nature of our system. Let's compare it with our internal record.
3955 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3956 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3957 + mCurAttribute.packageName + " packageName=" + packageName);
3958 return null;
3959 }
3960 final int imeUserId = UserHandle.getUserId(uid);
3961 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3962 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3963 appUserId);
3964 }
3965 }
3966
3967 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3969 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3970 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3973 + Binder.getCallingPid()
3974 + ", uid=" + Binder.getCallingUid());
3975 return;
3976 }
3977
3978 IInputMethod method;
3979 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003980 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 synchronized (mMethodMap) {
3985 p.println("Current Input Method Manager state:");
3986 int N = mMethodList.size();
3987 p.println(" Input Methods:");
3988 for (int i=0; i<N; i++) {
3989 InputMethodInfo info = mMethodList.get(i);
3990 p.println(" InputMethod #" + i + ":");
3991 info.dump(p, " ");
3992 }
3993 p.println(" Clients:");
3994 for (ClientState ci : mClients.values()) {
3995 p.println(" Client " + ci + ":");
3996 p.println(" client=" + ci.client);
3997 p.println(" inputContext=" + ci.inputContext);
3998 p.println(" sessionRequested=" + ci.sessionRequested);
3999 p.println(" curSession=" + ci.curSession);
4000 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004001 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004003 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4004 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004005 focusedWindowClient = mCurFocusedWindowClient;
4006 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4008 + " mBoundToMethod=" + mBoundToMethod);
4009 p.println(" mCurToken=" + mCurToken);
4010 p.println(" mCurIntent=" + mCurIntent);
4011 method = mCurMethod;
4012 p.println(" mCurMethod=" + mCurMethod);
4013 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004014 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 p.println(" mShowRequested=" + mShowRequested
4016 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4017 + " mShowForced=" + mShowForced
4018 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004019 p.println(" mCurUserActionNotificationSequenceNumber="
4020 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004021 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004022 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004023 p.println(" mSwitchingController:");
4024 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004025 p.println(" mSettings:");
4026 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004028
Jeff Brownb88102f2010-09-08 11:49:43 -07004029 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 pw.flush();
4032 try {
4033 client.client.asBinder().dump(fd, args);
4034 } catch (RemoteException e) {
4035 p.println("Input method client dead: " + e);
4036 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004037 } else {
4038 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004040
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004041 if (focusedWindowClient != null && client != focusedWindowClient) {
4042 p.println(" ");
4043 p.println("Warning: Current input method client doesn't match the last focused. "
4044 + "window.");
4045 p.println("Dumping input method client in the last focused window just in case.");
4046 p.println(" ");
4047 pw.flush();
4048 try {
4049 focusedWindowClient.client.asBinder().dump(fd, args);
4050 } catch (RemoteException e) {
4051 p.println("Input method client in focused window dead: " + e);
4052 }
4053 }
4054
Jeff Brownb88102f2010-09-08 11:49:43 -07004055 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 pw.flush();
4058 try {
4059 method.asBinder().dump(fd, args);
4060 } catch (RemoteException e) {
4061 p.println("Input method service dead: " + e);
4062 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004063 } else {
4064 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 }
4066 }
4067}