blob: 42dd9a8da29c50fdd954a48e6be18cf4181e633a [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080033import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070041import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.ActivityManagerNative;
43import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070044import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090045import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090046import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070064import android.content.pm.PackageManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.pm.ResolveInfo;
66import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080067import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.res.Resources;
70import android.content.res.TypedArray;
71import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080072import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080073import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080074import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070075import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040077import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070078import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090079import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Handler;
81import android.os.IBinder;
82import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090083import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Message;
85import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070086import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080088import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.ServiceManager;
90import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090091import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080092import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.provider.Settings;
94import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090095import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070096import android.util.ArrayMap;
97import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070098import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900100import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900101import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.util.PrintWriterPrinter;
103import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900104import android.util.Slog;
105import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900106import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700108import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.view.LayoutInflater;
110import android.view.View;
111import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700113import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900114import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.inputmethod.InputBinding;
116import android.view.inputmethod.InputMethod;
117import android.view.inputmethod.InputMethodInfo;
118import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700119import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900120import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900121import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900123import android.widget.CompoundButton;
124import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900126import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900131import java.io.FileInputStream;
132import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.io.IOException;
134import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100135import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900137import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.HashMap;
139import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900140import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
142/**
143 * This class provides a system service that manages input methods.
144 */
145public class InputMethodManagerService extends IInputMethodManager.Stub
146 implements ServiceConnection, Handler.Callback {
147 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700148 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700149 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700151 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
152 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
153 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int MSG_UNBIND_INPUT = 1000;
156 static final int MSG_BIND_INPUT = 1010;
157 static final int MSG_SHOW_SOFT_INPUT = 1020;
158 static final int MSG_HIDE_SOFT_INPUT = 1030;
159 static final int MSG_ATTACH_TOKEN = 1040;
160 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_START_INPUT = 2000;
163 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
Yohei Yukawa33e81792015-11-17 21:14:42 -0800165 static final int MSG_UNBIND_CLIENT = 3000;
166 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700167 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700168 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900169 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satok01038492012-04-09 21:08:27 +0900171 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
172
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700173 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satokf9f01002011-05-19 21:31:50 +0900175 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
176
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900178 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179
satok4e4569d2010-11-19 18:45:53 +0900180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800182 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900184 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700187 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700189 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900190 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900191 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900192 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // All known input methods. mMethodMap also serves as the global
197 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700198 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
199 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900200 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700201 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900202 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 // Used to bring IME service up to visible adjustment while it is being shown.
205 final ServiceConnection mVisibleConnection = new ServiceConnection() {
206 @Override public void onServiceConnected(ComponentName name, IBinder service) {
207 }
208
209 @Override public void onServiceDisconnected(ComponentName name) {
210 }
211 };
212 boolean mVisibleBound = false;
213
satok7cfc0ed2011-06-20 21:29:36 +0900214 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700215 private NotificationManager mNotificationManager;
216 private KeyguardManager mKeyguardManager;
217 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700219 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900220 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900221 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900222 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900223
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900224 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 final ClientState client;
226 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227
228 IInputMethodSession session;
229 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public String toString() {
233 return "SessionState{uid " + client.uid + " pid " + client.pid
234 + " method " + Integer.toHexString(
235 System.identityHashCode(method))
236 + " session " + Integer.toHexString(
237 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 + "}";
240 }
241
242 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 client = _client;
245 method = _method;
246 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
Jeff Brownc28867a2013-03-26 15:42:39 -0700251 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IInputMethodClient client;
253 final IInputContext inputContext;
254 final int uid;
255 final int pid;
256 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean sessionRequested;
259 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "ClientState{" + Integer.toHexString(
264 System.identityHashCode(this)) + " uid " + uid
265 + " pid " + pid + "}";
266 }
267
268 ClientState(IInputMethodClient _client, IInputContext _inputContext,
269 int _uid, int _pid) {
270 client = _client;
271 inputContext = _inputContext;
272 uid = _uid;
273 pid = _pid;
274 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
275 }
276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700278 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700281 * Set once the system is ready to run third party code.
282 */
283 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700285 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700286 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
287 * method. This is to be synchronized with the secure settings keyed with
288 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
289 *
290 * <p>This can be transiently {@code null} when the system is re-initializing input method
291 * settings, e.g., the system locale is just changed.</p>
292 *
293 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
294 * {@link InputMethodManagerService}.</p>
295 *
296 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700298 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * The current binding sequence number, incremented every time there is
303 * a new bind performed.
304 */
305 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * The client that is currently bound to an input method.
309 */
310 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800313 * The last window token that we confirmed to be focused. This is always updated upon reports
314 * from the input method client. If the window state is already changed before the report is
315 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700316 */
317 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700319 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800320 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
321 */
322 ClientState mCurFocusedWindowClient;
323
324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * The input context last provided by the current client.
326 */
327 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * The attributes last provided by the current client.
331 */
332 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700335 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700337 *
338 * <p>This can be {@code null} when no input method is connected.</p>
339 *
340 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700342 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
satokab751aa2010-09-14 19:17:36 +0900346 * The current subtype of the current input method.
347 */
348 private InputMethodSubtype mCurrentSubtype;
349
satok4e4569d2010-11-19 18:45:53 +0900350 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900351 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700352 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900353
John Spurlocke0980502013-10-25 11:59:29 -0400354 // Was the keyguard locked when this client became current?
355 private boolean mCurClientInKeyguard;
356
satokab751aa2010-09-14 19:17:36 +0900357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * Set to true if our ServiceConnection is currently actively bound to
359 * a service (whether or not we have gotten its IBinder back yet).
360 */
361 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * Set if the client has asked for the input method to be shown.
365 */
366 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Set if we were explicitly told to show the input method.
370 */
371 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * Set if we were forced to be shown.
375 */
376 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 /**
379 * Set if we last told the input method to show itself.
380 */
381 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
384 * The Intent used to connect to the current input method.
385 */
386 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
389 * The token we have made for the currently active input method, to
390 * identify it in the future.
391 */
392 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * If non-null, this is the input method service we are currently connected
396 * to.
397 */
398 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Time that we last initiated a bind to the input method, to determine
402 * if we should try to disconnect and reconnect to it.
403 */
404 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * Have we called mCurMethod.bindInput()?
408 */
409 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * Currently enabled session. Only touched by service thread, not
413 * protected by a lock.
414 */
415 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700418 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700420 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900422 int mCurUserActionNotificationSequenceNumber = 0;
423
Joe Onorato857fd9b2011-01-27 15:08:35 -0800424 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900425
426 /**
427 * A set of status bits regarding the active IME.
428 *
429 * <p>This value is a combination of following two bits:</p>
430 * <dl>
431 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
432 * <dd>
433 * If this bit is ON, connected IME is ready to accept touch/key events.
434 * </dd>
435 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
436 * <dd>
437 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
438 * </dd>
439 * </dl>
440 * <em>Do not update this value outside of setImeWindowStatus.</em>
441 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800442 int mImeWindowVis;
443
Ken Wakasa05dbb652011-08-22 15:22:43 +0900444 private AlertDialog.Builder mDialogBuilder;
445 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900446 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900447 private InputMethodInfo[] mIms;
448 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900449 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700450 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
452 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700455 int mUserId;
456 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800457 String mLastEnabled = "";
458
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 /**
460 * <em>This constructor must be called within the lock.</em>
461 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 SettingsObserver(Handler handler) {
463 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700464 }
465
466 public void registerContentObserverLocked(int userId) {
467 if (mRegistered && mUserId == userId) {
468 return;
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700471 if (mRegistered) {
472 mContext.getContentResolver().unregisterContentObserver(this);
473 mRegistered = false;
474 }
475 if (mUserId != userId) {
476 mLastEnabled = "";
477 mUserId = userId;
478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700480 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900481 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700482 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900483 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700485 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700486 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
487 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Michael Wright7b5a96b2014-08-09 19:28:42 -0700490 @Override public void onChange(boolean selfChange, Uri uri) {
491 final Uri showImeUri =
492 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700494 if (showImeUri.equals(uri)) {
495 updateKeyboardFromSettingsLocked();
496 } else {
497 boolean enabledChanged = false;
498 String newEnabled = mSettings.getEnabledInputMethodsStr();
499 if (!mLastEnabled.equals(newEnabled)) {
500 mLastEnabled = newEnabled;
501 enabledChanged = true;
502 }
503 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700507
508 @Override
509 public String toString() {
510 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
511 + " mLastEnabled=" + mLastEnabled + "}";
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900515 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900516 @Override
517 public void onReceive(Context context, Intent intent) {
518 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700519 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900520 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700521 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900522 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800523 } else if (Intent.ACTION_USER_ADDED.equals(action)
524 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100525 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800526 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700527 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
528 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
529 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
530 final String prevValue = intent.getStringExtra(
531 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
532 final String newValue = intent.getStringExtra(
533 Intent.EXTRA_SETTING_NEW_VALUE);
534 restoreEnabledInputMethods(mContext, prevValue, newValue);
535 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900536 } else {
537 Slog.w(TAG, "Unexpected intent " + intent);
538 }
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700542 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
543 // does not attempt to validate on the fly with any installed device policy, so must only
544 // be run in the context of initial device setup.
545 //
546 // TODO: Move this method to InputMethodUtils with adding unit tests.
547 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
548 if (DEBUG_RESTORE) {
549 Slog.i(TAG, "Restoring enabled input methods:");
550 Slog.i(TAG, "prev=" + prevValue);
551 Slog.i(TAG, " new=" + newValue);
552 }
553 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900554 ArrayMap<String, ArraySet<String>> prevMap =
555 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
556 ArrayMap<String, ArraySet<String>> newMap =
557 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700558
559 // Merge the restored ime+subtype enabled states into the live state
560 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
561 final String imeId = entry.getKey();
562 ArraySet<String> prevSubtypes = prevMap.get(imeId);
563 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700564 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700565 prevMap.put(imeId, prevSubtypes);
566 }
567 prevSubtypes.addAll(entry.getValue());
568 }
569
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700570 final String mergedImesAndSubtypesString =
571 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700572 if (DEBUG_RESTORE) {
573 Slog.i(TAG, "Merged IME string:");
574 Slog.i(TAG, " " + mergedImesAndSubtypesString);
575 }
576 Settings.Secure.putString(context.getContentResolver(),
577 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
578 }
579
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800580 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 private boolean isChangingPackagesOfCurrentUser() {
582 final int userId = getChangingUserId();
583 final boolean retval = userId == mSettings.getCurrentUserId();
584 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900585 if (!retval) {
586 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
587 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900588 }
589 return retval;
590 }
591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800593 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900594 if (!isChangingPackagesOfCurrentUser()) {
595 return false;
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900598 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 final int N = mMethodList.size();
600 if (curInputMethodId != null) {
601 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800602 InputMethodInfo imi = mMethodList.get(i);
603 if (imi.getId().equals(curInputMethodId)) {
604 for (String pkg : packages) {
605 if (imi.getPackageName().equals(pkg)) {
606 if (!doit) {
607 return true;
608 }
satok723a27e2010-11-11 14:58:11 +0900609 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800610 chooseNewDefaultIMELocked();
611 return true;
612 }
613 }
614 }
615 }
616 }
617 }
618 return false;
619 }
620
621 @Override
622 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900623 if (!isChangingPackagesOfCurrentUser()) {
624 return;
625 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800626 synchronized (mMethodMap) {
627 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900628 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 final int N = mMethodList.size();
630 if (curInputMethodId != null) {
631 for (int i=0; i<N; i++) {
632 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900633 final String imiId = imi.getId();
634 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800635 curIm = imi;
636 }
satoke7c6998e2011-06-03 17:57:59 +0900637
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800638 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900639 if (isPackageModified(imi.getPackageName())) {
640 mFileManager.deleteAllInputMethodSubtypes(imiId);
641 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800642 if (change == PACKAGE_TEMPORARY_CHANGE
643 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800644 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 + imi.getComponent());
646 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 }
649 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800650
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900651 buildInputMethodListLocked(
652 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800656 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800657 int change = isPackageDisappearing(curIm.getPackageName());
658 if (change == PACKAGE_TEMPORARY_CHANGE
659 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800660 ServiceInfo si = null;
661 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900662 si = mIPackageManager.getServiceInfo(
663 curIm.getComponent(), 0, mSettings.getCurrentUserId());
664 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800665 }
666 if (si == null) {
667 // Uh oh, current input method is no longer around!
668 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800669 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900670 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800672 changed = true;
673 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800674 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900675 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800676 }
677 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800678 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 }
satokab751aa2010-09-14 19:17:36 +0900680
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 if (curIm == null) {
682 // We currently don't have a default input method... is
683 // one now available?
684 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700685 } else if (!changed && isPackageModified(curIm.getPackageName())) {
686 // Even if the current input method is still available, mCurrentSubtype could
687 // be obsolete when the package is modified in practice.
688 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800691 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800692 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
695 }
696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
Jeff Brownc28867a2013-03-26 15:42:39 -0700698 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900699 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700700 private final IInputMethod mMethod;
701 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800702
Jeff Brownc28867a2013-03-26 15:42:39 -0700703 MethodCallback(InputMethodManagerService imms, IInputMethod method,
704 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900705 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700706 mMethod = method;
707 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
satoke7c6998e2011-06-03 17:57:59 +0900710 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700711 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700712 long ident = Binder.clearCallingIdentity();
713 try {
714 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
715 } finally {
716 Binder.restoreCallingIdentity(ident);
717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
satok01038492012-04-09 21:08:27 +0900721 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700722 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900723 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700724 public void onHardKeyboardStatusChange(boolean available) {
725 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
726 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900727 }
728
Michael Wright7b5a96b2014-08-09 19:28:42 -0700729 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900730 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700731 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900732 }
733 synchronized(mMethodMap) {
734 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
735 && mSwitchingDialog.isShowing()) {
736 mSwitchingDialogTitleView.findViewById(
737 com.android.internal.R.id.hard_keyboard_section).setVisibility(
738 available ? View.VISIBLE : View.GONE);
739 }
740 }
741 }
742 }
743
Seigo Nonaka7309b122015-08-17 18:34:13 -0700744 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900745 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800747 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700749 // Note: SettingsObserver doesn't register observers in its constructor.
750 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 mIWindowManager = IWindowManager.Stub.asInterface(
752 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700753 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800754 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 public void executeMessage(Message msg) {
757 handleMessage(msg);
758 }
Mita Yuned218c72012-12-06 17:18:25 -0800759 }, true /*asyncHandler*/);
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900760 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900761 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700762 mHasFeature = context.getPackageManager().hasSystemFeature(
763 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900764
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400765 Bundle extras = new Bundle();
766 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
767 mImeSwitcherNotification = new Notification.Builder(mContext)
768 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
769 .setWhen(0)
770 .setOngoing(true)
771 .addExtras(extras)
772 .setCategory(Notification.CATEGORY_SYSTEM)
773 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400774
satok7cfc0ed2011-06-20 21:29:36 +0900775 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900776 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900777
778 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900779
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900781 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800782 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
783 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700784 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900785 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
satok7cfc0ed2011-06-20 21:29:36 +0900787 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 int userId = 0;
789 try {
790 ActivityManagerNative.getDefault().registerUserSwitchObserver(
791 new IUserSwitchObserver.Stub() {
792 @Override
793 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900794 synchronized(mMethodMap) {
795 switchUserLocked(newUserId);
796 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900797 if (reply != null) {
798 try {
799 reply.sendResult(null);
800 } catch (RemoteException e) {
801 }
802 }
803 }
804
805 @Override
806 public void onUserSwitchComplete(int newUserId) throws RemoteException {
807 }
Kenny Guy42979622015-04-13 18:03:05 +0000808
809 @Override
810 public void onForegroundProfileSwitch(int newProfileId) {
811 // Ignore.
812 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 });
814 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
815 } catch (RemoteException e) {
816 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
817 }
satok81f8b7c2012-12-04 20:42:56 +0900818 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900819
satokd87c2592010-09-29 11:52:06 +0900820 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900821 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900822 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700823
824 // Let the package manager query which are the default imes
825 // as they get certain permissions granted by default.
826 PackageManagerInternal packageManagerInternal = LocalServices.getService(
827 PackageManagerInternal.class);
828 packageManagerInternal.setImePackagesProvider(
829 new PackageManagerInternal.PackagesProvider() {
830 @Override
831 public String[] getPackages(int userId) {
832 synchronized (mMethodMap) {
833 final int currentUserId = mSettings.getCurrentUserId();
834 // TODO: We are switching the current user id in the settings
835 // object to query it and then revert the user id. Ideally, we
836 // should call a API in settings with the user id as an argument.
837 mSettings.setCurrentUserId(userId);
838 List<InputMethodInfo> imes = mSettings
839 .getEnabledInputMethodListLocked();
840 String[] packageNames = null;
841 if (imes != null) {
842 final int imeCount = imes.size();
843 packageNames = new String[imeCount];
844 for (int i = 0; i < imeCount; i++) {
845 InputMethodInfo ime = imes.get(i);
846 packageNames[i] = ime.getPackageName();
847 }
848 }
849 mSettings.setCurrentUserId(currentUserId);
850 return packageNames;
851 }
852 }
853 });
854
Kenny Guy2a764942014-04-02 13:29:20 +0100855 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900856 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900857 synchronized (mMethodMap) {
858 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
859 mSettings, context);
860 }
satok0a1bcf42012-05-16 19:26:31 +0900861
862 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900863 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900864 if (DEBUG) {
865 Slog.d(TAG, "Initial default ime = " + defaultImiId);
866 }
satok0a1bcf42012-05-16 19:26:31 +0900867 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
868
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900869 synchronized (mMethodMap) {
870 buildInputMethodListLocked(mMethodList, mMethodMap,
871 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
872 }
satokd87c2592010-09-29 11:52:06 +0900873 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874
satok0a1bcf42012-05-16 19:26:31 +0900875 if (!mImeSelectedOnBoot) {
876 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900877 synchronized (mMethodMap) {
878 resetDefaultImeLocked(context);
879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900882 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700883 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900884 updateFromSettingsLocked(true);
885 }
satok5b927c432012-05-01 20:09:34 +0900886
887 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
888 // according to the new system locale.
889 final IntentFilter filter = new IntentFilter();
890 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
891 mContext.registerReceiver(
892 new BroadcastReceiver() {
893 @Override
894 public void onReceive(Context context, Intent intent) {
895 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900896 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900897 }
898 }
899 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700900 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900901 }
902
satok5b927c432012-05-01 20:09:34 +0900903 private void resetDefaultImeLocked(Context context) {
904 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900905 if (mCurMethodId != null
906 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900907 return;
908 }
909
910 InputMethodInfo defIm = null;
911 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900912 if (defIm == null && mSystemReady) {
913 final Locale systemLocale = context.getResources().getConfiguration().locale;
914 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
915 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
916 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900917 defIm = imi;
918 Slog.i(TAG, "Selected default: " + imi.getId());
919 }
920 }
921 }
922 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900923 defIm = InputMethodUtils.getMostApplicableDefaultIME(
924 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900925 if (defIm != null) {
926 Slog.i(TAG, "Default found, using " + defIm.getId());
927 } else {
928 Slog.i(TAG, "No default found");
929 }
satok5b927c432012-05-01 20:09:34 +0900930 }
931 if (defIm != null) {
932 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
933 }
934 }
935
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900936 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
937 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900938 if (!mSystemReady) {
939 // not system ready
940 return;
941 }
942 final Locale newLocale = mRes.getConfiguration().locale;
943 if (!updateOnlyWhenLocaleChanged
944 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
945 if (!updateOnlyWhenLocaleChanged) {
946 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800947 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 }
949 if (DEBUG) {
950 Slog.i(TAG, "Locale has been changed to " + newLocale);
951 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900952 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 if (!updateOnlyWhenLocaleChanged) {
954 final String selectedImiId = mSettings.getSelectedInputMethod();
955 if (TextUtils.isEmpty(selectedImiId)) {
956 // This is the first time of the user switch and
957 // set the current ime to the proper one.
958 resetDefaultImeLocked(mContext);
959 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900960 } else {
961 // If the locale is changed, needs to reset the default ime
962 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900963 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800964 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 mLastSystemLocale = newLocale;
966 if (!updateOnlyWhenLocaleChanged) {
967 try {
968 startInputInnerLocked();
969 } catch (RuntimeException e) {
970 Slog.w(TAG, "Unexpected exception", e);
971 }
972 }
973 }
974 }
975
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900976 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900977 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
978 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 }
980
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900981 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700982 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
983 + " currentUserId=" + mSettings.getCurrentUserId());
984
Yohei Yukawa81482972015-06-04 00:58:59 -0700985 // ContentObserver should be registered again when the user is changed
986 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100988 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900989 // InputMethodFileManager should be reset when the user is changed
990 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900991 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700992
993 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
994 + " defaultImiId=" + defaultImiId);
995
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900996 // For secondary users, the list of enabled IMEs may not have been updated since the
997 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
998 // not be empty even if the IME has been uninstalled by the primary user.
999 // Even in such cases, IMMS works fine because it will find the most applicable
1000 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001001 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001002 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001003 initialUserSwitch /* needsToResetDefaultIme */);
1004 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001005 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1006 mSettings.getEnabledInputMethodListLocked(), newUserId,
1007 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001008 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001009
1010 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1011 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001012 }
1013
Kenny Guy2a764942014-04-02 13:29:20 +01001014 void updateCurrentProfileIds() {
1015 List<UserInfo> profiles =
1016 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1017 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1018 for (int i = 0; i < currentProfileIds.length; i++) {
1019 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001020 }
Kenny Guy2a764942014-04-02 13:29:20 +01001021 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001022 }
1023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 @Override
1025 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1026 throws RemoteException {
1027 try {
1028 return super.onTransact(code, data, reply, flags);
1029 } catch (RuntimeException e) {
1030 // The input method manager only throws security exceptions, so let's
1031 // log all others.
1032 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001033 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035 throw e;
1036 }
1037 }
1038
Svetoslav Ganova0027152013-06-25 14:59:53 -07001039 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001040 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001041 if (DEBUG) {
1042 Slog.d(TAG, "--- systemReady");
1043 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001044 if (!mSystemReady) {
1045 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001046 mKeyguardManager =
1047 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001048 mNotificationManager = (NotificationManager)
1049 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1050 mStatusBar = statusBar;
1051 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001052 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001053 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1054 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001055 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001056 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001057 mHardKeyboardListener);
1058 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001059 buildInputMethodListLocked(mMethodList, mMethodMap,
1060 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001061 if (!mImeSelectedOnBoot) {
1062 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001063 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001064 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1065 mSettings.getEnabledInputMethodListLocked(),
1066 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001067 }
1068 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001069 try {
1070 startInputInnerLocked();
1071 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001072 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001073 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001074 }
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001078 // ---------------------------------------------------------------------------------------
1079 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1080 // 1) it comes from the system process
1081 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1082 private boolean calledFromValidUser() {
1083 final int uid = Binder.getCallingUid();
1084 final int userId = UserHandle.getUserId(uid);
1085 if (DEBUG) {
1086 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1087 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1088 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001089 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1090 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001091 }
Kenny Guy2a764942014-04-02 13:29:20 +01001092 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001093 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001094 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001095
1096 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1097 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1098 // must not manage background users' states in any functions.
1099 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1100 // by a token.
1101 if (mContext.checkCallingOrSelfPermission(
1102 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1103 == PackageManager.PERMISSION_GRANTED) {
1104 if (DEBUG) {
1105 Slog.d(TAG, "--- Access granted because the calling process has "
1106 + "the INTERACT_ACROSS_USERS_FULL permission");
1107 }
1108 return true;
1109 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001110 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1111 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001112 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001113 }
1114
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001115
1116 /**
1117 * Returns true iff the caller is identified to be the current input method with the token.
1118 * @param token The window token given to the input method when it was started.
1119 * @return true if and only if non-null valid token is specified.
1120 */
1121 private boolean calledWithValidToken(IBinder token) {
1122 if (token == null || mCurToken != token) {
1123 return false;
1124 }
1125 return true;
1126 }
1127
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 private boolean bindCurrentInputMethodService(
1129 Intent service, ServiceConnection conn, int flags) {
1130 if (service == null || conn == null) {
1131 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1132 return false;
1133 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001134 return mContext.bindServiceAsUser(service, conn, flags,
1135 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001136 }
1137
satoke7c6998e2011-06-03 17:57:59 +09001138 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 // TODO: Make this work even for non-current users?
1141 if (!calledFromValidUser()) {
1142 return Collections.emptyList();
1143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001145 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 }
1148
satoke7c6998e2011-06-03 17:57:59 +09001149 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 // TODO: Make this work even for non-current users?
1152 if (!calledFromValidUser()) {
1153 return Collections.emptyList();
1154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001156 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 }
1159
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001160 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001161 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001162 * @return enabled subtypes of the specified imi
1163 */
satoke7c6998e2011-06-03 17:57:59 +09001164 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001165 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001166 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 // TODO: Make this work even for non-current users?
1168 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001169 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001170 }
satok67ddf9c2010-11-17 09:45:54 +09001171 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001172 final InputMethodInfo imi;
1173 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001174 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001175 } else {
1176 imi = mMethodMap.get(imiId);
1177 }
1178 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001179 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001180 }
1181 return mSettings.getEnabledInputMethodSubtypeListLocked(
1182 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001183 }
1184 }
1185
satoke7c6998e2011-06-03 17:57:59 +09001186 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 public void addClient(IInputMethodClient client,
1188 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001189 if (!calledFromValidUser()) {
1190 return;
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 synchronized (mMethodMap) {
1193 mClients.put(client.asBinder(), new ClientState(client,
1194 inputContext, uid, pid));
1195 }
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
satoke7c6998e2011-06-03 17:57:59 +09001198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001200 if (!calledFromValidUser()) {
1201 return;
1202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001204 ClientState cs = mClients.remove(client.asBinder());
1205 if (cs != null) {
1206 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001207 if (mCurClient == cs) {
1208 mCurClient = null;
1209 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001210 if (mCurFocusedWindowClient == cs) {
1211 mCurFocusedWindowClient = null;
1212 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 void executeOrSendMessage(IInterface target, Message msg) {
1218 if (target.asBinder() instanceof Binder) {
1219 mCaller.sendMessage(msg);
1220 } else {
1221 handleMessage(msg);
1222 msg.recycle();
1223 }
1224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
Yohei Yukawa33e81792015-11-17 21:14:42 -08001226 void unbindCurrentClientLocked(
1227 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001229 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 + mCurClient.client.asBinder());
1231 if (mBoundToMethod) {
1232 mBoundToMethod = false;
1233 if (mCurMethod != null) {
1234 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1235 MSG_UNBIND_INPUT, mCurMethod));
1236 }
1237 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001238
1239 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1240 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001241 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1242 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project10592532009-03-18 17:39:46 -07001246 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 private int getImeShowFlags() {
1251 int flags = 0;
1252 if (mShowForced) {
1253 flags |= InputMethod.SHOW_FORCED
1254 | InputMethod.SHOW_EXPLICIT;
1255 } else if (mShowExplicitlyRequested) {
1256 flags |= InputMethod.SHOW_EXPLICIT;
1257 }
1258 return flags;
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 private int getAppShowFlags() {
1262 int flags = 0;
1263 if (mShowForced) {
1264 flags |= InputMethodManager.SHOW_FORCED;
1265 } else if (!mShowExplicitlyRequested) {
1266 flags |= InputMethodManager.SHOW_IMPLICIT;
1267 }
1268 return flags;
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
Dianne Hackborn7663d802012-02-24 13:08:49 -08001271 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 if (!mBoundToMethod) {
1273 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1274 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1275 mBoundToMethod = true;
1276 }
1277 final SessionState session = mCurClient.curSession;
1278 if (initial) {
1279 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1280 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1281 } else {
1282 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1283 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1284 }
1285 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001289 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001290 (session.channel != null ? session.channel.dup() : null),
1291 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001294 InputBindResult startInputLocked(
1295 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1296 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1297 int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 // If no method is currently selected, do nothing.
1299 if (mCurMethodId == null) {
1300 return mNoBinding;
1301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 ClientState cs = mClients.get(client.asBinder());
1304 if (cs == null) {
1305 throw new IllegalArgumentException("unknown client "
1306 + client.asBinder());
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 try {
1310 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1311 // Check with the window manager to make sure this client actually
1312 // has a window with focus. If not, reject. This is thread safe
1313 // because if the focus changes some time before or after, the
1314 // next client receiving focus that has any interest in input will
1315 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001316 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1318 return null;
1319 }
1320 } catch (RemoteException e) {
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
Dianne Hackborn7663d802012-02-24 13:08:49 -08001323 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1324 }
1325
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001326 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
1327 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001328 // If no method is currently selected, do nothing.
1329 if (mCurMethodId == null) {
1330 return mNoBinding;
1331 }
1332
Yohei Yukawad57ba672015-06-08 16:39:46 -07001333 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1334 attribute.packageName)) {
1335 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1336 + " uid=" + cs.uid + " package=" + attribute.packageName);
1337 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001338 }
1339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001341 // Was the keyguard locked when switching over to the new client?
1342 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 // If the client is changing, we need to switch over to the new
1344 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001345 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001346 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001347 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
1349 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001350 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001351 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001352 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 // Bump up the sequence for this client and attach it.
1357 mCurSeq++;
1358 if (mCurSeq <= 0) mCurSeq = 1;
1359 mCurClient = cs;
1360 mCurInputContext = inputContext;
1361 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 // Check if the input method is changing.
1364 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1365 if (cs.curSession != null) {
1366 // Fast case: if we are already connected to the input method,
1367 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001368 return attachNewInputLocked(
1369 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 if (mHaveConnection) {
1372 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 // Return to client, and we will get back with it when
1374 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001375 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001376 return new InputBindResult(null, null, mCurId, mCurSeq,
1377 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 } else if (SystemClock.uptimeMillis()
1379 < (mLastBindTime+TIME_TO_RECONNECT)) {
1380 // In this case we have connected to the service, but
1381 // don't yet have its interface. If it hasn't been too
1382 // long since we did the connection, we'll return to
1383 // the client and wait to get the service interface so
1384 // we can report back. If it has been too long, we want
1385 // to fall through so we can try a disconnect/reconnect
1386 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001387 return new InputBindResult(null, null, mCurId, mCurSeq,
1388 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1391 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393 }
1394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001395
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001396 return startInputInnerLocked();
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001399 InputBindResult startInputInnerLocked() {
1400 if (mCurMethodId == null) {
1401 return mNoBinding;
1402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001404 if (!mSystemReady) {
1405 // If the system is not yet ready, we shouldn't be running third
1406 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001407 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1408 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1412 if (info == null) {
1413 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001416 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1419 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001420 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1421 com.android.internal.R.string.input_method_binding_label);
1422 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1423 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001424 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001425 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1426 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 mLastBindTime = SystemClock.uptimeMillis();
1428 mHaveConnection = true;
1429 mCurId = info.getId();
1430 mCurToken = new Binder();
1431 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001432 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mIWindowManager.addWindowToken(mCurToken,
1434 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1435 } catch (RemoteException e) {
1436 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001437 return new InputBindResult(null, null, mCurId, mCurSeq,
1438 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 } else {
1440 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001441 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 + mCurIntent);
1443 }
1444 return null;
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
satoke7c6998e2011-06-03 17:57:59 +09001447 @Override
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001448 public InputBindResult startInput(
1449 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1450 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1451 int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001452 if (!calledFromValidUser()) {
1453 return null;
1454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001456 if (DEBUG) {
1457 Slog.v(TAG, "startInput: reason="
1458 + InputMethodClient.getStartInputReason(startInputReason)
1459 + " client = " + client.asBinder()
1460 + " inputContext=" + inputContext
1461 + " attribute=" + attribute
1462 + " controlFlags=#" + Integer.toHexString(controlFlags));
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 final long ident = Binder.clearCallingIdentity();
1465 try {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001466 return startInputLocked(startInputReason, client, inputContext, attribute,
1467 controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 } finally {
1469 Binder.restoreCallingIdentity(ident);
1470 }
1471 }
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
satoke7c6998e2011-06-03 17:57:59 +09001474 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 public void finishInput(IInputMethodClient client) {
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
satoke7c6998e2011-06-03 17:57:59 +09001478 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 public void onServiceConnected(ComponentName name, IBinder service) {
1480 synchronized (mMethodMap) {
1481 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1482 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001483 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001485 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001486 return;
1487 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001488 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001489 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1490 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001492 clearClientSessionLocked(mCurClient);
1493 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
1495 }
1496 }
1497 }
1498
Jeff Brownc28867a2013-03-26 15:42:39 -07001499 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1500 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 synchronized (mMethodMap) {
1502 if (mCurMethod != null && method != null
1503 && mCurMethod.asBinder() == method.asBinder()) {
1504 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001505 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001507 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001508 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 if (res.method != null) {
1510 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001511 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001513 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 }
1516 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001517
1518 // Session abandoned. Close its associated input channel.
1519 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001522 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001523 if (mVisibleBound) {
1524 mContext.unbindService(mVisibleConnection);
1525 mVisibleBound = false;
1526 }
1527
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001528 if (mHaveConnection) {
1529 mContext.unbindService(this);
1530 mHaveConnection = false;
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001533 if (mCurToken != null) {
1534 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001536 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001537 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001538 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001539 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001540 mIWindowManager.removeWindowToken(mCurToken);
1541 } catch (RemoteException e) {
1542 }
1543 mCurToken = null;
1544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
The Android Open Source Project10592532009-03-18 17:39:46 -07001546 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001547 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001549
Yohei Yukawa33e81792015-11-17 21:14:42 -08001550 void resetCurrentMethodAndClient(
1551 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001552 mCurMethodId = null;
1553 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001554 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001555 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001556
1557 void requestClientSessionLocked(ClientState cs) {
1558 if (!cs.sessionRequested) {
1559 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1560 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1561 cs.sessionRequested = true;
1562 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1563 MSG_CREATE_SESSION, mCurMethod, channels[1],
1564 new MethodCallback(this, mCurMethod, channels[0])));
1565 }
1566 }
1567
1568 void clearClientSessionLocked(ClientState cs) {
1569 finishSessionLocked(cs.curSession);
1570 cs.curSession = null;
1571 cs.sessionRequested = false;
1572 }
1573
1574 private void finishSessionLocked(SessionState sessionState) {
1575 if (sessionState != null) {
1576 if (sessionState.session != null) {
1577 try {
1578 sessionState.session.finishSession();
1579 } catch (RemoteException e) {
1580 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001581 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001582 }
1583 sessionState.session = null;
1584 }
1585 if (sessionState.channel != null) {
1586 sessionState.channel.dispose();
1587 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001588 }
1589 }
1590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001591
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001592 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 if (mCurMethod != null) {
1594 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001595 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001597
Jeff Brownc28867a2013-03-26 15:42:39 -07001598 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001599 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 mCurMethod = null;
1601 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001602 if (mStatusBar != null) {
1603 mStatusBar.setIconVisibility("ime", false);
1604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
satoke7c6998e2011-06-03 17:57:59 +09001607 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 public void onServiceDisconnected(ComponentName name) {
1609 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 + " mCurIntent=" + mCurIntent);
1612 if (mCurMethod != null && mCurIntent != null
1613 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001614 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 // We consider this to be a new bind attempt, since the system
1616 // should now try to restart the service for us.
1617 mLastBindTime = SystemClock.uptimeMillis();
1618 mShowRequested = mInputShown;
1619 mInputShown = false;
1620 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001621 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1622 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1623 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 }
1626 }
1627 }
1628
satokf9f01002011-05-19 21:31:50 +09001629 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1631 long ident = Binder.clearCallingIdentity();
1632 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001634 if (!calledWithValidToken(token)) {
1635 final int uid = Binder.getCallingUid();
1636 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1637 + " token:" + token);
1638 return;
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001642 if (mStatusBar != null) {
1643 mStatusBar.setIconVisibility("ime", false);
1644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001646 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001647 CharSequence contentDescription = null;
1648 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001649 // Use PackageManager to load label
1650 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001651 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001652 mIPackageManager.getApplicationInfo(packageName, 0,
1653 mSettings.getCurrentUserId()));
1654 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001655 /* ignore */
1656 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001657 if (mStatusBar != null) {
1658 mStatusBar.setIcon("ime", packageName, iconId, 0,
1659 contentDescription != null
1660 ? contentDescription.toString() : null);
1661 mStatusBar.setIconVisibility("ime", true);
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664 }
1665 } finally {
1666 Binder.restoreCallingIdentity(ident);
1667 }
1668 }
1669
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001670 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001671 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001672 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001673 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001674 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001675 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001676 // When physical keyboard is attached, we show the ime switcher (or notification if
1677 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1678 // exists in the IME switcher dialog. Might be OK to remove this condition once
1679 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1680 return true;
1681 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001682 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001683
1684 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1685 final int N = imis.size();
1686 if (N > 2) return true;
1687 if (N < 1) return false;
1688 int nonAuxCount = 0;
1689 int auxCount = 0;
1690 InputMethodSubtype nonAuxSubtype = null;
1691 InputMethodSubtype auxSubtype = null;
1692 for(int i = 0; i < N; ++i) {
1693 final InputMethodInfo imi = imis.get(i);
1694 final List<InputMethodSubtype> subtypes =
1695 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1696 final int subtypeCount = subtypes.size();
1697 if (subtypeCount == 0) {
1698 ++nonAuxCount;
1699 } else {
1700 for (int j = 0; j < subtypeCount; ++j) {
1701 final InputMethodSubtype subtype = subtypes.get(j);
1702 if (!subtype.isAuxiliary()) {
1703 ++nonAuxCount;
1704 nonAuxSubtype = subtype;
1705 } else {
1706 ++auxCount;
1707 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001708 }
1709 }
satok7cfc0ed2011-06-20 21:29:36 +09001710 }
1711 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001712 if (nonAuxCount > 1 || auxCount > 1) {
1713 return true;
1714 } else if (nonAuxCount == 1 && auxCount == 1) {
1715 if (nonAuxSubtype != null && auxSubtype != null
1716 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1717 || auxSubtype.overridesImplicitlyEnabledSubtype()
1718 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1719 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1720 return false;
1721 }
1722 return true;
1723 }
1724 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001725 }
1726
John Spurlocke0980502013-10-25 11:59:29 -04001727 private boolean isKeyguardLocked() {
1728 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1729 }
1730
satokdbf29502011-08-25 15:28:23 +09001731 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001732 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001733 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001734 if (!calledWithValidToken(token)) {
1735 final int uid = Binder.getCallingUid();
1736 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1737 + " token:" + token);
1738 return;
1739 }
1740
1741 synchronized (mMethodMap) {
1742 mImeWindowVis = vis;
1743 mBackDisposition = backDisposition;
1744 updateSystemUiLocked(token, vis, backDisposition);
1745 }
1746 }
1747
1748 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1749 synchronized (mMethodMap) {
1750 updateSystemUiLocked(token, vis, backDisposition);
1751 }
1752 }
1753
1754 // Caution! This method is called in this class. Handle multi-user carefully
1755 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1756 if (!calledWithValidToken(token)) {
1757 final int uid = Binder.getCallingUid();
1758 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1759 + " token:" + token);
1760 return;
1761 }
1762
1763 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1764 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001765 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001766 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001767 // apply policy for binder calls
1768 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1769 vis = 0;
satok06487a52010-10-29 11:37:18 +09001770 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001771 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1772 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1773 if (mStatusBar != null) {
1774 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1775 needsToShowImeSwitcher);
1776 }
1777 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1778 if (imi != null && needsToShowImeSwitcher) {
1779 // Used to load label
1780 final CharSequence title = mRes.getText(
1781 com.android.internal.R.string.select_input_method);
1782 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1783 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001784 mImeSwitcherNotification.setContentTitle(title)
1785 .setContentText(summary)
1786 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001787 try {
1788 if ((mNotificationManager != null)
1789 && !mIWindowManager.hasNavigationBar()) {
1790 if (DEBUG) {
1791 Slog.d(TAG, "--- show notification: label = " + summary);
1792 }
1793 mNotificationManager.notifyAsUser(null,
1794 com.android.internal.R.string.select_input_method,
1795 mImeSwitcherNotification.build(), UserHandle.ALL);
1796 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001797 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001798 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001799 }
1800 } else {
1801 if (mNotificationShown && mNotificationManager != null) {
1802 if (DEBUG) {
1803 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001804 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001805 mNotificationManager.cancelAsUser(null,
1806 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1807 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001808 }
satok06487a52010-10-29 11:37:18 +09001809 }
1810 } finally {
1811 Binder.restoreCallingIdentity(ident);
1812 }
1813 }
1814
satoke7c6998e2011-06-03 17:57:59 +09001815 @Override
satokf9f01002011-05-19 21:31:50 +09001816 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001817 if (!calledFromValidUser()) {
1818 return;
1819 }
satokf9f01002011-05-19 21:31:50 +09001820 synchronized (mMethodMap) {
1821 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1822 for (int i = 0; i < spans.length; ++i) {
1823 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001824 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001825 mSecureSuggestionSpans.put(ss, currentImi);
1826 }
1827 }
1828 }
1829 }
1830
satoke7c6998e2011-06-03 17:57:59 +09001831 @Override
satokf9f01002011-05-19 21:31:50 +09001832 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001833 if (!calledFromValidUser()) {
1834 return false;
1835 }
satokf9f01002011-05-19 21:31:50 +09001836 synchronized (mMethodMap) {
1837 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1838 // TODO: Do not send the intent if the process of the targetImi is already dead.
1839 if (targetImi != null) {
1840 final String[] suggestions = span.getSuggestions();
1841 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001842 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001843 final Intent intent = new Intent();
1844 // Ensures that only a class in the original IME package will receive the
1845 // notification.
satok42c5a162011-05-26 16:46:14 +09001846 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001847 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1848 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1849 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1850 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001851 final long ident = Binder.clearCallingIdentity();
1852 try {
1853 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1854 } finally {
1855 Binder.restoreCallingIdentity(ident);
1856 }
satokf9f01002011-05-19 21:31:50 +09001857 return true;
1858 }
1859 }
1860 return false;
1861 }
1862
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001863 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001864 updateInputMethodsFromSettingsLocked(enabledMayChange);
1865 updateKeyboardFromSettingsLocked();
1866 }
1867
1868 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001869 if (enabledMayChange) {
1870 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1871 for (int i=0; i<enabled.size(); i++) {
1872 // We allow the user to select "disabled until used" apps, so if they
1873 // are enabling one of those here we now need to make it enabled.
1874 InputMethodInfo imm = enabled.get(i);
1875 try {
1876 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1877 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1878 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001879 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001880 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001881 if (DEBUG) {
1882 Slog.d(TAG, "Update state(" + imm.getId()
1883 + "): DISABLED_UNTIL_USED -> DEFAULT");
1884 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001885 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1886 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001887 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1888 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001889 }
1890 } catch (RemoteException e) {
1891 }
1892 }
1893 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001894 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1895 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1896 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1897 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001898 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001899 // There is no input method selected, try to choose new applicable input method.
1900 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001901 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001902 }
1903 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001905 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001907 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001908 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
satokf3db1af2010-11-23 13:34:33 +09001910 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001911 } else {
1912 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001913 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001915 // Here is not the perfect place to reset the switching controller. Ideally
1916 // mSwitchingController and mSettings should be able to share the same state.
1917 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1918 // the same enabled IMEs list.
1919 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001920
1921 }
1922
1923 public void updateKeyboardFromSettingsLocked() {
1924 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1925 if (mSwitchingDialog != null
1926 && mSwitchingDialogTitleView != null
1927 && mSwitchingDialog.isShowing()) {
1928 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1929 com.android.internal.R.id.hard_keyboard_switch);
1930 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
Yohei Yukawab097b822015-12-01 10:43:08 -08001934 private void notifyInputMethodSubtypeChanged(final int userId,
1935 @Nullable final InputMethodInfo inputMethodInfo,
1936 @Nullable final InputMethodSubtype subtype) {
1937 final InputManagerInternal inputManagerInternal =
1938 LocalServices.getService(InputManagerInternal.class);
1939 if (inputManagerInternal != null) {
1940 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1941 }
1942 }
1943
satokab751aa2010-09-14 19:17:36 +09001944 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 InputMethodInfo info = mMethodMap.get(id);
1946 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001947 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001949
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001950 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001951 // We have already made sure that the package name belongs to the application's UID.
1952 // No further UID check is required.
1953 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1954 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001955 }
1956 }
1957
satokd81e9502012-05-21 12:58:45 +09001958 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001960 final int subtypeCount = info.getSubtypeCount();
1961 if (subtypeCount <= 0) {
1962 return;
satokcd7cd292010-11-20 15:46:23 +09001963 }
satokd81e9502012-05-21 12:58:45 +09001964 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1965 final InputMethodSubtype newSubtype;
1966 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1967 newSubtype = info.getSubtypeAt(subtypeId);
1968 } else {
1969 // If subtype is null, try to find the most applicable one from
1970 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001971 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001972 }
1973 if (newSubtype == null || oldSubtype == null) {
1974 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1975 + ", new subtype = " + newSubtype);
1976 return;
1977 }
1978 if (newSubtype != oldSubtype) {
1979 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1980 if (mCurMethod != null) {
1981 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001982 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001983 mCurMethod.changeInputMethodSubtype(newSubtype);
1984 } catch (RemoteException e) {
1985 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08001986 return;
satokab751aa2010-09-14 19:17:36 +09001987 }
1988 }
Yohei Yukawab097b822015-12-01 10:43:08 -08001989 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09001990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 return;
1992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001993
satokd81e9502012-05-21 12:58:45 +09001994 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 final long ident = Binder.clearCallingIdentity();
1996 try {
satokab751aa2010-09-14 19:17:36 +09001997 // Set a subtype to this input method.
1998 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001999 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2000 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2001 // because mCurMethodId is stored as a history in
2002 // setSelectedInputMethodAndSubtypeLocked().
2003 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004
2005 if (ActivityManagerNative.isSystemReady()) {
2006 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002007 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002009 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002011 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 } finally {
2013 Binder.restoreCallingIdentity(ident);
2014 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002015
2016 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2017 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
satok42c5a162011-05-26 16:46:14 +09002020 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002021 public boolean showSoftInput(IInputMethodClient client, int flags,
2022 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002023 if (!calledFromValidUser()) {
2024 return false;
2025 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002026 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 long ident = Binder.clearCallingIdentity();
2028 try {
2029 synchronized (mMethodMap) {
2030 if (mCurClient == null || client == null
2031 || mCurClient.client.asBinder() != client.asBinder()) {
2032 try {
2033 // We need to check if this is the current client with
2034 // focus in the window manager, to allow this call to
2035 // be made before input is started in it.
2036 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002037 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002038 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002041 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002044
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002045 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 } finally {
2049 Binder.restoreCallingIdentity(ident);
2050 }
2051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
The Android Open Source Project4df24232009-03-05 14:34:35 -08002053 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 mShowRequested = true;
2055 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2056 mShowExplicitlyRequested = true;
2057 }
2058 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2059 mShowExplicitlyRequested = true;
2060 mShowForced = true;
2061 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002062
Dianne Hackborncc278702009-09-02 23:07:23 -07002063 if (!mSystemReady) {
2064 return false;
2065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002066
The Android Open Source Project4df24232009-03-05 14:34:35 -08002067 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002069 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2071 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2072 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002074 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002075 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002076 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002077 | Context.BIND_TREAT_LIKE_ACTIVITY
2078 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002079 mVisibleBound = true;
2080 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002081 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002083 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 // The client has asked to have the input method shown, but
2085 // we have been sitting here too long with a connection to the
2086 // service and no interface received, so let's disconnect/connect
2087 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002088 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002090 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002092 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002093 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002094 } else {
2095 if (DEBUG) {
2096 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2097 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002100
The Android Open Source Project4df24232009-03-05 14:34:35 -08002101 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002103
satok42c5a162011-05-26 16:46:14 +09002104 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002105 public boolean hideSoftInput(IInputMethodClient client, int flags,
2106 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002107 if (!calledFromValidUser()) {
2108 return false;
2109 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002110 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 long ident = Binder.clearCallingIdentity();
2112 try {
2113 synchronized (mMethodMap) {
2114 if (mCurClient == null || client == null
2115 || mCurClient.client.asBinder() != client.asBinder()) {
2116 try {
2117 // We need to check if this is the current client with
2118 // focus in the window manager, to allow this call to
2119 // be made before input is started in it.
2120 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002121 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2122 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002123 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
2125 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002129
Joe Onorato8a9b2202010-02-26 18:56:32 -08002130 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
2133 } finally {
2134 Binder.restoreCallingIdentity(ident);
2135 }
2136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137
The Android Open Source Project4df24232009-03-05 14:34:35 -08002138 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2140 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002141 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 -08002142 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002145 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 -08002146 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002148
2149 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2150 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2151 // to be updated with the new value sent from IME process. Even in such a transient state
2152 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2153 // application process as a valid request, and have even promised such a behavior with CTS
2154 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2155 // IMMS#InputShown indicates that the software keyboard is shown.
2156 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2157 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2158 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002160 if (shouldHideSoftInput) {
2161 // The IME will report its visible state again after the following message finally
2162 // delivered to the IME process as an IPC. Hence the inconsistency between
2163 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2164 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002165 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2166 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2167 res = true;
2168 } else {
2169 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002171 if (mHaveConnection && mVisibleBound) {
2172 mContext.unbindService(mVisibleConnection);
2173 mVisibleBound = false;
2174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 mInputShown = false;
2176 mShowRequested = false;
2177 mShowExplicitlyRequested = false;
2178 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002179 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002181
satok42c5a162011-05-26 16:46:14 +09002182 @Override
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002183 public InputBindResult windowGainedFocus(
2184 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2185 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2186 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002187 // Needs to check the validity before clearing calling identity
2188 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002189 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 long ident = Binder.clearCallingIdentity();
2191 try {
2192 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002193 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2194 + InputMethodClient.getStartInputReason(startInputReason)
2195 + " client=" + client.asBinder()
2196 + " inputContext=" + inputContext
2197 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002198 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002200 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002201
Dianne Hackborn7663d802012-02-24 13:08:49 -08002202 ClientState cs = mClients.get(client.asBinder());
2203 if (cs == null) {
2204 throw new IllegalArgumentException("unknown client "
2205 + client.asBinder());
2206 }
2207
2208 try {
2209 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2210 // Check with the window manager to make sure this client actually
2211 // has a window with focus. If not, reject. This is thread safe
2212 // because if the focus changes some time before or after, the
2213 // next client receiving focus that has any interest in input will
2214 // be calling through here after that change happens.
2215 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2216 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2217 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002219 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002221
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002222 if (!calledFromValidUser) {
2223 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2224 Slog.w(TAG, "If you want to interect with IME, you need "
2225 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2226 hideCurrentInputLocked(0, null);
2227 return null;
2228 }
2229
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002230 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002231 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002232 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002233 if (attribute != null) {
2234 return startInputUncheckedLocked(cs, inputContext, attribute,
2235 controlFlags);
2236 }
2237 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002238 }
2239 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002240 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002241
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002242 // Should we auto-show the IME even if the caller has not
2243 // specified what should be done with it?
2244 // We only do this automatically if the window can resize
2245 // to accommodate the IME (so what the user sees will give
2246 // them good context without input information being obscured
2247 // by the IME) or if running on a large screen where there
2248 // is more room for the target window + IME.
2249 final boolean doAutoShow =
2250 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2251 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2252 || mRes.getConfiguration().isLayoutSizeAtLeast(
2253 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002254 final boolean isTextEditor =
2255 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2256
2257 // We want to start input before showing the IME, but after closing
2258 // it. We want to do this after closing it to help the IME disappear
2259 // more quickly (not get stuck behind it initializing itself for the
2260 // new focused input, even if its window wants to hide the IME).
2261 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2264 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002265 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2267 // There is no focus view, and this window will
2268 // be behind any soft input window, so hide the
2269 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002270 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002271 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002273 } else if (isTextEditor && doAutoShow && (softInputMode &
2274 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 // There is a focus view, and we are navigating forward
2276 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002277 // We only do this automatically if the window can resize
2278 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002279 // them good context without input information being obscured
2280 // by the IME) or if running on a large screen where there
2281 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 if (attribute != null) {
2284 res = startInputUncheckedLocked(cs, inputContext, attribute,
2285 controlFlags);
2286 didStart = true;
2287 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002288 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
2290 break;
2291 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2292 // Do nothing.
2293 break;
2294 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2295 if ((softInputMode &
2296 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002297 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002298 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 }
2300 break;
2301 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002302 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002303 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 break;
2305 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2306 if ((softInputMode &
2307 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002308 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002309 if (attribute != null) {
2310 res = startInputUncheckedLocked(cs, inputContext, attribute,
2311 controlFlags);
2312 didStart = true;
2313 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002314 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
2316 break;
2317 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002318 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002319 if (attribute != null) {
2320 res = startInputUncheckedLocked(cs, inputContext, attribute,
2321 controlFlags);
2322 didStart = true;
2323 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002324 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 break;
2326 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002327
2328 if (!didStart && attribute != null) {
2329 res = startInputUncheckedLocked(cs, inputContext, attribute,
2330 controlFlags);
2331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
2333 } finally {
2334 Binder.restoreCallingIdentity(ident);
2335 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002336
2337 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002339
satok42c5a162011-05-26 16:46:14 +09002340 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002341 public void showInputMethodPickerFromClient(
2342 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002343 if (!calledFromValidUser()) {
2344 return;
2345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 synchronized (mMethodMap) {
2347 if (mCurClient == null || client == null
2348 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002349 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002350 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
2352
satok440aab52010-11-25 09:43:11 +09002353 // Always call subtype picker, because subtype picker is a superset of input method
2354 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002355 mHandler.sendMessage(mCaller.obtainMessageI(
2356 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002357 }
2358 }
2359
satok42c5a162011-05-26 16:46:14 +09002360 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002362 if (!calledFromValidUser()) {
2363 return;
2364 }
satok28203512010-11-24 11:06:49 +09002365 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2366 }
2367
satok42c5a162011-05-26 16:46:14 +09002368 @Override
satok28203512010-11-24 11:06:49 +09002369 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002370 if (!calledFromValidUser()) {
2371 return;
2372 }
satok28203512010-11-24 11:06:49 +09002373 synchronized (mMethodMap) {
2374 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002375 setInputMethodWithSubtypeIdLocked(token, id,
2376 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2377 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002378 } else {
2379 setInputMethod(token, id);
2380 }
2381 }
satokab751aa2010-09-14 19:17:36 +09002382 }
2383
satok42c5a162011-05-26 16:46:14 +09002384 @Override
satokb416a712010-11-25 20:42:14 +09002385 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002386 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002387 if (!calledFromValidUser()) {
2388 return;
2389 }
satokb416a712010-11-25 20:42:14 +09002390 synchronized (mMethodMap) {
2391 if (mCurClient == null || client == null
2392 || mCurClient.client.asBinder() != client.asBinder()) {
2393 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2394 }
satok7fee71f2010-12-17 18:54:26 +09002395 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2396 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002397 }
2398 }
2399
satok4fc87d62011-05-20 16:13:43 +09002400 @Override
satok735cf382010-11-11 20:40:09 +09002401 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002402 if (!calledFromValidUser()) {
2403 return false;
2404 }
satok735cf382010-11-11 20:40:09 +09002405 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002406 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002407 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002408 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002409 lastImi = mMethodMap.get(lastIme.first);
2410 } else {
2411 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002412 }
satok4fc87d62011-05-20 16:13:43 +09002413 String targetLastImiId = null;
2414 int subtypeId = NOT_A_SUBTYPE_ID;
2415 if (lastIme != null && lastImi != null) {
2416 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2417 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2418 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2419 : mCurrentSubtype.hashCode();
2420 // If the last IME is the same as the current IME and the last subtype is not
2421 // defined, there is no need to switch to the last IME.
2422 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2423 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002424 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002425 }
2426 }
2427
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002428 if (TextUtils.isEmpty(targetLastImiId)
2429 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002430 // This is a safety net. If the currentSubtype can't be added to the history
2431 // and the framework couldn't find the last ime, we will make the last ime be
2432 // the most applicable enabled keyboard subtype of the system imes.
2433 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2434 if (enabled != null) {
2435 final int N = enabled.size();
2436 final String locale = mCurrentSubtype == null
2437 ? mRes.getConfiguration().locale.toString()
2438 : mCurrentSubtype.getLocale();
2439 for (int i = 0; i < N; ++i) {
2440 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002441 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002442 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002443 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2444 InputMethodUtils.getSubtypes(imi),
2445 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002446 if (keyboardSubtype != null) {
2447 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002448 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002449 imi, keyboardSubtype.hashCode());
2450 if(keyboardSubtype.getLocale().equals(locale)) {
2451 break;
2452 }
2453 }
2454 }
2455 }
2456 }
2457 }
2458
2459 if (!TextUtils.isEmpty(targetLastImiId)) {
2460 if (DEBUG) {
2461 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2462 + ", from: " + mCurMethodId + ", " + subtypeId);
2463 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002464 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002465 return true;
2466 } else {
2467 return false;
2468 }
satok735cf382010-11-11 20:40:09 +09002469 }
2470 }
2471
satoke7c6998e2011-06-03 17:57:59 +09002472 @Override
satok688bd472012-02-09 20:09:17 +09002473 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002474 if (!calledFromValidUser()) {
2475 return false;
2476 }
satok688bd472012-02-09 20:09:17 +09002477 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002478 if (!calledWithValidToken(token)) {
2479 final int uid = Binder.getCallingUid();
2480 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2481 + " token:" + token);
2482 return false;
2483 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002484 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002485 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2486 if (nextSubtype == null) {
2487 return false;
2488 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002489 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2490 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002491 return true;
2492 }
2493 }
2494
2495 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002496 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2497 if (!calledFromValidUser()) {
2498 return false;
2499 }
2500 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002501 if (!calledWithValidToken(token)) {
2502 final int uid = Binder.getCallingUid();
2503 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2504 + "token. uid:" + uid + " token:" + token);
2505 return false;
2506 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002507 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002508 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2509 if (nextSubtype == null) {
2510 return false;
2511 }
2512 return true;
2513 }
2514 }
2515
2516 @Override
satok68f1b782011-04-11 14:26:04 +09002517 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002518 if (!calledFromValidUser()) {
2519 return null;
2520 }
satok68f1b782011-04-11 14:26:04 +09002521 synchronized (mMethodMap) {
2522 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2523 // TODO: Handle the case of the last IME with no subtypes
2524 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2525 || TextUtils.isEmpty(lastIme.second)) return null;
2526 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2527 if (lastImi == null) return null;
2528 try {
2529 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002530 final int lastSubtypeId =
2531 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002532 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2533 return null;
2534 }
2535 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002536 } catch (NumberFormatException e) {
2537 return null;
2538 }
2539 }
2540 }
2541
satoke7c6998e2011-06-03 17:57:59 +09002542 @Override
satokee5e77c2011-09-02 18:50:15 +09002543 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002544 if (!calledFromValidUser()) {
2545 return;
2546 }
satok91e88122011-07-18 11:11:42 +09002547 // By this IPC call, only a process which shares the same uid with the IME can add
2548 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002549 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002550 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002551 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002552 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002553 final String[] packageInfos;
2554 try {
2555 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2556 } catch (RemoteException e) {
2557 Slog.e(TAG, "Failed to get package infos");
2558 return;
2559 }
satok91e88122011-07-18 11:11:42 +09002560 if (packageInfos != null) {
2561 final int packageNum = packageInfos.length;
2562 for (int i = 0; i < packageNum; ++i) {
2563 if (packageInfos[i].equals(imi.getPackageName())) {
2564 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002565 final long ident = Binder.clearCallingIdentity();
2566 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002567 buildInputMethodListLocked(mMethodList, mMethodMap,
2568 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002569 } finally {
2570 Binder.restoreCallingIdentity(ident);
2571 }
satokee5e77c2011-09-02 18:50:15 +09002572 return;
satok91e88122011-07-18 11:11:42 +09002573 }
2574 }
2575 }
satoke7c6998e2011-06-03 17:57:59 +09002576 }
satokee5e77c2011-09-02 18:50:15 +09002577 return;
satoke7c6998e2011-06-03 17:57:59 +09002578 }
2579
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002580 @Override
2581 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002582 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002583 }
2584
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002585 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002586 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002587 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002588 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002589 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002590 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002591 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2592 if (DEBUG) {
2593 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2594 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2595 + " actual: " + sequenceNumber);
2596 }
2597 return;
2598 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002599 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2600 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002601 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002602 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002603 }
2604 }
2605
satok28203512010-11-24 11:06:49 +09002606 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002608 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2609 }
2610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002612 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2613 if (token == null) {
2614 if (mContext.checkCallingOrSelfPermission(
2615 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2616 != PackageManager.PERMISSION_GRANTED) {
2617 throw new SecurityException(
2618 "Using null token requires permission "
2619 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002621 } else if (mCurToken != token) {
2622 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2623 + " token: " + token);
2624 return;
2625 }
2626
2627 final long ident = Binder.clearCallingIdentity();
2628 try {
2629 setInputMethodLocked(id, subtypeId);
2630 } finally {
2631 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 }
2633 }
2634
satok42c5a162011-05-26 16:46:14 +09002635 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002637 if (!calledFromValidUser()) {
2638 return;
2639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002641 if (!calledWithValidToken(token)) {
2642 final int uid = Binder.getCallingUid();
2643 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2644 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 return;
2646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 long ident = Binder.clearCallingIdentity();
2648 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002649 hideCurrentInputLocked(flags, null);
2650 } finally {
2651 Binder.restoreCallingIdentity(ident);
2652 }
2653 }
2654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002655
satok42c5a162011-05-26 16:46:14 +09002656 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002657 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002658 if (!calledFromValidUser()) {
2659 return;
2660 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002661 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002662 if (!calledWithValidToken(token)) {
2663 final int uid = Binder.getCallingUid();
2664 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2665 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002666 return;
2667 }
2668 long ident = Binder.clearCallingIdentity();
2669 try {
2670 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 } finally {
2672 Binder.restoreCallingIdentity(ident);
2673 }
2674 }
2675 }
2676
2677 void setEnabledSessionInMainThread(SessionState session) {
2678 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002679 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002681 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002682 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 } catch (RemoteException e) {
2684 }
2685 }
2686 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002687 if (mEnabledSession != null && mEnabledSession.session != null) {
2688 try {
2689 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2690 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2691 } catch (RemoteException e) {
2692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 }
2694 }
2695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002696
satok42c5a162011-05-26 16:46:14 +09002697 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002699 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002701 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002702 final boolean showAuxSubtypes;
2703 switch (msg.arg1) {
2704 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2705 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2706 // implemented so that auxiliary subtypes will be excluded when the soft
2707 // keyboard is invisible.
2708 showAuxSubtypes = mInputShown;
2709 break;
2710 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2711 showAuxSubtypes = true;
2712 break;
2713 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2714 showAuxSubtypes = false;
2715 break;
2716 default:
2717 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2718 return false;
2719 }
2720 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002721 return true;
2722
satok47a44912010-10-06 16:03:58 +09002723 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002725 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002726 args.recycle();
satok217f5482010-12-15 05:19:19 +09002727 return true;
2728
2729 case MSG_SHOW_IM_CONFIG:
2730 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002731 return true;
2732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 case MSG_UNBIND_INPUT:
2736 try {
2737 ((IInputMethod)msg.obj).unbindInput();
2738 } catch (RemoteException e) {
2739 // There is nothing interesting about the method dying.
2740 }
2741 return true;
2742 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002743 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 try {
2745 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2746 } catch (RemoteException e) {
2747 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002748 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 return true;
2750 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002751 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002753 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002754 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002755 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 } catch (RemoteException e) {
2757 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002758 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 return true;
2760 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002761 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002763 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002764 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002765 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 } catch (RemoteException e) {
2767 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002768 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 return true;
2770 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002771 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002773 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2775 } catch (RemoteException e) {
2776 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002777 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002779 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002780 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002781 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002782 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002784 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002786 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002787 // Dispose the channel if the input method is not local to this process
2788 // because the remote proxy will get its own copy when unparceled.
2789 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002790 channel.dispose();
2791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002793 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002799 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 try {
2801 SessionState session = (SessionState)args.arg1;
2802 setEnabledSessionInMainThread(session);
2803 session.method.startInput((IInputContext)args.arg2,
2804 (EditorInfo)args.arg3);
2805 } catch (RemoteException e) {
2806 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002807 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 return true;
2809 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002810 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 try {
2812 SessionState session = (SessionState)args.arg1;
2813 setEnabledSessionInMainThread(session);
2814 session.method.restartInput((IInputContext)args.arg2,
2815 (EditorInfo)args.arg3);
2816 } catch (RemoteException e) {
2817 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002818 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002822
Yohei Yukawa33e81792015-11-17 21:14:42 -08002823 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002825 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 } catch (RemoteException e) {
2827 // There is nothing interesting about the last client dying.
2828 }
2829 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002830 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002832 IInputMethodClient client = (IInputMethodClient)args.arg1;
2833 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002835 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002837 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002838 } finally {
2839 // Dispose the channel if the input method is not local to this process
2840 // because the remote proxy will get its own copy when unparceled.
2841 if (res.channel != null && Binder.isProxy(client)) {
2842 res.channel.dispose();
2843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002845 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002847 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002848 case MSG_SET_ACTIVE:
2849 try {
2850 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2851 } catch (RemoteException e) {
2852 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2853 + ((ClientState)msg.obj).pid + " uid "
2854 + ((ClientState)msg.obj).uid);
2855 }
2856 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002857 case MSG_SET_INTERACTIVE:
2858 handleSetInteractive(msg.arg1 != 0);
2859 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002860 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2861 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002862 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002863 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002864 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002865 } catch (RemoteException e) {
2866 Slog.w(TAG, "Got RemoteException sending "
2867 + "setUserActionNotificationSequenceNumber("
2868 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002869 + clientState.pid + " uid "
2870 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002871 }
2872 return true;
2873 }
satok01038492012-04-09 21:08:27 +09002874
2875 // --------------------------------------------------------------
2876 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002877 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002878 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
2880 return false;
2881 }
2882
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002883 private void handleSetInteractive(final boolean interactive) {
2884 synchronized (mMethodMap) {
2885 mIsInteractive = interactive;
2886 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2887
2888 // Inform the current client of the change in active status
2889 if (mCurClient != null && mCurClient.client != null) {
2890 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2891 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2892 }
2893 }
2894 }
2895
satokdc9ddae2011-10-06 12:22:36 +09002896 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002897 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2898 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002899 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002900 if (DEBUG) {
2901 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2902 }
satok723a27e2010-11-11 14:58:11 +09002903 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002904 return true;
2905 }
2906
2907 return false;
2908 }
2909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002911 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002912 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002913 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002914 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 list.clear();
2917 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002918
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002919 // Use for queryIntentServicesAsUser
2920 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002922 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002924 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2925 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002926
satoke7c6998e2011-06-03 17:57:59 +09002927 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2928 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 for (int i = 0; i < services.size(); ++i) {
2930 ResolveInfo ri = services.get(i);
2931 ServiceInfo si = ri.serviceInfo;
2932 ComponentName compName = new ComponentName(si.packageName, si.name);
2933 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2934 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002935 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 + ": it does not require the permission "
2937 + android.Manifest.permission.BIND_INPUT_METHOD);
2938 continue;
2939 }
2940
Joe Onorato8a9b2202010-02-26 18:56:32 -08002941 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942
2943 try {
satoke7c6998e2011-06-03 17:57:59 +09002944 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002946 final String id = p.getId();
2947 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948
2949 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002950 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002952 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002953 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002956
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002957 if (resetDefaultEnabledIme) {
2958 final ArrayList<InputMethodInfo> defaultEnabledIme =
2959 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2960 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2961 final InputMethodInfo imi = defaultEnabledIme.get(i);
2962 if (DEBUG) {
2963 Slog.d(TAG, "--- enable ime = " + imi);
2964 }
2965 setInputMethodEnabledLocked(imi.getId(), true);
2966 }
2967 }
2968
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002969 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002970 if (!TextUtils.isEmpty(defaultImiId)) {
2971 if (!map.containsKey(defaultImiId)) {
2972 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2973 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002974 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002975 }
2976 } else {
2977 // Double check that the default IME is certainly enabled.
2978 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002979 }
2980 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002981 // Here is not the perfect place to reset the switching controller. Ideally
2982 // mSwitchingController and mSettings should be able to share the same state.
2983 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2984 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002985 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
satok217f5482010-12-15 05:19:19 +09002990 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002991 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002992 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002993 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2994 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002995 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002996 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002997 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002998 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002999 }
3000
3001 private void showConfigureInputMethods() {
3002 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3003 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3004 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3005 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003006 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003007 }
3008
satok2c93efc2012-04-02 19:33:47 +09003009 private boolean isScreenLocked() {
3010 return mKeyguardManager != null
3011 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3012 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003013
Seigo Nonaka14e13912015-05-06 21:04:13 -07003014 private void showInputMethodMenu(boolean showAuxSubtypes) {
3015 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003018 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003019
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003021 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003022 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003023
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003024 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003025 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003026 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3027 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003028 if (immis == null || immis.size() == 0) {
3029 return;
3030 }
3031
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003032 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033
satok688bd472012-02-09 20:09:17 +09003034 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003035 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07003036 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003037
satokc3690562012-01-10 20:14:43 +09003038 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003039 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003040 if (currentSubtype != null) {
3041 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003042 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3043 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003044 }
3045 }
3046
Ken Wakasa761eb372011-03-04 19:06:18 +09003047 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003048 mIms = new InputMethodInfo[N];
3049 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003050 int checkedItem = 0;
3051 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003052 final ImeSubtypeListItem item = imList.get(i);
3053 mIms[i] = item.mImi;
3054 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003055 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003056 int subtypeId = mSubtypeIds[i];
3057 if ((subtypeId == NOT_A_SUBTYPE_ID)
3058 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3059 || (subtypeId == lastInputMethodSubtypeId)) {
3060 checkedItem = i;
3061 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003064
3065 final Context settingsContext = new ContextThemeWrapper(context,
3066 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3067
3068 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003069 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3070 @Override
3071 public void onCancel(DialogInterface dialog) {
3072 hideInputMethodMenu();
3073 }
3074 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003075
3076 final Context dialogContext = mDialogBuilder.getContext();
3077 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3078 com.android.internal.R.styleable.DialogPreference,
3079 com.android.internal.R.attr.alertDialogStyle, 0);
3080 final Drawable dialogIcon = a.getDrawable(
3081 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3082 a.recycle();
3083
3084 mDialogBuilder.setIcon(dialogIcon);
3085
3086 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3087 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003088 final View tv = inflater.inflate(
3089 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3090 mDialogBuilder.setCustomTitle(tv);
3091
3092 // Setup layout for a toggle switch of the hardware keyboard
3093 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003094 mSwitchingDialogTitleView
3095 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003096 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003097 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003098 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003099 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003100 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003101 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3102 @Override
3103 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003104 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003105 // Ensure that the input method dialog is dismissed when changing
3106 // the hardware keyboard state.
3107 hideInputMethodMenu();
3108 }
3109 });
3110
Alan Viverette505e3ab2014-11-24 15:22:11 -08003111 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003112 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3113 final OnClickListener choiceListener = new OnClickListener() {
3114 @Override
3115 public void onClick(final DialogInterface dialog, final int which) {
3116 synchronized (mMethodMap) {
3117 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3118 || mSubtypeIds.length <= which) {
3119 return;
satok01038492012-04-09 21:08:27 +09003120 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003121 final InputMethodInfo im = mIms[which];
3122 int subtypeId = mSubtypeIds[which];
3123 adapter.mCheckedItem = which;
3124 adapter.notifyDataSetChanged();
3125 hideInputMethodMenu();
3126 if (im != null) {
3127 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3128 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003129 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003130 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003132 }
3133 }
3134 };
3135 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003137 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003138 final OnClickListener positiveListener = new OnClickListener() {
3139 @Override
3140 public void onClick(DialogInterface dialog, int whichButton) {
3141 showConfigureInputMethods();
3142 }
3143 };
satok82beadf2010-12-27 19:03:06 +09003144 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003145 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003148 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 mSwitchingDialog.getWindow().setType(
3150 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003151 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3152 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003153 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003154 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 mSwitchingDialog.show();
3156 }
3157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003158
Ken Wakasa05dbb652011-08-22 15:22:43 +09003159 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3160 private final LayoutInflater mInflater;
3161 private final int mTextViewResourceId;
3162 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003163 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003164 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3165 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3166 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003167
Ken Wakasa05dbb652011-08-22 15:22:43 +09003168 mTextViewResourceId = textViewResourceId;
3169 mItemsList = itemsList;
3170 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003171 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003172 }
3173
3174 @Override
3175 public View getView(int position, View convertView, ViewGroup parent) {
3176 final View view = convertView != null ? convertView
3177 : mInflater.inflate(mTextViewResourceId, null);
3178 if (position < 0 || position >= mItemsList.size()) return view;
3179 final ImeSubtypeListItem item = mItemsList.get(position);
3180 final CharSequence imeName = item.mImeName;
3181 final CharSequence subtypeName = item.mSubtypeName;
3182 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3183 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3184 if (TextUtils.isEmpty(subtypeName)) {
3185 firstTextView.setText(imeName);
3186 secondTextView.setVisibility(View.GONE);
3187 } else {
3188 firstTextView.setText(subtypeName);
3189 secondTextView.setText(imeName);
3190 secondTextView.setVisibility(View.VISIBLE);
3191 }
3192 final RadioButton radioButton =
3193 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3194 radioButton.setChecked(position == mCheckedItem);
3195 return view;
3196 }
3197 }
3198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003200 synchronized (mMethodMap) {
3201 hideInputMethodMenuLocked();
3202 }
3203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003204
The Android Open Source Project10592532009-03-18 17:39:46 -07003205 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003206 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207
The Android Open Source Project10592532009-03-18 17:39:46 -07003208 if (mSwitchingDialog != null) {
3209 mSwitchingDialog.dismiss();
3210 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003212
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003213 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003214 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003215 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003219
satok42c5a162011-05-26 16:46:14 +09003220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003222 // TODO: Make this work even for non-current users?
3223 if (!calledFromValidUser()) {
3224 return false;
3225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 synchronized (mMethodMap) {
3227 if (mContext.checkCallingOrSelfPermission(
3228 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3229 != PackageManager.PERMISSION_GRANTED) {
3230 throw new SecurityException(
3231 "Requires permission "
3232 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3233 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 long ident = Binder.clearCallingIdentity();
3236 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003237 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 } finally {
3239 Binder.restoreCallingIdentity(ident);
3240 }
3241 }
3242 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003243
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003244 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3245 // Make sure this is a valid input method.
3246 InputMethodInfo imm = mMethodMap.get(id);
3247 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003248 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003249 }
3250
satokd87c2592010-09-29 11:52:06 +09003251 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3252 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003253
satokd87c2592010-09-29 11:52:06 +09003254 if (enabled) {
3255 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3256 if (pair.first.equals(id)) {
3257 // We are enabling this input method, but it is already enabled.
3258 // Nothing to do. The previous state was enabled.
3259 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003260 }
3261 }
satokd87c2592010-09-29 11:52:06 +09003262 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3263 // Previous state was disabled.
3264 return false;
3265 } else {
3266 StringBuilder builder = new StringBuilder();
3267 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3268 builder, enabledInputMethodsList, id)) {
3269 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003270 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003271 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3272 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3273 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003274 }
3275 // Previous state was enabled.
3276 return true;
3277 } else {
3278 // We are disabling the input method but it is already disabled.
3279 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003280 return false;
3281 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003282 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003283 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003284
satok723a27e2010-11-11 14:58:11 +09003285 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3286 boolean setSubtypeOnly) {
3287 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003288 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003289
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003290 mCurUserActionNotificationSequenceNumber =
3291 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3292 if (DEBUG) {
3293 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3294 + mCurUserActionNotificationSequenceNumber);
3295 }
3296
3297 if (mCurClient != null && mCurClient.client != null) {
3298 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3299 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003300 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003301 }
3302
satok723a27e2010-11-11 14:58:11 +09003303 // Set Subtype here
3304 if (imi == null || subtypeId < 0) {
3305 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003306 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003307 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003308 if (subtypeId < imi.getSubtypeCount()) {
3309 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3310 mSettings.putSelectedSubtype(subtype.hashCode());
3311 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003312 } else {
3313 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003314 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003315 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003316 }
satokab751aa2010-09-14 19:17:36 +09003317 }
satok723a27e2010-11-11 14:58:11 +09003318
satok4c0e7152012-06-20 20:08:44 +09003319 // Workaround.
3320 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3321 // IMEs are not recognized and considered uninstalled.
3322 // Actually, we can't move everything after SystemReady because
3323 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3324 // the default IME here and try cheking the default IME again in systemReady().
3325 // TODO: Do nothing before system ready and implement a separated logic for
3326 // the encryption lock screen.
3327 // TODO: ASEC should be ready before IMMS is instantiated.
3328 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003329 // Set InputMethod here
3330 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3331 }
3332 }
3333
3334 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3335 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3336 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3337 // newDefaultIme is empty when there is no candidate for the selected IME.
3338 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3339 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3340 if (subtypeHashCode != null) {
3341 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003342 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003343 imi, Integer.valueOf(subtypeHashCode));
3344 } catch (NumberFormatException e) {
3345 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3346 }
3347 }
3348 }
3349 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003350 }
3351
satok4e4569d2010-11-19 18:45:53 +09003352 // If there are no selected shortcuts, tries finding the most applicable ones.
3353 private Pair<InputMethodInfo, InputMethodSubtype>
3354 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3355 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3356 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003357 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003358 boolean foundInSystemIME = false;
3359
3360 // Search applicable subtype for each InputMethodInfo
3361 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003362 final String imiId = imi.getId();
3363 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3364 continue;
3365 }
satokcd7cd292010-11-20 15:46:23 +09003366 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003367 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003368 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003369 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003370 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003371 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003372 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003373 }
satokdf31ae62011-01-15 06:19:44 +09003374 // 2. Search by the system locale from enabledSubtypes.
3375 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003376 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003377 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003378 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003379 }
satoka86f5e42011-09-02 17:12:42 +09003380 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003381 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003382 final ArrayList<InputMethodSubtype> subtypesForSearch =
3383 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003384 ? InputMethodUtils.getSubtypes(imi)
3385 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003386 // 4. Search by the current subtype's locale from all subtypes.
3387 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003388 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003389 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003390 }
3391 // 5. Search by the system locale from all subtypes.
3392 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003393 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003395 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003396 }
satokcd7cd292010-11-20 15:46:23 +09003397 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003398 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003399 // The current input method is the most applicable IME.
3400 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003401 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003402 break;
satok7599a7f2010-12-22 13:45:23 +09003403 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003404 // The system input method is 2nd applicable IME.
3405 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003406 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003407 if ((imi.getServiceInfo().applicationInfo.flags
3408 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3409 foundInSystemIME = true;
3410 }
satok4e4569d2010-11-19 18:45:53 +09003411 }
3412 }
3413 }
3414 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003415 if (mostApplicableIMI != null) {
3416 Slog.w(TAG, "Most applicable shortcut input method was:"
3417 + mostApplicableIMI.getId());
3418 if (mostApplicableSubtype != null) {
3419 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3420 + "," + mostApplicableSubtype.getMode() + ","
3421 + mostApplicableSubtype.getLocale());
3422 }
3423 }
satok4e4569d2010-11-19 18:45:53 +09003424 }
satokcd7cd292010-11-20 15:46:23 +09003425 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003426 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003427 } else {
3428 return null;
3429 }
3430 }
3431
satokab751aa2010-09-14 19:17:36 +09003432 /**
3433 * @return Return the current subtype of this input method.
3434 */
satok42c5a162011-05-26 16:46:14 +09003435 @Override
satokab751aa2010-09-14 19:17:36 +09003436 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003437 // TODO: Make this work even for non-current users?
3438 if (!calledFromValidUser()) {
3439 return null;
3440 }
3441 synchronized (mMethodMap) {
3442 return getCurrentInputMethodSubtypeLocked();
3443 }
3444 }
3445
3446 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003447 if (mCurMethodId == null) {
3448 return null;
3449 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003450 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003451 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3452 if (imi == null || imi.getSubtypeCount() == 0) {
3453 return null;
satok4e4569d2010-11-19 18:45:53 +09003454 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003455 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003456 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3457 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003458 if (subtypeId == NOT_A_SUBTYPE_ID) {
3459 // If there are no selected subtypes, the framework will try to find
3460 // the most applicable subtype from explicitly or implicitly enabled
3461 // subtypes.
3462 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003463 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003464 // If there is only one explicitly or implicitly enabled subtype,
3465 // just returns it.
3466 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3467 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3468 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003469 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003470 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003472 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003473 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003474 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3475 true);
satok4e4569d2010-11-19 18:45:53 +09003476 }
satok3ef8b292010-11-23 06:06:29 +09003477 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003478 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003480 }
3481 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003482 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003483 }
3484
satok4e4569d2010-11-19 18:45:53 +09003485 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003486 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003487 @Override
satok4e4569d2010-11-19 18:45:53 +09003488 public List getShortcutInputMethodsAndSubtypes() {
3489 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003490 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003491 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003492 // If there are no selected shortcut subtypes, the framework will try to find
3493 // the most applicable subtype from all subtypes whose mode is
3494 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003495 Pair<InputMethodInfo, InputMethodSubtype> info =
3496 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003497 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003498 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003499 ret.add(info.first);
3500 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003501 }
satok3da92232011-01-11 22:46:30 +09003502 return ret;
satokf3db1af2010-11-23 13:34:33 +09003503 }
satokf3db1af2010-11-23 13:34:33 +09003504 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3505 ret.add(imi);
3506 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3507 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003508 }
3509 }
satokf3db1af2010-11-23 13:34:33 +09003510 return ret;
satok4e4569d2010-11-19 18:45:53 +09003511 }
3512 }
3513
satok42c5a162011-05-26 16:46:14 +09003514 @Override
satokb66d2872010-11-10 01:04:04 +09003515 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003516 // TODO: Make this work even for non-current users?
3517 if (!calledFromValidUser()) {
3518 return false;
3519 }
satokb66d2872010-11-10 01:04:04 +09003520 synchronized (mMethodMap) {
3521 if (subtype != null && mCurMethodId != null) {
3522 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003523 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003524 if (subtypeId != NOT_A_SUBTYPE_ID) {
3525 setInputMethodLocked(mCurMethodId, subtypeId);
3526 return true;
3527 }
3528 }
3529 return false;
3530 }
3531 }
3532
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003533 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003534 private static class InputMethodFileManager {
3535 private static final String SYSTEM_PATH = "system";
3536 private static final String INPUT_METHOD_PATH = "inputmethod";
3537 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3538 private static final String NODE_SUBTYPES = "subtypes";
3539 private static final String NODE_SUBTYPE = "subtype";
3540 private static final String NODE_IMI = "imi";
3541 private static final String ATTR_ID = "id";
3542 private static final String ATTR_LABEL = "label";
3543 private static final String ATTR_ICON = "icon";
3544 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3545 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3546 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3547 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3548 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3549 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003550 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003551 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003552 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003553 if (methodMap == null) {
3554 throw new NullPointerException("methodMap is null");
3555 }
3556 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003557 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003558 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3559 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003560 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003561 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003562 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3563 }
3564 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3565 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3566 if (!subtypeFile.exists()) {
3567 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003568 writeAdditionalInputMethodSubtypes(
3569 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003570 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003571 readAdditionalInputMethodSubtypes(
3572 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003573 }
3574 }
3575
3576 private void deleteAllInputMethodSubtypes(String imiId) {
3577 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003578 mAdditionalSubtypesMap.remove(imiId);
3579 writeAdditionalInputMethodSubtypes(
3580 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003581 }
3582 }
3583
3584 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003585 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003586 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003587 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003588 final int N = additionalSubtypes.length;
3589 for (int i = 0; i < N; ++i) {
3590 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003591 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003592 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003593 } else {
3594 Slog.w(TAG, "Duplicated subtype definition found: "
3595 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003596 }
3597 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003598 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3599 writeAdditionalInputMethodSubtypes(
3600 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003601 }
3602 }
3603
3604 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3605 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003606 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003607 }
3608 }
3609
3610 private static void writeAdditionalInputMethodSubtypes(
3611 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3612 HashMap<String, InputMethodInfo> methodMap) {
3613 // Safety net for the case that this function is called before methodMap is set.
3614 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3615 FileOutputStream fos = null;
3616 try {
3617 fos = subtypesFile.startWrite();
3618 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003619 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003620 out.startDocument(null, true);
3621 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3622 out.startTag(null, NODE_SUBTYPES);
3623 for (String imiId : allSubtypes.keySet()) {
3624 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3625 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3626 continue;
3627 }
3628 out.startTag(null, NODE_IMI);
3629 out.attribute(null, ATTR_ID, imiId);
3630 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3631 final int N = subtypesList.size();
3632 for (int i = 0; i < N; ++i) {
3633 final InputMethodSubtype subtype = subtypesList.get(i);
3634 out.startTag(null, NODE_SUBTYPE);
3635 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3636 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3637 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3638 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3639 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3640 out.attribute(null, ATTR_IS_AUXILIARY,
3641 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3642 out.endTag(null, NODE_SUBTYPE);
3643 }
3644 out.endTag(null, NODE_IMI);
3645 }
3646 out.endTag(null, NODE_SUBTYPES);
3647 out.endDocument();
3648 subtypesFile.finishWrite(fos);
3649 } catch (java.io.IOException e) {
3650 Slog.w(TAG, "Error writing subtypes", e);
3651 if (fos != null) {
3652 subtypesFile.failWrite(fos);
3653 }
3654 }
3655 }
3656
3657 private static void readAdditionalInputMethodSubtypes(
3658 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3659 if (allSubtypes == null || subtypesFile == null) return;
3660 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003661 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003662 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003663 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003664 int type = parser.getEventType();
3665 // Skip parsing until START_TAG
3666 while ((type = parser.next()) != XmlPullParser.START_TAG
3667 && type != XmlPullParser.END_DOCUMENT) {}
3668 String firstNodeName = parser.getName();
3669 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3670 throw new XmlPullParserException("Xml doesn't start with subtypes");
3671 }
3672 final int depth =parser.getDepth();
3673 String currentImiId = null;
3674 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3675 while (((type = parser.next()) != XmlPullParser.END_TAG
3676 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3677 if (type != XmlPullParser.START_TAG)
3678 continue;
3679 final String nodeName = parser.getName();
3680 if (NODE_IMI.equals(nodeName)) {
3681 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3682 if (TextUtils.isEmpty(currentImiId)) {
3683 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3684 continue;
3685 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003686 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003687 allSubtypes.put(currentImiId, tempSubtypesArray);
3688 } else if (NODE_SUBTYPE.equals(nodeName)) {
3689 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3690 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3691 continue;
3692 }
3693 final int icon = Integer.valueOf(
3694 parser.getAttributeValue(null, ATTR_ICON));
3695 final int label = Integer.valueOf(
3696 parser.getAttributeValue(null, ATTR_LABEL));
3697 final String imeSubtypeLocale =
3698 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3699 final String imeSubtypeMode =
3700 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3701 final String imeSubtypeExtraValue =
3702 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003703 final boolean isAuxiliary = "1".equals(String.valueOf(
3704 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003705 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3706 .setSubtypeNameResId(label)
3707 .setSubtypeIconResId(icon)
3708 .setSubtypeLocale(imeSubtypeLocale)
3709 .setSubtypeMode(imeSubtypeMode)
3710 .setSubtypeExtraValue(imeSubtypeExtraValue)
3711 .setIsAuxiliary(isAuxiliary)
3712 .build();
satoke7c6998e2011-06-03 17:57:59 +09003713 tempSubtypesArray.add(subtype);
3714 }
3715 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003716 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3717 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003718 return;
satoke7c6998e2011-06-03 17:57:59 +09003719 }
3720 }
3721 }
3722
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003723 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3724 @NonNull
3725 private final Handler mHandler;
3726
3727 LocalServiceImpl(@NonNull final Handler handler) {
3728 mHandler = handler;
3729 }
3730
3731 @Override
3732 public void setInteractive(boolean interactive) {
3733 // Do everything in handler so as not to block the caller.
3734 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3735 interactive ? 1 : 0, 0));
3736 }
3737 }
3738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 @Override
3740 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3741 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3742 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3745 + Binder.getCallingPid()
3746 + ", uid=" + Binder.getCallingUid());
3747 return;
3748 }
3749
3750 IInputMethod method;
3751 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003752 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 synchronized (mMethodMap) {
3757 p.println("Current Input Method Manager state:");
3758 int N = mMethodList.size();
3759 p.println(" Input Methods:");
3760 for (int i=0; i<N; i++) {
3761 InputMethodInfo info = mMethodList.get(i);
3762 p.println(" InputMethod #" + i + ":");
3763 info.dump(p, " ");
3764 }
3765 p.println(" Clients:");
3766 for (ClientState ci : mClients.values()) {
3767 p.println(" Client " + ci + ":");
3768 p.println(" client=" + ci.client);
3769 p.println(" inputContext=" + ci.inputContext);
3770 p.println(" sessionRequested=" + ci.sessionRequested);
3771 p.println(" curSession=" + ci.curSession);
3772 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003773 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003775 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3776 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003777 focusedWindowClient = mCurFocusedWindowClient;
3778 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3780 + " mBoundToMethod=" + mBoundToMethod);
3781 p.println(" mCurToken=" + mCurToken);
3782 p.println(" mCurIntent=" + mCurIntent);
3783 method = mCurMethod;
3784 p.println(" mCurMethod=" + mCurMethod);
3785 p.println(" mEnabledSession=" + mEnabledSession);
3786 p.println(" mShowRequested=" + mShowRequested
3787 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3788 + " mShowForced=" + mShowForced
3789 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003790 p.println(" mCurUserActionNotificationSequenceNumber="
3791 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003792 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003793 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003794 p.println(" mSwitchingController:");
3795 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003797
Jeff Brownb88102f2010-09-08 11:49:43 -07003798 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 pw.flush();
3801 try {
3802 client.client.asBinder().dump(fd, args);
3803 } catch (RemoteException e) {
3804 p.println("Input method client dead: " + e);
3805 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003806 } else {
3807 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003809
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003810 if (focusedWindowClient != null && client != focusedWindowClient) {
3811 p.println(" ");
3812 p.println("Warning: Current input method client doesn't match the last focused. "
3813 + "window.");
3814 p.println("Dumping input method client in the last focused window just in case.");
3815 p.println(" ");
3816 pw.flush();
3817 try {
3818 focusedWindowClient.client.asBinder().dump(fd, args);
3819 } catch (RemoteException e) {
3820 p.println("Input method client in focused window dead: " + e);
3821 }
3822 }
3823
Jeff Brownb88102f2010-09-08 11:49:43 -07003824 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 pw.flush();
3827 try {
3828 method.asBinder().dump(fd, args);
3829 } catch (RemoteException e) {
3830 p.println("Input method service dead: " + e);
3831 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003832 } else {
3833 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 }
3835 }
3836}