blob: e0d89f2fe6d3da4a87a5a17d499033318ff1dbc6 [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
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700180 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
satokf9f01002011-05-19 21:31:50 +0900182 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
183
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900184 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900185 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900186
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700187 @Retention(SOURCE)
188 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
189 private @interface HardKeyboardBehavior {
190 int WIRELESS_AFFORDANCE = 0;
191 int WIRED_AFFORDANCE = 1;
192 }
satok4e4569d2010-11-19 18:45:53 +0900193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800195 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900197 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700200 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700202 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900203 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900204 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900205 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800206 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900208 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 // All known input methods. mMethodMap also serves as the global
211 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700212 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
213 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900214 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700215 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900216 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700218 // Used to bring IME service up to visible adjustment while it is being shown.
219 final ServiceConnection mVisibleConnection = new ServiceConnection() {
220 @Override public void onServiceConnected(ComponentName name, IBinder service) {
221 }
222
223 @Override public void onServiceDisconnected(ComponentName name) {
224 }
225 };
226 boolean mVisibleBound = false;
227
satok7cfc0ed2011-06-20 21:29:36 +0900228 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700229 private NotificationManager mNotificationManager;
230 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700231 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400232 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700233 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900234 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900235 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900236 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900237
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900238 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 final ClientState client;
240 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700241
242 IInputMethodSession session;
243 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 @Override
246 public String toString() {
247 return "SessionState{uid " + client.uid + " pid " + client.pid
248 + " method " + Integer.toHexString(
249 System.identityHashCode(method))
250 + " session " + Integer.toHexString(
251 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700252 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 + "}";
254 }
255
256 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700257 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 client = _client;
259 method = _method;
260 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700261 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
Jeff Brownc28867a2013-03-26 15:42:39 -0700265 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 final IInputMethodClient client;
267 final IInputContext inputContext;
268 final int uid;
269 final int pid;
270 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 boolean sessionRequested;
273 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 @Override
276 public String toString() {
277 return "ClientState{" + Integer.toHexString(
278 System.identityHashCode(this)) + " uid " + uid
279 + " pid " + pid + "}";
280 }
281
282 ClientState(IInputMethodClient _client, IInputContext _inputContext,
283 int _uid, int _pid) {
284 client = _client;
285 inputContext = _inputContext;
286 uid = _uid;
287 pid = _pid;
288 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
289 }
290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700292 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700295 * Set once the system is ready to run third party code.
296 */
297 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700299 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700300 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
301 * method. This is to be synchronized with the secure settings keyed with
302 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
303 *
304 * <p>This can be transiently {@code null} when the system is re-initializing input method
305 * settings, e.g., the system locale is just changed.</p>
306 *
307 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
308 * {@link InputMethodManagerService}.</p>
309 *
310 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700312 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * The current binding sequence number, incremented every time there is
317 * a new bind performed.
318 */
319 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * The client that is currently bound to an input method.
323 */
324 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800327 * The last window token that we confirmed to be focused. This is always updated upon reports
328 * from the input method client. If the window state is already changed before the report is
329 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700330 */
331 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700333 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800334 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
335 */
336 ClientState mCurFocusedWindowClient;
337
338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * The input context last provided by the current client.
340 */
341 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700344 * The missing method flags for the input context last provided by the current client.
345 *
346 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
347 */
348 int mCurInputContextMissingMethods;
349
350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * The attributes last provided by the current client.
352 */
353 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700356 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700358 *
359 * <p>This can be {@code null} when no input method is connected.</p>
360 *
361 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700363 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
satokab751aa2010-09-14 19:17:36 +0900367 * The current subtype of the current input method.
368 */
369 private InputMethodSubtype mCurrentSubtype;
370
satok4e4569d2010-11-19 18:45:53 +0900371 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900372 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700373 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900374
John Spurlocke0980502013-10-25 11:59:29 -0400375 // Was the keyguard locked when this client became current?
376 private boolean mCurClientInKeyguard;
377
satokab751aa2010-09-14 19:17:36 +0900378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * Set to true if our ServiceConnection is currently actively bound to
380 * a service (whether or not we have gotten its IBinder back yet).
381 */
382 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
385 * Set if the client has asked for the input method to be shown.
386 */
387 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 /**
390 * Set if we were explicitly told to show the input method.
391 */
392 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * Set if we were forced to be shown.
396 */
397 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 /**
400 * Set if we last told the input method to show itself.
401 */
402 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
405 * The Intent used to connect to the current input method.
406 */
407 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * The token we have made for the currently active input method, to
411 * identify it in the future.
412 */
413 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * If non-null, this is the input method service we are currently connected
417 * to.
418 */
419 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 /**
422 * Time that we last initiated a bind to the input method, to determine
423 * if we should try to disconnect and reconnect to it.
424 */
425 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 /**
428 * Have we called mCurMethod.bindInput()?
429 */
430 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 /**
433 * Currently enabled session. Only touched by service thread, not
434 * protected by a lock.
435 */
436 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700439 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700441 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900443 int mCurUserActionNotificationSequenceNumber = 0;
444
Joe Onorato857fd9b2011-01-27 15:08:35 -0800445 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900446
447 /**
448 * A set of status bits regarding the active IME.
449 *
450 * <p>This value is a combination of following two bits:</p>
451 * <dl>
452 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
453 * <dd>
454 * If this bit is ON, connected IME is ready to accept touch/key events.
455 * </dd>
456 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
457 * <dd>
458 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
459 * </dd>
460 * </dl>
461 * <em>Do not update this value outside of setImeWindowStatus.</em>
462 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800463 int mImeWindowVis;
464
Ken Wakasa05dbb652011-08-22 15:22:43 +0900465 private AlertDialog.Builder mDialogBuilder;
466 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900467 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700468 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900469 private InputMethodInfo[] mIms;
470 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800471 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700472 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800473 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900474 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
475 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500476 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700477 @HardKeyboardBehavior
478 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700481 int mUserId;
482 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700483 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800484 String mLastEnabled = "";
485
Yohei Yukawa81482972015-06-04 00:58:59 -0700486 /**
487 * <em>This constructor must be called within the lock.</em>
488 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 SettingsObserver(Handler handler) {
490 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 }
492
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800493 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700494 if (mRegistered && mUserId == userId) {
495 return;
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700498 if (mRegistered) {
499 mContext.getContentResolver().unregisterContentObserver(this);
500 mRegistered = false;
501 }
502 if (mUserId != userId) {
503 mLastEnabled = "";
504 mUserId = userId;
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700507 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900508 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900510 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700512 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700513 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800514 resolver.registerContentObserver(Settings.Secure.getUriFor(
515 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700516 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Michael Wright7b5a96b2014-08-09 19:28:42 -0700519 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800520 final Uri showImeUri = Settings.Secure.getUriFor(
521 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
522 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
523 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700525 if (showImeUri.equals(uri)) {
526 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800527 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
528 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
529 mContext.getContentResolver(),
530 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
531 0, mUserId) == 1;
532 if (mAccessibilityRequestingNoSoftKeyboard) {
533 final boolean showRequested = mShowRequested;
534 hideCurrentInputLocked(0, null);
535 mShowRequested = showRequested;
536 } else if (mShowRequested) {
537 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
538 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700539 } else {
540 boolean enabledChanged = false;
541 String newEnabled = mSettings.getEnabledInputMethodsStr();
542 if (!mLastEnabled.equals(newEnabled)) {
543 mLastEnabled = newEnabled;
544 enabledChanged = true;
545 }
546 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700550
551 @Override
552 public String toString() {
553 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
554 + " mLastEnabled=" + mLastEnabled + "}";
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800557
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900558 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900559 @Override
560 public void onReceive(Context context, Intent intent) {
561 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700562 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900563 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700564 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900565 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800566 } else if (Intent.ACTION_USER_ADDED.equals(action)
567 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100568 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800569 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700570 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
571 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
572 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
573 final String prevValue = intent.getStringExtra(
574 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
575 final String newValue = intent.getStringExtra(
576 Intent.EXTRA_SETTING_NEW_VALUE);
577 restoreEnabledInputMethods(mContext, prevValue, newValue);
578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900579 } else {
580 Slog.w(TAG, "Unexpected intent " + intent);
581 }
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700585 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
586 // does not attempt to validate on the fly with any installed device policy, so must only
587 // be run in the context of initial device setup.
588 //
589 // TODO: Move this method to InputMethodUtils with adding unit tests.
590 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
591 if (DEBUG_RESTORE) {
592 Slog.i(TAG, "Restoring enabled input methods:");
593 Slog.i(TAG, "prev=" + prevValue);
594 Slog.i(TAG, " new=" + newValue);
595 }
596 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900597 ArrayMap<String, ArraySet<String>> prevMap =
598 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
599 ArrayMap<String, ArraySet<String>> newMap =
600 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700601
602 // Merge the restored ime+subtype enabled states into the live state
603 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
604 final String imeId = entry.getKey();
605 ArraySet<String> prevSubtypes = prevMap.get(imeId);
606 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700607 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700608 prevMap.put(imeId, prevSubtypes);
609 }
610 prevSubtypes.addAll(entry.getValue());
611 }
612
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700613 final String mergedImesAndSubtypesString =
614 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700615 if (DEBUG_RESTORE) {
616 Slog.i(TAG, "Merged IME string:");
617 Slog.i(TAG, " " + mergedImesAndSubtypesString);
618 }
619 Settings.Secure.putString(context.getContentResolver(),
620 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
621 }
622
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800623 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900624 private boolean isChangingPackagesOfCurrentUser() {
625 final int userId = getChangingUserId();
626 final boolean retval = userId == mSettings.getCurrentUserId();
627 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900628 if (!retval) {
629 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
630 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900631 }
632 return retval;
633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800636 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900637 if (!isChangingPackagesOfCurrentUser()) {
638 return false;
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 final int N = mMethodList.size();
643 if (curInputMethodId != null) {
644 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 InputMethodInfo imi = mMethodList.get(i);
646 if (imi.getId().equals(curInputMethodId)) {
647 for (String pkg : packages) {
648 if (imi.getPackageName().equals(pkg)) {
649 if (!doit) {
650 return true;
651 }
satok723a27e2010-11-11 14:58:11 +0900652 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 chooseNewDefaultIMELocked();
654 return true;
655 }
656 }
657 }
658 }
659 }
660 }
661 return false;
662 }
663
664 @Override
665 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900666 if (!isChangingPackagesOfCurrentUser()) {
667 return;
668 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 synchronized (mMethodMap) {
670 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900671 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 final int N = mMethodList.size();
673 if (curInputMethodId != null) {
674 for (int i=0; i<N; i++) {
675 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900676 final String imiId = imi.getId();
677 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800678 curIm = imi;
679 }
satoke7c6998e2011-06-03 17:57:59 +0900680
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900682 if (isPackageModified(imi.getPackageName())) {
683 mFileManager.deleteAllInputMethodSubtypes(imiId);
684 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800685 if (change == PACKAGE_TEMPORARY_CHANGE
686 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800687 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 + imi.getComponent());
689 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691 }
692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
Yohei Yukawa94e33302016-02-12 19:37:03 -0800694 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800698 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800699 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800700 if (change == PACKAGE_TEMPORARY_CHANGE
701 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800702 ServiceInfo si = null;
703 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900704 si = mIPackageManager.getServiceInfo(
705 curIm.getComponent(), 0, mSettings.getCurrentUserId());
706 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800707 }
708 if (si == null) {
709 // Uh oh, current input method is no longer around!
710 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900712 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800713 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800714 changed = true;
715 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800716 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900717 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 }
719 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800720 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800721 }
satokab751aa2010-09-14 19:17:36 +0900722
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800723 if (curIm == null) {
724 // We currently don't have a default input method... is
725 // one now available?
726 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700727 } else if (!changed && isPackageModified(curIm.getPackageName())) {
728 // Even if the current input method is still available, mCurrentSubtype could
729 // be obsolete when the package is modified in practice.
730 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800733 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800734 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
737 }
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
Jeff Brownc28867a2013-03-26 15:42:39 -0700740 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900741 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700742 private final IInputMethod mMethod;
743 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800744
Jeff Brownc28867a2013-03-26 15:42:39 -0700745 MethodCallback(InputMethodManagerService imms, IInputMethod method,
746 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900747 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700748 mMethod = method;
749 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800751
satoke7c6998e2011-06-03 17:57:59 +0900752 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700753 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700754 long ident = Binder.clearCallingIdentity();
755 try {
756 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
757 } finally {
758 Binder.restoreCallingIdentity(ident);
759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
satok01038492012-04-09 21:08:27 +0900763 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700764 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900765 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700766 public void onHardKeyboardStatusChange(boolean available) {
767 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
768 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900769 }
770
Michael Wright7b5a96b2014-08-09 19:28:42 -0700771 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900772 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700773 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900774 }
775 synchronized(mMethodMap) {
776 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
777 && mSwitchingDialog.isShowing()) {
778 mSwitchingDialogTitleView.findViewById(
779 com.android.internal.R.id.hard_keyboard_section).setVisibility(
780 available ? View.VISIBLE : View.GONE);
781 }
782 }
783 }
784 }
785
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800786 public static final class Lifecycle extends SystemService {
787 private InputMethodManagerService mService;
788
789 public Lifecycle(Context context) {
790 super(context);
791 mService = new InputMethodManagerService(context);
792 }
793
794 @Override
795 public void onStart() {
796 LocalServices.addService(InputMethodManagerInternal.class,
797 new LocalServiceImpl(mService.mHandler));
798 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
799 }
800
801 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800802 public void onSwitchUser(@UserIdInt int userHandle) {
803 // Called on the system server's main looper thread.
804 // TODO: Dispatch this to a worker thread as needed.
805 mService.onSwitchUser(userHandle);
806 }
807
808 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800809 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800810 // Called on the system server's main looper thread.
811 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800812 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
813 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
814 .getService(Context.STATUS_BAR_SERVICE);
815 mService.systemRunning(statusBarService);
816 }
817 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800818
819 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800820 public void onUnlockUser(@UserIdInt int userHandle) {
821 // Called on the system server's main looper thread.
822 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800823 mService.onUnlockUser(userHandle);
824 }
825 }
826
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800827 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800828 synchronized(mMethodMap) {
829 final int currentUserId = mSettings.getCurrentUserId();
830 if (DEBUG) {
831 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
832 }
833 if (userId != currentUserId) {
834 return;
835 }
836 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
837 // We need to rebuild IMEs.
838 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
839 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
840 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800841 }
842
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800843 void onSwitchUser(@UserIdInt int userId) {
844 synchronized (mMethodMap) {
845 switchUserLocked(userId);
846 }
847 }
848
Seigo Nonaka7309b122015-08-17 18:34:13 -0700849 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900850 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800852 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700854 // Note: SettingsObserver doesn't register observers in its constructor.
855 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 mIWindowManager = IWindowManager.Stub.asInterface(
857 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700858 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800859 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900860 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 public void executeMessage(Message msg) {
862 handleMessage(msg);
863 }
Mita Yuned218c72012-12-06 17:18:25 -0800864 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800865 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800866 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900867 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700868 mHasFeature = context.getPackageManager().hasSystemFeature(
869 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500870 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700871 mHardKeyboardBehavior = mContext.getResources().getInteger(
872 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900873
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400874 Bundle extras = new Bundle();
875 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
876 mImeSwitcherNotification = new Notification.Builder(mContext)
877 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
878 .setWhen(0)
879 .setOngoing(true)
880 .addExtras(extras)
881 .setCategory(Notification.CATEGORY_SYSTEM)
882 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400883
satok7cfc0ed2011-06-20 21:29:36 +0900884 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900885 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900886
887 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900888
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900889 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900890 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800891 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
892 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700893 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800895
satok7cfc0ed2011-06-20 21:29:36 +0900896 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 int userId = 0;
898 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
900 } catch (RemoteException e) {
901 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
902 }
satok81f8b7c2012-12-04 20:42:56 +0900903 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900904
satokd87c2592010-09-29 11:52:06 +0900905 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900906 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800907 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700908
Kenny Guy2a764942014-04-02 13:29:20 +0100909 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900910 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900911 synchronized (mMethodMap) {
912 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
913 mSettings, context);
914 }
satok0a1bcf42012-05-16 19:26:31 +0900915
916 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900917 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900918 if (DEBUG) {
919 Slog.d(TAG, "Initial default ime = " + defaultImiId);
920 }
satok0a1bcf42012-05-16 19:26:31 +0900921 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
922
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900923 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800924 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900925 }
satokd87c2592010-09-29 11:52:06 +0900926 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927
satok0a1bcf42012-05-16 19:26:31 +0900928 if (!mImeSelectedOnBoot) {
929 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900930 synchronized (mMethodMap) {
931 resetDefaultImeLocked(context);
932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800934
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900935 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700936 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900937 updateFromSettingsLocked(true);
938 }
satok5b927c432012-05-01 20:09:34 +0900939
940 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
941 // according to the new system locale.
942 final IntentFilter filter = new IntentFilter();
943 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
944 mContext.registerReceiver(
945 new BroadcastReceiver() {
946 @Override
947 public void onReceive(Context context, Intent intent) {
948 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900949 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900950 }
951 }
952 }, filter);
953 }
954
satok5b927c432012-05-01 20:09:34 +0900955 private void resetDefaultImeLocked(Context context) {
956 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800957 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900958 return;
959 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800960 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
961 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
962 if (suitableImes.isEmpty()) {
963 Slog.i(TAG, "No default found");
964 return;
satok5b927c432012-05-01 20:09:34 +0900965 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800966 final InputMethodInfo defIm = suitableImes.get(0);
967 Slog.i(TAG, "Default found, using " + defIm.getId());
968 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900969 }
970
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900971 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
972 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 if (!mSystemReady) {
974 // not system ready
975 return;
976 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800977 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800979 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 if (!updateOnlyWhenLocaleChanged) {
981 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800982 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 }
984 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800985 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800987 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 if (!updateOnlyWhenLocaleChanged) {
989 final String selectedImiId = mSettings.getSelectedInputMethod();
990 if (TextUtils.isEmpty(selectedImiId)) {
991 // This is the first time of the user switch and
992 // set the current ime to the proper one.
993 resetDefaultImeLocked(mContext);
994 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900995 } else {
996 // If the locale is changed, needs to reset the default ime
997 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800999 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001000 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001001 if (!updateOnlyWhenLocaleChanged) {
1002 try {
1003 startInputInnerLocked();
1004 } catch (RuntimeException e) {
1005 Slog.w(TAG, "Unexpected exception", e);
1006 }
1007 }
1008 }
1009 }
1010
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001011 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001012 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1013 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001014 }
1015
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001016 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001017 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1018 + " currentUserId=" + mSettings.getCurrentUserId());
1019
Yohei Yukawa81482972015-06-04 00:58:59 -07001020 // ContentObserver should be registered again when the user is changed
1021 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001022
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001023 // If the system is not ready or the device is not yed unlocked by the user, then we use
1024 // copy-on-write settings.
1025 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001026 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001027 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001028 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001029 // InputMethodFileManager should be reset when the user is changed
1030 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001031 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001032
1033 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1034 + " defaultImiId=" + defaultImiId);
1035
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001036 // For secondary users, the list of enabled IMEs may not have been updated since the
1037 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1038 // not be empty even if the IME has been uninstalled by the primary user.
1039 // Even in such cases, IMMS works fine because it will find the most applicable
1040 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001041 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001042 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001043 initialUserSwitch /* needsToResetDefaultIme */);
1044 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001045 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1046 mSettings.getEnabledInputMethodListLocked(), newUserId,
1047 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001048 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001049
1050 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1051 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 }
1053
Kenny Guy2a764942014-04-02 13:29:20 +01001054 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001055 mSettings.setCurrentProfileIds(
1056 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001057 }
1058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 @Override
1060 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1061 throws RemoteException {
1062 try {
1063 return super.onTransact(code, data, reply, flags);
1064 } catch (RuntimeException e) {
1065 // The input method manager only throws security exceptions, so let's
1066 // log all others.
1067 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001068 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 throw e;
1071 }
1072 }
1073
Svetoslav Ganova0027152013-06-25 14:59:53 -07001074 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001075 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001076 if (DEBUG) {
1077 Slog.d(TAG, "--- systemReady");
1078 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001079 if (!mSystemReady) {
1080 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001081 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001082 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001083 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001084 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1085 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001086 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001087 if (mStatusBar != null) {
1088 mStatusBar.setIconVisibility(mSlotIme, false);
1089 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001090 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001091 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1092 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001093 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001094 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001095 mHardKeyboardListener);
1096 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001097 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001098 if (!mImeSelectedOnBoot) {
1099 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001100 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001101 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1102 mSettings.getEnabledInputMethodListLocked(),
1103 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001104 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001105 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001106 try {
1107 startInputInnerLocked();
1108 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001109 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001110 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001111 }
1112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001115 // ---------------------------------------------------------------------------------------
1116 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1117 // 1) it comes from the system process
1118 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1119 private boolean calledFromValidUser() {
1120 final int uid = Binder.getCallingUid();
1121 final int userId = UserHandle.getUserId(uid);
1122 if (DEBUG) {
1123 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1124 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1125 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001126 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1127 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 }
Kenny Guy2a764942014-04-02 13:29:20 +01001129 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001130 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001132
1133 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1134 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1135 // must not manage background users' states in any functions.
1136 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1137 // by a token.
1138 if (mContext.checkCallingOrSelfPermission(
1139 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1140 == PackageManager.PERMISSION_GRANTED) {
1141 if (DEBUG) {
1142 Slog.d(TAG, "--- Access granted because the calling process has "
1143 + "the INTERACT_ACROSS_USERS_FULL permission");
1144 }
1145 return true;
1146 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001147 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1148 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001149 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001150 }
1151
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001152
1153 /**
1154 * Returns true iff the caller is identified to be the current input method with the token.
1155 * @param token The window token given to the input method when it was started.
1156 * @return true if and only if non-null valid token is specified.
1157 */
1158 private boolean calledWithValidToken(IBinder token) {
1159 if (token == null || mCurToken != token) {
1160 return false;
1161 }
1162 return true;
1163 }
1164
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 private boolean bindCurrentInputMethodService(
1166 Intent service, ServiceConnection conn, int flags) {
1167 if (service == null || conn == null) {
1168 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1169 return false;
1170 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001171 return mContext.bindServiceAsUser(service, conn, flags,
1172 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001173 }
1174
satoke7c6998e2011-06-03 17:57:59 +09001175 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001177 // TODO: Make this work even for non-current users?
1178 if (!calledFromValidUser()) {
1179 return Collections.emptyList();
1180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001182 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 }
1185
satoke7c6998e2011-06-03 17:57:59 +09001186 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001188 // TODO: Make this work even for non-current users?
1189 if (!calledFromValidUser()) {
1190 return Collections.emptyList();
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001193 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
1196
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001198 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001199 * @return enabled subtypes of the specified imi
1200 */
satoke7c6998e2011-06-03 17:57:59 +09001201 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001202 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001203 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001204 // TODO: Make this work even for non-current users?
1205 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001206 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 }
satok67ddf9c2010-11-17 09:45:54 +09001208 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001209 final InputMethodInfo imi;
1210 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001211 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001212 } else {
1213 imi = mMethodMap.get(imiId);
1214 }
1215 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001216 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001217 }
1218 return mSettings.getEnabledInputMethodSubtypeListLocked(
1219 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001220 }
1221 }
1222
satoke7c6998e2011-06-03 17:57:59 +09001223 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public void addClient(IInputMethodClient client,
1225 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001226 if (!calledFromValidUser()) {
1227 return;
1228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 synchronized (mMethodMap) {
1230 mClients.put(client.asBinder(), new ClientState(client,
1231 inputContext, uid, pid));
1232 }
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
satoke7c6998e2011-06-03 17:57:59 +09001235 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001237 if (!calledFromValidUser()) {
1238 return;
1239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001241 ClientState cs = mClients.remove(client.asBinder());
1242 if (cs != null) {
1243 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001244 if (mCurClient == cs) {
1245 mCurClient = null;
1246 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001247 if (mCurFocusedWindowClient == cs) {
1248 mCurFocusedWindowClient = null;
1249 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 void executeOrSendMessage(IInterface target, Message msg) {
1255 if (target.asBinder() instanceof Binder) {
1256 mCaller.sendMessage(msg);
1257 } else {
1258 handleMessage(msg);
1259 msg.recycle();
1260 }
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
Yohei Yukawa33e81792015-11-17 21:14:42 -08001263 void unbindCurrentClientLocked(
1264 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001266 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 + mCurClient.client.asBinder());
1268 if (mBoundToMethod) {
1269 mBoundToMethod = false;
1270 if (mCurMethod != null) {
1271 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1272 MSG_UNBIND_INPUT, mCurMethod));
1273 }
1274 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001275
1276 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1277 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001278 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1279 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
The Android Open Source Project10592532009-03-18 17:39:46 -07001283 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 private int getImeShowFlags() {
1288 int flags = 0;
1289 if (mShowForced) {
1290 flags |= InputMethod.SHOW_FORCED
1291 | InputMethod.SHOW_EXPLICIT;
1292 } else if (mShowExplicitlyRequested) {
1293 flags |= InputMethod.SHOW_EXPLICIT;
1294 }
1295 return flags;
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 private int getAppShowFlags() {
1299 int flags = 0;
1300 if (mShowForced) {
1301 flags |= InputMethodManager.SHOW_FORCED;
1302 } else if (!mShowExplicitlyRequested) {
1303 flags |= InputMethodManager.SHOW_IMPLICIT;
1304 }
1305 return flags;
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Dianne Hackborn7663d802012-02-24 13:08:49 -08001308 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 if (!mBoundToMethod) {
1310 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1311 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1312 mBoundToMethod = true;
1313 }
1314 final SessionState session = mCurClient.curSession;
1315 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001316 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1317 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1318 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001320 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1321 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1322 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001325 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001326 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001328 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001329 (session.channel != null ? session.channel.dup() : null),
1330 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001333 InputBindResult startInputLocked(
1334 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001335 IInputMethodClient client, IInputContext inputContext,
1336 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001337 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // If no method is currently selected, do nothing.
1339 if (mCurMethodId == null) {
1340 return mNoBinding;
1341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 ClientState cs = mClients.get(client.asBinder());
1344 if (cs == null) {
1345 throw new IllegalArgumentException("unknown client "
1346 + client.asBinder());
1347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
Yohei Yukawa74750f22016-03-22 12:54:22 -07001349 if (attribute == null) {
1350 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1351 + " uid=" + cs.uid + " pid=" + cs.pid);
1352 return null;
1353 }
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 try {
1356 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1357 // Check with the window manager to make sure this client actually
1358 // has a window with focus. If not, reject. This is thread safe
1359 // because if the focus changes some time before or after, the
1360 // next client receiving focus that has any interest in input will
1361 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001362 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1364 return null;
1365 }
1366 } catch (RemoteException e) {
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001369 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1370 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001371 }
1372
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001373 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001374 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001375 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001376 // If no method is currently selected, do nothing.
1377 if (mCurMethodId == null) {
1378 return mNoBinding;
1379 }
1380
Yohei Yukawad57ba672015-06-08 16:39:46 -07001381 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1382 attribute.packageName)) {
1383 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1384 + " uid=" + cs.uid + " package=" + attribute.packageName);
1385 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001386 }
1387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001389 // Was the keyguard locked when switching over to the new client?
1390 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 // If the client is changing, we need to switch over to the new
1392 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001393 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001394 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001395 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396
1397 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001398 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001399 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001400 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 // Bump up the sequence for this client and attach it.
1405 mCurSeq++;
1406 if (mCurSeq <= 0) mCurSeq = 1;
1407 mCurClient = cs;
1408 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001409 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 // Check if the input method is changing.
1413 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1414 if (cs.curSession != null) {
1415 // Fast case: if we are already connected to the input method,
1416 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001417 return attachNewInputLocked(
1418 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420 if (mHaveConnection) {
1421 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // Return to client, and we will get back with it when
1423 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001424 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001425 return new InputBindResult(null, null, mCurId, mCurSeq,
1426 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 } else if (SystemClock.uptimeMillis()
1428 < (mLastBindTime+TIME_TO_RECONNECT)) {
1429 // In this case we have connected to the service, but
1430 // don't yet have its interface. If it hasn't been too
1431 // long since we did the connection, we'll return to
1432 // the client and wait to get the service interface so
1433 // we can report back. If it has been too long, we want
1434 // to fall through so we can try a disconnect/reconnect
1435 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001436 return new InputBindResult(null, null, mCurId, mCurSeq,
1437 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1440 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001445 return startInputInnerLocked();
1446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001448 InputBindResult startInputInnerLocked() {
1449 if (mCurMethodId == null) {
1450 return mNoBinding;
1451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001453 if (!mSystemReady) {
1454 // If the system is not yet ready, we shouldn't be running third
1455 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001456 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1457 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1461 if (info == null) {
1462 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001464
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001465 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1468 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001469 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1470 com.android.internal.R.string.input_method_binding_label);
1471 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1472 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001473 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001474 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1475 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mLastBindTime = SystemClock.uptimeMillis();
1477 mHaveConnection = true;
1478 mCurId = info.getId();
1479 mCurToken = new Binder();
1480 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001481 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 mIWindowManager.addWindowToken(mCurToken,
1483 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1484 } catch (RemoteException e) {
1485 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001486 return new InputBindResult(null, null, mCurId, mCurSeq,
1487 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 } else {
1489 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 + mCurIntent);
1492 }
1493 return null;
1494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001496 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001497 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001498 IInputMethodClient client, IInputContext inputContext,
1499 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001500 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001501 if (!calledFromValidUser()) {
1502 return null;
1503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001505 if (DEBUG) {
1506 Slog.v(TAG, "startInput: reason="
1507 + InputMethodClient.getStartInputReason(startInputReason)
1508 + " client = " + client.asBinder()
1509 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001510 + " missingMethods="
1511 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001512 + " attribute=" + attribute
1513 + " controlFlags=#" + Integer.toHexString(controlFlags));
1514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 final long ident = Binder.clearCallingIdentity();
1516 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001517 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1518 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 } finally {
1520 Binder.restoreCallingIdentity(ident);
1521 }
1522 }
1523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001524
satoke7c6998e2011-06-03 17:57:59 +09001525 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 public void finishInput(IInputMethodClient client) {
1527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
satoke7c6998e2011-06-03 17:57:59 +09001529 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 public void onServiceConnected(ComponentName name, IBinder service) {
1531 synchronized (mMethodMap) {
1532 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1533 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001534 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001536 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001537 return;
1538 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001539 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001540 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1541 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001543 clearClientSessionLocked(mCurClient);
1544 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 }
1546 }
1547 }
1548 }
1549
Jeff Brownc28867a2013-03-26 15:42:39 -07001550 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1551 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 synchronized (mMethodMap) {
1553 if (mCurMethod != null && method != null
1554 && mCurMethod.asBinder() == method.asBinder()) {
1555 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001556 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001559 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 if (res.method != null) {
1561 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001562 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001564 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566 }
1567 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001568
1569 // Session abandoned. Close its associated input channel.
1570 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001573 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001574 if (mVisibleBound) {
1575 mContext.unbindService(mVisibleConnection);
1576 mVisibleBound = false;
1577 }
1578
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001579 if (mHaveConnection) {
1580 mContext.unbindService(this);
1581 mHaveConnection = false;
1582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001583
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 if (mCurToken != null) {
1585 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001586 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001587 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001588 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001589 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001590 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001591 mIWindowManager.removeWindowToken(mCurToken);
1592 } catch (RemoteException e) {
1593 }
1594 mCurToken = null;
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
The Android Open Source Project10592532009-03-18 17:39:46 -07001597 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001598 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
Yohei Yukawa33e81792015-11-17 21:14:42 -08001601 void resetCurrentMethodAndClient(
1602 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001603 mCurMethodId = null;
1604 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001605 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001606 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001607
1608 void requestClientSessionLocked(ClientState cs) {
1609 if (!cs.sessionRequested) {
1610 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1611 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1612 cs.sessionRequested = true;
1613 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1614 MSG_CREATE_SESSION, mCurMethod, channels[1],
1615 new MethodCallback(this, mCurMethod, channels[0])));
1616 }
1617 }
1618
1619 void clearClientSessionLocked(ClientState cs) {
1620 finishSessionLocked(cs.curSession);
1621 cs.curSession = null;
1622 cs.sessionRequested = false;
1623 }
1624
1625 private void finishSessionLocked(SessionState sessionState) {
1626 if (sessionState != null) {
1627 if (sessionState.session != null) {
1628 try {
1629 sessionState.session.finishSession();
1630 } catch (RemoteException e) {
1631 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001632 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001633 }
1634 sessionState.session = null;
1635 }
1636 if (sessionState.channel != null) {
1637 sessionState.channel.dispose();
1638 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001639 }
1640 }
1641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001642
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001643 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 if (mCurMethod != null) {
1645 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001646 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001648
Jeff Brownc28867a2013-03-26 15:42:39 -07001649 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001650 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 mCurMethod = null;
1652 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001653 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001654 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
satoke7c6998e2011-06-03 17:57:59 +09001658 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 public void onServiceDisconnected(ComponentName name) {
1660 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001661 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 + " mCurIntent=" + mCurIntent);
1663 if (mCurMethod != null && mCurIntent != null
1664 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001665 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 // We consider this to be a new bind attempt, since the system
1667 // should now try to restart the service for us.
1668 mLastBindTime = SystemClock.uptimeMillis();
1669 mShowRequested = mInputShown;
1670 mInputShown = false;
1671 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001672 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1673 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1674 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676 }
1677 }
1678 }
1679
satokf9f01002011-05-19 21:31:50 +09001680 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1682 long ident = Binder.clearCallingIdentity();
1683 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001685 if (!calledWithValidToken(token)) {
1686 final int uid = Binder.getCallingUid();
1687 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1688 + " token:" + token);
1689 return;
1690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001693 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001694 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001697 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001698 CharSequence contentDescription = null;
1699 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001700 // Use PackageManager to load label
1701 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001702 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001703 mIPackageManager.getApplicationInfo(packageName, 0,
1704 mSettings.getCurrentUserId()));
1705 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001706 /* ignore */
1707 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001708 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001709 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001710 contentDescription != null
1711 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001712 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
1715 }
1716 } finally {
1717 Binder.restoreCallingIdentity(ident);
1718 }
1719 }
1720
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001721 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001722 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001723 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001724 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001725 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001726 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001727 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1728 // When physical keyboard is attached, we show the ime switcher (or notification if
1729 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1730 // exists in the IME switcher dialog. Might be OK to remove this condition once
1731 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1732 return true;
1733 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001734 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1735 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001736 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001737
1738 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1739 final int N = imis.size();
1740 if (N > 2) return true;
1741 if (N < 1) return false;
1742 int nonAuxCount = 0;
1743 int auxCount = 0;
1744 InputMethodSubtype nonAuxSubtype = null;
1745 InputMethodSubtype auxSubtype = null;
1746 for(int i = 0; i < N; ++i) {
1747 final InputMethodInfo imi = imis.get(i);
1748 final List<InputMethodSubtype> subtypes =
1749 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1750 final int subtypeCount = subtypes.size();
1751 if (subtypeCount == 0) {
1752 ++nonAuxCount;
1753 } else {
1754 for (int j = 0; j < subtypeCount; ++j) {
1755 final InputMethodSubtype subtype = subtypes.get(j);
1756 if (!subtype.isAuxiliary()) {
1757 ++nonAuxCount;
1758 nonAuxSubtype = subtype;
1759 } else {
1760 ++auxCount;
1761 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001762 }
1763 }
satok7cfc0ed2011-06-20 21:29:36 +09001764 }
1765 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001766 if (nonAuxCount > 1 || auxCount > 1) {
1767 return true;
1768 } else if (nonAuxCount == 1 && auxCount == 1) {
1769 if (nonAuxSubtype != null && auxSubtype != null
1770 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1771 || auxSubtype.overridesImplicitlyEnabledSubtype()
1772 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1773 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1774 return false;
1775 }
1776 return true;
1777 }
1778 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001779 }
1780
John Spurlocke0980502013-10-25 11:59:29 -04001781 private boolean isKeyguardLocked() {
1782 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1783 }
1784
satokdbf29502011-08-25 15:28:23 +09001785 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001786 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001787 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001788 if (!calledWithValidToken(token)) {
1789 final int uid = Binder.getCallingUid();
1790 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1791 + " token:" + token);
1792 return;
1793 }
1794
1795 synchronized (mMethodMap) {
1796 mImeWindowVis = vis;
1797 mBackDisposition = backDisposition;
1798 updateSystemUiLocked(token, vis, backDisposition);
1799 }
1800 }
1801
1802 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1803 synchronized (mMethodMap) {
1804 updateSystemUiLocked(token, vis, backDisposition);
1805 }
1806 }
1807
1808 // Caution! This method is called in this class. Handle multi-user carefully
1809 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1810 if (!calledWithValidToken(token)) {
1811 final int uid = Binder.getCallingUid();
1812 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1813 + " token:" + token);
1814 return;
1815 }
1816
1817 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1818 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001819 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001820 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001821 // apply policy for binder calls
1822 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1823 vis = 0;
satok06487a52010-10-29 11:37:18 +09001824 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001825 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1826 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1827 if (mStatusBar != null) {
1828 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1829 needsToShowImeSwitcher);
1830 }
1831 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1832 if (imi != null && needsToShowImeSwitcher) {
1833 // Used to load label
1834 final CharSequence title = mRes.getText(
1835 com.android.internal.R.string.select_input_method);
1836 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1837 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001838 mImeSwitcherNotification.setContentTitle(title)
1839 .setContentText(summary)
1840 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001841 try {
1842 if ((mNotificationManager != null)
1843 && !mIWindowManager.hasNavigationBar()) {
1844 if (DEBUG) {
1845 Slog.d(TAG, "--- show notification: label = " + summary);
1846 }
1847 mNotificationManager.notifyAsUser(null,
1848 com.android.internal.R.string.select_input_method,
1849 mImeSwitcherNotification.build(), UserHandle.ALL);
1850 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001851 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001852 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001853 }
1854 } else {
1855 if (mNotificationShown && mNotificationManager != null) {
1856 if (DEBUG) {
1857 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001858 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001859 mNotificationManager.cancelAsUser(null,
1860 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1861 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001862 }
satok06487a52010-10-29 11:37:18 +09001863 }
1864 } finally {
1865 Binder.restoreCallingIdentity(ident);
1866 }
1867 }
1868
satoke7c6998e2011-06-03 17:57:59 +09001869 @Override
satokf9f01002011-05-19 21:31:50 +09001870 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001871 if (!calledFromValidUser()) {
1872 return;
1873 }
satokf9f01002011-05-19 21:31:50 +09001874 synchronized (mMethodMap) {
1875 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1876 for (int i = 0; i < spans.length; ++i) {
1877 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001878 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001879 mSecureSuggestionSpans.put(ss, currentImi);
1880 }
1881 }
1882 }
1883 }
1884
satoke7c6998e2011-06-03 17:57:59 +09001885 @Override
satokf9f01002011-05-19 21:31:50 +09001886 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001887 if (!calledFromValidUser()) {
1888 return false;
1889 }
satokf9f01002011-05-19 21:31:50 +09001890 synchronized (mMethodMap) {
1891 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1892 // TODO: Do not send the intent if the process of the targetImi is already dead.
1893 if (targetImi != null) {
1894 final String[] suggestions = span.getSuggestions();
1895 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001896 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001897 final Intent intent = new Intent();
1898 // Ensures that only a class in the original IME package will receive the
1899 // notification.
satok42c5a162011-05-26 16:46:14 +09001900 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001901 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1902 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1903 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1904 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001905 final long ident = Binder.clearCallingIdentity();
1906 try {
1907 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1908 } finally {
1909 Binder.restoreCallingIdentity(ident);
1910 }
satokf9f01002011-05-19 21:31:50 +09001911 return true;
1912 }
1913 }
1914 return false;
1915 }
1916
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001917 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001918 updateInputMethodsFromSettingsLocked(enabledMayChange);
1919 updateKeyboardFromSettingsLocked();
1920 }
1921
1922 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001923 if (enabledMayChange) {
1924 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1925 for (int i=0; i<enabled.size(); i++) {
1926 // We allow the user to select "disabled until used" apps, so if they
1927 // are enabling one of those here we now need to make it enabled.
1928 InputMethodInfo imm = enabled.get(i);
1929 try {
1930 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1931 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1932 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001933 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001934 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001935 if (DEBUG) {
1936 Slog.d(TAG, "Update state(" + imm.getId()
1937 + "): DISABLED_UNTIL_USED -> DEFAULT");
1938 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001939 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1940 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001941 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1942 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001943 }
1944 } catch (RemoteException e) {
1945 }
1946 }
1947 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001948 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1949 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1950 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1951 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001952 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001953 // There is no input method selected, try to choose new applicable input method.
1954 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001955 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001956 }
1957 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001959 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001961 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001962 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
satokf3db1af2010-11-23 13:34:33 +09001964 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001965 } else {
1966 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001967 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001969 // Here is not the perfect place to reset the switching controller. Ideally
1970 // mSwitchingController and mSettings should be able to share the same state.
1971 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1972 // the same enabled IMEs list.
1973 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001974
1975 }
1976
1977 public void updateKeyboardFromSettingsLocked() {
1978 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1979 if (mSwitchingDialog != null
1980 && mSwitchingDialogTitleView != null
1981 && mSwitchingDialog.isShowing()) {
1982 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1983 com.android.internal.R.id.hard_keyboard_switch);
1984 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001987
Yohei Yukawab097b822015-12-01 10:43:08 -08001988 private void notifyInputMethodSubtypeChanged(final int userId,
1989 @Nullable final InputMethodInfo inputMethodInfo,
1990 @Nullable final InputMethodSubtype subtype) {
1991 final InputManagerInternal inputManagerInternal =
1992 LocalServices.getService(InputManagerInternal.class);
1993 if (inputManagerInternal != null) {
1994 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1995 }
1996 }
1997
satokab751aa2010-09-14 19:17:36 +09001998 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 InputMethodInfo info = mMethodMap.get(id);
2000 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002001 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002003
satokd81e9502012-05-21 12:58:45 +09002004 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002006 final int subtypeCount = info.getSubtypeCount();
2007 if (subtypeCount <= 0) {
2008 return;
satokcd7cd292010-11-20 15:46:23 +09002009 }
satokd81e9502012-05-21 12:58:45 +09002010 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2011 final InputMethodSubtype newSubtype;
2012 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2013 newSubtype = info.getSubtypeAt(subtypeId);
2014 } else {
2015 // If subtype is null, try to find the most applicable one from
2016 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002017 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002018 }
2019 if (newSubtype == null || oldSubtype == null) {
2020 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2021 + ", new subtype = " + newSubtype);
2022 return;
2023 }
2024 if (newSubtype != oldSubtype) {
2025 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2026 if (mCurMethod != null) {
2027 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002028 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002029 mCurMethod.changeInputMethodSubtype(newSubtype);
2030 } catch (RemoteException e) {
2031 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002032 return;
satokab751aa2010-09-14 19:17:36 +09002033 }
2034 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002035 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 return;
2038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002039
satokd81e9502012-05-21 12:58:45 +09002040 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 final long ident = Binder.clearCallingIdentity();
2042 try {
satokab751aa2010-09-14 19:17:36 +09002043 // Set a subtype to this input method.
2044 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002045 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2046 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2047 // because mCurMethodId is stored as a history in
2048 // setSelectedInputMethodAndSubtypeLocked().
2049 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050
2051 if (ActivityManagerNative.isSystemReady()) {
2052 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002053 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002055 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002057 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 } finally {
2059 Binder.restoreCallingIdentity(ident);
2060 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002061
2062 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2063 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002065
satok42c5a162011-05-26 16:46:14 +09002066 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002067 public boolean showSoftInput(IInputMethodClient client, int flags,
2068 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002069 if (!calledFromValidUser()) {
2070 return false;
2071 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002072 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 long ident = Binder.clearCallingIdentity();
2074 try {
2075 synchronized (mMethodMap) {
2076 if (mCurClient == null || client == null
2077 || mCurClient.client.asBinder() != client.asBinder()) {
2078 try {
2079 // We need to check if this is the current client with
2080 // focus in the window manager, to allow this call to
2081 // be made before input is started in it.
2082 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002083 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002090
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002091 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 } finally {
2095 Binder.restoreCallingIdentity(ident);
2096 }
2097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
The Android Open Source Project4df24232009-03-05 14:34:35 -08002099 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002101 if (mAccessibilityRequestingNoSoftKeyboard) {
2102 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
Anna Galusza9b278112016-01-04 11:37:37 -08002104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2106 mShowExplicitlyRequested = true;
2107 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002108 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2109 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002111
Dianne Hackborncc278702009-09-02 23:07:23 -07002112 if (!mSystemReady) {
2113 return false;
2114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002115
The Android Open Source Project4df24232009-03-05 14:34:35 -08002116 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002118 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2120 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2121 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002123 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002125 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002126 | Context.BIND_TREAT_LIKE_ACTIVITY
2127 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002128 mVisibleBound = true;
2129 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002130 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002132 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 // The client has asked to have the input method shown, but
2134 // we have been sitting here too long with a connection to the
2135 // service and no interface received, so let's disconnect/connect
2136 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002139 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002141 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002142 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 } else {
2144 if (DEBUG) {
2145 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2146 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149
The Android Open Source Project4df24232009-03-05 14:34:35 -08002150 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
satok42c5a162011-05-26 16:46:14 +09002153 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002154 public boolean hideSoftInput(IInputMethodClient client, int flags,
2155 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002156 if (!calledFromValidUser()) {
2157 return false;
2158 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002159 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 long ident = Binder.clearCallingIdentity();
2161 try {
2162 synchronized (mMethodMap) {
2163 if (mCurClient == null || client == null
2164 || mCurClient.client.asBinder() != client.asBinder()) {
2165 try {
2166 // We need to check if this is the current client with
2167 // focus in the window manager, to allow this call to
2168 // be made before input is started in it.
2169 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002170 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2171 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
Joe Onorato8a9b2202010-02-26 18:56:32 -08002179 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002180 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 } finally {
2183 Binder.restoreCallingIdentity(ident);
2184 }
2185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002186
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2189 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002190 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 -08002191 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002194 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 -08002195 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002197
2198 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2199 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2200 // to be updated with the new value sent from IME process. Even in such a transient state
2201 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2202 // application process as a valid request, and have even promised such a behavior with CTS
2203 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2204 // IMMS#InputShown indicates that the software keyboard is shown.
2205 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2206 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2207 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002209 if (shouldHideSoftInput) {
2210 // The IME will report its visible state again after the following message finally
2211 // delivered to the IME process as an IPC. Hence the inconsistency between
2212 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2213 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002214 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2215 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2216 res = true;
2217 } else {
2218 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002220 if (mHaveConnection && mVisibleBound) {
2221 mContext.unbindService(mVisibleConnection);
2222 mVisibleBound = false;
2223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 mInputShown = false;
2225 mShowRequested = false;
2226 mShowExplicitlyRequested = false;
2227 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002228 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002230
satok42c5a162011-05-26 16:46:14 +09002231 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002232 public InputBindResult startInputOrWindowGainedFocus(
2233 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2234 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002235 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002236 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002237 if (windowToken != null) {
2238 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002239 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002240 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002241 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2242 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002243 }
2244 }
2245
2246 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002247 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2248 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002249 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2250 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002251 // Needs to check the validity before clearing calling identity
2252 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002253 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 long ident = Binder.clearCallingIdentity();
2255 try {
2256 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002257 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2258 + InputMethodClient.getStartInputReason(startInputReason)
2259 + " client=" + client.asBinder()
2260 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002261 + " missingMethods="
2262 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002263 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002264 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
Dianne Hackborn7663d802012-02-24 13:08:49 -08002268 ClientState cs = mClients.get(client.asBinder());
2269 if (cs == null) {
2270 throw new IllegalArgumentException("unknown client "
2271 + client.asBinder());
2272 }
2273
2274 try {
2275 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2276 // Check with the window manager to make sure this client actually
2277 // has a window with focus. If not, reject. This is thread safe
2278 // because if the focus changes some time before or after, the
2279 // next client receiving focus that has any interest in input will
2280 // be calling through here after that change happens.
2281 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2282 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2283 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002287
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002288 if (!calledFromValidUser) {
2289 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2290 Slog.w(TAG, "If you want to interect with IME, you need "
2291 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2292 hideCurrentInputLocked(0, null);
2293 return null;
2294 }
2295
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002296 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002297 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002298 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002299 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002300 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2301 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002302 }
2303 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002304 }
2305 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002306 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002307
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002308 // Should we auto-show the IME even if the caller has not
2309 // specified what should be done with it?
2310 // We only do this automatically if the window can resize
2311 // to accommodate the IME (so what the user sees will give
2312 // them good context without input information being obscured
2313 // by the IME) or if running on a large screen where there
2314 // is more room for the target window + IME.
2315 final boolean doAutoShow =
2316 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2317 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2318 || mRes.getConfiguration().isLayoutSizeAtLeast(
2319 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002320 final boolean isTextEditor =
2321 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2322
2323 // We want to start input before showing the IME, but after closing
2324 // it. We want to do this after closing it to help the IME disappear
2325 // more quickly (not get stuck behind it initializing itself for the
2326 // new focused input, even if its window wants to hide the IME).
2327 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2330 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002331 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2333 // There is no focus view, and this window will
2334 // be behind any soft input window, so hide the
2335 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002336 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002337 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002339 } else if (isTextEditor && doAutoShow && (softInputMode &
2340 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 // There is a focus view, and we are navigating forward
2342 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002343 // We only do this automatically if the window can resize
2344 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002345 // them good context without input information being obscured
2346 // by the IME) or if running on a large screen where there
2347 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002348 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002349 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002350 res = startInputUncheckedLocked(cs, inputContext,
2351 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002352 didStart = true;
2353 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002354 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356 break;
2357 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2358 // Do nothing.
2359 break;
2360 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2361 if ((softInputMode &
2362 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002363 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002364 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 break;
2367 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002368 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002369 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 break;
2371 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2372 if ((softInputMode &
2373 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002374 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002375 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002376 res = startInputUncheckedLocked(cs, inputContext,
2377 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002378 didStart = true;
2379 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002380 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 }
2382 break;
2383 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002384 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002385 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002386 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2387 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388 didStart = true;
2389 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002390 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 break;
2392 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002393
2394 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002395 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002396 controlFlags);
2397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
2399 } finally {
2400 Binder.restoreCallingIdentity(ident);
2401 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002402
2403 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002405
satok42c5a162011-05-26 16:46:14 +09002406 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002407 public void showInputMethodPickerFromClient(
2408 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002409 if (!calledFromValidUser()) {
2410 return;
2411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 synchronized (mMethodMap) {
2413 if (mCurClient == null || client == null
2414 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002415 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002416 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 }
2418
satok440aab52010-11-25 09:43:11 +09002419 // Always call subtype picker, because subtype picker is a superset of input method
2420 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002421 mHandler.sendMessage(mCaller.obtainMessageI(
2422 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002423 }
2424 }
2425
satok42c5a162011-05-26 16:46:14 +09002426 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002428 if (!calledFromValidUser()) {
2429 return;
2430 }
satok28203512010-11-24 11:06:49 +09002431 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2432 }
2433
satok42c5a162011-05-26 16:46:14 +09002434 @Override
satok28203512010-11-24 11:06:49 +09002435 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002436 if (!calledFromValidUser()) {
2437 return;
2438 }
satok28203512010-11-24 11:06:49 +09002439 synchronized (mMethodMap) {
2440 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002441 setInputMethodWithSubtypeIdLocked(token, id,
2442 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2443 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002444 } else {
2445 setInputMethod(token, id);
2446 }
2447 }
satokab751aa2010-09-14 19:17:36 +09002448 }
2449
satok42c5a162011-05-26 16:46:14 +09002450 @Override
satokb416a712010-11-25 20:42:14 +09002451 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002452 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002453 if (!calledFromValidUser()) {
2454 return;
2455 }
satokb416a712010-11-25 20:42:14 +09002456 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002457 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2458 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002459 }
2460 }
2461
satok4fc87d62011-05-20 16:13:43 +09002462 @Override
satok735cf382010-11-11 20:40:09 +09002463 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002464 if (!calledFromValidUser()) {
2465 return false;
2466 }
satok735cf382010-11-11 20:40:09 +09002467 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002468 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002469 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002470 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002471 lastImi = mMethodMap.get(lastIme.first);
2472 } else {
2473 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002474 }
satok4fc87d62011-05-20 16:13:43 +09002475 String targetLastImiId = null;
2476 int subtypeId = NOT_A_SUBTYPE_ID;
2477 if (lastIme != null && lastImi != null) {
2478 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002479 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002480 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2481 : mCurrentSubtype.hashCode();
2482 // If the last IME is the same as the current IME and the last subtype is not
2483 // defined, there is no need to switch to the last IME.
2484 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2485 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002486 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002487 }
2488 }
2489
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002490 if (TextUtils.isEmpty(targetLastImiId)
2491 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002492 // This is a safety net. If the currentSubtype can't be added to the history
2493 // and the framework couldn't find the last ime, we will make the last ime be
2494 // the most applicable enabled keyboard subtype of the system imes.
2495 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2496 if (enabled != null) {
2497 final int N = enabled.size();
2498 final String locale = mCurrentSubtype == null
2499 ? mRes.getConfiguration().locale.toString()
2500 : mCurrentSubtype.getLocale();
2501 for (int i = 0; i < N; ++i) {
2502 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002503 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002504 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002505 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2506 InputMethodUtils.getSubtypes(imi),
2507 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002508 if (keyboardSubtype != null) {
2509 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002510 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002511 imi, keyboardSubtype.hashCode());
2512 if(keyboardSubtype.getLocale().equals(locale)) {
2513 break;
2514 }
2515 }
2516 }
2517 }
2518 }
2519 }
2520
2521 if (!TextUtils.isEmpty(targetLastImiId)) {
2522 if (DEBUG) {
2523 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2524 + ", from: " + mCurMethodId + ", " + subtypeId);
2525 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002526 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002527 return true;
2528 } else {
2529 return false;
2530 }
satok735cf382010-11-11 20:40:09 +09002531 }
2532 }
2533
satoke7c6998e2011-06-03 17:57:59 +09002534 @Override
satok688bd472012-02-09 20:09:17 +09002535 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 if (!calledFromValidUser()) {
2537 return false;
2538 }
satok688bd472012-02-09 20:09:17 +09002539 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002540 if (!calledWithValidToken(token)) {
2541 final int uid = Binder.getCallingUid();
2542 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2543 + " token:" + token);
2544 return false;
2545 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002546 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002547 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2548 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002549 if (nextSubtype == null) {
2550 return false;
2551 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002552 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2553 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002554 return true;
2555 }
2556 }
2557
2558 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002559 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2560 if (!calledFromValidUser()) {
2561 return false;
2562 }
2563 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002564 if (!calledWithValidToken(token)) {
2565 final int uid = Binder.getCallingUid();
2566 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2567 + "token. uid:" + uid + " token:" + token);
2568 return false;
2569 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002570 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002571 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2572 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002573 if (nextSubtype == null) {
2574 return false;
2575 }
2576 return true;
2577 }
2578 }
2579
2580 @Override
satok68f1b782011-04-11 14:26:04 +09002581 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002582 if (!calledFromValidUser()) {
2583 return null;
2584 }
satok68f1b782011-04-11 14:26:04 +09002585 synchronized (mMethodMap) {
2586 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2587 // TODO: Handle the case of the last IME with no subtypes
2588 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2589 || TextUtils.isEmpty(lastIme.second)) return null;
2590 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2591 if (lastImi == null) return null;
2592 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002593 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002594 final int lastSubtypeId =
2595 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002596 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2597 return null;
2598 }
2599 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002600 } catch (NumberFormatException e) {
2601 return null;
2602 }
2603 }
2604 }
2605
satoke7c6998e2011-06-03 17:57:59 +09002606 @Override
satokee5e77c2011-09-02 18:50:15 +09002607 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 if (!calledFromValidUser()) {
2609 return;
2610 }
satok91e88122011-07-18 11:11:42 +09002611 // By this IPC call, only a process which shares the same uid with the IME can add
2612 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002613 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002614 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002615 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002616 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002617 final String[] packageInfos;
2618 try {
2619 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2620 } catch (RemoteException e) {
2621 Slog.e(TAG, "Failed to get package infos");
2622 return;
2623 }
satok91e88122011-07-18 11:11:42 +09002624 if (packageInfos != null) {
2625 final int packageNum = packageInfos.length;
2626 for (int i = 0; i < packageNum; ++i) {
2627 if (packageInfos[i].equals(imi.getPackageName())) {
2628 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002629 final long ident = Binder.clearCallingIdentity();
2630 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002631 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002632 } finally {
2633 Binder.restoreCallingIdentity(ident);
2634 }
satokee5e77c2011-09-02 18:50:15 +09002635 return;
satok91e88122011-07-18 11:11:42 +09002636 }
2637 }
2638 }
satoke7c6998e2011-06-03 17:57:59 +09002639 }
satokee5e77c2011-09-02 18:50:15 +09002640 return;
satoke7c6998e2011-06-03 17:57:59 +09002641 }
2642
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002643 @Override
2644 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002645 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002646 }
2647
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002648 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002649 public void clearLastInputMethodWindowForTransition(IBinder token) {
2650 if (!calledFromValidUser()) {
2651 return;
2652 }
2653 final long ident = Binder.clearCallingIdentity();
2654 try {
2655 synchronized (mMethodMap) {
2656 if (!calledWithValidToken(token)) {
2657 final int uid = Binder.getCallingUid();
2658 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2659 + "invalid token. uid:" + uid + " token:" + token);
2660 return;
2661 }
2662 }
2663 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2664 } finally {
2665 Binder.restoreCallingIdentity(ident);
2666 }
2667 }
2668
2669 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002670 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002671 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002672 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002673 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002674 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002675 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2676 if (DEBUG) {
2677 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2678 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2679 + " actual: " + sequenceNumber);
2680 }
2681 return;
2682 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002683 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2684 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002685 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002686 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002687 }
2688 }
2689
satok28203512010-11-24 11:06:49 +09002690 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002692 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2693 }
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002696 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2697 if (token == null) {
2698 if (mContext.checkCallingOrSelfPermission(
2699 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2700 != PackageManager.PERMISSION_GRANTED) {
2701 throw new SecurityException(
2702 "Using null token requires permission "
2703 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002705 } else if (mCurToken != token) {
2706 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2707 + " token: " + token);
2708 return;
2709 }
2710
2711 final long ident = Binder.clearCallingIdentity();
2712 try {
2713 setInputMethodLocked(id, subtypeId);
2714 } finally {
2715 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
2717 }
2718
satok42c5a162011-05-26 16:46:14 +09002719 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002721 if (!calledFromValidUser()) {
2722 return;
2723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002725 if (!calledWithValidToken(token)) {
2726 final int uid = Binder.getCallingUid();
2727 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2728 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 return;
2730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 long ident = Binder.clearCallingIdentity();
2732 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002733 hideCurrentInputLocked(flags, null);
2734 } finally {
2735 Binder.restoreCallingIdentity(ident);
2736 }
2737 }
2738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002739
satok42c5a162011-05-26 16:46:14 +09002740 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002741 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002742 if (!calledFromValidUser()) {
2743 return;
2744 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002745 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002746 if (!calledWithValidToken(token)) {
2747 final int uid = Binder.getCallingUid();
2748 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2749 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002750 return;
2751 }
2752 long ident = Binder.clearCallingIdentity();
2753 try {
2754 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 } finally {
2756 Binder.restoreCallingIdentity(ident);
2757 }
2758 }
2759 }
2760
2761 void setEnabledSessionInMainThread(SessionState session) {
2762 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002763 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002765 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002766 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 } catch (RemoteException e) {
2768 }
2769 }
2770 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002771 if (mEnabledSession != null && mEnabledSession.session != null) {
2772 try {
2773 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2774 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2775 } catch (RemoteException e) {
2776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
2778 }
2779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002780
satok42c5a162011-05-26 16:46:14 +09002781 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002783 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002785 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002786 final boolean showAuxSubtypes;
2787 switch (msg.arg1) {
2788 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2789 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2790 // implemented so that auxiliary subtypes will be excluded when the soft
2791 // keyboard is invisible.
2792 showAuxSubtypes = mInputShown;
2793 break;
2794 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2795 showAuxSubtypes = true;
2796 break;
2797 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2798 showAuxSubtypes = false;
2799 break;
2800 default:
2801 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2802 return false;
2803 }
2804 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002805 return true;
2806
satok47a44912010-10-06 16:03:58 +09002807 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002808 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002809 return true;
2810
2811 case MSG_SHOW_IM_CONFIG:
2812 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002813 return true;
2814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 case MSG_UNBIND_INPUT:
2818 try {
2819 ((IInputMethod)msg.obj).unbindInput();
2820 } catch (RemoteException e) {
2821 // There is nothing interesting about the method dying.
2822 }
2823 return true;
2824 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002825 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 try {
2827 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2828 } catch (RemoteException e) {
2829 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002830 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 return true;
2832 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002835 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002836 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002837 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 } catch (RemoteException e) {
2839 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002840 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 return true;
2842 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002845 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002846 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002847 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 } catch (RemoteException e) {
2849 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002852 case MSG_HIDE_CURRENT_INPUT_METHOD:
2853 synchronized (mMethodMap) {
2854 hideCurrentInputLocked(0, null);
2855 }
2856 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002858 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002860 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2862 } catch (RemoteException e) {
2863 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002866 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002867 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002868 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002869 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002871 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002873 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002874 // Dispose the channel if the input method is not local to this process
2875 // because the remote proxy will get its own copy when unparceled.
2876 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002877 channel.dispose();
2878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002880 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002884
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002885 case MSG_START_INPUT: {
2886 int missingMethods = msg.arg1;
2887 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002889 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002891 session.method.startInput((IInputContext) args.arg2, missingMethods,
2892 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 } catch (RemoteException e) {
2894 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002895 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002897 }
2898 case MSG_RESTART_INPUT: {
2899 int missingMethods = msg.arg1;
2900 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002902 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002904 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2905 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } catch (RemoteException e) {
2907 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002908 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002913
Yohei Yukawa33e81792015-11-17 21:14:42 -08002914 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002916 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 } catch (RemoteException e) {
2918 // There is nothing interesting about the last client dying.
2919 }
2920 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002921 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002922 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002923 IInputMethodClient client = (IInputMethodClient)args.arg1;
2924 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002926 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002928 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002929 } finally {
2930 // Dispose the channel if the input method is not local to this process
2931 // because the remote proxy will get its own copy when unparceled.
2932 if (res.channel != null && Binder.isProxy(client)) {
2933 res.channel.dispose();
2934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002936 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002938 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002939 case MSG_SET_ACTIVE:
2940 try {
2941 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2942 } catch (RemoteException e) {
2943 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2944 + ((ClientState)msg.obj).pid + " uid "
2945 + ((ClientState)msg.obj).uid);
2946 }
2947 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002948 case MSG_SET_INTERACTIVE:
2949 handleSetInteractive(msg.arg1 != 0);
2950 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002951 case MSG_SWITCH_IME:
2952 handleSwitchInputMethod(msg.arg1 != 0);
2953 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002954 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2955 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002956 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002957 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002958 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002959 } catch (RemoteException e) {
2960 Slog.w(TAG, "Got RemoteException sending "
2961 + "setUserActionNotificationSequenceNumber("
2962 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002963 + clientState.pid + " uid "
2964 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002965 }
2966 return true;
2967 }
satok01038492012-04-09 21:08:27 +09002968
2969 // --------------------------------------------------------------
2970 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002971 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002972 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 }
2974 return false;
2975 }
2976
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002977 private void handleSetInteractive(final boolean interactive) {
2978 synchronized (mMethodMap) {
2979 mIsInteractive = interactive;
2980 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2981
2982 // Inform the current client of the change in active status
2983 if (mCurClient != null && mCurClient.client != null) {
2984 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2985 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2986 }
2987 }
2988 }
2989
Yohei Yukawaae61f712015-12-09 13:00:10 -08002990 private void handleSwitchInputMethod(final boolean forwardDirection) {
2991 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002992 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002993 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002994 if (nextSubtype == null) {
2995 return;
2996 }
2997 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002998 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2999 if (newInputMethodInfo == null) {
3000 return;
3001 }
3002 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3003 newInputMethodInfo, mCurrentSubtype);
3004 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003005 if (mSubtypeSwitchedByShortCutToast == null) {
3006 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3007 Toast.LENGTH_SHORT);
3008 } else {
3009 mSubtypeSwitchedByShortCutToast.setText(toastText);
3010 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003011 mSubtypeSwitchedByShortCutToast.show();
3012 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003013 }
3014 }
3015
satokdc9ddae2011-10-06 12:22:36 +09003016 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003017 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3018 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003019 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003020 if (DEBUG) {
3021 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3022 }
satok723a27e2010-11-11 14:58:11 +09003023 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003024 return true;
3025 }
3026
3027 return false;
3028 }
3029
Yohei Yukawa94e33302016-02-12 19:37:03 -08003030 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003031 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003032 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003033 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003034 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003035 mMethodList.clear();
3036 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003037
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 // Use for queryIntentServicesAsUser
3039 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003041 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3042 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3043 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003044 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003046 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3047 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003048
satoke7c6998e2011-06-03 17:57:59 +09003049 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3050 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 for (int i = 0; i < services.size(); ++i) {
3052 ResolveInfo ri = services.get(i);
3053 ServiceInfo si = ri.serviceInfo;
3054 ComponentName compName = new ComponentName(si.packageName, si.name);
3055 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3056 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003057 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 + ": it does not require the permission "
3059 + android.Manifest.permission.BIND_INPUT_METHOD);
3060 continue;
3061 }
3062
Joe Onorato8a9b2202010-02-26 18:56:32 -08003063 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064
3065 try {
satoke7c6998e2011-06-03 17:57:59 +09003066 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003067 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003068 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003069 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070
3071 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003072 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003074 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003075 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003078
Yohei Yukawa859df052016-02-17 07:56:46 -08003079 // TODO: The following code should find better place to live.
3080 if (!resetDefaultEnabledIme) {
3081 boolean enabledImeFound = false;
3082 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3083 final int N = enabledImes.size();
3084 for (int i = 0; i < N; ++i) {
3085 final InputMethodInfo imi = enabledImes.get(i);
3086 if (mMethodList.contains(imi)) {
3087 enabledImeFound = true;
3088 break;
3089 }
3090 }
3091 if (!enabledImeFound) {
3092 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3093 resetDefaultEnabledIme = true;
3094 resetSelectedInputMethodAndSubtypeLocked("");
3095 }
3096 }
3097
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003098 if (resetDefaultEnabledIme) {
3099 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003100 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003101 final int N = defaultEnabledIme.size();
3102 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003103 final InputMethodInfo imi = defaultEnabledIme.get(i);
3104 if (DEBUG) {
3105 Slog.d(TAG, "--- enable ime = " + imi);
3106 }
3107 setInputMethodEnabledLocked(imi.getId(), true);
3108 }
3109 }
3110
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003111 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003112 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003113 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003114 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3115 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003116 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003117 }
3118 } else {
3119 // Double check that the default IME is certainly enabled.
3120 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003121 }
3122 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003123 // Here is not the perfect place to reset the switching controller. Ideally
3124 // mSwitchingController and mSettings should be able to share the same state.
3125 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3126 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003127 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003131
satok217f5482010-12-15 05:19:19 +09003132 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003133 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003134 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003135 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3136 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003137 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003138 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003139 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003140 final int userId;
3141 synchronized (mMethodMap) {
3142 userId = mSettings.getCurrentUserId();
3143 }
3144 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003145 }
3146
3147 private void showConfigureInputMethods() {
3148 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3149 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3150 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3151 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003152 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003153 }
3154
satok2c93efc2012-04-02 19:33:47 +09003155 private boolean isScreenLocked() {
3156 return mKeyguardManager != null
3157 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3158 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003159
Seigo Nonaka14e13912015-05-06 21:04:13 -07003160 private void showInputMethodMenu(boolean showAuxSubtypes) {
3161 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003164 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003165
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003166 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003167 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003168 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003169
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003170 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003171 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003172 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3173 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003174 if (immis == null || immis.size() == 0) {
3175 return;
3176 }
3177
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003178 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179
satok688bd472012-02-09 20:09:17 +09003180 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003181 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003182 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003183
satokc3690562012-01-10 20:14:43 +09003184 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003185 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003186 if (currentSubtype != null) {
3187 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003188 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3189 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003190 }
3191 }
3192
Ken Wakasa761eb372011-03-04 19:06:18 +09003193 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003194 mIms = new InputMethodInfo[N];
3195 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003196 int checkedItem = 0;
3197 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003198 final ImeSubtypeListItem item = imList.get(i);
3199 mIms[i] = item.mImi;
3200 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003201 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003202 int subtypeId = mSubtypeIds[i];
3203 if ((subtypeId == NOT_A_SUBTYPE_ID)
3204 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3205 || (subtypeId == lastInputMethodSubtypeId)) {
3206 checkedItem = i;
3207 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003210
3211 final Context settingsContext = new ContextThemeWrapper(context,
3212 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3213
3214 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003215 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3216 @Override
3217 public void onCancel(DialogInterface dialog) {
3218 hideInputMethodMenu();
3219 }
3220 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003221
3222 final Context dialogContext = mDialogBuilder.getContext();
3223 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3224 com.android.internal.R.styleable.DialogPreference,
3225 com.android.internal.R.attr.alertDialogStyle, 0);
3226 final Drawable dialogIcon = a.getDrawable(
3227 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3228 a.recycle();
3229
3230 mDialogBuilder.setIcon(dialogIcon);
3231
Yohei Yukawad34e1482016-02-11 08:03:52 -08003232 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003233 final View tv = inflater.inflate(
3234 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3235 mDialogBuilder.setCustomTitle(tv);
3236
3237 // Setup layout for a toggle switch of the hardware keyboard
3238 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003239 mSwitchingDialogTitleView
3240 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003241 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003242 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003243 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003244 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003245 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003246 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3247 @Override
3248 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003249 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250 // Ensure that the input method dialog is dismissed when changing
3251 // the hardware keyboard state.
3252 hideInputMethodMenu();
3253 }
3254 });
3255
Alan Viverette505e3ab2014-11-24 15:22:11 -08003256 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3258 final OnClickListener choiceListener = new OnClickListener() {
3259 @Override
3260 public void onClick(final DialogInterface dialog, final int which) {
3261 synchronized (mMethodMap) {
3262 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3263 || mSubtypeIds.length <= which) {
3264 return;
satok01038492012-04-09 21:08:27 +09003265 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003266 final InputMethodInfo im = mIms[which];
3267 int subtypeId = mSubtypeIds[which];
3268 adapter.mCheckedItem = which;
3269 adapter.notifyDataSetChanged();
3270 hideInputMethodMenu();
3271 if (im != null) {
3272 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3273 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003274 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003275 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003277 }
3278 }
3279 };
3280 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003283 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 mSwitchingDialog.getWindow().setType(
3285 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003286 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3287 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003288 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003289 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 mSwitchingDialog.show();
3291 }
3292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003293
Ken Wakasa05dbb652011-08-22 15:22:43 +09003294 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3295 private final LayoutInflater mInflater;
3296 private final int mTextViewResourceId;
3297 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003298 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003299 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3300 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3301 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003302
Ken Wakasa05dbb652011-08-22 15:22:43 +09003303 mTextViewResourceId = textViewResourceId;
3304 mItemsList = itemsList;
3305 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003306 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003307 }
3308
3309 @Override
3310 public View getView(int position, View convertView, ViewGroup parent) {
3311 final View view = convertView != null ? convertView
3312 : mInflater.inflate(mTextViewResourceId, null);
3313 if (position < 0 || position >= mItemsList.size()) return view;
3314 final ImeSubtypeListItem item = mItemsList.get(position);
3315 final CharSequence imeName = item.mImeName;
3316 final CharSequence subtypeName = item.mSubtypeName;
3317 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3318 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3319 if (TextUtils.isEmpty(subtypeName)) {
3320 firstTextView.setText(imeName);
3321 secondTextView.setVisibility(View.GONE);
3322 } else {
3323 firstTextView.setText(subtypeName);
3324 secondTextView.setText(imeName);
3325 secondTextView.setVisibility(View.VISIBLE);
3326 }
3327 final RadioButton radioButton =
3328 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3329 radioButton.setChecked(position == mCheckedItem);
3330 return view;
3331 }
3332 }
3333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003335 synchronized (mMethodMap) {
3336 hideInputMethodMenuLocked();
3337 }
3338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003339
The Android Open Source Project10592532009-03-18 17:39:46 -07003340 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003341 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342
The Android Open Source Project10592532009-03-18 17:39:46 -07003343 if (mSwitchingDialog != null) {
3344 mSwitchingDialog.dismiss();
3345 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003347
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003348 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003349 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003350 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003354
satok42c5a162011-05-26 16:46:14 +09003355 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003357 // TODO: Make this work even for non-current users?
3358 if (!calledFromValidUser()) {
3359 return false;
3360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 synchronized (mMethodMap) {
3362 if (mContext.checkCallingOrSelfPermission(
3363 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3364 != PackageManager.PERMISSION_GRANTED) {
3365 throw new SecurityException(
3366 "Requires permission "
3367 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3368 }
Anna Galusza9b278112016-01-04 11:37:37 -08003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 long ident = Binder.clearCallingIdentity();
3371 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003372 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 } finally {
3374 Binder.restoreCallingIdentity(ident);
3375 }
3376 }
3377 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003378
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003379 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3380 // Make sure this is a valid input method.
3381 InputMethodInfo imm = mMethodMap.get(id);
3382 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003383 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003384 }
3385
satokd87c2592010-09-29 11:52:06 +09003386 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3387 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003388
satokd87c2592010-09-29 11:52:06 +09003389 if (enabled) {
3390 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3391 if (pair.first.equals(id)) {
3392 // We are enabling this input method, but it is already enabled.
3393 // Nothing to do. The previous state was enabled.
3394 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003395 }
3396 }
satokd87c2592010-09-29 11:52:06 +09003397 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3398 // Previous state was disabled.
3399 return false;
3400 } else {
3401 StringBuilder builder = new StringBuilder();
3402 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3403 builder, enabledInputMethodsList, id)) {
3404 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003405 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003406 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3407 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3408 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003409 }
3410 // Previous state was enabled.
3411 return true;
3412 } else {
3413 // We are disabling the input method but it is already disabled.
3414 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003415 return false;
3416 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003417 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003418 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003419
satok723a27e2010-11-11 14:58:11 +09003420 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3421 boolean setSubtypeOnly) {
3422 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003424
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003425 mCurUserActionNotificationSequenceNumber =
3426 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3427 if (DEBUG) {
3428 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3429 + mCurUserActionNotificationSequenceNumber);
3430 }
3431
3432 if (mCurClient != null && mCurClient.client != null) {
3433 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3434 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003435 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003436 }
3437
satok723a27e2010-11-11 14:58:11 +09003438 // Set Subtype here
3439 if (imi == null || subtypeId < 0) {
3440 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003441 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003442 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003443 if (subtypeId < imi.getSubtypeCount()) {
3444 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3445 mSettings.putSelectedSubtype(subtype.hashCode());
3446 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003447 } else {
3448 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003449 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003451 }
satokab751aa2010-09-14 19:17:36 +09003452 }
satok723a27e2010-11-11 14:58:11 +09003453
Yohei Yukawa68645a62016-02-17 07:54:20 -08003454 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003455 // Set InputMethod here
3456 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3457 }
3458 }
3459
3460 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3461 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3462 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3463 // newDefaultIme is empty when there is no candidate for the selected IME.
3464 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3465 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3466 if (subtypeHashCode != null) {
3467 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003468 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003469 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003470 } catch (NumberFormatException e) {
3471 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3472 }
3473 }
3474 }
3475 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003476 }
3477
satok4e4569d2010-11-19 18:45:53 +09003478 // If there are no selected shortcuts, tries finding the most applicable ones.
3479 private Pair<InputMethodInfo, InputMethodSubtype>
3480 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3481 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3482 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003483 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003484 boolean foundInSystemIME = false;
3485
3486 // Search applicable subtype for each InputMethodInfo
3487 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003488 final String imiId = imi.getId();
3489 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3490 continue;
3491 }
satokcd7cd292010-11-20 15:46:23 +09003492 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003493 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003494 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003495 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003496 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003497 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003498 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003499 }
satokdf31ae62011-01-15 06:19:44 +09003500 // 2. Search by the system locale from enabledSubtypes.
3501 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003502 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003503 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003504 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003505 }
satoka86f5e42011-09-02 17:12:42 +09003506 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003507 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003508 final ArrayList<InputMethodSubtype> subtypesForSearch =
3509 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003510 ? InputMethodUtils.getSubtypes(imi)
3511 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003512 // 4. Search by the current subtype's locale from all subtypes.
3513 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003514 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003515 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003516 }
3517 // 5. Search by the system locale from all subtypes.
3518 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003519 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003520 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003521 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003522 }
satokcd7cd292010-11-20 15:46:23 +09003523 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003524 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003525 // The current input method is the most applicable IME.
3526 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003527 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003528 break;
satok7599a7f2010-12-22 13:45:23 +09003529 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003530 // The system input method is 2nd applicable IME.
3531 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003532 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003533 if ((imi.getServiceInfo().applicationInfo.flags
3534 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3535 foundInSystemIME = true;
3536 }
satok4e4569d2010-11-19 18:45:53 +09003537 }
3538 }
3539 }
3540 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003541 if (mostApplicableIMI != null) {
3542 Slog.w(TAG, "Most applicable shortcut input method was:"
3543 + mostApplicableIMI.getId());
3544 if (mostApplicableSubtype != null) {
3545 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3546 + "," + mostApplicableSubtype.getMode() + ","
3547 + mostApplicableSubtype.getLocale());
3548 }
3549 }
satok4e4569d2010-11-19 18:45:53 +09003550 }
satokcd7cd292010-11-20 15:46:23 +09003551 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003552 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003553 } else {
3554 return null;
3555 }
3556 }
3557
satokab751aa2010-09-14 19:17:36 +09003558 /**
3559 * @return Return the current subtype of this input method.
3560 */
satok42c5a162011-05-26 16:46:14 +09003561 @Override
satokab751aa2010-09-14 19:17:36 +09003562 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003563 // TODO: Make this work even for non-current users?
3564 if (!calledFromValidUser()) {
3565 return null;
3566 }
3567 synchronized (mMethodMap) {
3568 return getCurrentInputMethodSubtypeLocked();
3569 }
3570 }
3571
3572 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003573 if (mCurMethodId == null) {
3574 return null;
3575 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003576 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003577 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3578 if (imi == null || imi.getSubtypeCount() == 0) {
3579 return null;
satok4e4569d2010-11-19 18:45:53 +09003580 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003581 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003582 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3583 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003584 if (subtypeId == NOT_A_SUBTYPE_ID) {
3585 // If there are no selected subtypes, the framework will try to find
3586 // the most applicable subtype from explicitly or implicitly enabled
3587 // subtypes.
3588 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003590 // If there is only one explicitly or implicitly enabled subtype,
3591 // just returns it.
3592 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3593 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3594 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003595 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003596 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003597 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003598 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003599 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003600 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3601 true);
satok4e4569d2010-11-19 18:45:53 +09003602 }
satok3ef8b292010-11-23 06:06:29 +09003603 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003604 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003605 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003606 }
3607 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003608 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003609 }
3610
satok4e4569d2010-11-19 18:45:53 +09003611 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003612 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003613 @Override
satok4e4569d2010-11-19 18:45:53 +09003614 public List getShortcutInputMethodsAndSubtypes() {
3615 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003616 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003617 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003618 // If there are no selected shortcut subtypes, the framework will try to find
3619 // the most applicable subtype from all subtypes whose mode is
3620 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003621 Pair<InputMethodInfo, InputMethodSubtype> info =
3622 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003623 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003624 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003625 ret.add(info.first);
3626 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003627 }
satok3da92232011-01-11 22:46:30 +09003628 return ret;
satokf3db1af2010-11-23 13:34:33 +09003629 }
satokf3db1af2010-11-23 13:34:33 +09003630 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3631 ret.add(imi);
3632 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3633 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003634 }
3635 }
satokf3db1af2010-11-23 13:34:33 +09003636 return ret;
satok4e4569d2010-11-19 18:45:53 +09003637 }
3638 }
3639
satok42c5a162011-05-26 16:46:14 +09003640 @Override
satokb66d2872010-11-10 01:04:04 +09003641 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003642 // TODO: Make this work even for non-current users?
3643 if (!calledFromValidUser()) {
3644 return false;
3645 }
satokb66d2872010-11-10 01:04:04 +09003646 synchronized (mMethodMap) {
3647 if (subtype != null && mCurMethodId != null) {
3648 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003649 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003650 if (subtypeId != NOT_A_SUBTYPE_ID) {
3651 setInputMethodLocked(mCurMethodId, subtypeId);
3652 return true;
3653 }
3654 }
3655 return false;
3656 }
3657 }
3658
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003659 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003660 private static class InputMethodFileManager {
3661 private static final String SYSTEM_PATH = "system";
3662 private static final String INPUT_METHOD_PATH = "inputmethod";
3663 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3664 private static final String NODE_SUBTYPES = "subtypes";
3665 private static final String NODE_SUBTYPE = "subtype";
3666 private static final String NODE_IMI = "imi";
3667 private static final String ATTR_ID = "id";
3668 private static final String ATTR_LABEL = "label";
3669 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003670 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003671 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003672 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003673 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3674 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3675 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003676 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003677 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3678 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003679 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003680 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003681 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003682 if (methodMap == null) {
3683 throw new NullPointerException("methodMap is null");
3684 }
3685 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003686 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003687 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3688 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003689 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003690 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003691 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3692 }
3693 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3694 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3695 if (!subtypeFile.exists()) {
3696 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003697 writeAdditionalInputMethodSubtypes(
3698 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003699 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003700 readAdditionalInputMethodSubtypes(
3701 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003702 }
3703 }
3704
3705 private void deleteAllInputMethodSubtypes(String imiId) {
3706 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003707 mAdditionalSubtypesMap.remove(imiId);
3708 writeAdditionalInputMethodSubtypes(
3709 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003710 }
3711 }
3712
3713 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003714 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003715 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003716 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003717 final int N = additionalSubtypes.length;
3718 for (int i = 0; i < N; ++i) {
3719 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003720 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003721 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003722 } else {
3723 Slog.w(TAG, "Duplicated subtype definition found: "
3724 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003725 }
3726 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003727 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3728 writeAdditionalInputMethodSubtypes(
3729 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003730 }
3731 }
3732
3733 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3734 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003735 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003736 }
3737 }
3738
3739 private static void writeAdditionalInputMethodSubtypes(
3740 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3741 HashMap<String, InputMethodInfo> methodMap) {
3742 // Safety net for the case that this function is called before methodMap is set.
3743 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3744 FileOutputStream fos = null;
3745 try {
3746 fos = subtypesFile.startWrite();
3747 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003748 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003749 out.startDocument(null, true);
3750 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3751 out.startTag(null, NODE_SUBTYPES);
3752 for (String imiId : allSubtypes.keySet()) {
3753 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3754 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3755 continue;
3756 }
3757 out.startTag(null, NODE_IMI);
3758 out.attribute(null, ATTR_ID, imiId);
3759 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3760 final int N = subtypesList.size();
3761 for (int i = 0; i < N; ++i) {
3762 final InputMethodSubtype subtype = subtypesList.get(i);
3763 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003764 if (subtype.hasSubtypeId()) {
3765 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3766 String.valueOf(subtype.getSubtypeId()));
3767 }
satoke7c6998e2011-06-03 17:57:59 +09003768 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3769 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3770 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003771 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3772 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003773 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3774 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3775 out.attribute(null, ATTR_IS_AUXILIARY,
3776 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003777 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3778 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003779 out.endTag(null, NODE_SUBTYPE);
3780 }
3781 out.endTag(null, NODE_IMI);
3782 }
3783 out.endTag(null, NODE_SUBTYPES);
3784 out.endDocument();
3785 subtypesFile.finishWrite(fos);
3786 } catch (java.io.IOException e) {
3787 Slog.w(TAG, "Error writing subtypes", e);
3788 if (fos != null) {
3789 subtypesFile.failWrite(fos);
3790 }
3791 }
3792 }
3793
3794 private static void readAdditionalInputMethodSubtypes(
3795 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3796 if (allSubtypes == null || subtypesFile == null) return;
3797 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003798 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003799 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003800 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003801 int type = parser.getEventType();
3802 // Skip parsing until START_TAG
3803 while ((type = parser.next()) != XmlPullParser.START_TAG
3804 && type != XmlPullParser.END_DOCUMENT) {}
3805 String firstNodeName = parser.getName();
3806 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3807 throw new XmlPullParserException("Xml doesn't start with subtypes");
3808 }
3809 final int depth =parser.getDepth();
3810 String currentImiId = null;
3811 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3812 while (((type = parser.next()) != XmlPullParser.END_TAG
3813 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3814 if (type != XmlPullParser.START_TAG)
3815 continue;
3816 final String nodeName = parser.getName();
3817 if (NODE_IMI.equals(nodeName)) {
3818 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3819 if (TextUtils.isEmpty(currentImiId)) {
3820 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3821 continue;
3822 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003823 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003824 allSubtypes.put(currentImiId, tempSubtypesArray);
3825 } else if (NODE_SUBTYPE.equals(nodeName)) {
3826 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3827 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3828 continue;
3829 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003830 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003831 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003832 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003833 parser.getAttributeValue(null, ATTR_LABEL));
3834 final String imeSubtypeLocale =
3835 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003836 final String languageTag =
3837 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003838 final String imeSubtypeMode =
3839 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3840 final String imeSubtypeExtraValue =
3841 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003842 final boolean isAuxiliary = "1".equals(String.valueOf(
3843 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003844 final boolean isAsciiCapable = "1".equals(String.valueOf(
3845 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003846 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003847 .setSubtypeNameResId(label)
3848 .setSubtypeIconResId(icon)
3849 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003850 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003851 .setSubtypeMode(imeSubtypeMode)
3852 .setSubtypeExtraValue(imeSubtypeExtraValue)
3853 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003854 .setIsAsciiCapable(isAsciiCapable);
3855 final String subtypeIdString =
3856 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3857 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003858 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003859 }
3860 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003861 }
3862 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003863 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3864 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003865 return;
satoke7c6998e2011-06-03 17:57:59 +09003866 }
3867 }
3868 }
3869
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003870 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3871 @NonNull
3872 private final Handler mHandler;
3873
3874 LocalServiceImpl(@NonNull final Handler handler) {
3875 mHandler = handler;
3876 }
3877
3878 @Override
3879 public void setInteractive(boolean interactive) {
3880 // Do everything in handler so as not to block the caller.
3881 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3882 interactive ? 1 : 0, 0));
3883 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003884
3885 @Override
3886 public void switchInputMethod(boolean forwardDirection) {
3887 // Do everything in handler so as not to block the caller.
3888 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3889 forwardDirection ? 1 : 0, 0));
3890 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003891
3892 @Override
3893 public void hideCurrentInputMethod() {
3894 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3895 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3896 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003897 }
3898
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003899 private static String imeWindowStatusToString(final int imeWindowVis) {
3900 final StringBuilder sb = new StringBuilder();
3901 boolean first = true;
3902 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3903 sb.append("Active");
3904 first = false;
3905 }
3906 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3907 if (!first) {
3908 sb.append("|");
3909 }
3910 sb.append("Visible");
3911 }
3912 return sb.toString();
3913 }
3914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003916 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3917 @Nullable Uri contentUri, @Nullable String packageName) {
3918 if (!calledFromValidUser()) {
3919 return null;
3920 }
3921
3922 if (token == null) {
3923 throw new NullPointerException("token");
3924 }
3925 if (packageName == null) {
3926 throw new NullPointerException("packageName");
3927 }
3928 if (contentUri == null) {
3929 throw new NullPointerException("contentUri");
3930 }
3931 final String contentUriScheme = contentUri.getScheme();
3932 if (!"content".equals(contentUriScheme)) {
3933 throw new InvalidParameterException("contentUri must have content scheme");
3934 }
3935
3936 synchronized (mMethodMap) {
3937 final int uid = Binder.getCallingUid();
3938 if (mCurMethodId == null) {
3939 return null;
3940 }
3941 if (mCurToken != token) {
3942 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3943 + " token=" + token);
3944 return null;
3945 }
3946 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3947 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3948 // nature of our system. Let's compare it with our internal record.
3949 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3950 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3951 + mCurAttribute.packageName + " packageName=" + packageName);
3952 return null;
3953 }
3954 final int imeUserId = UserHandle.getUserId(uid);
3955 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3956 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3957 appUserId);
3958 }
3959 }
3960
3961 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3963 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3964 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3967 + Binder.getCallingPid()
3968 + ", uid=" + Binder.getCallingUid());
3969 return;
3970 }
3971
3972 IInputMethod method;
3973 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003974 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 synchronized (mMethodMap) {
3979 p.println("Current Input Method Manager state:");
3980 int N = mMethodList.size();
3981 p.println(" Input Methods:");
3982 for (int i=0; i<N; i++) {
3983 InputMethodInfo info = mMethodList.get(i);
3984 p.println(" InputMethod #" + i + ":");
3985 info.dump(p, " ");
3986 }
3987 p.println(" Clients:");
3988 for (ClientState ci : mClients.values()) {
3989 p.println(" Client " + ci + ":");
3990 p.println(" client=" + ci.client);
3991 p.println(" inputContext=" + ci.inputContext);
3992 p.println(" sessionRequested=" + ci.sessionRequested);
3993 p.println(" curSession=" + ci.curSession);
3994 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003995 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003997 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3998 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003999 focusedWindowClient = mCurFocusedWindowClient;
4000 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4002 + " mBoundToMethod=" + mBoundToMethod);
4003 p.println(" mCurToken=" + mCurToken);
4004 p.println(" mCurIntent=" + mCurIntent);
4005 method = mCurMethod;
4006 p.println(" mCurMethod=" + mCurMethod);
4007 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004008 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 p.println(" mShowRequested=" + mShowRequested
4010 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4011 + " mShowForced=" + mShowForced
4012 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004013 p.println(" mCurUserActionNotificationSequenceNumber="
4014 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004015 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004016 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004017 p.println(" mSwitchingController:");
4018 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004019 p.println(" mSettings:");
4020 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004022
Jeff Brownb88102f2010-09-08 11:49:43 -07004023 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 pw.flush();
4026 try {
4027 client.client.asBinder().dump(fd, args);
4028 } catch (RemoteException e) {
4029 p.println("Input method client dead: " + e);
4030 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004031 } else {
4032 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004034
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004035 if (focusedWindowClient != null && client != focusedWindowClient) {
4036 p.println(" ");
4037 p.println("Warning: Current input method client doesn't match the last focused. "
4038 + "window.");
4039 p.println("Dumping input method client in the last focused window just in case.");
4040 p.println(" ");
4041 pw.flush();
4042 try {
4043 focusedWindowClient.client.asBinder().dump(fd, args);
4044 } catch (RemoteException e) {
4045 p.println("Input method client in focused window dead: " + e);
4046 }
4047 }
4048
Jeff Brownb88102f2010-09-08 11:49:43 -07004049 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 pw.flush();
4052 try {
4053 method.asBinder().dump(fd, args);
4054 } catch (RemoteException e) {
4055 p.println("Input method service dead: " + e);
4056 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004057 } else {
4058 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 }
4060 }
4061}