blob: d404a42762d8fd825fb70db13343d02d45796711 [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;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
satoke7c6998e2011-06-03 17:57:59 +090035import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070040import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.app.ActivityManagerNative;
42import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090044import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090045import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090050import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.ComponentName;
52import android.content.ContentResolver;
53import android.content.Context;
54import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090056import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090058import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070060import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090061import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.pm.PackageManager;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070063import android.content.pm.PackageManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080072import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070073import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040075import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070076import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090077import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Handler;
79import android.os.IBinder;
80import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Message;
83import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070084import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090098import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090099import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.PrintWriterPrinter;
101import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900102import android.util.Slog;
103import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900104import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700106import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.view.LayoutInflater;
108import android.view.View;
109import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700111import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700117import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900119import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900121import android.widget.CompoundButton;
122import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900124import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
satoke7c6998e2011-06-03 17:57:59 +0900127import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.FileInputStream;
130import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.IOException;
132import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100133import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900135import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.HashMap;
137import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900138import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140/**
141 * This class provides a system service that manages input methods.
142 */
143public class InputMethodManagerService extends IInputMethodManager.Stub
144 implements ServiceConnection, Handler.Callback {
145 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700147 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700166 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900167 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
satok01038492012-04-09 21:08:27 +0900169 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
170
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700171 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
satokf9f01002011-05-19 21:31:50 +0900173 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
174
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900175 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900176 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177
satok4e4569d2010-11-19 18:45:53 +0900178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800180 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900182 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700185 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700187 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900188 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900189 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900190 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900192 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 // All known input methods. mMethodMap also serves as the global
195 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700196 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
197 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900198 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700199 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900200 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700202 // Used to bring IME service up to visible adjustment while it is being shown.
203 final ServiceConnection mVisibleConnection = new ServiceConnection() {
204 @Override public void onServiceConnected(ComponentName name, IBinder service) {
205 }
206
207 @Override public void onServiceDisconnected(ComponentName name) {
208 }
209 };
210 boolean mVisibleBound = false;
211
satok7cfc0ed2011-06-20 21:29:36 +0900212 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700213 private NotificationManager mNotificationManager;
214 private KeyguardManager mKeyguardManager;
215 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400216 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900218 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900219 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900220 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900221
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900222 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final ClientState client;
224 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700225
226 IInputMethodSession session;
227 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public String toString() {
231 return "SessionState{uid " + client.uid + " pid " + client.pid
232 + " method " + Integer.toHexString(
233 System.identityHashCode(method))
234 + " session " + Integer.toHexString(
235 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700236 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 + "}";
238 }
239
240 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700241 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 client = _client;
243 method = _method;
244 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final IInputMethodClient client;
251 final IInputContext inputContext;
252 final int uid;
253 final int pid;
254 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 boolean sessionRequested;
257 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 @Override
260 public String toString() {
261 return "ClientState{" + Integer.toHexString(
262 System.identityHashCode(this)) + " uid " + uid
263 + " pid " + pid + "}";
264 }
265
266 ClientState(IInputMethodClient _client, IInputContext _inputContext,
267 int _uid, int _pid) {
268 client = _client;
269 inputContext = _inputContext;
270 uid = _uid;
271 pid = _pid;
272 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
273 }
274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700276 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700284 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
285 * method. This is to be synchronized with the secure settings keyed with
286 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
287 *
288 * <p>This can be transiently {@code null} when the system is re-initializing input method
289 * settings, e.g., the system locale is just changed.</p>
290 *
291 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
292 * {@link InputMethodManagerService}.</p>
293 *
294 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700296 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * The current binding sequence number, incremented every time there is
301 * a new bind performed.
302 */
303 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * The client that is currently bound to an input method.
307 */
308 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700311 * The last window token that gained focus.
312 */
313 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 * The input context last provided by the current client.
317 */
318 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * The attributes last provided by the current client.
322 */
323 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700326 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700328 *
329 * <p>This can be {@code null} when no input method is connected.</p>
330 *
331 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700333 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
satokab751aa2010-09-14 19:17:36 +0900337 * The current subtype of the current input method.
338 */
339 private InputMethodSubtype mCurrentSubtype;
340
satok4e4569d2010-11-19 18:45:53 +0900341 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900342 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700343 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900344
John Spurlocke0980502013-10-25 11:59:29 -0400345 // Was the keyguard locked when this client became current?
346 private boolean mCurClientInKeyguard;
347
satokab751aa2010-09-14 19:17:36 +0900348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * Set to true if our ServiceConnection is currently actively bound to
350 * a service (whether or not we have gotten its IBinder back yet).
351 */
352 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Set if the client has asked for the input method to be shown.
356 */
357 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * Set if we were explicitly told to show the input method.
361 */
362 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * Set if we were forced to be shown.
366 */
367 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * Set if we last told the input method to show itself.
371 */
372 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * The Intent used to connect to the current input method.
376 */
377 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * The token we have made for the currently active input method, to
381 * identify it in the future.
382 */
383 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
386 * If non-null, this is the input method service we are currently connected
387 * to.
388 */
389 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * Time that we last initiated a bind to the input method, to determine
393 * if we should try to disconnect and reconnect to it.
394 */
395 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
398 * Have we called mCurMethod.bindInput()?
399 */
400 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * Currently enabled session. Only touched by service thread, not
404 * protected by a lock.
405 */
406 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700409 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700411 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900413 int mCurUserActionNotificationSequenceNumber = 0;
414
Joe Onorato857fd9b2011-01-27 15:08:35 -0800415 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900416
417 /**
418 * A set of status bits regarding the active IME.
419 *
420 * <p>This value is a combination of following two bits:</p>
421 * <dl>
422 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
423 * <dd>
424 * If this bit is ON, connected IME is ready to accept touch/key events.
425 * </dd>
426 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
427 * <dd>
428 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
429 * </dd>
430 * </dl>
431 * <em>Do not update this value outside of setImeWindowStatus.</em>
432 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800433 int mImeWindowVis;
434
Ken Wakasa05dbb652011-08-22 15:22:43 +0900435 private AlertDialog.Builder mDialogBuilder;
436 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900437 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900438 private InputMethodInfo[] mIms;
439 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900440 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700441 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900442 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
443 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700446 int mUserId;
447 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800448 String mLastEnabled = "";
449
Yohei Yukawa81482972015-06-04 00:58:59 -0700450 /**
451 * <em>This constructor must be called within the lock.</em>
452 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 SettingsObserver(Handler handler) {
454 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700455 }
456
457 public void registerContentObserverLocked(int userId) {
458 if (mRegistered && mUserId == userId) {
459 return;
460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700462 if (mRegistered) {
463 mContext.getContentResolver().unregisterContentObserver(this);
464 mRegistered = false;
465 }
466 if (mUserId != userId) {
467 mLastEnabled = "";
468 mUserId = userId;
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700471 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900472 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700473 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900474 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700475 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700476 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700477 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
478 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Michael Wright7b5a96b2014-08-09 19:28:42 -0700481 @Override public void onChange(boolean selfChange, Uri uri) {
482 final Uri showImeUri =
483 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700485 if (showImeUri.equals(uri)) {
486 updateKeyboardFromSettingsLocked();
487 } else {
488 boolean enabledChanged = false;
489 String newEnabled = mSettings.getEnabledInputMethodsStr();
490 if (!mLastEnabled.equals(newEnabled)) {
491 mLastEnabled = newEnabled;
492 enabledChanged = true;
493 }
494 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700498
499 @Override
500 public String toString() {
501 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
502 + " mLastEnabled=" + mLastEnabled + "}";
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800505
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900506 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900507 @Override
508 public void onReceive(Context context, Intent intent) {
509 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700510 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900511 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700512 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900513 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800514 } else if (Intent.ACTION_USER_ADDED.equals(action)
515 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100516 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800517 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700518 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
519 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
520 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
521 final String prevValue = intent.getStringExtra(
522 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
523 final String newValue = intent.getStringExtra(
524 Intent.EXTRA_SETTING_NEW_VALUE);
525 restoreEnabledInputMethods(mContext, prevValue, newValue);
526 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900527 } else {
528 Slog.w(TAG, "Unexpected intent " + intent);
529 }
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700533 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
534 // does not attempt to validate on the fly with any installed device policy, so must only
535 // be run in the context of initial device setup.
536 //
537 // TODO: Move this method to InputMethodUtils with adding unit tests.
538 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
539 if (DEBUG_RESTORE) {
540 Slog.i(TAG, "Restoring enabled input methods:");
541 Slog.i(TAG, "prev=" + prevValue);
542 Slog.i(TAG, " new=" + newValue);
543 }
544 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900545 ArrayMap<String, ArraySet<String>> prevMap =
546 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
547 ArrayMap<String, ArraySet<String>> newMap =
548 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700549
550 // Merge the restored ime+subtype enabled states into the live state
551 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
552 final String imeId = entry.getKey();
553 ArraySet<String> prevSubtypes = prevMap.get(imeId);
554 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700555 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700556 prevMap.put(imeId, prevSubtypes);
557 }
558 prevSubtypes.addAll(entry.getValue());
559 }
560
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700561 final String mergedImesAndSubtypesString =
562 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700563 if (DEBUG_RESTORE) {
564 Slog.i(TAG, "Merged IME string:");
565 Slog.i(TAG, " " + mergedImesAndSubtypesString);
566 }
567 Settings.Secure.putString(context.getContentResolver(),
568 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
569 }
570
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800571 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900572 private boolean isChangingPackagesOfCurrentUser() {
573 final int userId = getChangingUserId();
574 final boolean retval = userId == mSettings.getCurrentUserId();
575 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900576 if (!retval) {
577 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900579 }
580 return retval;
581 }
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800584 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900585 if (!isChangingPackagesOfCurrentUser()) {
586 return false;
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900589 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 final int N = mMethodList.size();
591 if (curInputMethodId != null) {
592 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800593 InputMethodInfo imi = mMethodList.get(i);
594 if (imi.getId().equals(curInputMethodId)) {
595 for (String pkg : packages) {
596 if (imi.getPackageName().equals(pkg)) {
597 if (!doit) {
598 return true;
599 }
satok723a27e2010-11-11 14:58:11 +0900600 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800601 chooseNewDefaultIMELocked();
602 return true;
603 }
604 }
605 }
606 }
607 }
608 }
609 return false;
610 }
611
612 @Override
613 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900614 if (!isChangingPackagesOfCurrentUser()) {
615 return;
616 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800617 synchronized (mMethodMap) {
618 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 final int N = mMethodList.size();
621 if (curInputMethodId != null) {
622 for (int i=0; i<N; i++) {
623 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900624 final String imiId = imi.getId();
625 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800626 curIm = imi;
627 }
satoke7c6998e2011-06-03 17:57:59 +0900628
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900630 if (isPackageModified(imi.getPackageName())) {
631 mFileManager.deleteAllInputMethodSubtypes(imiId);
632 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 if (change == PACKAGE_TEMPORARY_CHANGE
634 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800635 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800636 + imi.getComponent());
637 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639 }
640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900642 buildInputMethodListLocked(
643 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800647 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 int change = isPackageDisappearing(curIm.getPackageName());
649 if (change == PACKAGE_TEMPORARY_CHANGE
650 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800651 ServiceInfo si = null;
652 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900653 si = mIPackageManager.getServiceInfo(
654 curIm.getComponent(), 0, mSettings.getCurrentUserId());
655 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800656 }
657 if (si == null) {
658 // Uh oh, current input method is no longer around!
659 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800660 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900661 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800663 changed = true;
664 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900666 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800667 }
668 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670 }
satokab751aa2010-09-14 19:17:36 +0900671
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 if (curIm == null) {
673 // We currently don't have a default input method... is
674 // one now available?
675 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700676 } else if (!changed && isPackageModified(curIm.getPackageName())) {
677 // Even if the current input method is still available, mCurrentSubtype could
678 // be obsolete when the package is modified in practice.
679 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800683 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
686 }
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
Jeff Brownc28867a2013-03-26 15:42:39 -0700689 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900690 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700691 private final IInputMethod mMethod;
692 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
Jeff Brownc28867a2013-03-26 15:42:39 -0700694 MethodCallback(InputMethodManagerService imms, IInputMethod method,
695 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900696 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700697 mMethod = method;
698 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
satoke7c6998e2011-06-03 17:57:59 +0900701 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700702 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700703 long ident = Binder.clearCallingIdentity();
704 try {
705 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
706 } finally {
707 Binder.restoreCallingIdentity(ident);
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
satok01038492012-04-09 21:08:27 +0900712 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700713 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900714 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700715 public void onHardKeyboardStatusChange(boolean available) {
716 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
717 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900718 }
719
Michael Wright7b5a96b2014-08-09 19:28:42 -0700720 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900721 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700722 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900723 }
724 synchronized(mMethodMap) {
725 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
726 && mSwitchingDialog.isShowing()) {
727 mSwitchingDialogTitleView.findViewById(
728 com.android.internal.R.id.hard_keyboard_section).setVisibility(
729 available ? View.VISIBLE : View.GONE);
730 }
731 }
732 }
733 }
734
Seigo Nonaka7309b122015-08-17 18:34:13 -0700735 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800738 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700740 // Note: SettingsObserver doesn't register observers in its constructor.
741 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 mIWindowManager = IWindowManager.Stub.asInterface(
743 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700744 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800745 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900746 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public void executeMessage(Message msg) {
748 handleMessage(msg);
749 }
Mita Yuned218c72012-12-06 17:18:25 -0800750 }, true /*asyncHandler*/);
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900751 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900752 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700753 mHasFeature = context.getPackageManager().hasSystemFeature(
754 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900755
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400756 Bundle extras = new Bundle();
757 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
758 mImeSwitcherNotification = new Notification.Builder(mContext)
759 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
760 .setWhen(0)
761 .setOngoing(true)
762 .addExtras(extras)
763 .setCategory(Notification.CATEGORY_SYSTEM)
764 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400765
satok7cfc0ed2011-06-20 21:29:36 +0900766 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900767 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900768
769 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900770
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800773 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
774 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700775 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
satok7cfc0ed2011-06-20 21:29:36 +0900778 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 int userId = 0;
780 try {
781 ActivityManagerNative.getDefault().registerUserSwitchObserver(
782 new IUserSwitchObserver.Stub() {
783 @Override
784 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900785 synchronized(mMethodMap) {
786 switchUserLocked(newUserId);
787 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 if (reply != null) {
789 try {
790 reply.sendResult(null);
791 } catch (RemoteException e) {
792 }
793 }
794 }
795
796 @Override
797 public void onUserSwitchComplete(int newUserId) throws RemoteException {
798 }
Kenny Guy42979622015-04-13 18:03:05 +0000799
800 @Override
801 public void onForegroundProfileSwitch(int newProfileId) {
802 // Ignore.
803 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 });
805 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
806 } catch (RemoteException e) {
807 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
808 }
satok81f8b7c2012-12-04 20:42:56 +0900809 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900810
satokd87c2592010-09-29 11:52:06 +0900811 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900812 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700814
815 // Let the package manager query which are the default imes
816 // as they get certain permissions granted by default.
817 PackageManagerInternal packageManagerInternal = LocalServices.getService(
818 PackageManagerInternal.class);
819 packageManagerInternal.setImePackagesProvider(
820 new PackageManagerInternal.PackagesProvider() {
821 @Override
822 public String[] getPackages(int userId) {
823 synchronized (mMethodMap) {
824 final int currentUserId = mSettings.getCurrentUserId();
825 // TODO: We are switching the current user id in the settings
826 // object to query it and then revert the user id. Ideally, we
827 // should call a API in settings with the user id as an argument.
828 mSettings.setCurrentUserId(userId);
829 List<InputMethodInfo> imes = mSettings
830 .getEnabledInputMethodListLocked();
831 String[] packageNames = null;
832 if (imes != null) {
833 final int imeCount = imes.size();
834 packageNames = new String[imeCount];
835 for (int i = 0; i < imeCount; i++) {
836 InputMethodInfo ime = imes.get(i);
837 packageNames[i] = ime.getPackageName();
838 }
839 }
840 mSettings.setCurrentUserId(currentUserId);
841 return packageNames;
842 }
843 }
844 });
845
Kenny Guy2a764942014-04-02 13:29:20 +0100846 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900847 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900848 synchronized (mMethodMap) {
849 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
850 mSettings, context);
851 }
satok0a1bcf42012-05-16 19:26:31 +0900852
853 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900854 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900855 if (DEBUG) {
856 Slog.d(TAG, "Initial default ime = " + defaultImiId);
857 }
satok0a1bcf42012-05-16 19:26:31 +0900858 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
859
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900860 synchronized (mMethodMap) {
861 buildInputMethodListLocked(mMethodList, mMethodMap,
862 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
863 }
satokd87c2592010-09-29 11:52:06 +0900864 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
satok0a1bcf42012-05-16 19:26:31 +0900866 if (!mImeSelectedOnBoot) {
867 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900868 synchronized (mMethodMap) {
869 resetDefaultImeLocked(context);
870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900873 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700874 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900875 updateFromSettingsLocked(true);
876 }
satok5b927c432012-05-01 20:09:34 +0900877
878 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
879 // according to the new system locale.
880 final IntentFilter filter = new IntentFilter();
881 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
882 mContext.registerReceiver(
883 new BroadcastReceiver() {
884 @Override
885 public void onReceive(Context context, Intent intent) {
886 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900887 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900888 }
889 }
890 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700891 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900892 }
893
satok5b927c432012-05-01 20:09:34 +0900894 private void resetDefaultImeLocked(Context context) {
895 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900896 if (mCurMethodId != null
897 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900898 return;
899 }
900
901 InputMethodInfo defIm = null;
902 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900903 if (defIm == null && mSystemReady) {
904 final Locale systemLocale = context.getResources().getConfiguration().locale;
905 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
906 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
907 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900908 defIm = imi;
909 Slog.i(TAG, "Selected default: " + imi.getId());
910 }
911 }
912 }
913 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900914 defIm = InputMethodUtils.getMostApplicableDefaultIME(
915 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900916 if (defIm != null) {
917 Slog.i(TAG, "Default found, using " + defIm.getId());
918 } else {
919 Slog.i(TAG, "No default found");
920 }
satok5b927c432012-05-01 20:09:34 +0900921 }
922 if (defIm != null) {
923 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
924 }
925 }
926
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900927 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
928 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 if (!mSystemReady) {
930 // not system ready
931 return;
932 }
933 final Locale newLocale = mRes.getConfiguration().locale;
934 if (!updateOnlyWhenLocaleChanged
935 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
936 if (!updateOnlyWhenLocaleChanged) {
937 hideCurrentInputLocked(0, null);
Yohei Yukawabc7b5262015-11-17 17:38:41 -0800938 resetCurrentMethodAndClient();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 }
940 if (DEBUG) {
941 Slog.i(TAG, "Locale has been changed to " + newLocale);
942 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900943 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 if (!updateOnlyWhenLocaleChanged) {
945 final String selectedImiId = mSettings.getSelectedInputMethod();
946 if (TextUtils.isEmpty(selectedImiId)) {
947 // This is the first time of the user switch and
948 // set the current ime to the proper one.
949 resetDefaultImeLocked(mContext);
950 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900951 } else {
952 // If the locale is changed, needs to reset the default ime
953 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800955 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 mLastSystemLocale = newLocale;
957 if (!updateOnlyWhenLocaleChanged) {
958 try {
959 startInputInnerLocked();
960 } catch (RuntimeException e) {
961 Slog.w(TAG, "Unexpected exception", e);
962 }
963 }
964 }
965 }
966
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900967 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900968 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
969 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 }
971
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900972 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700973 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
974 + " currentUserId=" + mSettings.getCurrentUserId());
975
Yohei Yukawa81482972015-06-04 00:58:59 -0700976 // ContentObserver should be registered again when the user is changed
977 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100979 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900980 // InputMethodFileManager should be reset when the user is changed
981 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700983
984 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
985 + " defaultImiId=" + defaultImiId);
986
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900987 // For secondary users, the list of enabled IMEs may not have been updated since the
988 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
989 // not be empty even if the IME has been uninstalled by the primary user.
990 // Even in such cases, IMMS works fine because it will find the most applicable
991 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900992 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900993 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900994 initialUserSwitch /* needsToResetDefaultIme */);
995 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -0700996 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
997 mSettings.getEnabledInputMethodListLocked(), newUserId,
998 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900999 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001000
1001 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1002 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 }
1004
Kenny Guy2a764942014-04-02 13:29:20 +01001005 void updateCurrentProfileIds() {
1006 List<UserInfo> profiles =
1007 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1008 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1009 for (int i = 0; i < currentProfileIds.length; i++) {
1010 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001011 }
Kenny Guy2a764942014-04-02 13:29:20 +01001012 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001013 }
1014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 @Override
1016 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1017 throws RemoteException {
1018 try {
1019 return super.onTransact(code, data, reply, flags);
1020 } catch (RuntimeException e) {
1021 // The input method manager only throws security exceptions, so let's
1022 // log all others.
1023 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001024 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 throw e;
1027 }
1028 }
1029
Svetoslav Ganova0027152013-06-25 14:59:53 -07001030 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001031 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001032 if (DEBUG) {
1033 Slog.d(TAG, "--- systemReady");
1034 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001035 if (!mSystemReady) {
1036 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001037 mKeyguardManager =
1038 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001039 mNotificationManager = (NotificationManager)
1040 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1041 mStatusBar = statusBar;
1042 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001043 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001044 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1045 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001046 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001047 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001048 mHardKeyboardListener);
1049 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001050 buildInputMethodListLocked(mMethodList, mMethodMap,
1051 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001052 if (!mImeSelectedOnBoot) {
1053 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001055 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1056 mSettings.getEnabledInputMethodListLocked(),
1057 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001058 }
1059 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001060 try {
1061 startInputInnerLocked();
1062 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001064 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001065 }
1066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001069 // ---------------------------------------------------------------------------------------
1070 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1071 // 1) it comes from the system process
1072 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1073 private boolean calledFromValidUser() {
1074 final int uid = Binder.getCallingUid();
1075 final int userId = UserHandle.getUserId(uid);
1076 if (DEBUG) {
1077 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1078 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1079 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001080 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1081 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001082 }
Kenny Guy2a764942014-04-02 13:29:20 +01001083 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001084 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001086
1087 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1088 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1089 // must not manage background users' states in any functions.
1090 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1091 // by a token.
1092 if (mContext.checkCallingOrSelfPermission(
1093 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1094 == PackageManager.PERMISSION_GRANTED) {
1095 if (DEBUG) {
1096 Slog.d(TAG, "--- Access granted because the calling process has "
1097 + "the INTERACT_ACROSS_USERS_FULL permission");
1098 }
1099 return true;
1100 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001101 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1102 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001103 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001104 }
1105
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001106
1107 /**
1108 * Returns true iff the caller is identified to be the current input method with the token.
1109 * @param token The window token given to the input method when it was started.
1110 * @return true if and only if non-null valid token is specified.
1111 */
1112 private boolean calledWithValidToken(IBinder token) {
1113 if (token == null || mCurToken != token) {
1114 return false;
1115 }
1116 return true;
1117 }
1118
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 private boolean bindCurrentInputMethodService(
1120 Intent service, ServiceConnection conn, int flags) {
1121 if (service == null || conn == null) {
1122 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1123 return false;
1124 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001125 return mContext.bindServiceAsUser(service, conn, flags,
1126 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 }
1128
satoke7c6998e2011-06-03 17:57:59 +09001129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 // TODO: Make this work even for non-current users?
1132 if (!calledFromValidUser()) {
1133 return Collections.emptyList();
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001136 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 }
1139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 // TODO: Make this work even for non-current users?
1143 if (!calledFromValidUser()) {
1144 return Collections.emptyList();
1145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001147 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
1150
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001151 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001152 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001153 * @return enabled subtypes of the specified imi
1154 */
satoke7c6998e2011-06-03 17:57:59 +09001155 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001156 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001157 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001158 // TODO: Make this work even for non-current users?
1159 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001160 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 }
satok67ddf9c2010-11-17 09:45:54 +09001162 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001163 final InputMethodInfo imi;
1164 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001165 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001166 } else {
1167 imi = mMethodMap.get(imiId);
1168 }
1169 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001170 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001171 }
1172 return mSettings.getEnabledInputMethodSubtypeListLocked(
1173 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001174 }
1175 }
1176
satoke7c6998e2011-06-03 17:57:59 +09001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public void addClient(IInputMethodClient client,
1179 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 if (!calledFromValidUser()) {
1181 return;
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 synchronized (mMethodMap) {
1184 mClients.put(client.asBinder(), new ClientState(client,
1185 inputContext, uid, pid));
1186 }
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 if (!calledFromValidUser()) {
1192 return;
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001195 ClientState cs = mClients.remove(client.asBinder());
1196 if (cs != null) {
1197 clearClientSessionLocked(cs);
1198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 void executeOrSendMessage(IInterface target, Message msg) {
1203 if (target.asBinder() instanceof Binder) {
1204 mCaller.sendMessage(msg);
1205 } else {
1206 handleMessage(msg);
1207 msg.recycle();
1208 }
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001211 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 + mCurClient.client.asBinder());
1215 if (mBoundToMethod) {
1216 mBoundToMethod = false;
1217 if (mCurMethod != null) {
1218 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1219 MSG_UNBIND_INPUT, mCurMethod));
1220 }
1221 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001222
1223 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1224 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1226 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1227 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project10592532009-03-18 17:39:46 -07001230 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 private int getImeShowFlags() {
1235 int flags = 0;
1236 if (mShowForced) {
1237 flags |= InputMethod.SHOW_FORCED
1238 | InputMethod.SHOW_EXPLICIT;
1239 } else if (mShowExplicitlyRequested) {
1240 flags |= InputMethod.SHOW_EXPLICIT;
1241 }
1242 return flags;
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 private int getAppShowFlags() {
1246 int flags = 0;
1247 if (mShowForced) {
1248 flags |= InputMethodManager.SHOW_FORCED;
1249 } else if (!mShowExplicitlyRequested) {
1250 flags |= InputMethodManager.SHOW_IMPLICIT;
1251 }
1252 return flags;
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn7663d802012-02-24 13:08:49 -08001255 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (!mBoundToMethod) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1258 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1259 mBoundToMethod = true;
1260 }
1261 final SessionState session = mCurClient.curSession;
1262 if (initial) {
1263 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1264 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1265 } else {
1266 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1267 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1268 }
1269 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001271 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001273 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001274 (session.channel != null ? session.channel.dup() : null),
1275 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001279 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 // If no method is currently selected, do nothing.
1281 if (mCurMethodId == null) {
1282 return mNoBinding;
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 ClientState cs = mClients.get(client.asBinder());
1286 if (cs == null) {
1287 throw new IllegalArgumentException("unknown client "
1288 + client.asBinder());
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 try {
1292 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1293 // Check with the window manager to make sure this client actually
1294 // has a window with focus. If not, reject. This is thread safe
1295 // because if the focus changes some time before or after, the
1296 // next client receiving focus that has any interest in input will
1297 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1300 return null;
1301 }
1302 } catch (RemoteException e) {
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn7663d802012-02-24 13:08:49 -08001305 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1306 }
1307
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001308 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001309 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 // If no method is currently selected, do nothing.
1311 if (mCurMethodId == null) {
1312 return mNoBinding;
1313 }
1314
Yohei Yukawad57ba672015-06-08 16:39:46 -07001315 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1316 attribute.packageName)) {
1317 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1318 + " uid=" + cs.uid + " package=" + attribute.packageName);
1319 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001320 }
1321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001323 // Was the keyguard locked when switching over to the new client?
1324 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // If the client is changing, we need to switch over to the new
1326 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001327 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001329 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
1331 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001332 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001333 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001334 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // Bump up the sequence for this client and attach it.
1339 mCurSeq++;
1340 if (mCurSeq <= 0) mCurSeq = 1;
1341 mCurClient = cs;
1342 mCurInputContext = inputContext;
1343 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 // Check if the input method is changing.
1346 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1347 if (cs.curSession != null) {
1348 // Fast case: if we are already connected to the input method,
1349 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001350 return attachNewInputLocked(
1351 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 if (mHaveConnection) {
1354 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // Return to client, and we will get back with it when
1356 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001357 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001358 return new InputBindResult(null, null, mCurId, mCurSeq,
1359 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } else if (SystemClock.uptimeMillis()
1361 < (mLastBindTime+TIME_TO_RECONNECT)) {
1362 // In this case we have connected to the service, but
1363 // don't yet have its interface. If it hasn't been too
1364 // long since we did the connection, we'll return to
1365 // the client and wait to get the service interface so
1366 // we can report back. If it has been too long, we want
1367 // to fall through so we can try a disconnect/reconnect
1368 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001369 return new InputBindResult(null, null, mCurId, mCurSeq,
1370 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1373 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375 }
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001378 return startInputInnerLocked();
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001381 InputBindResult startInputInnerLocked() {
1382 if (mCurMethodId == null) {
1383 return mNoBinding;
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001386 if (!mSystemReady) {
1387 // If the system is not yet ready, we shouldn't be running third
1388 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001389 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1390 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1394 if (info == null) {
1395 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001398 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1401 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001402 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1403 com.android.internal.R.string.input_method_binding_label);
1404 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1405 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001406 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001407 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1408 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 mLastBindTime = SystemClock.uptimeMillis();
1410 mHaveConnection = true;
1411 mCurId = info.getId();
1412 mCurToken = new Binder();
1413 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001414 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 mIWindowManager.addWindowToken(mCurToken,
1416 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1417 } catch (RemoteException e) {
1418 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001419 return new InputBindResult(null, null, mCurId, mCurSeq,
1420 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 } else {
1422 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 + mCurIntent);
1425 }
1426 return null;
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
satoke7c6998e2011-06-03 17:57:59 +09001429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001431 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001432 if (!calledFromValidUser()) {
1433 return null;
1434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 synchronized (mMethodMap) {
1436 final long ident = Binder.clearCallingIdentity();
1437 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001438 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 } finally {
1440 Binder.restoreCallingIdentity(ident);
1441 }
1442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
satoke7c6998e2011-06-03 17:57:59 +09001445 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 public void finishInput(IInputMethodClient client) {
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
satoke7c6998e2011-06-03 17:57:59 +09001449 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 public void onServiceConnected(ComponentName name, IBinder service) {
1451 synchronized (mMethodMap) {
1452 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1453 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001454 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001456 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001457 return;
1458 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001459 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001460 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1461 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001463 clearClientSessionLocked(mCurClient);
1464 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467 }
1468 }
1469
Jeff Brownc28867a2013-03-26 15:42:39 -07001470 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1471 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 synchronized (mMethodMap) {
1473 if (mCurMethod != null && method != null
1474 && mCurMethod.asBinder() == method.asBinder()) {
1475 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001476 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001478 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001479 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 if (res.method != null) {
1481 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1482 MSG_BIND_METHOD, mCurClient.client, res));
1483 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001484 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486 }
1487 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001488
1489 // Session abandoned. Close its associated input channel.
1490 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001493 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001494 if (mVisibleBound) {
1495 mContext.unbindService(mVisibleConnection);
1496 mVisibleBound = false;
1497 }
1498
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001499 if (mHaveConnection) {
1500 mContext.unbindService(this);
1501 mHaveConnection = false;
1502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001504 if (mCurToken != null) {
1505 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001506 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001507 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001508 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001509 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001510 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001511 mIWindowManager.removeWindowToken(mCurToken);
1512 } catch (RemoteException e) {
1513 }
1514 mCurToken = null;
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
The Android Open Source Project10592532009-03-18 17:39:46 -07001517 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001518 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001521 void resetCurrentMethodAndClient() {
1522 mCurMethodId = null;
1523 unbindCurrentMethodLocked(false);
1524 unbindCurrentClientLocked();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001525 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001526
1527 void requestClientSessionLocked(ClientState cs) {
1528 if (!cs.sessionRequested) {
1529 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1530 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1531 cs.sessionRequested = true;
1532 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1533 MSG_CREATE_SESSION, mCurMethod, channels[1],
1534 new MethodCallback(this, mCurMethod, channels[0])));
1535 }
1536 }
1537
1538 void clearClientSessionLocked(ClientState cs) {
1539 finishSessionLocked(cs.curSession);
1540 cs.curSession = null;
1541 cs.sessionRequested = false;
1542 }
1543
1544 private void finishSessionLocked(SessionState sessionState) {
1545 if (sessionState != null) {
1546 if (sessionState.session != null) {
1547 try {
1548 sessionState.session.finishSession();
1549 } catch (RemoteException e) {
1550 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001551 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 }
1553 sessionState.session = null;
1554 }
1555 if (sessionState.channel != null) {
1556 sessionState.channel.dispose();
1557 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001558 }
1559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001562 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (mCurMethod != null) {
1564 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001567
Jeff Brownc28867a2013-03-26 15:42:39 -07001568 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001569 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 mCurMethod = null;
1571 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001572 if (mStatusBar != null) {
1573 mStatusBar.setIconVisibility("ime", false);
1574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
satoke7c6998e2011-06-03 17:57:59 +09001577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public void onServiceDisconnected(ComponentName name) {
1579 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001580 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 + " mCurIntent=" + mCurIntent);
1582 if (mCurMethod != null && mCurIntent != null
1583 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 // We consider this to be a new bind attempt, since the system
1586 // should now try to restart the service for us.
1587 mLastBindTime = SystemClock.uptimeMillis();
1588 mShowRequested = mInputShown;
1589 mInputShown = false;
1590 if (mCurClient != null) {
1591 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1592 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1593 }
1594 }
1595 }
1596 }
1597
satokf9f01002011-05-19 21:31:50 +09001598 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1600 long ident = Binder.clearCallingIdentity();
1601 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001603 if (!calledWithValidToken(token)) {
1604 final int uid = Binder.getCallingUid();
1605 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1606 + " token:" + token);
1607 return;
1608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001611 if (mStatusBar != null) {
1612 mStatusBar.setIconVisibility("ime", false);
1613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001616 CharSequence contentDescription = null;
1617 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001618 // Use PackageManager to load label
1619 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001620 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 mIPackageManager.getApplicationInfo(packageName, 0,
1622 mSettings.getCurrentUserId()));
1623 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001624 /* ignore */
1625 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001626 if (mStatusBar != null) {
1627 mStatusBar.setIcon("ime", packageName, iconId, 0,
1628 contentDescription != null
1629 ? contentDescription.toString() : null);
1630 mStatusBar.setIconVisibility("ime", true);
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 }
1634 } finally {
1635 Binder.restoreCallingIdentity(ident);
1636 }
1637 }
1638
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001639 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001640 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001641 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001642 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001643 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001644 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001645 // When physical keyboard is attached, we show the ime switcher (or notification if
1646 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1647 // exists in the IME switcher dialog. Might be OK to remove this condition once
1648 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1649 return true;
1650 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001651 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001652
1653 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1654 final int N = imis.size();
1655 if (N > 2) return true;
1656 if (N < 1) return false;
1657 int nonAuxCount = 0;
1658 int auxCount = 0;
1659 InputMethodSubtype nonAuxSubtype = null;
1660 InputMethodSubtype auxSubtype = null;
1661 for(int i = 0; i < N; ++i) {
1662 final InputMethodInfo imi = imis.get(i);
1663 final List<InputMethodSubtype> subtypes =
1664 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1665 final int subtypeCount = subtypes.size();
1666 if (subtypeCount == 0) {
1667 ++nonAuxCount;
1668 } else {
1669 for (int j = 0; j < subtypeCount; ++j) {
1670 final InputMethodSubtype subtype = subtypes.get(j);
1671 if (!subtype.isAuxiliary()) {
1672 ++nonAuxCount;
1673 nonAuxSubtype = subtype;
1674 } else {
1675 ++auxCount;
1676 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001677 }
1678 }
satok7cfc0ed2011-06-20 21:29:36 +09001679 }
1680 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001681 if (nonAuxCount > 1 || auxCount > 1) {
1682 return true;
1683 } else if (nonAuxCount == 1 && auxCount == 1) {
1684 if (nonAuxSubtype != null && auxSubtype != null
1685 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1686 || auxSubtype.overridesImplicitlyEnabledSubtype()
1687 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1688 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1689 return false;
1690 }
1691 return true;
1692 }
1693 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001694 }
1695
John Spurlocke0980502013-10-25 11:59:29 -04001696 private boolean isKeyguardLocked() {
1697 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1698 }
1699
satokdbf29502011-08-25 15:28:23 +09001700 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001701 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001702 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001703 if (!calledWithValidToken(token)) {
1704 final int uid = Binder.getCallingUid();
1705 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1706 + " token:" + token);
1707 return;
1708 }
1709
1710 synchronized (mMethodMap) {
1711 mImeWindowVis = vis;
1712 mBackDisposition = backDisposition;
1713 updateSystemUiLocked(token, vis, backDisposition);
1714 }
1715 }
1716
1717 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1718 synchronized (mMethodMap) {
1719 updateSystemUiLocked(token, vis, backDisposition);
1720 }
1721 }
1722
1723 // Caution! This method is called in this class. Handle multi-user carefully
1724 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1725 if (!calledWithValidToken(token)) {
1726 final int uid = Binder.getCallingUid();
1727 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1728 + " token:" + token);
1729 return;
1730 }
1731
1732 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1733 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001734 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001735 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001736 // apply policy for binder calls
1737 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1738 vis = 0;
satok06487a52010-10-29 11:37:18 +09001739 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001740 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1741 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1742 if (mStatusBar != null) {
1743 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1744 needsToShowImeSwitcher);
1745 }
1746 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1747 if (imi != null && needsToShowImeSwitcher) {
1748 // Used to load label
1749 final CharSequence title = mRes.getText(
1750 com.android.internal.R.string.select_input_method);
1751 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1752 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001753 mImeSwitcherNotification.setContentTitle(title)
1754 .setContentText(summary)
1755 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001756 try {
1757 if ((mNotificationManager != null)
1758 && !mIWindowManager.hasNavigationBar()) {
1759 if (DEBUG) {
1760 Slog.d(TAG, "--- show notification: label = " + summary);
1761 }
1762 mNotificationManager.notifyAsUser(null,
1763 com.android.internal.R.string.select_input_method,
1764 mImeSwitcherNotification.build(), UserHandle.ALL);
1765 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001766 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001767 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001768 }
1769 } else {
1770 if (mNotificationShown && mNotificationManager != null) {
1771 if (DEBUG) {
1772 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001773 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001774 mNotificationManager.cancelAsUser(null,
1775 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1776 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001777 }
satok06487a52010-10-29 11:37:18 +09001778 }
1779 } finally {
1780 Binder.restoreCallingIdentity(ident);
1781 }
1782 }
1783
satoke7c6998e2011-06-03 17:57:59 +09001784 @Override
satokf9f01002011-05-19 21:31:50 +09001785 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001786 if (!calledFromValidUser()) {
1787 return;
1788 }
satokf9f01002011-05-19 21:31:50 +09001789 synchronized (mMethodMap) {
1790 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1791 for (int i = 0; i < spans.length; ++i) {
1792 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001793 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001794 mSecureSuggestionSpans.put(ss, currentImi);
1795 }
1796 }
1797 }
1798 }
1799
satoke7c6998e2011-06-03 17:57:59 +09001800 @Override
satokf9f01002011-05-19 21:31:50 +09001801 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001802 if (!calledFromValidUser()) {
1803 return false;
1804 }
satokf9f01002011-05-19 21:31:50 +09001805 synchronized (mMethodMap) {
1806 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1807 // TODO: Do not send the intent if the process of the targetImi is already dead.
1808 if (targetImi != null) {
1809 final String[] suggestions = span.getSuggestions();
1810 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001811 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001812 final Intent intent = new Intent();
1813 // Ensures that only a class in the original IME package will receive the
1814 // notification.
satok42c5a162011-05-26 16:46:14 +09001815 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001816 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1817 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1818 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1819 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001820 final long ident = Binder.clearCallingIdentity();
1821 try {
1822 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
satokf9f01002011-05-19 21:31:50 +09001826 return true;
1827 }
1828 }
1829 return false;
1830 }
1831
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001832 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001833 updateInputMethodsFromSettingsLocked(enabledMayChange);
1834 updateKeyboardFromSettingsLocked();
1835 }
1836
1837 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001838 if (enabledMayChange) {
1839 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1840 for (int i=0; i<enabled.size(); i++) {
1841 // We allow the user to select "disabled until used" apps, so if they
1842 // are enabling one of those here we now need to make it enabled.
1843 InputMethodInfo imm = enabled.get(i);
1844 try {
1845 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1846 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1847 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001848 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001849 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001850 if (DEBUG) {
1851 Slog.d(TAG, "Update state(" + imm.getId()
1852 + "): DISABLED_UNTIL_USED -> DEFAULT");
1853 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001854 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1855 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001856 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1857 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001858 }
1859 } catch (RemoteException e) {
1860 }
1861 }
1862 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001863 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1864 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1865 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1866 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001867 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001868 // There is no input method selected, try to choose new applicable input method.
1869 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001870 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001871 }
1872 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001874 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001876 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001877 resetCurrentMethodAndClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
satokf3db1af2010-11-23 13:34:33 +09001879 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001880 } else {
1881 // There is no longer an input method set, so stop any current one.
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001882 resetCurrentMethodAndClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001884 // Here is not the perfect place to reset the switching controller. Ideally
1885 // mSwitchingController and mSettings should be able to share the same state.
1886 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1887 // the same enabled IMEs list.
1888 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001889
1890 }
1891
1892 public void updateKeyboardFromSettingsLocked() {
1893 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1894 if (mSwitchingDialog != null
1895 && mSwitchingDialogTitleView != null
1896 && mSwitchingDialog.isShowing()) {
1897 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1898 com.android.internal.R.id.hard_keyboard_switch);
1899 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001902
satokab751aa2010-09-14 19:17:36 +09001903 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 InputMethodInfo info = mMethodMap.get(id);
1905 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001906 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001908
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001909 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001910 // We have already made sure that the package name belongs to the application's UID.
1911 // No further UID check is required.
1912 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1913 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001914 }
1915 }
1916
satokd81e9502012-05-21 12:58:45 +09001917 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001919 final int subtypeCount = info.getSubtypeCount();
1920 if (subtypeCount <= 0) {
1921 return;
satokcd7cd292010-11-20 15:46:23 +09001922 }
satokd81e9502012-05-21 12:58:45 +09001923 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1924 final InputMethodSubtype newSubtype;
1925 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1926 newSubtype = info.getSubtypeAt(subtypeId);
1927 } else {
1928 // If subtype is null, try to find the most applicable one from
1929 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001930 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001931 }
1932 if (newSubtype == null || oldSubtype == null) {
1933 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1934 + ", new subtype = " + newSubtype);
1935 return;
1936 }
1937 if (newSubtype != oldSubtype) {
1938 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1939 if (mCurMethod != null) {
1940 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001941 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001942 mCurMethod.changeInputMethodSubtype(newSubtype);
1943 } catch (RemoteException e) {
1944 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001945 }
1946 }
1947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 return;
1949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001950
satokd81e9502012-05-21 12:58:45 +09001951 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 final long ident = Binder.clearCallingIdentity();
1953 try {
satokab751aa2010-09-14 19:17:36 +09001954 // Set a subtype to this input method.
1955 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001956 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1957 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1958 // because mCurMethodId is stored as a history in
1959 // setSelectedInputMethodAndSubtypeLocked().
1960 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961
1962 if (ActivityManagerNative.isSystemReady()) {
1963 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001964 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001966 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001968 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 } finally {
1970 Binder.restoreCallingIdentity(ident);
1971 }
1972 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001973
satok42c5a162011-05-26 16:46:14 +09001974 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001975 public boolean showSoftInput(IInputMethodClient client, int flags,
1976 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001977 if (!calledFromValidUser()) {
1978 return false;
1979 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001980 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 long ident = Binder.clearCallingIdentity();
1982 try {
1983 synchronized (mMethodMap) {
1984 if (mCurClient == null || client == null
1985 || mCurClient.client.asBinder() != client.asBinder()) {
1986 try {
1987 // We need to check if this is the current client with
1988 // focus in the window manager, to allow this call to
1989 // be made before input is started in it.
1990 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001991 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001992 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
1994 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001995 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001998
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001999 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002000 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002 } finally {
2003 Binder.restoreCallingIdentity(ident);
2004 }
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
The Android Open Source Project4df24232009-03-05 14:34:35 -08002007 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 mShowRequested = true;
2009 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2010 mShowExplicitlyRequested = true;
2011 }
2012 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2013 mShowExplicitlyRequested = true;
2014 mShowForced = true;
2015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002016
Dianne Hackborncc278702009-09-02 23:07:23 -07002017 if (!mSystemReady) {
2018 return false;
2019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
The Android Open Source Project4df24232009-03-05 14:34:35 -08002021 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002023 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002024 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2025 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2026 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002028 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002029 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002030 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002031 | Context.BIND_TREAT_LIKE_ACTIVITY
2032 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002033 mVisibleBound = true;
2034 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002035 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002037 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 // The client has asked to have the input method shown, but
2039 // we have been sitting here too long with a connection to the
2040 // service and no interface received, so let's disconnect/connect
2041 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002042 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002044 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002046 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002047 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002048 } else {
2049 if (DEBUG) {
2050 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2051 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002054
The Android Open Source Project4df24232009-03-05 14:34:35 -08002055 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
satok42c5a162011-05-26 16:46:14 +09002058 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002059 public boolean hideSoftInput(IInputMethodClient client, int flags,
2060 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002061 if (!calledFromValidUser()) {
2062 return false;
2063 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002064 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 long ident = Binder.clearCallingIdentity();
2066 try {
2067 synchronized (mMethodMap) {
2068 if (mCurClient == null || client == null
2069 || mCurClient.client.asBinder() != client.asBinder()) {
2070 try {
2071 // We need to check if this is the current client with
2072 // focus in the window manager, to allow this call to
2073 // be made before input is started in it.
2074 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002075 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2076 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002077 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002080 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 }
2082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002083
Joe Onorato8a9b2202010-02-26 18:56:32 -08002084 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002085 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087 } finally {
2088 Binder.restoreCallingIdentity(ident);
2089 }
2090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002091
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2094 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002095 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 -08002096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
2098 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002099 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 -08002100 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002102
2103 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2104 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2105 // to be updated with the new value sent from IME process. Even in such a transient state
2106 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2107 // application process as a valid request, and have even promised such a behavior with CTS
2108 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2109 // IMMS#InputShown indicates that the software keyboard is shown.
2110 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2111 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2112 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002114 if (shouldHideSoftInput) {
2115 // The IME will report its visible state again after the following message finally
2116 // delivered to the IME process as an IPC. Hence the inconsistency between
2117 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2118 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2120 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2121 res = true;
2122 } else {
2123 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002125 if (mHaveConnection && mVisibleBound) {
2126 mContext.unbindService(mVisibleConnection);
2127 mVisibleBound = false;
2128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 mInputShown = false;
2130 mShowRequested = false;
2131 mShowExplicitlyRequested = false;
2132 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
satok42c5a162011-05-26 16:46:14 +09002136 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002137 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2138 int controlFlags, int softInputMode, int windowFlags,
2139 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002140 // Needs to check the validity before clearing calling identity
2141 final boolean calledFromValidUser = calledFromValidUser();
2142
Dianne Hackborn7663d802012-02-24 13:08:49 -08002143 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 long ident = Binder.clearCallingIdentity();
2145 try {
2146 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002147 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002148 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002150 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002151
Dianne Hackborn7663d802012-02-24 13:08:49 -08002152 ClientState cs = mClients.get(client.asBinder());
2153 if (cs == null) {
2154 throw new IllegalArgumentException("unknown client "
2155 + client.asBinder());
2156 }
2157
2158 try {
2159 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2160 // Check with the window manager to make sure this client actually
2161 // has a window with focus. If not, reject. This is thread safe
2162 // because if the focus changes some time before or after, the
2163 // next client receiving focus that has any interest in input will
2164 // be calling through here after that change happens.
2165 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2166 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2167 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002169 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002171
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002172 if (!calledFromValidUser) {
2173 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2174 Slog.w(TAG, "If you want to interect with IME, you need "
2175 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2176 hideCurrentInputLocked(0, null);
2177 return null;
2178 }
2179
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002180 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002181 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002182 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002183 if (attribute != null) {
2184 return startInputUncheckedLocked(cs, inputContext, attribute,
2185 controlFlags);
2186 }
2187 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002188 }
2189 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002190
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002191 // Should we auto-show the IME even if the caller has not
2192 // specified what should be done with it?
2193 // We only do this automatically if the window can resize
2194 // to accommodate the IME (so what the user sees will give
2195 // them good context without input information being obscured
2196 // by the IME) or if running on a large screen where there
2197 // is more room for the target window + IME.
2198 final boolean doAutoShow =
2199 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2200 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2201 || mRes.getConfiguration().isLayoutSizeAtLeast(
2202 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002203 final boolean isTextEditor =
2204 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2205
2206 // We want to start input before showing the IME, but after closing
2207 // it. We want to do this after closing it to help the IME disappear
2208 // more quickly (not get stuck behind it initializing itself for the
2209 // new focused input, even if its window wants to hide the IME).
2210 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2213 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002214 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2216 // There is no focus view, and this window will
2217 // be behind any soft input window, so hide the
2218 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002219 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002222 } else if (isTextEditor && doAutoShow && (softInputMode &
2223 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 // There is a focus view, and we are navigating forward
2225 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002226 // We only do this automatically if the window can resize
2227 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002228 // them good context without input information being obscured
2229 // by the IME) or if running on a large screen where there
2230 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002231 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002232 if (attribute != null) {
2233 res = startInputUncheckedLocked(cs, inputContext, attribute,
2234 controlFlags);
2235 didStart = true;
2236 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002237 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239 break;
2240 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2241 // Do nothing.
2242 break;
2243 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2244 if ((softInputMode &
2245 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002246 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002247 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
2249 break;
2250 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002251 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002252 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 break;
2254 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2255 if ((softInputMode &
2256 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002257 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002258 if (attribute != null) {
2259 res = startInputUncheckedLocked(cs, inputContext, attribute,
2260 controlFlags);
2261 didStart = true;
2262 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002263 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 }
2265 break;
2266 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002267 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002268 if (attribute != null) {
2269 res = startInputUncheckedLocked(cs, inputContext, attribute,
2270 controlFlags);
2271 didStart = true;
2272 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002273 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 break;
2275 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002276
2277 if (!didStart && attribute != null) {
2278 res = startInputUncheckedLocked(cs, inputContext, attribute,
2279 controlFlags);
2280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 }
2282 } finally {
2283 Binder.restoreCallingIdentity(ident);
2284 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285
2286 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
satok42c5a162011-05-26 16:46:14 +09002289 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002290 public void showInputMethodPickerFromClient(
2291 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002292 if (!calledFromValidUser()) {
2293 return;
2294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 synchronized (mMethodMap) {
2296 if (mCurClient == null || client == null
2297 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002298 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002299 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 }
2301
satok440aab52010-11-25 09:43:11 +09002302 // Always call subtype picker, because subtype picker is a superset of input method
2303 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002304 mHandler.sendMessage(mCaller.obtainMessageI(
2305 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002306 }
2307 }
2308
satok42c5a162011-05-26 16:46:14 +09002309 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002311 if (!calledFromValidUser()) {
2312 return;
2313 }
satok28203512010-11-24 11:06:49 +09002314 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2315 }
2316
satok42c5a162011-05-26 16:46:14 +09002317 @Override
satok28203512010-11-24 11:06:49 +09002318 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002319 if (!calledFromValidUser()) {
2320 return;
2321 }
satok28203512010-11-24 11:06:49 +09002322 synchronized (mMethodMap) {
2323 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002324 setInputMethodWithSubtypeIdLocked(token, id,
2325 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2326 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002327 } else {
2328 setInputMethod(token, id);
2329 }
2330 }
satokab751aa2010-09-14 19:17:36 +09002331 }
2332
satok42c5a162011-05-26 16:46:14 +09002333 @Override
satokb416a712010-11-25 20:42:14 +09002334 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002335 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002336 if (!calledFromValidUser()) {
2337 return;
2338 }
satokb416a712010-11-25 20:42:14 +09002339 synchronized (mMethodMap) {
2340 if (mCurClient == null || client == null
2341 || mCurClient.client.asBinder() != client.asBinder()) {
2342 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2343 }
satok7fee71f2010-12-17 18:54:26 +09002344 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2345 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002346 }
2347 }
2348
satok4fc87d62011-05-20 16:13:43 +09002349 @Override
satok735cf382010-11-11 20:40:09 +09002350 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002351 if (!calledFromValidUser()) {
2352 return false;
2353 }
satok735cf382010-11-11 20:40:09 +09002354 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002355 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002356 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002357 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002358 lastImi = mMethodMap.get(lastIme.first);
2359 } else {
2360 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002361 }
satok4fc87d62011-05-20 16:13:43 +09002362 String targetLastImiId = null;
2363 int subtypeId = NOT_A_SUBTYPE_ID;
2364 if (lastIme != null && lastImi != null) {
2365 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2366 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2367 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2368 : mCurrentSubtype.hashCode();
2369 // If the last IME is the same as the current IME and the last subtype is not
2370 // defined, there is no need to switch to the last IME.
2371 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2372 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002373 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002374 }
2375 }
2376
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002377 if (TextUtils.isEmpty(targetLastImiId)
2378 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002379 // This is a safety net. If the currentSubtype can't be added to the history
2380 // and the framework couldn't find the last ime, we will make the last ime be
2381 // the most applicable enabled keyboard subtype of the system imes.
2382 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2383 if (enabled != null) {
2384 final int N = enabled.size();
2385 final String locale = mCurrentSubtype == null
2386 ? mRes.getConfiguration().locale.toString()
2387 : mCurrentSubtype.getLocale();
2388 for (int i = 0; i < N; ++i) {
2389 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002390 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002391 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002392 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2393 InputMethodUtils.getSubtypes(imi),
2394 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002395 if (keyboardSubtype != null) {
2396 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002397 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002398 imi, keyboardSubtype.hashCode());
2399 if(keyboardSubtype.getLocale().equals(locale)) {
2400 break;
2401 }
2402 }
2403 }
2404 }
2405 }
2406 }
2407
2408 if (!TextUtils.isEmpty(targetLastImiId)) {
2409 if (DEBUG) {
2410 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2411 + ", from: " + mCurMethodId + ", " + subtypeId);
2412 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002413 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002414 return true;
2415 } else {
2416 return false;
2417 }
satok735cf382010-11-11 20:40:09 +09002418 }
2419 }
2420
satoke7c6998e2011-06-03 17:57:59 +09002421 @Override
satok688bd472012-02-09 20:09:17 +09002422 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002423 if (!calledFromValidUser()) {
2424 return false;
2425 }
satok688bd472012-02-09 20:09:17 +09002426 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002427 if (!calledWithValidToken(token)) {
2428 final int uid = Binder.getCallingUid();
2429 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2430 + " token:" + token);
2431 return false;
2432 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002433 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002434 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2435 if (nextSubtype == null) {
2436 return false;
2437 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002438 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2439 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002440 return true;
2441 }
2442 }
2443
2444 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002445 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2446 if (!calledFromValidUser()) {
2447 return false;
2448 }
2449 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002450 if (!calledWithValidToken(token)) {
2451 final int uid = Binder.getCallingUid();
2452 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2453 + "token. uid:" + uid + " token:" + token);
2454 return false;
2455 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002456 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002457 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2458 if (nextSubtype == null) {
2459 return false;
2460 }
2461 return true;
2462 }
2463 }
2464
2465 @Override
satok68f1b782011-04-11 14:26:04 +09002466 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (!calledFromValidUser()) {
2468 return null;
2469 }
satok68f1b782011-04-11 14:26:04 +09002470 synchronized (mMethodMap) {
2471 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2472 // TODO: Handle the case of the last IME with no subtypes
2473 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2474 || TextUtils.isEmpty(lastIme.second)) return null;
2475 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2476 if (lastImi == null) return null;
2477 try {
2478 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002479 final int lastSubtypeId =
2480 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002481 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2482 return null;
2483 }
2484 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002485 } catch (NumberFormatException e) {
2486 return null;
2487 }
2488 }
2489 }
2490
satoke7c6998e2011-06-03 17:57:59 +09002491 @Override
satokee5e77c2011-09-02 18:50:15 +09002492 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002493 if (!calledFromValidUser()) {
2494 return;
2495 }
satok91e88122011-07-18 11:11:42 +09002496 // By this IPC call, only a process which shares the same uid with the IME can add
2497 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002498 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002499 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002500 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002501 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002502 final String[] packageInfos;
2503 try {
2504 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2505 } catch (RemoteException e) {
2506 Slog.e(TAG, "Failed to get package infos");
2507 return;
2508 }
satok91e88122011-07-18 11:11:42 +09002509 if (packageInfos != null) {
2510 final int packageNum = packageInfos.length;
2511 for (int i = 0; i < packageNum; ++i) {
2512 if (packageInfos[i].equals(imi.getPackageName())) {
2513 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002514 final long ident = Binder.clearCallingIdentity();
2515 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002516 buildInputMethodListLocked(mMethodList, mMethodMap,
2517 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002518 } finally {
2519 Binder.restoreCallingIdentity(ident);
2520 }
satokee5e77c2011-09-02 18:50:15 +09002521 return;
satok91e88122011-07-18 11:11:42 +09002522 }
2523 }
2524 }
satoke7c6998e2011-06-03 17:57:59 +09002525 }
satokee5e77c2011-09-02 18:50:15 +09002526 return;
satoke7c6998e2011-06-03 17:57:59 +09002527 }
2528
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002529 @Override
2530 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002531 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002532 }
2533
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002534 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002535 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002536 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002537 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002538 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002539 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002540 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2541 if (DEBUG) {
2542 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2543 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2544 + " actual: " + sequenceNumber);
2545 }
2546 return;
2547 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002548 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2549 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002550 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002551 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002552 }
2553 }
2554
satok28203512010-11-24 11:06:49 +09002555 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002557 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2558 }
2559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002561 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2562 if (token == null) {
2563 if (mContext.checkCallingOrSelfPermission(
2564 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2565 != PackageManager.PERMISSION_GRANTED) {
2566 throw new SecurityException(
2567 "Using null token requires permission "
2568 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002570 } else if (mCurToken != token) {
2571 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2572 + " token: " + token);
2573 return;
2574 }
2575
2576 final long ident = Binder.clearCallingIdentity();
2577 try {
2578 setInputMethodLocked(id, subtypeId);
2579 } finally {
2580 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
2582 }
2583
satok42c5a162011-05-26 16:46:14 +09002584 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002586 if (!calledFromValidUser()) {
2587 return;
2588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002590 if (!calledWithValidToken(token)) {
2591 final int uid = Binder.getCallingUid();
2592 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2593 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 return;
2595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 long ident = Binder.clearCallingIdentity();
2597 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002598 hideCurrentInputLocked(flags, null);
2599 } finally {
2600 Binder.restoreCallingIdentity(ident);
2601 }
2602 }
2603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002604
satok42c5a162011-05-26 16:46:14 +09002605 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002606 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002607 if (!calledFromValidUser()) {
2608 return;
2609 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002610 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002611 if (!calledWithValidToken(token)) {
2612 final int uid = Binder.getCallingUid();
2613 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2614 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002615 return;
2616 }
2617 long ident = Binder.clearCallingIdentity();
2618 try {
2619 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 } finally {
2621 Binder.restoreCallingIdentity(ident);
2622 }
2623 }
2624 }
2625
2626 void setEnabledSessionInMainThread(SessionState session) {
2627 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002628 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002630 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002631 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 } catch (RemoteException e) {
2633 }
2634 }
2635 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002636 if (mEnabledSession != null && mEnabledSession.session != null) {
2637 try {
2638 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2639 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2640 } catch (RemoteException e) {
2641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
2643 }
2644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002645
satok42c5a162011-05-26 16:46:14 +09002646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002648 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002650 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002651 final boolean showAuxSubtypes;
2652 switch (msg.arg1) {
2653 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2654 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2655 // implemented so that auxiliary subtypes will be excluded when the soft
2656 // keyboard is invisible.
2657 showAuxSubtypes = mInputShown;
2658 break;
2659 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2660 showAuxSubtypes = true;
2661 break;
2662 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2663 showAuxSubtypes = false;
2664 break;
2665 default:
2666 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2667 return false;
2668 }
2669 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002670 return true;
2671
satok47a44912010-10-06 16:03:58 +09002672 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002674 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002675 args.recycle();
satok217f5482010-12-15 05:19:19 +09002676 return true;
2677
2678 case MSG_SHOW_IM_CONFIG:
2679 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002680 return true;
2681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 case MSG_UNBIND_INPUT:
2685 try {
2686 ((IInputMethod)msg.obj).unbindInput();
2687 } catch (RemoteException e) {
2688 // There is nothing interesting about the method dying.
2689 }
2690 return true;
2691 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002692 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 try {
2694 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2695 } catch (RemoteException e) {
2696 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002697 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 return true;
2699 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002700 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002702 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002703 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002704 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 } catch (RemoteException e) {
2706 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002707 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 return true;
2709 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002710 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002712 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002713 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002714 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 } catch (RemoteException e) {
2716 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002717 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 return true;
2719 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002720 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002722 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2724 } catch (RemoteException e) {
2725 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002726 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002728 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002729 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002730 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002731 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002733 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002735 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002736 // Dispose the channel if the input method is not local to this process
2737 // because the remote proxy will get its own copy when unparceled.
2738 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002739 channel.dispose();
2740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002742 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002748 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 try {
2750 SessionState session = (SessionState)args.arg1;
2751 setEnabledSessionInMainThread(session);
2752 session.method.startInput((IInputContext)args.arg2,
2753 (EditorInfo)args.arg3);
2754 } catch (RemoteException e) {
2755 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002756 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 return true;
2758 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002759 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 try {
2761 SessionState session = (SessionState)args.arg1;
2762 setEnabledSessionInMainThread(session);
2763 session.method.restartInput((IInputContext)args.arg2,
2764 (EditorInfo)args.arg3);
2765 } catch (RemoteException e) {
2766 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002767 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 case MSG_UNBIND_METHOD:
2773 try {
2774 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2775 } catch (RemoteException e) {
2776 // There is nothing interesting about the last client dying.
2777 }
2778 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002779 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002780 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002781 IInputMethodClient client = (IInputMethodClient)args.arg1;
2782 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002784 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002786 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002787 } finally {
2788 // Dispose the channel if the input method is not local to this process
2789 // because the remote proxy will get its own copy when unparceled.
2790 if (res.channel != null && Binder.isProxy(client)) {
2791 res.channel.dispose();
2792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002794 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002796 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002797 case MSG_SET_ACTIVE:
2798 try {
2799 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2800 } catch (RemoteException e) {
2801 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2802 + ((ClientState)msg.obj).pid + " uid "
2803 + ((ClientState)msg.obj).uid);
2804 }
2805 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002806 case MSG_SET_INTERACTIVE:
2807 handleSetInteractive(msg.arg1 != 0);
2808 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002809 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2810 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002811 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002812 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002813 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002814 } catch (RemoteException e) {
2815 Slog.w(TAG, "Got RemoteException sending "
2816 + "setUserActionNotificationSequenceNumber("
2817 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002818 + clientState.pid + " uid "
2819 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002820 }
2821 return true;
2822 }
satok01038492012-04-09 21:08:27 +09002823
2824 // --------------------------------------------------------------
2825 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002826 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002827 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
2829 return false;
2830 }
2831
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002832 private void handleSetInteractive(final boolean interactive) {
2833 synchronized (mMethodMap) {
2834 mIsInteractive = interactive;
2835 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2836
2837 // Inform the current client of the change in active status
2838 if (mCurClient != null && mCurClient.client != null) {
2839 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2840 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2841 }
2842 }
2843 }
2844
satokdc9ddae2011-10-06 12:22:36 +09002845 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002846 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2847 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002848 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002849 if (DEBUG) {
2850 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2851 }
satok723a27e2010-11-11 14:58:11 +09002852 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002853 return true;
2854 }
2855
2856 return false;
2857 }
2858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002860 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002861 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002862 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002863 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 list.clear();
2866 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002867
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002868 // Use for queryIntentServicesAsUser
2869 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002871 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002873 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2874 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002875
satoke7c6998e2011-06-03 17:57:59 +09002876 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2877 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 for (int i = 0; i < services.size(); ++i) {
2879 ResolveInfo ri = services.get(i);
2880 ServiceInfo si = ri.serviceInfo;
2881 ComponentName compName = new ComponentName(si.packageName, si.name);
2882 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2883 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002884 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 + ": it does not require the permission "
2886 + android.Manifest.permission.BIND_INPUT_METHOD);
2887 continue;
2888 }
2889
Joe Onorato8a9b2202010-02-26 18:56:32 -08002890 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891
2892 try {
satoke7c6998e2011-06-03 17:57:59 +09002893 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002895 final String id = p.getId();
2896 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897
2898 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002899 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002901 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002902 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002905
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002906 if (resetDefaultEnabledIme) {
2907 final ArrayList<InputMethodInfo> defaultEnabledIme =
2908 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2909 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2910 final InputMethodInfo imi = defaultEnabledIme.get(i);
2911 if (DEBUG) {
2912 Slog.d(TAG, "--- enable ime = " + imi);
2913 }
2914 setInputMethodEnabledLocked(imi.getId(), true);
2915 }
2916 }
2917
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002918 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002919 if (!TextUtils.isEmpty(defaultImiId)) {
2920 if (!map.containsKey(defaultImiId)) {
2921 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2922 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002923 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002924 }
2925 } else {
2926 // Double check that the default IME is certainly enabled.
2927 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002928 }
2929 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002930 // Here is not the perfect place to reset the switching controller. Ideally
2931 // mSwitchingController and mSettings should be able to share the same state.
2932 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2933 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002934 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002938
satok217f5482010-12-15 05:19:19 +09002939 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002940 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002941 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002942 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2943 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002944 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002945 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002946 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002947 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002948 }
2949
2950 private void showConfigureInputMethods() {
2951 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2952 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2953 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2954 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002955 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002956 }
2957
satok2c93efc2012-04-02 19:33:47 +09002958 private boolean isScreenLocked() {
2959 return mKeyguardManager != null
2960 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2961 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002962
Seigo Nonaka14e13912015-05-06 21:04:13 -07002963 private void showInputMethodMenu(boolean showAuxSubtypes) {
2964 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002967 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002968
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002969 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002970 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002971 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002972
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002973 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002974 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002975 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2976 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002977 if (immis == null || immis.size() == 0) {
2978 return;
2979 }
2980
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002981 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982
satok688bd472012-02-09 20:09:17 +09002983 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002984 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002985 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002986
satokc3690562012-01-10 20:14:43 +09002987 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002988 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002989 if (currentSubtype != null) {
2990 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002991 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2992 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002993 }
2994 }
2995
Ken Wakasa761eb372011-03-04 19:06:18 +09002996 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002997 mIms = new InputMethodInfo[N];
2998 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002999 int checkedItem = 0;
3000 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003001 final ImeSubtypeListItem item = imList.get(i);
3002 mIms[i] = item.mImi;
3003 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003004 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003005 int subtypeId = mSubtypeIds[i];
3006 if ((subtypeId == NOT_A_SUBTYPE_ID)
3007 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3008 || (subtypeId == lastInputMethodSubtypeId)) {
3009 checkedItem = i;
3010 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003013
3014 final Context settingsContext = new ContextThemeWrapper(context,
3015 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3016
3017 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003018 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3019 @Override
3020 public void onCancel(DialogInterface dialog) {
3021 hideInputMethodMenu();
3022 }
3023 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003024
3025 final Context dialogContext = mDialogBuilder.getContext();
3026 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3027 com.android.internal.R.styleable.DialogPreference,
3028 com.android.internal.R.attr.alertDialogStyle, 0);
3029 final Drawable dialogIcon = a.getDrawable(
3030 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3031 a.recycle();
3032
3033 mDialogBuilder.setIcon(dialogIcon);
3034
3035 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3036 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003037 final View tv = inflater.inflate(
3038 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3039 mDialogBuilder.setCustomTitle(tv);
3040
3041 // Setup layout for a toggle switch of the hardware keyboard
3042 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003043 mSwitchingDialogTitleView
3044 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003045 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003046 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003047 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003048 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003049 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003050 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3051 @Override
3052 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003053 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003054 // Ensure that the input method dialog is dismissed when changing
3055 // the hardware keyboard state.
3056 hideInputMethodMenu();
3057 }
3058 });
3059
Alan Viverette505e3ab2014-11-24 15:22:11 -08003060 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003061 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3062 final OnClickListener choiceListener = new OnClickListener() {
3063 @Override
3064 public void onClick(final DialogInterface dialog, final int which) {
3065 synchronized (mMethodMap) {
3066 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3067 || mSubtypeIds.length <= which) {
3068 return;
satok01038492012-04-09 21:08:27 +09003069 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003070 final InputMethodInfo im = mIms[which];
3071 int subtypeId = mSubtypeIds[which];
3072 adapter.mCheckedItem = which;
3073 adapter.notifyDataSetChanged();
3074 hideInputMethodMenu();
3075 if (im != null) {
3076 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3077 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003078 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003079 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003081 }
3082 }
3083 };
3084 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003086 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003087 final OnClickListener positiveListener = new OnClickListener() {
3088 @Override
3089 public void onClick(DialogInterface dialog, int whichButton) {
3090 showConfigureInputMethods();
3091 }
3092 };
satok82beadf2010-12-27 19:03:06 +09003093 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003094 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003097 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 mSwitchingDialog.getWindow().setType(
3099 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003100 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3101 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003102 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003103 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 mSwitchingDialog.show();
3105 }
3106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003107
Ken Wakasa05dbb652011-08-22 15:22:43 +09003108 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3109 private final LayoutInflater mInflater;
3110 private final int mTextViewResourceId;
3111 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003112 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003113 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3114 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3115 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003116
Ken Wakasa05dbb652011-08-22 15:22:43 +09003117 mTextViewResourceId = textViewResourceId;
3118 mItemsList = itemsList;
3119 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003120 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003121 }
3122
3123 @Override
3124 public View getView(int position, View convertView, ViewGroup parent) {
3125 final View view = convertView != null ? convertView
3126 : mInflater.inflate(mTextViewResourceId, null);
3127 if (position < 0 || position >= mItemsList.size()) return view;
3128 final ImeSubtypeListItem item = mItemsList.get(position);
3129 final CharSequence imeName = item.mImeName;
3130 final CharSequence subtypeName = item.mSubtypeName;
3131 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3132 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3133 if (TextUtils.isEmpty(subtypeName)) {
3134 firstTextView.setText(imeName);
3135 secondTextView.setVisibility(View.GONE);
3136 } else {
3137 firstTextView.setText(subtypeName);
3138 secondTextView.setText(imeName);
3139 secondTextView.setVisibility(View.VISIBLE);
3140 }
3141 final RadioButton radioButton =
3142 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3143 radioButton.setChecked(position == mCheckedItem);
3144 return view;
3145 }
3146 }
3147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003149 synchronized (mMethodMap) {
3150 hideInputMethodMenuLocked();
3151 }
3152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003153
The Android Open Source Project10592532009-03-18 17:39:46 -07003154 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003155 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156
The Android Open Source Project10592532009-03-18 17:39:46 -07003157 if (mSwitchingDialog != null) {
3158 mSwitchingDialog.dismiss();
3159 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003161
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003162 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003163 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003164 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003168
satok42c5a162011-05-26 16:46:14 +09003169 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003171 // TODO: Make this work even for non-current users?
3172 if (!calledFromValidUser()) {
3173 return false;
3174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 synchronized (mMethodMap) {
3176 if (mContext.checkCallingOrSelfPermission(
3177 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3178 != PackageManager.PERMISSION_GRANTED) {
3179 throw new SecurityException(
3180 "Requires permission "
3181 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3182 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 long ident = Binder.clearCallingIdentity();
3185 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003186 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 } finally {
3188 Binder.restoreCallingIdentity(ident);
3189 }
3190 }
3191 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003192
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003193 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3194 // Make sure this is a valid input method.
3195 InputMethodInfo imm = mMethodMap.get(id);
3196 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003197 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003198 }
3199
satokd87c2592010-09-29 11:52:06 +09003200 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3201 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003202
satokd87c2592010-09-29 11:52:06 +09003203 if (enabled) {
3204 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3205 if (pair.first.equals(id)) {
3206 // We are enabling this input method, but it is already enabled.
3207 // Nothing to do. The previous state was enabled.
3208 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003209 }
3210 }
satokd87c2592010-09-29 11:52:06 +09003211 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3212 // Previous state was disabled.
3213 return false;
3214 } else {
3215 StringBuilder builder = new StringBuilder();
3216 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3217 builder, enabledInputMethodsList, id)) {
3218 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003219 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003220 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3221 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3222 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003223 }
3224 // Previous state was enabled.
3225 return true;
3226 } else {
3227 // We are disabling the input method but it is already disabled.
3228 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003229 return false;
3230 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003231 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003232 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003233
satok723a27e2010-11-11 14:58:11 +09003234 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3235 boolean setSubtypeOnly) {
3236 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003237 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003238
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003239 mCurUserActionNotificationSequenceNumber =
3240 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3241 if (DEBUG) {
3242 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3243 + mCurUserActionNotificationSequenceNumber);
3244 }
3245
3246 if (mCurClient != null && mCurClient.client != null) {
3247 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3248 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003249 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003250 }
3251
satok723a27e2010-11-11 14:58:11 +09003252 // Set Subtype here
3253 if (imi == null || subtypeId < 0) {
3254 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003255 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003256 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003257 if (subtypeId < imi.getSubtypeCount()) {
3258 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3259 mSettings.putSelectedSubtype(subtype.hashCode());
3260 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003261 } else {
3262 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003263 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003264 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003265 }
satokab751aa2010-09-14 19:17:36 +09003266 }
satok723a27e2010-11-11 14:58:11 +09003267
satok4c0e7152012-06-20 20:08:44 +09003268 // Workaround.
3269 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3270 // IMEs are not recognized and considered uninstalled.
3271 // Actually, we can't move everything after SystemReady because
3272 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3273 // the default IME here and try cheking the default IME again in systemReady().
3274 // TODO: Do nothing before system ready and implement a separated logic for
3275 // the encryption lock screen.
3276 // TODO: ASEC should be ready before IMMS is instantiated.
3277 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003278 // Set InputMethod here
3279 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3280 }
3281 }
3282
3283 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3284 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3285 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3286 // newDefaultIme is empty when there is no candidate for the selected IME.
3287 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3288 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3289 if (subtypeHashCode != null) {
3290 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003291 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003292 imi, Integer.valueOf(subtypeHashCode));
3293 } catch (NumberFormatException e) {
3294 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3295 }
3296 }
3297 }
3298 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003299 }
3300
satok4e4569d2010-11-19 18:45:53 +09003301 // If there are no selected shortcuts, tries finding the most applicable ones.
3302 private Pair<InputMethodInfo, InputMethodSubtype>
3303 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3304 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3305 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003306 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003307 boolean foundInSystemIME = false;
3308
3309 // Search applicable subtype for each InputMethodInfo
3310 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003311 final String imiId = imi.getId();
3312 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3313 continue;
3314 }
satokcd7cd292010-11-20 15:46:23 +09003315 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003316 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003317 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003318 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003319 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003320 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003321 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003322 }
satokdf31ae62011-01-15 06:19:44 +09003323 // 2. Search by the system locale from enabledSubtypes.
3324 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003325 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003326 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003327 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003328 }
satoka86f5e42011-09-02 17:12:42 +09003329 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003330 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003331 final ArrayList<InputMethodSubtype> subtypesForSearch =
3332 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003333 ? InputMethodUtils.getSubtypes(imi)
3334 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003335 // 4. Search by the current subtype's locale from all subtypes.
3336 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003337 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003338 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003339 }
3340 // 5. Search by the system locale from all subtypes.
3341 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003342 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003343 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003344 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003345 }
satokcd7cd292010-11-20 15:46:23 +09003346 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003347 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003348 // The current input method is the most applicable IME.
3349 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003350 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003351 break;
satok7599a7f2010-12-22 13:45:23 +09003352 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003353 // The system input method is 2nd applicable IME.
3354 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003355 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003356 if ((imi.getServiceInfo().applicationInfo.flags
3357 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3358 foundInSystemIME = true;
3359 }
satok4e4569d2010-11-19 18:45:53 +09003360 }
3361 }
3362 }
3363 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003364 if (mostApplicableIMI != null) {
3365 Slog.w(TAG, "Most applicable shortcut input method was:"
3366 + mostApplicableIMI.getId());
3367 if (mostApplicableSubtype != null) {
3368 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3369 + "," + mostApplicableSubtype.getMode() + ","
3370 + mostApplicableSubtype.getLocale());
3371 }
3372 }
satok4e4569d2010-11-19 18:45:53 +09003373 }
satokcd7cd292010-11-20 15:46:23 +09003374 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003375 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003376 } else {
3377 return null;
3378 }
3379 }
3380
satokab751aa2010-09-14 19:17:36 +09003381 /**
3382 * @return Return the current subtype of this input method.
3383 */
satok42c5a162011-05-26 16:46:14 +09003384 @Override
satokab751aa2010-09-14 19:17:36 +09003385 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003386 // TODO: Make this work even for non-current users?
3387 if (!calledFromValidUser()) {
3388 return null;
3389 }
3390 synchronized (mMethodMap) {
3391 return getCurrentInputMethodSubtypeLocked();
3392 }
3393 }
3394
3395 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003396 if (mCurMethodId == null) {
3397 return null;
3398 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003399 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003400 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3401 if (imi == null || imi.getSubtypeCount() == 0) {
3402 return null;
satok4e4569d2010-11-19 18:45:53 +09003403 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003404 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003405 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3406 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003407 if (subtypeId == NOT_A_SUBTYPE_ID) {
3408 // If there are no selected subtypes, the framework will try to find
3409 // the most applicable subtype from explicitly or implicitly enabled
3410 // subtypes.
3411 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003412 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003413 // If there is only one explicitly or implicitly enabled subtype,
3414 // just returns it.
3415 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3416 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3417 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003418 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003419 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003420 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003422 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003423 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3424 true);
satok4e4569d2010-11-19 18:45:53 +09003425 }
satok3ef8b292010-11-23 06:06:29 +09003426 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003427 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003428 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003429 }
3430 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003431 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003432 }
3433
satok4e4569d2010-11-19 18:45:53 +09003434 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003435 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003436 @Override
satok4e4569d2010-11-19 18:45:53 +09003437 public List getShortcutInputMethodsAndSubtypes() {
3438 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003439 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003440 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003441 // If there are no selected shortcut subtypes, the framework will try to find
3442 // the most applicable subtype from all subtypes whose mode is
3443 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003444 Pair<InputMethodInfo, InputMethodSubtype> info =
3445 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003446 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003447 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003448 ret.add(info.first);
3449 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003450 }
satok3da92232011-01-11 22:46:30 +09003451 return ret;
satokf3db1af2010-11-23 13:34:33 +09003452 }
satokf3db1af2010-11-23 13:34:33 +09003453 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3454 ret.add(imi);
3455 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3456 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003457 }
3458 }
satokf3db1af2010-11-23 13:34:33 +09003459 return ret;
satok4e4569d2010-11-19 18:45:53 +09003460 }
3461 }
3462
satok42c5a162011-05-26 16:46:14 +09003463 @Override
satokb66d2872010-11-10 01:04:04 +09003464 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003465 // TODO: Make this work even for non-current users?
3466 if (!calledFromValidUser()) {
3467 return false;
3468 }
satokb66d2872010-11-10 01:04:04 +09003469 synchronized (mMethodMap) {
3470 if (subtype != null && mCurMethodId != null) {
3471 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003472 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003473 if (subtypeId != NOT_A_SUBTYPE_ID) {
3474 setInputMethodLocked(mCurMethodId, subtypeId);
3475 return true;
3476 }
3477 }
3478 return false;
3479 }
3480 }
3481
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003482 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003483 private static class InputMethodFileManager {
3484 private static final String SYSTEM_PATH = "system";
3485 private static final String INPUT_METHOD_PATH = "inputmethod";
3486 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3487 private static final String NODE_SUBTYPES = "subtypes";
3488 private static final String NODE_SUBTYPE = "subtype";
3489 private static final String NODE_IMI = "imi";
3490 private static final String ATTR_ID = "id";
3491 private static final String ATTR_LABEL = "label";
3492 private static final String ATTR_ICON = "icon";
3493 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3494 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3495 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3496 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3497 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3498 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003499 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003500 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003501 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003502 if (methodMap == null) {
3503 throw new NullPointerException("methodMap is null");
3504 }
3505 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003506 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003507 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3508 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003509 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003510 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003511 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3512 }
3513 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3514 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3515 if (!subtypeFile.exists()) {
3516 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003517 writeAdditionalInputMethodSubtypes(
3518 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003519 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003520 readAdditionalInputMethodSubtypes(
3521 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003522 }
3523 }
3524
3525 private void deleteAllInputMethodSubtypes(String imiId) {
3526 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003527 mAdditionalSubtypesMap.remove(imiId);
3528 writeAdditionalInputMethodSubtypes(
3529 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003530 }
3531 }
3532
3533 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003534 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003535 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003536 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003537 final int N = additionalSubtypes.length;
3538 for (int i = 0; i < N; ++i) {
3539 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003540 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003541 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003542 } else {
3543 Slog.w(TAG, "Duplicated subtype definition found: "
3544 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003545 }
3546 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003547 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3548 writeAdditionalInputMethodSubtypes(
3549 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003550 }
3551 }
3552
3553 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3554 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003555 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003556 }
3557 }
3558
3559 private static void writeAdditionalInputMethodSubtypes(
3560 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3561 HashMap<String, InputMethodInfo> methodMap) {
3562 // Safety net for the case that this function is called before methodMap is set.
3563 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3564 FileOutputStream fos = null;
3565 try {
3566 fos = subtypesFile.startWrite();
3567 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003568 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003569 out.startDocument(null, true);
3570 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3571 out.startTag(null, NODE_SUBTYPES);
3572 for (String imiId : allSubtypes.keySet()) {
3573 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3574 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3575 continue;
3576 }
3577 out.startTag(null, NODE_IMI);
3578 out.attribute(null, ATTR_ID, imiId);
3579 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3580 final int N = subtypesList.size();
3581 for (int i = 0; i < N; ++i) {
3582 final InputMethodSubtype subtype = subtypesList.get(i);
3583 out.startTag(null, NODE_SUBTYPE);
3584 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3585 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3586 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3587 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3588 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3589 out.attribute(null, ATTR_IS_AUXILIARY,
3590 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3591 out.endTag(null, NODE_SUBTYPE);
3592 }
3593 out.endTag(null, NODE_IMI);
3594 }
3595 out.endTag(null, NODE_SUBTYPES);
3596 out.endDocument();
3597 subtypesFile.finishWrite(fos);
3598 } catch (java.io.IOException e) {
3599 Slog.w(TAG, "Error writing subtypes", e);
3600 if (fos != null) {
3601 subtypesFile.failWrite(fos);
3602 }
3603 }
3604 }
3605
3606 private static void readAdditionalInputMethodSubtypes(
3607 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3608 if (allSubtypes == null || subtypesFile == null) return;
3609 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003610 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003611 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003612 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003613 int type = parser.getEventType();
3614 // Skip parsing until START_TAG
3615 while ((type = parser.next()) != XmlPullParser.START_TAG
3616 && type != XmlPullParser.END_DOCUMENT) {}
3617 String firstNodeName = parser.getName();
3618 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3619 throw new XmlPullParserException("Xml doesn't start with subtypes");
3620 }
3621 final int depth =parser.getDepth();
3622 String currentImiId = null;
3623 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3624 while (((type = parser.next()) != XmlPullParser.END_TAG
3625 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3626 if (type != XmlPullParser.START_TAG)
3627 continue;
3628 final String nodeName = parser.getName();
3629 if (NODE_IMI.equals(nodeName)) {
3630 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3631 if (TextUtils.isEmpty(currentImiId)) {
3632 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3633 continue;
3634 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003635 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003636 allSubtypes.put(currentImiId, tempSubtypesArray);
3637 } else if (NODE_SUBTYPE.equals(nodeName)) {
3638 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3639 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3640 continue;
3641 }
3642 final int icon = Integer.valueOf(
3643 parser.getAttributeValue(null, ATTR_ICON));
3644 final int label = Integer.valueOf(
3645 parser.getAttributeValue(null, ATTR_LABEL));
3646 final String imeSubtypeLocale =
3647 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3648 final String imeSubtypeMode =
3649 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3650 final String imeSubtypeExtraValue =
3651 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003652 final boolean isAuxiliary = "1".equals(String.valueOf(
3653 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003654 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3655 .setSubtypeNameResId(label)
3656 .setSubtypeIconResId(icon)
3657 .setSubtypeLocale(imeSubtypeLocale)
3658 .setSubtypeMode(imeSubtypeMode)
3659 .setSubtypeExtraValue(imeSubtypeExtraValue)
3660 .setIsAuxiliary(isAuxiliary)
3661 .build();
satoke7c6998e2011-06-03 17:57:59 +09003662 tempSubtypesArray.add(subtype);
3663 }
3664 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003665 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3666 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003667 return;
satoke7c6998e2011-06-03 17:57:59 +09003668 }
3669 }
3670 }
3671
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003672 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3673 @NonNull
3674 private final Handler mHandler;
3675
3676 LocalServiceImpl(@NonNull final Handler handler) {
3677 mHandler = handler;
3678 }
3679
3680 @Override
3681 public void setInteractive(boolean interactive) {
3682 // Do everything in handler so as not to block the caller.
3683 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3684 interactive ? 1 : 0, 0));
3685 }
3686 }
3687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 @Override
3689 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3690 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3691 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3694 + Binder.getCallingPid()
3695 + ", uid=" + Binder.getCallingUid());
3696 return;
3697 }
3698
3699 IInputMethod method;
3700 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 synchronized (mMethodMap) {
3705 p.println("Current Input Method Manager state:");
3706 int N = mMethodList.size();
3707 p.println(" Input Methods:");
3708 for (int i=0; i<N; i++) {
3709 InputMethodInfo info = mMethodList.get(i);
3710 p.println(" InputMethod #" + i + ":");
3711 info.dump(p, " ");
3712 }
3713 p.println(" Clients:");
3714 for (ClientState ci : mClients.values()) {
3715 p.println(" Client " + ci + ":");
3716 p.println(" client=" + ci.client);
3717 p.println(" inputContext=" + ci.inputContext);
3718 p.println(" sessionRequested=" + ci.sessionRequested);
3719 p.println(" curSession=" + ci.curSession);
3720 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003721 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003723 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3724 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3726 + " mBoundToMethod=" + mBoundToMethod);
3727 p.println(" mCurToken=" + mCurToken);
3728 p.println(" mCurIntent=" + mCurIntent);
3729 method = mCurMethod;
3730 p.println(" mCurMethod=" + mCurMethod);
3731 p.println(" mEnabledSession=" + mEnabledSession);
3732 p.println(" mShowRequested=" + mShowRequested
3733 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3734 + " mShowForced=" + mShowForced
3735 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003736 p.println(" mCurUserActionNotificationSequenceNumber="
3737 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003738 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003739 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003740 p.println(" mSwitchingController:");
3741 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003743
Jeff Brownb88102f2010-09-08 11:49:43 -07003744 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 pw.flush();
3747 try {
3748 client.client.asBinder().dump(fd, args);
3749 } catch (RemoteException e) {
3750 p.println("Input method client dead: " + e);
3751 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003752 } else {
3753 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003755
Jeff Brownb88102f2010-09-08 11:49:43 -07003756 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 pw.flush();
3759 try {
3760 method.asBinder().dump(fd, args);
3761 } catch (RemoteException e) {
3762 p.println("Input method service dead: " + e);
3763 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003764 } else {
3765 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
3767 }
3768}