blob: 40f7c5c5586cef387ac11f84c820b141a3eeb6df [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;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070077import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090078import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Handler;
80import android.os.IBinder;
81import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090082import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.Message;
84import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070085import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080087import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.ServiceManager;
89import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090090import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080091import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.provider.Settings;
93import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090094import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArrayMap;
96import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070097import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700112import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900113import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.inputmethod.InputBinding;
115import android.view.inputmethod.InputMethod;
116import android.view.inputmethod.InputMethodInfo;
117import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700118import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900119import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900120import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900122import android.widget.CompoundButton;
123import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900125import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900126import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
satoke7c6998e2011-06-03 17:57:59 +0900128import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900130import java.io.FileInputStream;
131import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.io.IOException;
133import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100134import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900136import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.HashMap;
138import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900139import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141/**
142 * This class provides a system service that manages input methods.
143 */
144public class InputMethodManagerService extends IInputMethodManager.Stub
145 implements ServiceConnection, Handler.Callback {
146 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700148 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700150 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
151 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
152 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int MSG_UNBIND_INPUT = 1000;
155 static final int MSG_BIND_INPUT = 1010;
156 static final int MSG_SHOW_SOFT_INPUT = 1020;
157 static final int MSG_HIDE_SOFT_INPUT = 1030;
158 static final int MSG_ATTACH_TOKEN = 1040;
159 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 static final int MSG_START_INPUT = 2000;
162 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
Yohei Yukawa33e81792015-11-17 21:14:42 -0800164 static final int MSG_UNBIND_CLIENT = 3000;
165 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700166 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700167 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900168 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
satok01038492012-04-09 21:08:27 +0900170 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
171
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700172 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
satokf9f01002011-05-19 21:31:50 +0900174 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
175
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900177 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900178
satok4e4569d2010-11-19 18:45:53 +0900179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800181 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900183 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700186 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700188 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900189 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900190 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900191 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900193 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 // All known input methods. mMethodMap also serves as the global
196 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700197 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
198 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900199 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700200 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900201 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700203 // Used to bring IME service up to visible adjustment while it is being shown.
204 final ServiceConnection mVisibleConnection = new ServiceConnection() {
205 @Override public void onServiceConnected(ComponentName name, IBinder service) {
206 }
207
208 @Override public void onServiceDisconnected(ComponentName name) {
209 }
210 };
211 boolean mVisibleBound = false;
212
satok7cfc0ed2011-06-20 21:29:36 +0900213 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700214 private NotificationManager mNotificationManager;
215 private KeyguardManager mKeyguardManager;
216 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400217 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700218 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900219 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900220 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900221 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900222
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900223 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 final ClientState client;
225 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700226
227 IInputMethodSession session;
228 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 @Override
231 public String toString() {
232 return "SessionState{uid " + client.uid + " pid " + client.pid
233 + " method " + Integer.toHexString(
234 System.identityHashCode(method))
235 + " session " + Integer.toHexString(
236 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700237 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 + "}";
239 }
240
241 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700242 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 client = _client;
244 method = _method;
245 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700246 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
Jeff Brownc28867a2013-03-26 15:42:39 -0700250 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final IInputMethodClient client;
252 final IInputContext inputContext;
253 final int uid;
254 final int pid;
255 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 boolean sessionRequested;
258 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 @Override
261 public String toString() {
262 return "ClientState{" + Integer.toHexString(
263 System.identityHashCode(this)) + " uid " + uid
264 + " pid " + pid + "}";
265 }
266
267 ClientState(IInputMethodClient _client, IInputContext _inputContext,
268 int _uid, int _pid) {
269 client = _client;
270 inputContext = _inputContext;
271 uid = _uid;
272 pid = _pid;
273 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
274 }
275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700277 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700280 * Set once the system is ready to run third party code.
281 */
282 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700284 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700285 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
286 * method. This is to be synchronized with the secure settings keyed with
287 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
288 *
289 * <p>This can be transiently {@code null} when the system is re-initializing input method
290 * settings, e.g., the system locale is just changed.</p>
291 *
292 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
293 * {@link InputMethodManagerService}.</p>
294 *
295 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700297 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * The current binding sequence number, incremented every time there is
302 * a new bind performed.
303 */
304 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
307 * The client that is currently bound to an input method.
308 */
309 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700312 * The last window token that gained focus.
313 */
314 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * The input context last provided by the current client.
318 */
319 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * The attributes last provided by the current client.
323 */
324 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700327 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700329 *
330 * <p>This can be {@code null} when no input method is connected.</p>
331 *
332 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700334 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
satokab751aa2010-09-14 19:17:36 +0900338 * The current subtype of the current input method.
339 */
340 private InputMethodSubtype mCurrentSubtype;
341
satok4e4569d2010-11-19 18:45:53 +0900342 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900343 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700344 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900345
John Spurlocke0980502013-10-25 11:59:29 -0400346 // Was the keyguard locked when this client became current?
347 private boolean mCurClientInKeyguard;
348
satokab751aa2010-09-14 19:17:36 +0900349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * Set to true if our ServiceConnection is currently actively bound to
351 * a service (whether or not we have gotten its IBinder back yet).
352 */
353 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Set if the client has asked for the input method to be shown.
357 */
358 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Set if we were explicitly told to show the input method.
362 */
363 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Set if we were forced to be shown.
367 */
368 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * Set if we last told the input method to show itself.
372 */
373 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * The Intent used to connect to the current input method.
377 */
378 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
381 * The token we have made for the currently active input method, to
382 * identify it in the future.
383 */
384 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * If non-null, this is the input method service we are currently connected
388 * to.
389 */
390 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Time that we last initiated a bind to the input method, to determine
394 * if we should try to disconnect and reconnect to it.
395 */
396 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
399 * Have we called mCurMethod.bindInput()?
400 */
401 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * Currently enabled session. Only touched by service thread, not
405 * protected by a lock.
406 */
407 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700410 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700412 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900414 int mCurUserActionNotificationSequenceNumber = 0;
415
Joe Onorato857fd9b2011-01-27 15:08:35 -0800416 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900417
418 /**
419 * A set of status bits regarding the active IME.
420 *
421 * <p>This value is a combination of following two bits:</p>
422 * <dl>
423 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
424 * <dd>
425 * If this bit is ON, connected IME is ready to accept touch/key events.
426 * </dd>
427 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
428 * <dd>
429 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
430 * </dd>
431 * </dl>
432 * <em>Do not update this value outside of setImeWindowStatus.</em>
433 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800434 int mImeWindowVis;
435
Ken Wakasa05dbb652011-08-22 15:22:43 +0900436 private AlertDialog.Builder mDialogBuilder;
437 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900438 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900439 private InputMethodInfo[] mIms;
440 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900441 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700442 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
444 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700447 int mUserId;
448 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800449 String mLastEnabled = "";
450
Yohei Yukawa81482972015-06-04 00:58:59 -0700451 /**
452 * <em>This constructor must be called within the lock.</em>
453 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 SettingsObserver(Handler handler) {
455 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700456 }
457
458 public void registerContentObserverLocked(int userId) {
459 if (mRegistered && mUserId == userId) {
460 return;
461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700463 if (mRegistered) {
464 mContext.getContentResolver().unregisterContentObserver(this);
465 mRegistered = false;
466 }
467 if (mUserId != userId) {
468 mLastEnabled = "";
469 mUserId = userId;
470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700472 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900473 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700474 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900475 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700476 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700477 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700478 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
479 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
Michael Wright7b5a96b2014-08-09 19:28:42 -0700482 @Override public void onChange(boolean selfChange, Uri uri) {
483 final Uri showImeUri =
484 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700486 if (showImeUri.equals(uri)) {
487 updateKeyboardFromSettingsLocked();
488 } else {
489 boolean enabledChanged = false;
490 String newEnabled = mSettings.getEnabledInputMethodsStr();
491 if (!mLastEnabled.equals(newEnabled)) {
492 mLastEnabled = newEnabled;
493 enabledChanged = true;
494 }
495 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700499
500 @Override
501 public String toString() {
502 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
503 + " mLastEnabled=" + mLastEnabled + "}";
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800506
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900507 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900508 @Override
509 public void onReceive(Context context, Intent intent) {
510 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700511 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900512 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700513 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900514 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800515 } else if (Intent.ACTION_USER_ADDED.equals(action)
516 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100517 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800518 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700519 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
520 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
521 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
522 final String prevValue = intent.getStringExtra(
523 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
524 final String newValue = intent.getStringExtra(
525 Intent.EXTRA_SETTING_NEW_VALUE);
526 restoreEnabledInputMethods(mContext, prevValue, newValue);
527 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900528 } else {
529 Slog.w(TAG, "Unexpected intent " + intent);
530 }
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800533
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700534 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
535 // does not attempt to validate on the fly with any installed device policy, so must only
536 // be run in the context of initial device setup.
537 //
538 // TODO: Move this method to InputMethodUtils with adding unit tests.
539 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
540 if (DEBUG_RESTORE) {
541 Slog.i(TAG, "Restoring enabled input methods:");
542 Slog.i(TAG, "prev=" + prevValue);
543 Slog.i(TAG, " new=" + newValue);
544 }
545 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900546 ArrayMap<String, ArraySet<String>> prevMap =
547 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
548 ArrayMap<String, ArraySet<String>> newMap =
549 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700550
551 // Merge the restored ime+subtype enabled states into the live state
552 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
553 final String imeId = entry.getKey();
554 ArraySet<String> prevSubtypes = prevMap.get(imeId);
555 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700556 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700557 prevMap.put(imeId, prevSubtypes);
558 }
559 prevSubtypes.addAll(entry.getValue());
560 }
561
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700562 final String mergedImesAndSubtypesString =
563 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700564 if (DEBUG_RESTORE) {
565 Slog.i(TAG, "Merged IME string:");
566 Slog.i(TAG, " " + mergedImesAndSubtypesString);
567 }
568 Settings.Secure.putString(context.getContentResolver(),
569 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
570 }
571
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800572 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900573 private boolean isChangingPackagesOfCurrentUser() {
574 final int userId = getChangingUserId();
575 final boolean retval = userId == mSettings.getCurrentUserId();
576 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900577 if (!retval) {
578 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
579 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900580 }
581 return retval;
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800585 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900586 if (!isChangingPackagesOfCurrentUser()) {
587 return false;
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900590 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 final int N = mMethodList.size();
592 if (curInputMethodId != null) {
593 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800594 InputMethodInfo imi = mMethodList.get(i);
595 if (imi.getId().equals(curInputMethodId)) {
596 for (String pkg : packages) {
597 if (imi.getPackageName().equals(pkg)) {
598 if (!doit) {
599 return true;
600 }
satok723a27e2010-11-11 14:58:11 +0900601 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800602 chooseNewDefaultIMELocked();
603 return true;
604 }
605 }
606 }
607 }
608 }
609 }
610 return false;
611 }
612
613 @Override
614 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900615 if (!isChangingPackagesOfCurrentUser()) {
616 return;
617 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800618 synchronized (mMethodMap) {
619 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900620 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800621 final int N = mMethodList.size();
622 if (curInputMethodId != null) {
623 for (int i=0; i<N; i++) {
624 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900625 final String imiId = imi.getId();
626 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800627 curIm = imi;
628 }
satoke7c6998e2011-06-03 17:57:59 +0900629
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800630 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900631 if (isPackageModified(imi.getPackageName())) {
632 mFileManager.deleteAllInputMethodSubtypes(imiId);
633 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 if (change == PACKAGE_TEMPORARY_CHANGE
635 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800636 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 + imi.getComponent());
638 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640 }
641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800642
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900643 buildInputMethodListLocked(
644 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800648 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800649 int change = isPackageDisappearing(curIm.getPackageName());
650 if (change == PACKAGE_TEMPORARY_CHANGE
651 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800652 ServiceInfo si = null;
653 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900654 si = mIPackageManager.getServiceInfo(
655 curIm.getComponent(), 0, mSettings.getCurrentUserId());
656 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800657 }
658 if (si == null) {
659 // Uh oh, current input method is no longer around!
660 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800661 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900662 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800664 changed = true;
665 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800666 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900667 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800668 }
669 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800670 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 }
satokab751aa2010-09-14 19:17:36 +0900672
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800673 if (curIm == null) {
674 // We currently don't have a default input method... is
675 // one now available?
676 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700677 } else if (!changed && isPackageModified(curIm.getPackageName())) {
678 // Even if the current input method is still available, mCurrentSubtype could
679 // be obsolete when the package is modified in practice.
680 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800684 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
686 }
687 }
688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800689
Jeff Brownc28867a2013-03-26 15:42:39 -0700690 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900691 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700692 private final IInputMethod mMethod;
693 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
Jeff Brownc28867a2013-03-26 15:42:39 -0700695 MethodCallback(InputMethodManagerService imms, IInputMethod method,
696 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900697 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700698 mMethod = method;
699 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
satoke7c6998e2011-06-03 17:57:59 +0900702 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700703 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700704 long ident = Binder.clearCallingIdentity();
705 try {
706 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
707 } finally {
708 Binder.restoreCallingIdentity(ident);
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
satok01038492012-04-09 21:08:27 +0900713 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700714 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900715 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700716 public void onHardKeyboardStatusChange(boolean available) {
717 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
718 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900719 }
720
Michael Wright7b5a96b2014-08-09 19:28:42 -0700721 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900722 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700723 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900724 }
725 synchronized(mMethodMap) {
726 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
727 && mSwitchingDialog.isShowing()) {
728 mSwitchingDialogTitleView.findViewById(
729 com.android.internal.R.id.hard_keyboard_section).setVisibility(
730 available ? View.VISIBLE : View.GONE);
731 }
732 }
733 }
734 }
735
Seigo Nonaka7309b122015-08-17 18:34:13 -0700736 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900737 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800739 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700741 // Note: SettingsObserver doesn't register observers in its constructor.
742 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 mIWindowManager = IWindowManager.Stub.asInterface(
744 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700745 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800746 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900747 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public void executeMessage(Message msg) {
749 handleMessage(msg);
750 }
Mita Yuned218c72012-12-06 17:18:25 -0800751 }, true /*asyncHandler*/);
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900752 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900753 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700754 mHasFeature = context.getPackageManager().hasSystemFeature(
755 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900756
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400757 Bundle extras = new Bundle();
758 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
759 mImeSwitcherNotification = new Notification.Builder(mContext)
760 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
761 .setWhen(0)
762 .setOngoing(true)
763 .addExtras(extras)
764 .setCategory(Notification.CATEGORY_SYSTEM)
765 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400766
satok7cfc0ed2011-06-20 21:29:36 +0900767 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900768 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900769
770 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900771
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800774 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
775 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700776 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900777 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
satok7cfc0ed2011-06-20 21:29:36 +0900779 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 int userId = 0;
781 try {
782 ActivityManagerNative.getDefault().registerUserSwitchObserver(
783 new IUserSwitchObserver.Stub() {
784 @Override
785 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900786 synchronized(mMethodMap) {
787 switchUserLocked(newUserId);
788 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 if (reply != null) {
790 try {
791 reply.sendResult(null);
792 } catch (RemoteException e) {
793 }
794 }
795 }
796
797 @Override
798 public void onUserSwitchComplete(int newUserId) throws RemoteException {
799 }
Kenny Guy42979622015-04-13 18:03:05 +0000800
801 @Override
802 public void onForegroundProfileSwitch(int newProfileId) {
803 // Ignore.
804 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900805 });
806 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
807 } catch (RemoteException e) {
808 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
809 }
satok81f8b7c2012-12-04 20:42:56 +0900810 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900811
satokd87c2592010-09-29 11:52:06 +0900812 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900813 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700815
816 // Let the package manager query which are the default imes
817 // as they get certain permissions granted by default.
818 PackageManagerInternal packageManagerInternal = LocalServices.getService(
819 PackageManagerInternal.class);
820 packageManagerInternal.setImePackagesProvider(
821 new PackageManagerInternal.PackagesProvider() {
822 @Override
823 public String[] getPackages(int userId) {
824 synchronized (mMethodMap) {
825 final int currentUserId = mSettings.getCurrentUserId();
826 // TODO: We are switching the current user id in the settings
827 // object to query it and then revert the user id. Ideally, we
828 // should call a API in settings with the user id as an argument.
829 mSettings.setCurrentUserId(userId);
830 List<InputMethodInfo> imes = mSettings
831 .getEnabledInputMethodListLocked();
832 String[] packageNames = null;
833 if (imes != null) {
834 final int imeCount = imes.size();
835 packageNames = new String[imeCount];
836 for (int i = 0; i < imeCount; i++) {
837 InputMethodInfo ime = imes.get(i);
838 packageNames[i] = ime.getPackageName();
839 }
840 }
841 mSettings.setCurrentUserId(currentUserId);
842 return packageNames;
843 }
844 }
845 });
846
Kenny Guy2a764942014-04-02 13:29:20 +0100847 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900848 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900849 synchronized (mMethodMap) {
850 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
851 mSettings, context);
852 }
satok0a1bcf42012-05-16 19:26:31 +0900853
854 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900855 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900856 if (DEBUG) {
857 Slog.d(TAG, "Initial default ime = " + defaultImiId);
858 }
satok0a1bcf42012-05-16 19:26:31 +0900859 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
860
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900861 synchronized (mMethodMap) {
862 buildInputMethodListLocked(mMethodList, mMethodMap,
863 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
864 }
satokd87c2592010-09-29 11:52:06 +0900865 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866
satok0a1bcf42012-05-16 19:26:31 +0900867 if (!mImeSelectedOnBoot) {
868 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900869 synchronized (mMethodMap) {
870 resetDefaultImeLocked(context);
871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900874 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700875 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900876 updateFromSettingsLocked(true);
877 }
satok5b927c432012-05-01 20:09:34 +0900878
879 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
880 // according to the new system locale.
881 final IntentFilter filter = new IntentFilter();
882 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
883 mContext.registerReceiver(
884 new BroadcastReceiver() {
885 @Override
886 public void onReceive(Context context, Intent intent) {
887 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900888 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900889 }
890 }
891 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700892 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900893 }
894
satok5b927c432012-05-01 20:09:34 +0900895 private void resetDefaultImeLocked(Context context) {
896 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900897 if (mCurMethodId != null
898 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900899 return;
900 }
901
902 InputMethodInfo defIm = null;
903 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900904 if (defIm == null && mSystemReady) {
905 final Locale systemLocale = context.getResources().getConfiguration().locale;
906 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
907 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
908 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900909 defIm = imi;
910 Slog.i(TAG, "Selected default: " + imi.getId());
911 }
912 }
913 }
914 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900915 defIm = InputMethodUtils.getMostApplicableDefaultIME(
916 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900917 if (defIm != null) {
918 Slog.i(TAG, "Default found, using " + defIm.getId());
919 } else {
920 Slog.i(TAG, "No default found");
921 }
satok5b927c432012-05-01 20:09:34 +0900922 }
923 if (defIm != null) {
924 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
925 }
926 }
927
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900928 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
929 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 if (!mSystemReady) {
931 // not system ready
932 return;
933 }
934 final Locale newLocale = mRes.getConfiguration().locale;
935 if (!updateOnlyWhenLocaleChanged
936 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
937 if (!updateOnlyWhenLocaleChanged) {
938 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800939 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
941 if (DEBUG) {
942 Slog.i(TAG, "Locale has been changed to " + newLocale);
943 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900944 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 if (!updateOnlyWhenLocaleChanged) {
946 final String selectedImiId = mSettings.getSelectedInputMethod();
947 if (TextUtils.isEmpty(selectedImiId)) {
948 // This is the first time of the user switch and
949 // set the current ime to the proper one.
950 resetDefaultImeLocked(mContext);
951 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900952 } else {
953 // If the locale is changed, needs to reset the default ime
954 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800956 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 mLastSystemLocale = newLocale;
958 if (!updateOnlyWhenLocaleChanged) {
959 try {
960 startInputInnerLocked();
961 } catch (RuntimeException e) {
962 Slog.w(TAG, "Unexpected exception", e);
963 }
964 }
965 }
966 }
967
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900968 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900969 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
970 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 }
972
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900973 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700974 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
975 + " currentUserId=" + mSettings.getCurrentUserId());
976
Yohei Yukawa81482972015-06-04 00:58:59 -0700977 // ContentObserver should be registered again when the user is changed
978 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100980 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900981 // InputMethodFileManager should be reset when the user is changed
982 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900983 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700984
985 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
986 + " defaultImiId=" + defaultImiId);
987
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900988 // For secondary users, the list of enabled IMEs may not have been updated since the
989 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
990 // not be empty even if the IME has been uninstalled by the primary user.
991 // Even in such cases, IMMS works fine because it will find the most applicable
992 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900993 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900994 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900995 initialUserSwitch /* needsToResetDefaultIme */);
996 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -0700997 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
998 mSettings.getEnabledInputMethodListLocked(), newUserId,
999 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001000 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001001
1002 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1003 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 }
1005
Kenny Guy2a764942014-04-02 13:29:20 +01001006 void updateCurrentProfileIds() {
1007 List<UserInfo> profiles =
1008 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1009 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1010 for (int i = 0; i < currentProfileIds.length; i++) {
1011 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001012 }
Kenny Guy2a764942014-04-02 13:29:20 +01001013 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001014 }
1015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 @Override
1017 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1018 throws RemoteException {
1019 try {
1020 return super.onTransact(code, data, reply, flags);
1021 } catch (RuntimeException e) {
1022 // The input method manager only throws security exceptions, so let's
1023 // log all others.
1024 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001025 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 throw e;
1028 }
1029 }
1030
Svetoslav Ganova0027152013-06-25 14:59:53 -07001031 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001032 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001033 if (DEBUG) {
1034 Slog.d(TAG, "--- systemReady");
1035 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001036 if (!mSystemReady) {
1037 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001038 mKeyguardManager =
1039 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001040 mNotificationManager = (NotificationManager)
1041 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1042 mStatusBar = statusBar;
1043 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001044 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001045 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1046 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001047 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001048 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001049 mHardKeyboardListener);
1050 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001051 buildInputMethodListLocked(mMethodList, mMethodMap,
1052 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001053 if (!mImeSelectedOnBoot) {
1054 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001055 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001056 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1057 mSettings.getEnabledInputMethodListLocked(),
1058 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001059 }
1060 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001061 try {
1062 startInputInnerLocked();
1063 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001065 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001066 }
1067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 // ---------------------------------------------------------------------------------------
1071 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1072 // 1) it comes from the system process
1073 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1074 private boolean calledFromValidUser() {
1075 final int uid = Binder.getCallingUid();
1076 final int userId = UserHandle.getUserId(uid);
1077 if (DEBUG) {
1078 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1079 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1080 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001081 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1082 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001083 }
Kenny Guy2a764942014-04-02 13:29:20 +01001084 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001086 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001087
1088 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1089 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1090 // must not manage background users' states in any functions.
1091 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1092 // by a token.
1093 if (mContext.checkCallingOrSelfPermission(
1094 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1095 == PackageManager.PERMISSION_GRANTED) {
1096 if (DEBUG) {
1097 Slog.d(TAG, "--- Access granted because the calling process has "
1098 + "the INTERACT_ACROSS_USERS_FULL permission");
1099 }
1100 return true;
1101 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001102 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1103 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001104 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 }
1106
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001107
1108 /**
1109 * Returns true iff the caller is identified to be the current input method with the token.
1110 * @param token The window token given to the input method when it was started.
1111 * @return true if and only if non-null valid token is specified.
1112 */
1113 private boolean calledWithValidToken(IBinder token) {
1114 if (token == null || mCurToken != token) {
1115 return false;
1116 }
1117 return true;
1118 }
1119
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001120 private boolean bindCurrentInputMethodService(
1121 Intent service, ServiceConnection conn, int flags) {
1122 if (service == null || conn == null) {
1123 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1124 return false;
1125 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001126 return mContext.bindServiceAsUser(service, conn, flags,
1127 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 }
1129
satoke7c6998e2011-06-03 17:57:59 +09001130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 // TODO: Make this work even for non-current users?
1133 if (!calledFromValidUser()) {
1134 return Collections.emptyList();
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001137 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
1140
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001143 // TODO: Make this work even for non-current users?
1144 if (!calledFromValidUser()) {
1145 return Collections.emptyList();
1146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001148 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150 }
1151
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001152 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001153 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001154 * @return enabled subtypes of the specified imi
1155 */
satoke7c6998e2011-06-03 17:57:59 +09001156 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001157 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001158 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 // TODO: Make this work even for non-current users?
1160 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001161 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001162 }
satok67ddf9c2010-11-17 09:45:54 +09001163 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001164 final InputMethodInfo imi;
1165 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001166 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001167 } else {
1168 imi = mMethodMap.get(imiId);
1169 }
1170 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001171 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001172 }
1173 return mSettings.getEnabledInputMethodSubtypeListLocked(
1174 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001175 }
1176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public void addClient(IInputMethodClient client,
1180 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 if (!calledFromValidUser()) {
1182 return;
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
1185 mClients.put(client.asBinder(), new ClientState(client,
1186 inputContext, uid, pid));
1187 }
1188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189
satoke7c6998e2011-06-03 17:57:59 +09001190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 if (!calledFromValidUser()) {
1193 return;
1194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001196 ClientState cs = mClients.remove(client.asBinder());
1197 if (cs != null) {
1198 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001199 if (mCurClient == cs) {
1200 mCurClient = null;
1201 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 void executeOrSendMessage(IInterface target, Message msg) {
1207 if (target.asBinder() instanceof Binder) {
1208 mCaller.sendMessage(msg);
1209 } else {
1210 handleMessage(msg);
1211 msg.recycle();
1212 }
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
Yohei Yukawa33e81792015-11-17 21:14:42 -08001215 void unbindCurrentClientLocked(
1216 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001218 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 + mCurClient.client.asBinder());
1220 if (mBoundToMethod) {
1221 mBoundToMethod = false;
1222 if (mCurMethod != null) {
1223 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1224 MSG_UNBIND_INPUT, mCurMethod));
1225 }
1226 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001227
1228 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1229 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001230 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1231 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
The Android Open Source Project10592532009-03-18 17:39:46 -07001235 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 private int getImeShowFlags() {
1240 int flags = 0;
1241 if (mShowForced) {
1242 flags |= InputMethod.SHOW_FORCED
1243 | InputMethod.SHOW_EXPLICIT;
1244 } else if (mShowExplicitlyRequested) {
1245 flags |= InputMethod.SHOW_EXPLICIT;
1246 }
1247 return flags;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 private int getAppShowFlags() {
1251 int flags = 0;
1252 if (mShowForced) {
1253 flags |= InputMethodManager.SHOW_FORCED;
1254 } else if (!mShowExplicitlyRequested) {
1255 flags |= InputMethodManager.SHOW_IMPLICIT;
1256 }
1257 return flags;
1258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
Dianne Hackborn7663d802012-02-24 13:08:49 -08001260 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if (!mBoundToMethod) {
1262 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1263 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1264 mBoundToMethod = true;
1265 }
1266 final SessionState session = mCurClient.curSession;
1267 if (initial) {
1268 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1269 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1270 } else {
1271 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1272 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1273 }
1274 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001275 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001276 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001278 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001279 (session.channel != null ? session.channel.dup() : null),
1280 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001284 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 // If no method is currently selected, do nothing.
1286 if (mCurMethodId == null) {
1287 return mNoBinding;
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 ClientState cs = mClients.get(client.asBinder());
1291 if (cs == null) {
1292 throw new IllegalArgumentException("unknown client "
1293 + client.asBinder());
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 try {
1297 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1298 // Check with the window manager to make sure this client actually
1299 // has a window with focus. If not, reject. This is thread safe
1300 // because if the focus changes some time before or after, the
1301 // next client receiving focus that has any interest in input will
1302 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1305 return null;
1306 }
1307 } catch (RemoteException e) {
1308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1311 }
1312
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001313 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001314 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001315 // If no method is currently selected, do nothing.
1316 if (mCurMethodId == null) {
1317 return mNoBinding;
1318 }
1319
Yohei Yukawad57ba672015-06-08 16:39:46 -07001320 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1321 attribute.packageName)) {
1322 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1323 + " uid=" + cs.uid + " package=" + attribute.packageName);
1324 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001325 }
1326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001328 // Was the keyguard locked when switching over to the new client?
1329 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 // If the client is changing, we need to switch over to the new
1331 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001332 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001333 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001334 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335
1336 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001337 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001338 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001339 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 // Bump up the sequence for this client and attach it.
1344 mCurSeq++;
1345 if (mCurSeq <= 0) mCurSeq = 1;
1346 mCurClient = cs;
1347 mCurInputContext = inputContext;
1348 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 // Check if the input method is changing.
1351 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1352 if (cs.curSession != null) {
1353 // Fast case: if we are already connected to the input method,
1354 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001355 return attachNewInputLocked(
1356 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358 if (mHaveConnection) {
1359 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 // Return to client, and we will get back with it when
1361 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001362 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001363 return new InputBindResult(null, null, mCurId, mCurSeq,
1364 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 } else if (SystemClock.uptimeMillis()
1366 < (mLastBindTime+TIME_TO_RECONNECT)) {
1367 // In this case we have connected to the service, but
1368 // don't yet have its interface. If it hasn't been too
1369 // long since we did the connection, we'll return to
1370 // the client and wait to get the service interface so
1371 // we can report back. If it has been too long, we want
1372 // to fall through so we can try a disconnect/reconnect
1373 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001374 return new InputBindResult(null, null, mCurId, mCurSeq,
1375 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1378 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 }
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001383 return startInputInnerLocked();
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001386 InputBindResult startInputInnerLocked() {
1387 if (mCurMethodId == null) {
1388 return mNoBinding;
1389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001391 if (!mSystemReady) {
1392 // If the system is not yet ready, we shouldn't be running third
1393 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001394 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1395 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1399 if (info == null) {
1400 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001403 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1406 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001407 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1408 com.android.internal.R.string.input_method_binding_label);
1409 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1410 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001411 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001412 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1413 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 mLastBindTime = SystemClock.uptimeMillis();
1415 mHaveConnection = true;
1416 mCurId = info.getId();
1417 mCurToken = new Binder();
1418 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001419 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 mIWindowManager.addWindowToken(mCurToken,
1421 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1422 } catch (RemoteException e) {
1423 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001424 return new InputBindResult(null, null, mCurId, mCurSeq,
1425 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 } else {
1427 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001428 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 + mCurIntent);
1430 }
1431 return null;
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
satoke7c6998e2011-06-03 17:57:59 +09001434 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001436 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001437 if (!calledFromValidUser()) {
1438 return null;
1439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 synchronized (mMethodMap) {
1441 final long ident = Binder.clearCallingIdentity();
1442 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001443 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 } finally {
1445 Binder.restoreCallingIdentity(ident);
1446 }
1447 }
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
satoke7c6998e2011-06-03 17:57:59 +09001450 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public void finishInput(IInputMethodClient client) {
1452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001453
satoke7c6998e2011-06-03 17:57:59 +09001454 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 public void onServiceConnected(ComponentName name, IBinder service) {
1456 synchronized (mMethodMap) {
1457 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1458 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001459 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001460 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001461 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001462 return;
1463 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001464 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001465 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1466 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001468 clearClientSessionLocked(mCurClient);
1469 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471 }
1472 }
1473 }
1474
Jeff Brownc28867a2013-03-26 15:42:39 -07001475 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1476 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 synchronized (mMethodMap) {
1478 if (mCurMethod != null && method != null
1479 && mCurMethod.asBinder() == method.asBinder()) {
1480 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001481 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001483 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001484 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (res.method != null) {
1486 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001487 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001489 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
1492 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001493
1494 // Session abandoned. Close its associated input channel.
1495 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001498 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001499 if (mVisibleBound) {
1500 mContext.unbindService(mVisibleConnection);
1501 mVisibleBound = false;
1502 }
1503
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001504 if (mHaveConnection) {
1505 mContext.unbindService(this);
1506 mHaveConnection = false;
1507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001509 if (mCurToken != null) {
1510 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001511 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001512 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001513 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001514 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001515 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001516 mIWindowManager.removeWindowToken(mCurToken);
1517 } catch (RemoteException e) {
1518 }
1519 mCurToken = null;
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project10592532009-03-18 17:39:46 -07001522 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001523 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Yohei Yukawa33e81792015-11-17 21:14:42 -08001526 void resetCurrentMethodAndClient(
1527 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001528 mCurMethodId = null;
1529 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001530 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001531 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001532
1533 void requestClientSessionLocked(ClientState cs) {
1534 if (!cs.sessionRequested) {
1535 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1536 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1537 cs.sessionRequested = true;
1538 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1539 MSG_CREATE_SESSION, mCurMethod, channels[1],
1540 new MethodCallback(this, mCurMethod, channels[0])));
1541 }
1542 }
1543
1544 void clearClientSessionLocked(ClientState cs) {
1545 finishSessionLocked(cs.curSession);
1546 cs.curSession = null;
1547 cs.sessionRequested = false;
1548 }
1549
1550 private void finishSessionLocked(SessionState sessionState) {
1551 if (sessionState != null) {
1552 if (sessionState.session != null) {
1553 try {
1554 sessionState.session.finishSession();
1555 } catch (RemoteException e) {
1556 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001557 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 }
1559 sessionState.session = null;
1560 }
1561 if (sessionState.channel != null) {
1562 sessionState.channel.dispose();
1563 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001564 }
1565 }
1566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001567
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001568 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (mCurMethod != null) {
1570 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001571 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001573
Jeff Brownc28867a2013-03-26 15:42:39 -07001574 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001575 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 mCurMethod = null;
1577 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001578 if (mStatusBar != null) {
1579 mStatusBar.setIconVisibility("ime", false);
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
satoke7c6998e2011-06-03 17:57:59 +09001583 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 public void onServiceDisconnected(ComponentName name) {
1585 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001586 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 + " mCurIntent=" + mCurIntent);
1588 if (mCurMethod != null && mCurIntent != null
1589 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 // We consider this to be a new bind attempt, since the system
1592 // should now try to restart the service for us.
1593 mLastBindTime = SystemClock.uptimeMillis();
1594 mShowRequested = mInputShown;
1595 mInputShown = false;
1596 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001597 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1598 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1599 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601 }
1602 }
1603 }
1604
satokf9f01002011-05-19 21:31:50 +09001605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1607 long ident = Binder.clearCallingIdentity();
1608 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001610 if (!calledWithValidToken(token)) {
1611 final int uid = Binder.getCallingUid();
1612 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1613 + " token:" + token);
1614 return;
1615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001618 if (mStatusBar != null) {
1619 mStatusBar.setIconVisibility("ime", false);
1620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001622 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001623 CharSequence contentDescription = null;
1624 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001625 // Use PackageManager to load label
1626 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001627 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 mIPackageManager.getApplicationInfo(packageName, 0,
1629 mSettings.getCurrentUserId()));
1630 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001631 /* ignore */
1632 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001633 if (mStatusBar != null) {
1634 mStatusBar.setIcon("ime", packageName, iconId, 0,
1635 contentDescription != null
1636 ? contentDescription.toString() : null);
1637 mStatusBar.setIconVisibility("ime", true);
1638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640 }
1641 } finally {
1642 Binder.restoreCallingIdentity(ident);
1643 }
1644 }
1645
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001646 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001647 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001648 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001649 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001650 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001651 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001652 // When physical keyboard is attached, we show the ime switcher (or notification if
1653 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1654 // exists in the IME switcher dialog. Might be OK to remove this condition once
1655 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1656 return true;
1657 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001658 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001659
1660 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1661 final int N = imis.size();
1662 if (N > 2) return true;
1663 if (N < 1) return false;
1664 int nonAuxCount = 0;
1665 int auxCount = 0;
1666 InputMethodSubtype nonAuxSubtype = null;
1667 InputMethodSubtype auxSubtype = null;
1668 for(int i = 0; i < N; ++i) {
1669 final InputMethodInfo imi = imis.get(i);
1670 final List<InputMethodSubtype> subtypes =
1671 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1672 final int subtypeCount = subtypes.size();
1673 if (subtypeCount == 0) {
1674 ++nonAuxCount;
1675 } else {
1676 for (int j = 0; j < subtypeCount; ++j) {
1677 final InputMethodSubtype subtype = subtypes.get(j);
1678 if (!subtype.isAuxiliary()) {
1679 ++nonAuxCount;
1680 nonAuxSubtype = subtype;
1681 } else {
1682 ++auxCount;
1683 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001684 }
1685 }
satok7cfc0ed2011-06-20 21:29:36 +09001686 }
1687 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001688 if (nonAuxCount > 1 || auxCount > 1) {
1689 return true;
1690 } else if (nonAuxCount == 1 && auxCount == 1) {
1691 if (nonAuxSubtype != null && auxSubtype != null
1692 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1693 || auxSubtype.overridesImplicitlyEnabledSubtype()
1694 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1695 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1696 return false;
1697 }
1698 return true;
1699 }
1700 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001701 }
1702
John Spurlocke0980502013-10-25 11:59:29 -04001703 private boolean isKeyguardLocked() {
1704 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1705 }
1706
satokdbf29502011-08-25 15:28:23 +09001707 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001708 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001709 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001710 if (!calledWithValidToken(token)) {
1711 final int uid = Binder.getCallingUid();
1712 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1713 + " token:" + token);
1714 return;
1715 }
1716
1717 synchronized (mMethodMap) {
1718 mImeWindowVis = vis;
1719 mBackDisposition = backDisposition;
1720 updateSystemUiLocked(token, vis, backDisposition);
1721 }
1722 }
1723
1724 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1725 synchronized (mMethodMap) {
1726 updateSystemUiLocked(token, vis, backDisposition);
1727 }
1728 }
1729
1730 // Caution! This method is called in this class. Handle multi-user carefully
1731 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1732 if (!calledWithValidToken(token)) {
1733 final int uid = Binder.getCallingUid();
1734 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1735 + " token:" + token);
1736 return;
1737 }
1738
1739 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1740 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001741 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001742 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001743 // apply policy for binder calls
1744 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1745 vis = 0;
satok06487a52010-10-29 11:37:18 +09001746 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001747 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1748 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1749 if (mStatusBar != null) {
1750 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1751 needsToShowImeSwitcher);
1752 }
1753 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1754 if (imi != null && needsToShowImeSwitcher) {
1755 // Used to load label
1756 final CharSequence title = mRes.getText(
1757 com.android.internal.R.string.select_input_method);
1758 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1759 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001760 mImeSwitcherNotification.setContentTitle(title)
1761 .setContentText(summary)
1762 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001763 try {
1764 if ((mNotificationManager != null)
1765 && !mIWindowManager.hasNavigationBar()) {
1766 if (DEBUG) {
1767 Slog.d(TAG, "--- show notification: label = " + summary);
1768 }
1769 mNotificationManager.notifyAsUser(null,
1770 com.android.internal.R.string.select_input_method,
1771 mImeSwitcherNotification.build(), UserHandle.ALL);
1772 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001773 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001774 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001775 }
1776 } else {
1777 if (mNotificationShown && mNotificationManager != null) {
1778 if (DEBUG) {
1779 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001780 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001781 mNotificationManager.cancelAsUser(null,
1782 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1783 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001784 }
satok06487a52010-10-29 11:37:18 +09001785 }
1786 } finally {
1787 Binder.restoreCallingIdentity(ident);
1788 }
1789 }
1790
satoke7c6998e2011-06-03 17:57:59 +09001791 @Override
satokf9f01002011-05-19 21:31:50 +09001792 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001793 if (!calledFromValidUser()) {
1794 return;
1795 }
satokf9f01002011-05-19 21:31:50 +09001796 synchronized (mMethodMap) {
1797 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1798 for (int i = 0; i < spans.length; ++i) {
1799 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001800 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001801 mSecureSuggestionSpans.put(ss, currentImi);
1802 }
1803 }
1804 }
1805 }
1806
satoke7c6998e2011-06-03 17:57:59 +09001807 @Override
satokf9f01002011-05-19 21:31:50 +09001808 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001809 if (!calledFromValidUser()) {
1810 return false;
1811 }
satokf9f01002011-05-19 21:31:50 +09001812 synchronized (mMethodMap) {
1813 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1814 // TODO: Do not send the intent if the process of the targetImi is already dead.
1815 if (targetImi != null) {
1816 final String[] suggestions = span.getSuggestions();
1817 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001818 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001819 final Intent intent = new Intent();
1820 // Ensures that only a class in the original IME package will receive the
1821 // notification.
satok42c5a162011-05-26 16:46:14 +09001822 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001823 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1824 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1825 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1826 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001827 final long ident = Binder.clearCallingIdentity();
1828 try {
1829 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1830 } finally {
1831 Binder.restoreCallingIdentity(ident);
1832 }
satokf9f01002011-05-19 21:31:50 +09001833 return true;
1834 }
1835 }
1836 return false;
1837 }
1838
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001839 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001840 updateInputMethodsFromSettingsLocked(enabledMayChange);
1841 updateKeyboardFromSettingsLocked();
1842 }
1843
1844 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001845 if (enabledMayChange) {
1846 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1847 for (int i=0; i<enabled.size(); i++) {
1848 // We allow the user to select "disabled until used" apps, so if they
1849 // are enabling one of those here we now need to make it enabled.
1850 InputMethodInfo imm = enabled.get(i);
1851 try {
1852 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1853 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1854 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001855 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001856 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001857 if (DEBUG) {
1858 Slog.d(TAG, "Update state(" + imm.getId()
1859 + "): DISABLED_UNTIL_USED -> DEFAULT");
1860 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001861 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1862 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001863 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1864 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001865 }
1866 } catch (RemoteException e) {
1867 }
1868 }
1869 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001870 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1871 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1872 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1873 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001874 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001875 // There is no input method selected, try to choose new applicable input method.
1876 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001877 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001878 }
1879 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001881 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001883 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001884 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
satokf3db1af2010-11-23 13:34:33 +09001886 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001887 } else {
1888 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001889 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001891 // Here is not the perfect place to reset the switching controller. Ideally
1892 // mSwitchingController and mSettings should be able to share the same state.
1893 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1894 // the same enabled IMEs list.
1895 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001896
1897 }
1898
1899 public void updateKeyboardFromSettingsLocked() {
1900 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1901 if (mSwitchingDialog != null
1902 && mSwitchingDialogTitleView != null
1903 && mSwitchingDialog.isShowing()) {
1904 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1905 com.android.internal.R.id.hard_keyboard_switch);
1906 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
satokab751aa2010-09-14 19:17:36 +09001910 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 InputMethodInfo info = mMethodMap.get(id);
1912 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001913 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001916 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001917 // We have already made sure that the package name belongs to the application's UID.
1918 // No further UID check is required.
1919 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1920 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001921 }
1922 }
1923
satokd81e9502012-05-21 12:58:45 +09001924 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001926 final int subtypeCount = info.getSubtypeCount();
1927 if (subtypeCount <= 0) {
1928 return;
satokcd7cd292010-11-20 15:46:23 +09001929 }
satokd81e9502012-05-21 12:58:45 +09001930 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1931 final InputMethodSubtype newSubtype;
1932 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1933 newSubtype = info.getSubtypeAt(subtypeId);
1934 } else {
1935 // If subtype is null, try to find the most applicable one from
1936 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001937 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001938 }
1939 if (newSubtype == null || oldSubtype == null) {
1940 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1941 + ", new subtype = " + newSubtype);
1942 return;
1943 }
1944 if (newSubtype != oldSubtype) {
1945 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1946 if (mCurMethod != null) {
1947 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001948 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001949 mCurMethod.changeInputMethodSubtype(newSubtype);
1950 } catch (RemoteException e) {
1951 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001952 }
1953 }
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 return;
1956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001957
satokd81e9502012-05-21 12:58:45 +09001958 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 final long ident = Binder.clearCallingIdentity();
1960 try {
satokab751aa2010-09-14 19:17:36 +09001961 // Set a subtype to this input method.
1962 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001963 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1964 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1965 // because mCurMethodId is stored as a history in
1966 // setSelectedInputMethodAndSubtypeLocked().
1967 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968
1969 if (ActivityManagerNative.isSystemReady()) {
1970 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001971 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001973 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08001975 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 } finally {
1977 Binder.restoreCallingIdentity(ident);
1978 }
1979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
satok42c5a162011-05-26 16:46:14 +09001981 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001982 public boolean showSoftInput(IInputMethodClient client, int flags,
1983 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001984 if (!calledFromValidUser()) {
1985 return false;
1986 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001987 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 long ident = Binder.clearCallingIdentity();
1989 try {
1990 synchronized (mMethodMap) {
1991 if (mCurClient == null || client == null
1992 || mCurClient.client.asBinder() != client.asBinder()) {
1993 try {
1994 // We need to check if this is the current client with
1995 // focus in the window manager, to allow this call to
1996 // be made before input is started in it.
1997 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001998 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001999 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002002 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002006 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002007 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009 } finally {
2010 Binder.restoreCallingIdentity(ident);
2011 }
2012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
The Android Open Source Project4df24232009-03-05 14:34:35 -08002014 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 mShowRequested = true;
2016 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2017 mShowExplicitlyRequested = true;
2018 }
2019 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2020 mShowExplicitlyRequested = true;
2021 mShowForced = true;
2022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
Dianne Hackborncc278702009-09-02 23:07:23 -07002024 if (!mSystemReady) {
2025 return false;
2026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002027
The Android Open Source Project4df24232009-03-05 14:34:35 -08002028 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002030 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002031 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2032 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2033 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002035 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002036 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002037 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002038 | Context.BIND_TREAT_LIKE_ACTIVITY
2039 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002040 mVisibleBound = true;
2041 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002042 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002044 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 // The client has asked to have the input method shown, but
2046 // we have been sitting here too long with a connection to the
2047 // service and no interface received, so let's disconnect/connect
2048 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002051 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002053 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002054 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002055 } else {
2056 if (DEBUG) {
2057 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2058 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002061
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002064
satok42c5a162011-05-26 16:46:14 +09002065 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002066 public boolean hideSoftInput(IInputMethodClient client, int flags,
2067 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002068 if (!calledFromValidUser()) {
2069 return false;
2070 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002071 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 long ident = Binder.clearCallingIdentity();
2073 try {
2074 synchronized (mMethodMap) {
2075 if (mCurClient == null || client == null
2076 || mCurClient.client.asBinder() != client.asBinder()) {
2077 try {
2078 // We need to check if this is the current client with
2079 // focus in the window manager, to allow this call to
2080 // be made before input is started in it.
2081 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002082 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2083 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002090
Joe Onorato8a9b2202010-02-26 18:56:32 -08002091 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 } finally {
2095 Binder.restoreCallingIdentity(ident);
2096 }
2097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
The Android Open Source Project4df24232009-03-05 14:34:35 -08002099 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2101 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002102 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 -08002103 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002106 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 -08002107 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002109
2110 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2111 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2112 // to be updated with the new value sent from IME process. Even in such a transient state
2113 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2114 // application process as a valid request, and have even promised such a behavior with CTS
2115 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2116 // IMMS#InputShown indicates that the software keyboard is shown.
2117 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2118 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2119 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002120 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002121 if (shouldHideSoftInput) {
2122 // The IME will report its visible state again after the following message finally
2123 // delivered to the IME process as an IPC. Hence the inconsistency between
2124 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2125 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2127 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2128 res = true;
2129 } else {
2130 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002132 if (mHaveConnection && mVisibleBound) {
2133 mContext.unbindService(mVisibleConnection);
2134 mVisibleBound = false;
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 mInputShown = false;
2137 mShowRequested = false;
2138 mShowExplicitlyRequested = false;
2139 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002140 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
satok42c5a162011-05-26 16:46:14 +09002143 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002144 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2145 int controlFlags, int softInputMode, int windowFlags,
2146 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002147 // Needs to check the validity before clearing calling identity
2148 final boolean calledFromValidUser = calledFromValidUser();
2149
Dianne Hackborn7663d802012-02-24 13:08:49 -08002150 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 long ident = Binder.clearCallingIdentity();
2152 try {
2153 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002154 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002155 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002157 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002158
Dianne Hackborn7663d802012-02-24 13:08:49 -08002159 ClientState cs = mClients.get(client.asBinder());
2160 if (cs == null) {
2161 throw new IllegalArgumentException("unknown client "
2162 + client.asBinder());
2163 }
2164
2165 try {
2166 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2167 // Check with the window manager to make sure this client actually
2168 // has a window with focus. If not, reject. This is thread safe
2169 // because if the focus changes some time before or after, the
2170 // next client receiving focus that has any interest in input will
2171 // be calling through here after that change happens.
2172 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2173 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2174 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002176 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002179 if (!calledFromValidUser) {
2180 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2181 Slog.w(TAG, "If you want to interect with IME, you need "
2182 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2183 hideCurrentInputLocked(0, null);
2184 return null;
2185 }
2186
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002187 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002188 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002189 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002190 if (attribute != null) {
2191 return startInputUncheckedLocked(cs, inputContext, attribute,
2192 controlFlags);
2193 }
2194 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002195 }
2196 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002197
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002198 // Should we auto-show the IME even if the caller has not
2199 // specified what should be done with it?
2200 // We only do this automatically if the window can resize
2201 // to accommodate the IME (so what the user sees will give
2202 // them good context without input information being obscured
2203 // by the IME) or if running on a large screen where there
2204 // is more room for the target window + IME.
2205 final boolean doAutoShow =
2206 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2207 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2208 || mRes.getConfiguration().isLayoutSizeAtLeast(
2209 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002210 final boolean isTextEditor =
2211 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2212
2213 // We want to start input before showing the IME, but after closing
2214 // it. We want to do this after closing it to help the IME disappear
2215 // more quickly (not get stuck behind it initializing itself for the
2216 // new focused input, even if its window wants to hide the IME).
2217 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2220 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002221 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2223 // There is no focus view, and this window will
2224 // be behind any soft input window, so hide the
2225 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002226 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002229 } else if (isTextEditor && doAutoShow && (softInputMode &
2230 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 // There is a focus view, and we are navigating forward
2232 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002233 // We only do this automatically if the window can resize
2234 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002235 // them good context without input information being obscured
2236 // by the IME) or if running on a large screen where there
2237 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002238 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002239 if (attribute != null) {
2240 res = startInputUncheckedLocked(cs, inputContext, attribute,
2241 controlFlags);
2242 didStart = true;
2243 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002244 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
2246 break;
2247 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2248 // Do nothing.
2249 break;
2250 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2251 if ((softInputMode &
2252 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002253 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002254 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
2256 break;
2257 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002259 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 break;
2261 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2262 if ((softInputMode &
2263 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002264 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002265 if (attribute != null) {
2266 res = startInputUncheckedLocked(cs, inputContext, attribute,
2267 controlFlags);
2268 didStart = true;
2269 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002270 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 }
2272 break;
2273 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002274 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002275 if (attribute != null) {
2276 res = startInputUncheckedLocked(cs, inputContext, attribute,
2277 controlFlags);
2278 didStart = true;
2279 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002280 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 break;
2282 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283
2284 if (!didStart && attribute != null) {
2285 res = startInputUncheckedLocked(cs, inputContext, attribute,
2286 controlFlags);
2287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289 } finally {
2290 Binder.restoreCallingIdentity(ident);
2291 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002292
2293 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002295
satok42c5a162011-05-26 16:46:14 +09002296 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002297 public void showInputMethodPickerFromClient(
2298 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002299 if (!calledFromValidUser()) {
2300 return;
2301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 synchronized (mMethodMap) {
2303 if (mCurClient == null || client == null
2304 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002305 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002306 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308
satok440aab52010-11-25 09:43:11 +09002309 // Always call subtype picker, because subtype picker is a superset of input method
2310 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002311 mHandler.sendMessage(mCaller.obtainMessageI(
2312 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002313 }
2314 }
2315
satok42c5a162011-05-26 16:46:14 +09002316 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002318 if (!calledFromValidUser()) {
2319 return;
2320 }
satok28203512010-11-24 11:06:49 +09002321 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2322 }
2323
satok42c5a162011-05-26 16:46:14 +09002324 @Override
satok28203512010-11-24 11:06:49 +09002325 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002326 if (!calledFromValidUser()) {
2327 return;
2328 }
satok28203512010-11-24 11:06:49 +09002329 synchronized (mMethodMap) {
2330 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002331 setInputMethodWithSubtypeIdLocked(token, id,
2332 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2333 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002334 } else {
2335 setInputMethod(token, id);
2336 }
2337 }
satokab751aa2010-09-14 19:17:36 +09002338 }
2339
satok42c5a162011-05-26 16:46:14 +09002340 @Override
satokb416a712010-11-25 20:42:14 +09002341 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002342 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002343 if (!calledFromValidUser()) {
2344 return;
2345 }
satokb416a712010-11-25 20:42:14 +09002346 synchronized (mMethodMap) {
2347 if (mCurClient == null || client == null
2348 || mCurClient.client.asBinder() != client.asBinder()) {
2349 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2350 }
satok7fee71f2010-12-17 18:54:26 +09002351 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2352 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002353 }
2354 }
2355
satok4fc87d62011-05-20 16:13:43 +09002356 @Override
satok735cf382010-11-11 20:40:09 +09002357 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002358 if (!calledFromValidUser()) {
2359 return false;
2360 }
satok735cf382010-11-11 20:40:09 +09002361 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002362 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002363 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002364 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002365 lastImi = mMethodMap.get(lastIme.first);
2366 } else {
2367 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002368 }
satok4fc87d62011-05-20 16:13:43 +09002369 String targetLastImiId = null;
2370 int subtypeId = NOT_A_SUBTYPE_ID;
2371 if (lastIme != null && lastImi != null) {
2372 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2373 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2374 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2375 : mCurrentSubtype.hashCode();
2376 // If the last IME is the same as the current IME and the last subtype is not
2377 // defined, there is no need to switch to the last IME.
2378 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2379 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002380 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002381 }
2382 }
2383
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002384 if (TextUtils.isEmpty(targetLastImiId)
2385 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002386 // This is a safety net. If the currentSubtype can't be added to the history
2387 // and the framework couldn't find the last ime, we will make the last ime be
2388 // the most applicable enabled keyboard subtype of the system imes.
2389 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2390 if (enabled != null) {
2391 final int N = enabled.size();
2392 final String locale = mCurrentSubtype == null
2393 ? mRes.getConfiguration().locale.toString()
2394 : mCurrentSubtype.getLocale();
2395 for (int i = 0; i < N; ++i) {
2396 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002397 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002398 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002399 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2400 InputMethodUtils.getSubtypes(imi),
2401 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002402 if (keyboardSubtype != null) {
2403 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002404 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002405 imi, keyboardSubtype.hashCode());
2406 if(keyboardSubtype.getLocale().equals(locale)) {
2407 break;
2408 }
2409 }
2410 }
2411 }
2412 }
2413 }
2414
2415 if (!TextUtils.isEmpty(targetLastImiId)) {
2416 if (DEBUG) {
2417 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2418 + ", from: " + mCurMethodId + ", " + subtypeId);
2419 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002420 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002421 return true;
2422 } else {
2423 return false;
2424 }
satok735cf382010-11-11 20:40:09 +09002425 }
2426 }
2427
satoke7c6998e2011-06-03 17:57:59 +09002428 @Override
satok688bd472012-02-09 20:09:17 +09002429 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 if (!calledFromValidUser()) {
2431 return false;
2432 }
satok688bd472012-02-09 20:09:17 +09002433 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002434 if (!calledWithValidToken(token)) {
2435 final int uid = Binder.getCallingUid();
2436 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2437 + " token:" + token);
2438 return false;
2439 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002440 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002441 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2442 if (nextSubtype == null) {
2443 return false;
2444 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002445 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2446 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002447 return true;
2448 }
2449 }
2450
2451 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002452 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2453 if (!calledFromValidUser()) {
2454 return false;
2455 }
2456 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002457 if (!calledWithValidToken(token)) {
2458 final int uid = Binder.getCallingUid();
2459 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2460 + "token. uid:" + uid + " token:" + token);
2461 return false;
2462 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002463 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002464 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2465 if (nextSubtype == null) {
2466 return false;
2467 }
2468 return true;
2469 }
2470 }
2471
2472 @Override
satok68f1b782011-04-11 14:26:04 +09002473 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002474 if (!calledFromValidUser()) {
2475 return null;
2476 }
satok68f1b782011-04-11 14:26:04 +09002477 synchronized (mMethodMap) {
2478 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2479 // TODO: Handle the case of the last IME with no subtypes
2480 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2481 || TextUtils.isEmpty(lastIme.second)) return null;
2482 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2483 if (lastImi == null) return null;
2484 try {
2485 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002486 final int lastSubtypeId =
2487 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002488 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2489 return null;
2490 }
2491 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002492 } catch (NumberFormatException e) {
2493 return null;
2494 }
2495 }
2496 }
2497
satoke7c6998e2011-06-03 17:57:59 +09002498 @Override
satokee5e77c2011-09-02 18:50:15 +09002499 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002500 if (!calledFromValidUser()) {
2501 return;
2502 }
satok91e88122011-07-18 11:11:42 +09002503 // By this IPC call, only a process which shares the same uid with the IME can add
2504 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002505 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002506 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002507 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002508 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002509 final String[] packageInfos;
2510 try {
2511 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2512 } catch (RemoteException e) {
2513 Slog.e(TAG, "Failed to get package infos");
2514 return;
2515 }
satok91e88122011-07-18 11:11:42 +09002516 if (packageInfos != null) {
2517 final int packageNum = packageInfos.length;
2518 for (int i = 0; i < packageNum; ++i) {
2519 if (packageInfos[i].equals(imi.getPackageName())) {
2520 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002521 final long ident = Binder.clearCallingIdentity();
2522 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002523 buildInputMethodListLocked(mMethodList, mMethodMap,
2524 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002525 } finally {
2526 Binder.restoreCallingIdentity(ident);
2527 }
satokee5e77c2011-09-02 18:50:15 +09002528 return;
satok91e88122011-07-18 11:11:42 +09002529 }
2530 }
2531 }
satoke7c6998e2011-06-03 17:57:59 +09002532 }
satokee5e77c2011-09-02 18:50:15 +09002533 return;
satoke7c6998e2011-06-03 17:57:59 +09002534 }
2535
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002536 @Override
2537 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002538 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002539 }
2540
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002541 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002542 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002543 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002544 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002545 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002546 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002547 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2548 if (DEBUG) {
2549 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2550 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2551 + " actual: " + sequenceNumber);
2552 }
2553 return;
2554 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002555 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2556 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002557 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002558 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002559 }
2560 }
2561
satok28203512010-11-24 11:06:49 +09002562 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002564 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2565 }
2566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002568 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2569 if (token == null) {
2570 if (mContext.checkCallingOrSelfPermission(
2571 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2572 != PackageManager.PERMISSION_GRANTED) {
2573 throw new SecurityException(
2574 "Using null token requires permission "
2575 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002577 } else if (mCurToken != token) {
2578 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2579 + " token: " + token);
2580 return;
2581 }
2582
2583 final long ident = Binder.clearCallingIdentity();
2584 try {
2585 setInputMethodLocked(id, subtypeId);
2586 } finally {
2587 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 }
2589 }
2590
satok42c5a162011-05-26 16:46:14 +09002591 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002593 if (!calledFromValidUser()) {
2594 return;
2595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002597 if (!calledWithValidToken(token)) {
2598 final int uid = Binder.getCallingUid();
2599 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2600 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 return;
2602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 long ident = Binder.clearCallingIdentity();
2604 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002605 hideCurrentInputLocked(flags, null);
2606 } finally {
2607 Binder.restoreCallingIdentity(ident);
2608 }
2609 }
2610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002611
satok42c5a162011-05-26 16:46:14 +09002612 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002613 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002614 if (!calledFromValidUser()) {
2615 return;
2616 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002617 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002618 if (!calledWithValidToken(token)) {
2619 final int uid = Binder.getCallingUid();
2620 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2621 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002622 return;
2623 }
2624 long ident = Binder.clearCallingIdentity();
2625 try {
2626 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 } finally {
2628 Binder.restoreCallingIdentity(ident);
2629 }
2630 }
2631 }
2632
2633 void setEnabledSessionInMainThread(SessionState session) {
2634 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002635 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002637 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002638 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 } catch (RemoteException e) {
2640 }
2641 }
2642 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002643 if (mEnabledSession != null && mEnabledSession.session != null) {
2644 try {
2645 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2646 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2647 } catch (RemoteException e) {
2648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
2650 }
2651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002652
satok42c5a162011-05-26 16:46:14 +09002653 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002655 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002657 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002658 final boolean showAuxSubtypes;
2659 switch (msg.arg1) {
2660 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2661 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2662 // implemented so that auxiliary subtypes will be excluded when the soft
2663 // keyboard is invisible.
2664 showAuxSubtypes = mInputShown;
2665 break;
2666 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2667 showAuxSubtypes = true;
2668 break;
2669 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2670 showAuxSubtypes = false;
2671 break;
2672 default:
2673 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2674 return false;
2675 }
2676 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002677 return true;
2678
satok47a44912010-10-06 16:03:58 +09002679 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002680 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002681 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002682 args.recycle();
satok217f5482010-12-15 05:19:19 +09002683 return true;
2684
2685 case MSG_SHOW_IM_CONFIG:
2686 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002687 return true;
2688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 case MSG_UNBIND_INPUT:
2692 try {
2693 ((IInputMethod)msg.obj).unbindInput();
2694 } catch (RemoteException e) {
2695 // There is nothing interesting about the method dying.
2696 }
2697 return true;
2698 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002699 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 try {
2701 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2702 } catch (RemoteException e) {
2703 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002704 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 return true;
2706 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002707 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002709 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002710 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002711 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 } catch (RemoteException e) {
2713 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002714 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 return true;
2716 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002717 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002719 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002720 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002721 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 } catch (RemoteException e) {
2723 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 return true;
2726 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002727 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002729 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2731 } catch (RemoteException e) {
2732 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002733 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002735 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002736 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002737 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002738 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002740 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002742 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002743 // Dispose the channel if the input method is not local to this process
2744 // because the remote proxy will get its own copy when unparceled.
2745 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002746 channel.dispose();
2747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002749 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002755 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 try {
2757 SessionState session = (SessionState)args.arg1;
2758 setEnabledSessionInMainThread(session);
2759 session.method.startInput((IInputContext)args.arg2,
2760 (EditorInfo)args.arg3);
2761 } catch (RemoteException e) {
2762 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002763 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 return true;
2765 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002766 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 try {
2768 SessionState session = (SessionState)args.arg1;
2769 setEnabledSessionInMainThread(session);
2770 session.method.restartInput((IInputContext)args.arg2,
2771 (EditorInfo)args.arg3);
2772 } catch (RemoteException e) {
2773 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002774 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002778
Yohei Yukawa33e81792015-11-17 21:14:42 -08002779 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002781 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 } catch (RemoteException e) {
2783 // There is nothing interesting about the last client dying.
2784 }
2785 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002786 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002787 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002788 IInputMethodClient client = (IInputMethodClient)args.arg1;
2789 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002791 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002793 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002794 } finally {
2795 // Dispose the channel if the input method is not local to this process
2796 // because the remote proxy will get its own copy when unparceled.
2797 if (res.channel != null && Binder.isProxy(client)) {
2798 res.channel.dispose();
2799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002801 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002803 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002804 case MSG_SET_ACTIVE:
2805 try {
2806 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2807 } catch (RemoteException e) {
2808 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2809 + ((ClientState)msg.obj).pid + " uid "
2810 + ((ClientState)msg.obj).uid);
2811 }
2812 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002813 case MSG_SET_INTERACTIVE:
2814 handleSetInteractive(msg.arg1 != 0);
2815 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002816 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2817 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002818 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002819 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002820 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002821 } catch (RemoteException e) {
2822 Slog.w(TAG, "Got RemoteException sending "
2823 + "setUserActionNotificationSequenceNumber("
2824 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002825 + clientState.pid + " uid "
2826 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002827 }
2828 return true;
2829 }
satok01038492012-04-09 21:08:27 +09002830
2831 // --------------------------------------------------------------
2832 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002833 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002834 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
2836 return false;
2837 }
2838
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002839 private void handleSetInteractive(final boolean interactive) {
2840 synchronized (mMethodMap) {
2841 mIsInteractive = interactive;
2842 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2843
2844 // Inform the current client of the change in active status
2845 if (mCurClient != null && mCurClient.client != null) {
2846 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2847 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2848 }
2849 }
2850 }
2851
satokdc9ddae2011-10-06 12:22:36 +09002852 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002853 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2854 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002855 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002856 if (DEBUG) {
2857 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2858 }
satok723a27e2010-11-11 14:58:11 +09002859 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002860 return true;
2861 }
2862
2863 return false;
2864 }
2865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002867 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002868 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002869 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002870 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 list.clear();
2873 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002874
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002875 // Use for queryIntentServicesAsUser
2876 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002878 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002880 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2881 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002882
satoke7c6998e2011-06-03 17:57:59 +09002883 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2884 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 for (int i = 0; i < services.size(); ++i) {
2886 ResolveInfo ri = services.get(i);
2887 ServiceInfo si = ri.serviceInfo;
2888 ComponentName compName = new ComponentName(si.packageName, si.name);
2889 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2890 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 + ": it does not require the permission "
2893 + android.Manifest.permission.BIND_INPUT_METHOD);
2894 continue;
2895 }
2896
Joe Onorato8a9b2202010-02-26 18:56:32 -08002897 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
2899 try {
satoke7c6998e2011-06-03 17:57:59 +09002900 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002902 final String id = p.getId();
2903 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904
2905 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002906 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002908 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002909 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 }
2911 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002912
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002913 if (resetDefaultEnabledIme) {
2914 final ArrayList<InputMethodInfo> defaultEnabledIme =
2915 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2916 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2917 final InputMethodInfo imi = defaultEnabledIme.get(i);
2918 if (DEBUG) {
2919 Slog.d(TAG, "--- enable ime = " + imi);
2920 }
2921 setInputMethodEnabledLocked(imi.getId(), true);
2922 }
2923 }
2924
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002925 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002926 if (!TextUtils.isEmpty(defaultImiId)) {
2927 if (!map.containsKey(defaultImiId)) {
2928 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2929 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002930 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002931 }
2932 } else {
2933 // Double check that the default IME is certainly enabled.
2934 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002935 }
2936 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002937 // Here is not the perfect place to reset the switching controller. Ideally
2938 // mSwitchingController and mSettings should be able to share the same state.
2939 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2940 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002941 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002945
satok217f5482010-12-15 05:19:19 +09002946 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002947 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002948 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002949 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2950 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002951 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002952 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002953 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002954 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002955 }
2956
2957 private void showConfigureInputMethods() {
2958 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2959 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2960 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2961 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002962 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002963 }
2964
satok2c93efc2012-04-02 19:33:47 +09002965 private boolean isScreenLocked() {
2966 return mKeyguardManager != null
2967 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2968 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002969
Seigo Nonaka14e13912015-05-06 21:04:13 -07002970 private void showInputMethodMenu(boolean showAuxSubtypes) {
2971 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002974 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002975
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002976 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002977 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002978 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002979
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002980 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002981 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002982 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2983 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002984 if (immis == null || immis.size() == 0) {
2985 return;
2986 }
2987
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002988 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989
satok688bd472012-02-09 20:09:17 +09002990 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002991 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002992 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002993
satokc3690562012-01-10 20:14:43 +09002994 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002995 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002996 if (currentSubtype != null) {
2997 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002998 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2999 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003000 }
3001 }
3002
Ken Wakasa761eb372011-03-04 19:06:18 +09003003 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003004 mIms = new InputMethodInfo[N];
3005 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003006 int checkedItem = 0;
3007 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003008 final ImeSubtypeListItem item = imList.get(i);
3009 mIms[i] = item.mImi;
3010 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003011 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003012 int subtypeId = mSubtypeIds[i];
3013 if ((subtypeId == NOT_A_SUBTYPE_ID)
3014 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3015 || (subtypeId == lastInputMethodSubtypeId)) {
3016 checkedItem = i;
3017 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003020
3021 final Context settingsContext = new ContextThemeWrapper(context,
3022 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3023
3024 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003025 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3026 @Override
3027 public void onCancel(DialogInterface dialog) {
3028 hideInputMethodMenu();
3029 }
3030 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003031
3032 final Context dialogContext = mDialogBuilder.getContext();
3033 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3034 com.android.internal.R.styleable.DialogPreference,
3035 com.android.internal.R.attr.alertDialogStyle, 0);
3036 final Drawable dialogIcon = a.getDrawable(
3037 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3038 a.recycle();
3039
3040 mDialogBuilder.setIcon(dialogIcon);
3041
3042 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3043 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003044 final View tv = inflater.inflate(
3045 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3046 mDialogBuilder.setCustomTitle(tv);
3047
3048 // Setup layout for a toggle switch of the hardware keyboard
3049 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003050 mSwitchingDialogTitleView
3051 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003052 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003053 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003054 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003055 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003056 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003057 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3058 @Override
3059 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003060 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003061 // Ensure that the input method dialog is dismissed when changing
3062 // the hardware keyboard state.
3063 hideInputMethodMenu();
3064 }
3065 });
3066
Alan Viverette505e3ab2014-11-24 15:22:11 -08003067 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003068 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3069 final OnClickListener choiceListener = new OnClickListener() {
3070 @Override
3071 public void onClick(final DialogInterface dialog, final int which) {
3072 synchronized (mMethodMap) {
3073 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3074 || mSubtypeIds.length <= which) {
3075 return;
satok01038492012-04-09 21:08:27 +09003076 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003077 final InputMethodInfo im = mIms[which];
3078 int subtypeId = mSubtypeIds[which];
3079 adapter.mCheckedItem = which;
3080 adapter.notifyDataSetChanged();
3081 hideInputMethodMenu();
3082 if (im != null) {
3083 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3084 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003085 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003086 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003088 }
3089 }
3090 };
3091 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003093 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003094 final OnClickListener positiveListener = new OnClickListener() {
3095 @Override
3096 public void onClick(DialogInterface dialog, int whichButton) {
3097 showConfigureInputMethods();
3098 }
3099 };
satok82beadf2010-12-27 19:03:06 +09003100 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003101 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003104 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 mSwitchingDialog.getWindow().setType(
3106 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003107 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3108 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003109 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003110 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 mSwitchingDialog.show();
3112 }
3113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003114
Ken Wakasa05dbb652011-08-22 15:22:43 +09003115 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3116 private final LayoutInflater mInflater;
3117 private final int mTextViewResourceId;
3118 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003119 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003120 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3121 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3122 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003123
Ken Wakasa05dbb652011-08-22 15:22:43 +09003124 mTextViewResourceId = textViewResourceId;
3125 mItemsList = itemsList;
3126 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003127 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003128 }
3129
3130 @Override
3131 public View getView(int position, View convertView, ViewGroup parent) {
3132 final View view = convertView != null ? convertView
3133 : mInflater.inflate(mTextViewResourceId, null);
3134 if (position < 0 || position >= mItemsList.size()) return view;
3135 final ImeSubtypeListItem item = mItemsList.get(position);
3136 final CharSequence imeName = item.mImeName;
3137 final CharSequence subtypeName = item.mSubtypeName;
3138 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3139 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3140 if (TextUtils.isEmpty(subtypeName)) {
3141 firstTextView.setText(imeName);
3142 secondTextView.setVisibility(View.GONE);
3143 } else {
3144 firstTextView.setText(subtypeName);
3145 secondTextView.setText(imeName);
3146 secondTextView.setVisibility(View.VISIBLE);
3147 }
3148 final RadioButton radioButton =
3149 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3150 radioButton.setChecked(position == mCheckedItem);
3151 return view;
3152 }
3153 }
3154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003156 synchronized (mMethodMap) {
3157 hideInputMethodMenuLocked();
3158 }
3159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003160
The Android Open Source Project10592532009-03-18 17:39:46 -07003161 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003162 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163
The Android Open Source Project10592532009-03-18 17:39:46 -07003164 if (mSwitchingDialog != null) {
3165 mSwitchingDialog.dismiss();
3166 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003168
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003169 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003170 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003171 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003175
satok42c5a162011-05-26 16:46:14 +09003176 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003178 // TODO: Make this work even for non-current users?
3179 if (!calledFromValidUser()) {
3180 return false;
3181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 synchronized (mMethodMap) {
3183 if (mContext.checkCallingOrSelfPermission(
3184 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3185 != PackageManager.PERMISSION_GRANTED) {
3186 throw new SecurityException(
3187 "Requires permission "
3188 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3189 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 long ident = Binder.clearCallingIdentity();
3192 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003193 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 } finally {
3195 Binder.restoreCallingIdentity(ident);
3196 }
3197 }
3198 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003199
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003200 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3201 // Make sure this is a valid input method.
3202 InputMethodInfo imm = mMethodMap.get(id);
3203 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003204 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003205 }
3206
satokd87c2592010-09-29 11:52:06 +09003207 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3208 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003209
satokd87c2592010-09-29 11:52:06 +09003210 if (enabled) {
3211 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3212 if (pair.first.equals(id)) {
3213 // We are enabling this input method, but it is already enabled.
3214 // Nothing to do. The previous state was enabled.
3215 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003216 }
3217 }
satokd87c2592010-09-29 11:52:06 +09003218 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3219 // Previous state was disabled.
3220 return false;
3221 } else {
3222 StringBuilder builder = new StringBuilder();
3223 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3224 builder, enabledInputMethodsList, id)) {
3225 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003226 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003227 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3228 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3229 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003230 }
3231 // Previous state was enabled.
3232 return true;
3233 } else {
3234 // We are disabling the input method but it is already disabled.
3235 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003236 return false;
3237 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003238 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003239 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003240
satok723a27e2010-11-11 14:58:11 +09003241 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3242 boolean setSubtypeOnly) {
3243 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003244 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003245
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003246 mCurUserActionNotificationSequenceNumber =
3247 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3248 if (DEBUG) {
3249 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3250 + mCurUserActionNotificationSequenceNumber);
3251 }
3252
3253 if (mCurClient != null && mCurClient.client != null) {
3254 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3255 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003256 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003257 }
3258
satok723a27e2010-11-11 14:58:11 +09003259 // Set Subtype here
3260 if (imi == null || subtypeId < 0) {
3261 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003262 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003263 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003264 if (subtypeId < imi.getSubtypeCount()) {
3265 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3266 mSettings.putSelectedSubtype(subtype.hashCode());
3267 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003268 } else {
3269 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003270 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003271 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003272 }
satokab751aa2010-09-14 19:17:36 +09003273 }
satok723a27e2010-11-11 14:58:11 +09003274
satok4c0e7152012-06-20 20:08:44 +09003275 // Workaround.
3276 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3277 // IMEs are not recognized and considered uninstalled.
3278 // Actually, we can't move everything after SystemReady because
3279 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3280 // the default IME here and try cheking the default IME again in systemReady().
3281 // TODO: Do nothing before system ready and implement a separated logic for
3282 // the encryption lock screen.
3283 // TODO: ASEC should be ready before IMMS is instantiated.
3284 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003285 // Set InputMethod here
3286 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3287 }
3288 }
3289
3290 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3291 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3292 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3293 // newDefaultIme is empty when there is no candidate for the selected IME.
3294 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3295 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3296 if (subtypeHashCode != null) {
3297 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003298 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003299 imi, Integer.valueOf(subtypeHashCode));
3300 } catch (NumberFormatException e) {
3301 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3302 }
3303 }
3304 }
3305 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003306 }
3307
satok4e4569d2010-11-19 18:45:53 +09003308 // If there are no selected shortcuts, tries finding the most applicable ones.
3309 private Pair<InputMethodInfo, InputMethodSubtype>
3310 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3311 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3312 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003313 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003314 boolean foundInSystemIME = false;
3315
3316 // Search applicable subtype for each InputMethodInfo
3317 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003318 final String imiId = imi.getId();
3319 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3320 continue;
3321 }
satokcd7cd292010-11-20 15:46:23 +09003322 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003323 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003324 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003325 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003326 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003327 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003328 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003329 }
satokdf31ae62011-01-15 06:19:44 +09003330 // 2. Search by the system locale from enabledSubtypes.
3331 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003332 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003333 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003334 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003335 }
satoka86f5e42011-09-02 17:12:42 +09003336 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003337 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003338 final ArrayList<InputMethodSubtype> subtypesForSearch =
3339 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003340 ? InputMethodUtils.getSubtypes(imi)
3341 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003342 // 4. Search by the current subtype's locale from all subtypes.
3343 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003344 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003345 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003346 }
3347 // 5. Search by the system locale from all subtypes.
3348 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003349 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003350 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003351 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003352 }
satokcd7cd292010-11-20 15:46:23 +09003353 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003354 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003355 // The current input method is the most applicable IME.
3356 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003357 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003358 break;
satok7599a7f2010-12-22 13:45:23 +09003359 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003360 // The system input method is 2nd applicable IME.
3361 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003362 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003363 if ((imi.getServiceInfo().applicationInfo.flags
3364 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3365 foundInSystemIME = true;
3366 }
satok4e4569d2010-11-19 18:45:53 +09003367 }
3368 }
3369 }
3370 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003371 if (mostApplicableIMI != null) {
3372 Slog.w(TAG, "Most applicable shortcut input method was:"
3373 + mostApplicableIMI.getId());
3374 if (mostApplicableSubtype != null) {
3375 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3376 + "," + mostApplicableSubtype.getMode() + ","
3377 + mostApplicableSubtype.getLocale());
3378 }
3379 }
satok4e4569d2010-11-19 18:45:53 +09003380 }
satokcd7cd292010-11-20 15:46:23 +09003381 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003382 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003383 } else {
3384 return null;
3385 }
3386 }
3387
satokab751aa2010-09-14 19:17:36 +09003388 /**
3389 * @return Return the current subtype of this input method.
3390 */
satok42c5a162011-05-26 16:46:14 +09003391 @Override
satokab751aa2010-09-14 19:17:36 +09003392 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003393 // TODO: Make this work even for non-current users?
3394 if (!calledFromValidUser()) {
3395 return null;
3396 }
3397 synchronized (mMethodMap) {
3398 return getCurrentInputMethodSubtypeLocked();
3399 }
3400 }
3401
3402 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003403 if (mCurMethodId == null) {
3404 return null;
3405 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003406 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003407 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3408 if (imi == null || imi.getSubtypeCount() == 0) {
3409 return null;
satok4e4569d2010-11-19 18:45:53 +09003410 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003412 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3413 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003414 if (subtypeId == NOT_A_SUBTYPE_ID) {
3415 // If there are no selected subtypes, the framework will try to find
3416 // the most applicable subtype from explicitly or implicitly enabled
3417 // subtypes.
3418 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003419 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 // If there is only one explicitly or implicitly enabled subtype,
3421 // just returns it.
3422 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3423 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3424 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003425 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003426 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003427 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003428 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003429 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003430 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3431 true);
satok4e4569d2010-11-19 18:45:53 +09003432 }
satok3ef8b292010-11-23 06:06:29 +09003433 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003434 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003435 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003436 }
3437 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003438 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003439 }
3440
satok4e4569d2010-11-19 18:45:53 +09003441 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003442 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003443 @Override
satok4e4569d2010-11-19 18:45:53 +09003444 public List getShortcutInputMethodsAndSubtypes() {
3445 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003446 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003447 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003448 // If there are no selected shortcut subtypes, the framework will try to find
3449 // the most applicable subtype from all subtypes whose mode is
3450 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003451 Pair<InputMethodInfo, InputMethodSubtype> info =
3452 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003453 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003454 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003455 ret.add(info.first);
3456 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003457 }
satok3da92232011-01-11 22:46:30 +09003458 return ret;
satokf3db1af2010-11-23 13:34:33 +09003459 }
satokf3db1af2010-11-23 13:34:33 +09003460 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3461 ret.add(imi);
3462 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3463 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003464 }
3465 }
satokf3db1af2010-11-23 13:34:33 +09003466 return ret;
satok4e4569d2010-11-19 18:45:53 +09003467 }
3468 }
3469
satok42c5a162011-05-26 16:46:14 +09003470 @Override
satokb66d2872010-11-10 01:04:04 +09003471 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003472 // TODO: Make this work even for non-current users?
3473 if (!calledFromValidUser()) {
3474 return false;
3475 }
satokb66d2872010-11-10 01:04:04 +09003476 synchronized (mMethodMap) {
3477 if (subtype != null && mCurMethodId != null) {
3478 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003480 if (subtypeId != NOT_A_SUBTYPE_ID) {
3481 setInputMethodLocked(mCurMethodId, subtypeId);
3482 return true;
3483 }
3484 }
3485 return false;
3486 }
3487 }
3488
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003489 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003490 private static class InputMethodFileManager {
3491 private static final String SYSTEM_PATH = "system";
3492 private static final String INPUT_METHOD_PATH = "inputmethod";
3493 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3494 private static final String NODE_SUBTYPES = "subtypes";
3495 private static final String NODE_SUBTYPE = "subtype";
3496 private static final String NODE_IMI = "imi";
3497 private static final String ATTR_ID = "id";
3498 private static final String ATTR_LABEL = "label";
3499 private static final String ATTR_ICON = "icon";
3500 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3501 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3502 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3503 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3504 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3505 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003506 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003507 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003508 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003509 if (methodMap == null) {
3510 throw new NullPointerException("methodMap is null");
3511 }
3512 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003513 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003514 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3515 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003516 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003517 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003518 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3519 }
3520 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3521 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3522 if (!subtypeFile.exists()) {
3523 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003524 writeAdditionalInputMethodSubtypes(
3525 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003526 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003527 readAdditionalInputMethodSubtypes(
3528 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003529 }
3530 }
3531
3532 private void deleteAllInputMethodSubtypes(String imiId) {
3533 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003534 mAdditionalSubtypesMap.remove(imiId);
3535 writeAdditionalInputMethodSubtypes(
3536 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003537 }
3538 }
3539
3540 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003541 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003542 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003543 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003544 final int N = additionalSubtypes.length;
3545 for (int i = 0; i < N; ++i) {
3546 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003547 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003548 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003549 } else {
3550 Slog.w(TAG, "Duplicated subtype definition found: "
3551 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003552 }
3553 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003554 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3555 writeAdditionalInputMethodSubtypes(
3556 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003557 }
3558 }
3559
3560 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3561 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003562 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003563 }
3564 }
3565
3566 private static void writeAdditionalInputMethodSubtypes(
3567 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3568 HashMap<String, InputMethodInfo> methodMap) {
3569 // Safety net for the case that this function is called before methodMap is set.
3570 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3571 FileOutputStream fos = null;
3572 try {
3573 fos = subtypesFile.startWrite();
3574 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003575 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003576 out.startDocument(null, true);
3577 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3578 out.startTag(null, NODE_SUBTYPES);
3579 for (String imiId : allSubtypes.keySet()) {
3580 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3581 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3582 continue;
3583 }
3584 out.startTag(null, NODE_IMI);
3585 out.attribute(null, ATTR_ID, imiId);
3586 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3587 final int N = subtypesList.size();
3588 for (int i = 0; i < N; ++i) {
3589 final InputMethodSubtype subtype = subtypesList.get(i);
3590 out.startTag(null, NODE_SUBTYPE);
3591 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3592 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3593 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3594 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3595 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3596 out.attribute(null, ATTR_IS_AUXILIARY,
3597 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3598 out.endTag(null, NODE_SUBTYPE);
3599 }
3600 out.endTag(null, NODE_IMI);
3601 }
3602 out.endTag(null, NODE_SUBTYPES);
3603 out.endDocument();
3604 subtypesFile.finishWrite(fos);
3605 } catch (java.io.IOException e) {
3606 Slog.w(TAG, "Error writing subtypes", e);
3607 if (fos != null) {
3608 subtypesFile.failWrite(fos);
3609 }
3610 }
3611 }
3612
3613 private static void readAdditionalInputMethodSubtypes(
3614 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3615 if (allSubtypes == null || subtypesFile == null) return;
3616 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003617 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003618 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003619 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003620 int type = parser.getEventType();
3621 // Skip parsing until START_TAG
3622 while ((type = parser.next()) != XmlPullParser.START_TAG
3623 && type != XmlPullParser.END_DOCUMENT) {}
3624 String firstNodeName = parser.getName();
3625 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3626 throw new XmlPullParserException("Xml doesn't start with subtypes");
3627 }
3628 final int depth =parser.getDepth();
3629 String currentImiId = null;
3630 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3631 while (((type = parser.next()) != XmlPullParser.END_TAG
3632 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3633 if (type != XmlPullParser.START_TAG)
3634 continue;
3635 final String nodeName = parser.getName();
3636 if (NODE_IMI.equals(nodeName)) {
3637 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3638 if (TextUtils.isEmpty(currentImiId)) {
3639 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3640 continue;
3641 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003642 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003643 allSubtypes.put(currentImiId, tempSubtypesArray);
3644 } else if (NODE_SUBTYPE.equals(nodeName)) {
3645 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3646 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3647 continue;
3648 }
3649 final int icon = Integer.valueOf(
3650 parser.getAttributeValue(null, ATTR_ICON));
3651 final int label = Integer.valueOf(
3652 parser.getAttributeValue(null, ATTR_LABEL));
3653 final String imeSubtypeLocale =
3654 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3655 final String imeSubtypeMode =
3656 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3657 final String imeSubtypeExtraValue =
3658 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003659 final boolean isAuxiliary = "1".equals(String.valueOf(
3660 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003661 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3662 .setSubtypeNameResId(label)
3663 .setSubtypeIconResId(icon)
3664 .setSubtypeLocale(imeSubtypeLocale)
3665 .setSubtypeMode(imeSubtypeMode)
3666 .setSubtypeExtraValue(imeSubtypeExtraValue)
3667 .setIsAuxiliary(isAuxiliary)
3668 .build();
satoke7c6998e2011-06-03 17:57:59 +09003669 tempSubtypesArray.add(subtype);
3670 }
3671 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003672 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3673 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003674 return;
satoke7c6998e2011-06-03 17:57:59 +09003675 }
3676 }
3677 }
3678
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003679 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3680 @NonNull
3681 private final Handler mHandler;
3682
3683 LocalServiceImpl(@NonNull final Handler handler) {
3684 mHandler = handler;
3685 }
3686
3687 @Override
3688 public void setInteractive(boolean interactive) {
3689 // Do everything in handler so as not to block the caller.
3690 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3691 interactive ? 1 : 0, 0));
3692 }
3693 }
3694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 @Override
3696 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3697 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3698 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3701 + Binder.getCallingPid()
3702 + ", uid=" + Binder.getCallingUid());
3703 return;
3704 }
3705
3706 IInputMethod method;
3707 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 synchronized (mMethodMap) {
3712 p.println("Current Input Method Manager state:");
3713 int N = mMethodList.size();
3714 p.println(" Input Methods:");
3715 for (int i=0; i<N; i++) {
3716 InputMethodInfo info = mMethodList.get(i);
3717 p.println(" InputMethod #" + i + ":");
3718 info.dump(p, " ");
3719 }
3720 p.println(" Clients:");
3721 for (ClientState ci : mClients.values()) {
3722 p.println(" Client " + ci + ":");
3723 p.println(" client=" + ci.client);
3724 p.println(" inputContext=" + ci.inputContext);
3725 p.println(" sessionRequested=" + ci.sessionRequested);
3726 p.println(" curSession=" + ci.curSession);
3727 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003728 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003730 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3731 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3733 + " mBoundToMethod=" + mBoundToMethod);
3734 p.println(" mCurToken=" + mCurToken);
3735 p.println(" mCurIntent=" + mCurIntent);
3736 method = mCurMethod;
3737 p.println(" mCurMethod=" + mCurMethod);
3738 p.println(" mEnabledSession=" + mEnabledSession);
3739 p.println(" mShowRequested=" + mShowRequested
3740 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3741 + " mShowForced=" + mShowForced
3742 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003743 p.println(" mCurUserActionNotificationSequenceNumber="
3744 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003745 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003746 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003747 p.println(" mSwitchingController:");
3748 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003750
Jeff Brownb88102f2010-09-08 11:49:43 -07003751 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 pw.flush();
3754 try {
3755 client.client.asBinder().dump(fd, args);
3756 } catch (RemoteException e) {
3757 p.println("Input method client dead: " + e);
3758 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003759 } else {
3760 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003762
Jeff Brownb88102f2010-09-08 11:49:43 -07003763 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 pw.flush();
3766 try {
3767 method.asBinder().dump(fd, args);
3768 } catch (RemoteException e) {
3769 p.println("Input method service dead: " + e);
3770 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003771 } else {
3772 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 }
3774 }
3775}