blob: a5d536eee825d706508dd74854b7d45f3ce14d79 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090022import com.android.internal.inputmethod.InputMethodUtils;
23import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070025import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090026import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputContext;
28import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070029import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputMethodClient;
31import com.android.internal.view.IInputMethodManager;
32import com.android.internal.view.IInputMethodSession;
33import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090035import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090042import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.AlertDialog;
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;
63import android.content.pm.ResolveInfo;
64import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080065import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070066import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.res.Resources;
68import android.content.res.TypedArray;
69import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080070import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080071import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070072import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040074import android.os.Bundle;
satoke7c6998e2011-06-03 17:57:59 +090075import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Handler;
77import android.os.IBinder;
78import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090079import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Parcel;
83import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080084import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.ServiceManager;
86import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090087import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080088import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.provider.Settings;
90import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070091import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090092import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070093import android.util.ArrayMap;
94import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070095import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090097import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090098import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.PrintWriterPrinter;
100import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900101import android.util.Slog;
102import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900103import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700105import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.view.LayoutInflater;
107import android.view.View;
108import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900110import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.inputmethod.InputBinding;
112import android.view.inputmethod.InputMethod;
113import android.view.inputmethod.InputMethodInfo;
114import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900115import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900116import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900117import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900118import android.widget.CompoundButton;
119import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900121import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
satoke7c6998e2011-06-03 17:57:59 +0900124import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900126import java.io.FileInputStream;
127import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.IOException;
129import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100130import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900132import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700134import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900136import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138/**
139 * This class provides a system service that manages input methods.
140 */
141public class InputMethodManagerService extends IInputMethodManager.Stub
142 implements ServiceConnection, Handler.Callback {
143 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700144 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700145 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 private static final char INPUT_METHOD_SEPARATOR = ':';
148 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
149
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700150 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
151 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
152 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int MSG_UNBIND_INPUT = 1000;
155 static final int MSG_BIND_INPUT = 1010;
156 static final int MSG_SHOW_SOFT_INPUT = 1020;
157 static final int MSG_HIDE_SOFT_INPUT = 1030;
158 static final int MSG_ATTACH_TOKEN = 1040;
159 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 static final int MSG_START_INPUT = 2000;
162 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 static final int MSG_UNBIND_METHOD = 3000;
165 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700166 static final int MSG_SET_ACTIVE = 3020;
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;
185 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700186 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900187 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900188 private final HardKeyboardListener mHardKeyboardListener;
189 private final WindowManagerService mWindowManagerService;
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.
satokd87c2592010-09-29 11:52:06 +0900196 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
197 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900198 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
199 new LruCache<SuggestionSpan, InputMethodInfo>(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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final HashMap<IBinder, ClientState> mClients
277 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700280 * Set once the system is ready to run third party code.
281 */
282 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * Id of the currently selected input method.
286 */
287 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * The current binding sequence number, incremented every time there is
291 * a new bind performed.
292 */
293 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * The client that is currently bound to an input method.
297 */
298 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700301 * The last window token that gained focus.
302 */
303 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * The input context last provided by the current client.
307 */
308 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * The attributes last provided by the current client.
312 */
313 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * The input method ID of the input method service that we are currently
317 * connected to or in the process of connecting to.
318 */
319 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
satokab751aa2010-09-14 19:17:36 +0900322 * The current subtype of the current input method.
323 */
324 private InputMethodSubtype mCurrentSubtype;
325
satok4e4569d2010-11-19 18:45:53 +0900326 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900327 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
328 mShortcutInputMethodsAndSubtypes =
329 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900330
John Spurlocke0980502013-10-25 11:59:29 -0400331 // Was the keyguard locked when this client became current?
332 private boolean mCurClientInKeyguard;
333
satokab751aa2010-09-14 19:17:36 +0900334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 * Set to true if our ServiceConnection is currently actively bound to
336 * a service (whether or not we have gotten its IBinder back yet).
337 */
338 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
341 * Set if the client has asked for the input method to be shown.
342 */
343 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * Set if we were explicitly told to show the input method.
347 */
348 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * Set if we were forced to be shown.
352 */
353 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Set if we last told the input method to show itself.
357 */
358 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * The Intent used to connect to the current input method.
362 */
363 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * The token we have made for the currently active input method, to
367 * identify it in the future.
368 */
369 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * If non-null, this is the input method service we are currently connected
373 * to.
374 */
375 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
378 * Time that we last initiated a bind to the input method, to determine
379 * if we should try to disconnect and reconnect to it.
380 */
381 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
384 * Have we called mCurMethod.bindInput()?
385 */
386 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
389 * Currently enabled session. Only touched by service thread, not
390 * protected by a lock.
391 */
392 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * True if the screen is on. The value is true initially.
396 */
397 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900399 int mCurUserActionNotificationSequenceNumber = 0;
400
Joe Onorato857fd9b2011-01-27 15:08:35 -0800401 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900402
403 /**
404 * A set of status bits regarding the active IME.
405 *
406 * <p>This value is a combination of following two bits:</p>
407 * <dl>
408 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
409 * <dd>
410 * If this bit is ON, connected IME is ready to accept touch/key events.
411 * </dd>
412 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
413 * <dd>
414 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
415 * </dd>
416 * </dl>
417 * <em>Do not update this value outside of setImeWindowStatus.</em>
418 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800419 int mImeWindowVis;
420
Ken Wakasa05dbb652011-08-22 15:22:43 +0900421 private AlertDialog.Builder mDialogBuilder;
422 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900423 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900424 private InputMethodInfo[] mIms;
425 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900426 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700427 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900428 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
429 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700432 int mUserId;
433 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800434 String mLastEnabled = "";
435
Yohei Yukawa81482972015-06-04 00:58:59 -0700436 /**
437 * <em>This constructor must be called within the lock.</em>
438 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 SettingsObserver(Handler handler) {
440 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700441 }
442
443 public void registerContentObserverLocked(int userId) {
444 if (mRegistered && mUserId == userId) {
445 return;
446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700448 if (mRegistered) {
449 mContext.getContentResolver().unregisterContentObserver(this);
450 mRegistered = false;
451 }
452 if (mUserId != userId) {
453 mLastEnabled = "";
454 mUserId = userId;
455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700457 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900458 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700462 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700463 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
464 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
Michael Wright7b5a96b2014-08-09 19:28:42 -0700467 @Override public void onChange(boolean selfChange, Uri uri) {
468 final Uri showImeUri =
469 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700471 if (showImeUri.equals(uri)) {
472 updateKeyboardFromSettingsLocked();
473 } else {
474 boolean enabledChanged = false;
475 String newEnabled = mSettings.getEnabledInputMethodsStr();
476 if (!mLastEnabled.equals(newEnabled)) {
477 mLastEnabled = newEnabled;
478 enabledChanged = true;
479 }
480 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700484
485 @Override
486 public String toString() {
487 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
488 + " mLastEnabled=" + mLastEnabled + "}";
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900492 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
493 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700495 if (mCurClient != null && mCurClient.client != null) {
496 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
497 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900500
501 @Override
502 public void onReceive(Context context, Intent intent) {
503 final String action = intent.getAction();
504 if (Intent.ACTION_SCREEN_ON.equals(action)) {
505 mScreenOn = true;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900506 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900507 updateActive();
508 return;
509 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
510 mScreenOn = false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900511 updateSystemUi(mCurToken, 0 /* vis */, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900512 updateActive();
513 return;
514 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
515 hideInputMethodMenu();
516 // No need to updateActive
517 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800518 } else if (Intent.ACTION_USER_ADDED.equals(action)
519 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100520 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800521 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700522 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
523 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
524 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
525 final String prevValue = intent.getStringExtra(
526 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
527 final String newValue = intent.getStringExtra(
528 Intent.EXTRA_SETTING_NEW_VALUE);
529 restoreEnabledInputMethods(mContext, prevValue, newValue);
530 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900531 } else {
532 Slog.w(TAG, "Unexpected intent " + intent);
533 }
534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700537 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
538 // does not attempt to validate on the fly with any installed device policy, so must only
539 // be run in the context of initial device setup.
540 //
541 // TODO: Move this method to InputMethodUtils with adding unit tests.
542 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
543 if (DEBUG_RESTORE) {
544 Slog.i(TAG, "Restoring enabled input methods:");
545 Slog.i(TAG, "prev=" + prevValue);
546 Slog.i(TAG, " new=" + newValue);
547 }
548 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
549 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
550 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
551
552 // Merge the restored ime+subtype enabled states into the live state
553 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
554 final String imeId = entry.getKey();
555 ArraySet<String> prevSubtypes = prevMap.get(imeId);
556 if (prevSubtypes == null) {
557 prevSubtypes = new ArraySet<String>(2);
558 prevMap.put(imeId, prevSubtypes);
559 }
560 prevSubtypes.addAll(entry.getValue());
561 }
562
563 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
564 if (DEBUG_RESTORE) {
565 Slog.i(TAG, "Merged IME string:");
566 Slog.i(TAG, " " + mergedImesAndSubtypesString);
567 }
568 Settings.Secure.putString(context.getContentResolver(),
569 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
570 }
571
572 // TODO: Move this method to InputMethodUtils with adding unit tests.
573 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
574 // we want to use the canonical InputMethodSettings implementation,
575 // so we convert data structures first.
576 List<Pair<String, ArrayList<String>>> imeMap =
577 new ArrayList<Pair<String, ArrayList<String>>>(4);
578 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
579 final String imeName = entry.getKey();
580 final ArraySet<String> subtypeSet = entry.getValue();
581 final ArrayList<String> subtypes = new ArrayList<String>(2);
582 if (subtypeSet != null) {
583 subtypes.addAll(subtypeSet);
584 }
585 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
586 }
587 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
588 }
589
590 // TODO: Move this method to InputMethodUtils with adding unit tests.
591 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
592 final String inputMethodsAndSubtypesString) {
593 final ArrayMap<String, ArraySet<String>> imeMap =
594 new ArrayMap<String, ArraySet<String>>();
595 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
596 return imeMap;
597 }
598
599 final SimpleStringSplitter typeSplitter =
600 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
601 final SimpleStringSplitter subtypeSplitter =
602 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
603 List<Pair<String, ArrayList<String>>> allImeSettings =
604 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
605 typeSplitter,
606 subtypeSplitter);
607 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
608 ArraySet<String> subtypes = new ArraySet<String>();
609 if (ime.second != null) {
610 subtypes.addAll(ime.second);
611 }
612 imeMap.put(ime.first, subtypes);
613 }
614 return imeMap;
615 }
616
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800617 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900618 private boolean isChangingPackagesOfCurrentUser() {
619 final int userId = getChangingUserId();
620 final boolean retval = userId == mSettings.getCurrentUserId();
621 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900622 if (!retval) {
623 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
624 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900625 }
626 return retval;
627 }
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800630 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900631 if (!isChangingPackagesOfCurrentUser()) {
632 return false;
633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 final int N = mMethodList.size();
637 if (curInputMethodId != null) {
638 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800639 InputMethodInfo imi = mMethodList.get(i);
640 if (imi.getId().equals(curInputMethodId)) {
641 for (String pkg : packages) {
642 if (imi.getPackageName().equals(pkg)) {
643 if (!doit) {
644 return true;
645 }
satok723a27e2010-11-11 14:58:11 +0900646 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 chooseNewDefaultIMELocked();
648 return true;
649 }
650 }
651 }
652 }
653 }
654 }
655 return false;
656 }
657
658 @Override
659 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900660 if (!isChangingPackagesOfCurrentUser()) {
661 return;
662 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 synchronized (mMethodMap) {
664 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800666 final int N = mMethodList.size();
667 if (curInputMethodId != null) {
668 for (int i=0; i<N; i++) {
669 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900670 final String imiId = imi.getId();
671 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 curIm = imi;
673 }
satoke7c6998e2011-06-03 17:57:59 +0900674
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900676 if (isPackageModified(imi.getPackageName())) {
677 mFileManager.deleteAllInputMethodSubtypes(imiId);
678 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 if (change == PACKAGE_TEMPORARY_CHANGE
680 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800681 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 + imi.getComponent());
683 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900688 buildInputMethodListLocked(
689 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800693 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800694 int change = isPackageDisappearing(curIm.getPackageName());
695 if (change == PACKAGE_TEMPORARY_CHANGE
696 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800697 ServiceInfo si = null;
698 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900699 si = mIPackageManager.getServiceInfo(
700 curIm.getComponent(), 0, mSettings.getCurrentUserId());
701 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800702 }
703 if (si == null) {
704 // Uh oh, current input method is no longer around!
705 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800706 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900707 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800709 changed = true;
710 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900712 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800713 }
714 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800715 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800716 }
satokab751aa2010-09-14 19:17:36 +0900717
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800718 if (curIm == null) {
719 // We currently don't have a default input method... is
720 // one now available?
721 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700722 } else if (!changed && isPackageModified(curIm.getPackageName())) {
723 // Even if the current input method is still available, mCurrentSubtype could
724 // be obsolete when the package is modified in practice.
725 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800727
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800729 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731 }
732 }
733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
Jeff Brownc28867a2013-03-26 15:42:39 -0700735 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900736 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700737 private final IInputMethod mMethod;
738 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
Jeff Brownc28867a2013-03-26 15:42:39 -0700740 MethodCallback(InputMethodManagerService imms, IInputMethod method,
741 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900742 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700743 mMethod = method;
744 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
satoke7c6998e2011-06-03 17:57:59 +0900747 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700748 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700749 long ident = Binder.clearCallingIdentity();
750 try {
751 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
752 } finally {
753 Binder.restoreCallingIdentity(ident);
754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800757
satok01038492012-04-09 21:08:27 +0900758 private class HardKeyboardListener
759 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
760 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700761 public void onHardKeyboardStatusChange(boolean available) {
762 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
763 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900764 }
765
Michael Wright7b5a96b2014-08-09 19:28:42 -0700766 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900767 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700768 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900769 }
770 synchronized(mMethodMap) {
771 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
772 && mSwitchingDialog.isShowing()) {
773 mSwitchingDialogTitleView.findViewById(
774 com.android.internal.R.id.hard_keyboard_section).setVisibility(
775 available ? View.VISIBLE : View.GONE);
776 }
777 }
778 }
779 }
780
781 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800784 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700786 // Note: SettingsObserver doesn't register observers in its constructor.
787 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 mIWindowManager = IWindowManager.Stub.asInterface(
789 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800790 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 public void executeMessage(Message msg) {
793 handleMessage(msg);
794 }
Mita Yuned218c72012-12-06 17:18:25 -0800795 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900796 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900797 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900798 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700799 mHasFeature = context.getPackageManager().hasSystemFeature(
800 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900801
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400802 Bundle extras = new Bundle();
803 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
804 mImeSwitcherNotification = new Notification.Builder(mContext)
805 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
806 .setWhen(0)
807 .setOngoing(true)
808 .addExtras(extras)
809 .setCategory(Notification.CATEGORY_SYSTEM)
810 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400811
satok7cfc0ed2011-06-20 21:29:36 +0900812 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900813 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900814
815 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900816
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 final IntentFilter broadcastFilter = new IntentFilter();
818 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
819 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
820 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800821 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
822 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700823 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900824 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
satok7cfc0ed2011-06-20 21:29:36 +0900826 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900827 int userId = 0;
828 try {
829 ActivityManagerNative.getDefault().registerUserSwitchObserver(
830 new IUserSwitchObserver.Stub() {
831 @Override
832 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900833 synchronized(mMethodMap) {
834 switchUserLocked(newUserId);
835 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 if (reply != null) {
837 try {
838 reply.sendResult(null);
839 } catch (RemoteException e) {
840 }
841 }
842 }
843
844 @Override
845 public void onUserSwitchComplete(int newUserId) throws RemoteException {
846 }
Kenny Guy42979622015-04-13 18:03:05 +0000847
848 @Override
849 public void onForegroundProfileSwitch(int newProfileId) {
850 // Ignore.
851 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900852 });
853 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
854 } catch (RemoteException e) {
855 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
856 }
satok81f8b7c2012-12-04 20:42:56 +0900857 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900858
satokd87c2592010-09-29 11:52:06 +0900859 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900860 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900861 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100862 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900863 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900864 synchronized (mMethodMap) {
865 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
866 mSettings, context);
867 }
satok0a1bcf42012-05-16 19:26:31 +0900868
869 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900870 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900871 if (DEBUG) {
872 Slog.d(TAG, "Initial default ime = " + defaultImiId);
873 }
satok0a1bcf42012-05-16 19:26:31 +0900874 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
875
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900876 synchronized (mMethodMap) {
877 buildInputMethodListLocked(mMethodList, mMethodMap,
878 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
879 }
satokd87c2592010-09-29 11:52:06 +0900880 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
satok0a1bcf42012-05-16 19:26:31 +0900882 if (!mImeSelectedOnBoot) {
883 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900884 synchronized (mMethodMap) {
885 resetDefaultImeLocked(context);
886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800888
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900889 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700890 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900891 updateFromSettingsLocked(true);
892 }
satok5b927c432012-05-01 20:09:34 +0900893
894 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
895 // according to the new system locale.
896 final IntentFilter filter = new IntentFilter();
897 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
898 mContext.registerReceiver(
899 new BroadcastReceiver() {
900 @Override
901 public void onReceive(Context context, Intent intent) {
902 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900903 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900904 }
905 }
906 }, filter);
907 }
908
satok5b927c432012-05-01 20:09:34 +0900909 private void resetDefaultImeLocked(Context context) {
910 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900911 if (mCurMethodId != null
912 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900913 return;
914 }
915
916 InputMethodInfo defIm = null;
917 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900918 if (defIm == null && mSystemReady) {
919 final Locale systemLocale = context.getResources().getConfiguration().locale;
920 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
921 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
922 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900923 defIm = imi;
924 Slog.i(TAG, "Selected default: " + imi.getId());
925 }
926 }
927 }
928 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900929 defIm = InputMethodUtils.getMostApplicableDefaultIME(
930 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900931 if (defIm != null) {
932 Slog.i(TAG, "Default found, using " + defIm.getId());
933 } else {
934 Slog.i(TAG, "No default found");
935 }
satok5b927c432012-05-01 20:09:34 +0900936 }
937 if (defIm != null) {
938 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
939 }
940 }
941
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900942 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
943 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 if (!mSystemReady) {
945 // not system ready
946 return;
947 }
948 final Locale newLocale = mRes.getConfiguration().locale;
949 if (!updateOnlyWhenLocaleChanged
950 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
951 if (!updateOnlyWhenLocaleChanged) {
952 hideCurrentInputLocked(0, null);
953 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700954 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
956 if (DEBUG) {
957 Slog.i(TAG, "Locale has been changed to " + newLocale);
958 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900959 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 if (!updateOnlyWhenLocaleChanged) {
961 final String selectedImiId = mSettings.getSelectedInputMethod();
962 if (TextUtils.isEmpty(selectedImiId)) {
963 // This is the first time of the user switch and
964 // set the current ime to the proper one.
965 resetDefaultImeLocked(mContext);
966 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900967 } else {
968 // If the locale is changed, needs to reset the default ime
969 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800971 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900972 mLastSystemLocale = newLocale;
973 if (!updateOnlyWhenLocaleChanged) {
974 try {
975 startInputInnerLocked();
976 } catch (RuntimeException e) {
977 Slog.w(TAG, "Unexpected exception", e);
978 }
979 }
980 }
981 }
982
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900983 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900984 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
985 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 }
987
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900988 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700989 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
990 + " currentUserId=" + mSettings.getCurrentUserId());
991
Yohei Yukawa81482972015-06-04 00:58:59 -0700992 // ContentObserver should be registered again when the user is changed
993 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100995 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900996 // InputMethodFileManager should be reset when the user is changed
997 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900998 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700999
1000 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1001 + " defaultImiId=" + defaultImiId);
1002
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001003 // For secondary users, the list of enabled IMEs may not have been updated since the
1004 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1005 // not be empty even if the IME has been uninstalled by the primary user.
1006 // Even in such cases, IMMS works fine because it will find the most applicable
1007 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001008 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001009 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001010 initialUserSwitch /* needsToResetDefaultIme */);
1011 if (initialUserSwitch) {
1012 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
1013 mSettings.getEnabledInputMethodListLocked());
1014 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001015
1016 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1017 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001018 }
1019
Kenny Guy2a764942014-04-02 13:29:20 +01001020 void updateCurrentProfileIds() {
1021 List<UserInfo> profiles =
1022 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1023 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1024 for (int i = 0; i < currentProfileIds.length; i++) {
1025 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001026 }
Kenny Guy2a764942014-04-02 13:29:20 +01001027 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001028 }
1029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 @Override
1031 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1032 throws RemoteException {
1033 try {
1034 return super.onTransact(code, data, reply, flags);
1035 } catch (RuntimeException e) {
1036 // The input method manager only throws security exceptions, so let's
1037 // log all others.
1038 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001039 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041 throw e;
1042 }
1043 }
1044
Svetoslav Ganova0027152013-06-25 14:59:53 -07001045 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001046 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001047 if (DEBUG) {
1048 Slog.d(TAG, "--- systemReady");
1049 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001050 if (!mSystemReady) {
1051 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 mKeyguardManager =
1053 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001054 mNotificationManager = (NotificationManager)
1055 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1056 mStatusBar = statusBar;
1057 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001058 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001059 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1060 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001061 if (mShowOngoingImeSwitcherForPhones) {
1062 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1063 mHardKeyboardListener);
1064 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001065 buildInputMethodListLocked(mMethodList, mMethodMap,
1066 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001067 if (!mImeSelectedOnBoot) {
1068 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001069 resetStateIfCurrentLocaleChangedLocked();
1070 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1071 mContext.getPackageManager(),
1072 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001073 }
1074 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001075 try {
1076 startInputInnerLocked();
1077 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001079 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001080 }
1081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001084 // ---------------------------------------------------------------------------------------
1085 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1086 // 1) it comes from the system process
1087 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1088 private boolean calledFromValidUser() {
1089 final int uid = Binder.getCallingUid();
1090 final int userId = UserHandle.getUserId(uid);
1091 if (DEBUG) {
1092 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1093 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1094 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001095 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1096 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001097 }
Kenny Guy2a764942014-04-02 13:29:20 +01001098 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001099 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001100 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001101
1102 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1103 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1104 // must not manage background users' states in any functions.
1105 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1106 // by a token.
1107 if (mContext.checkCallingOrSelfPermission(
1108 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1109 == PackageManager.PERMISSION_GRANTED) {
1110 if (DEBUG) {
1111 Slog.d(TAG, "--- Access granted because the calling process has "
1112 + "the INTERACT_ACROSS_USERS_FULL permission");
1113 }
1114 return true;
1115 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001116 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1117 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001118 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 }
1120
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001121
1122 /**
1123 * Returns true iff the caller is identified to be the current input method with the token.
1124 * @param token The window token given to the input method when it was started.
1125 * @return true if and only if non-null valid token is specified.
1126 */
1127 private boolean calledWithValidToken(IBinder token) {
1128 if (token == null || mCurToken != token) {
1129 return false;
1130 }
1131 return true;
1132 }
1133
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 private boolean bindCurrentInputMethodService(
1135 Intent service, ServiceConnection conn, int flags) {
1136 if (service == null || conn == null) {
1137 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1138 return false;
1139 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001140 return mContext.bindServiceAsUser(service, conn, flags,
1141 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 }
1143
satoke7c6998e2011-06-03 17:57:59 +09001144 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001146 // TODO: Make this work even for non-current users?
1147 if (!calledFromValidUser()) {
1148 return Collections.emptyList();
1149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 synchronized (mMethodMap) {
1151 return new ArrayList<InputMethodInfo>(mMethodList);
1152 }
1153 }
1154
satoke7c6998e2011-06-03 17:57:59 +09001155 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001157 // TODO: Make this work even for non-current users?
1158 if (!calledFromValidUser()) {
1159 return Collections.emptyList();
1160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001162 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001166 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001167 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001168 * @return enabled subtypes of the specified imi
1169 */
satoke7c6998e2011-06-03 17:57:59 +09001170 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001171 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001172 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001173 // TODO: Make this work even for non-current users?
1174 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001175 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 }
satok67ddf9c2010-11-17 09:45:54 +09001177 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001178 final InputMethodInfo imi;
1179 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001180 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001181 } else {
1182 imi = mMethodMap.get(imiId);
1183 }
1184 if (imi == null) {
1185 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001186 }
1187 return mSettings.getEnabledInputMethodSubtypeListLocked(
1188 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001189 }
1190 }
1191
satoke7c6998e2011-06-03 17:57:59 +09001192 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public void addClient(IInputMethodClient client,
1194 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001195 if (!calledFromValidUser()) {
1196 return;
1197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 synchronized (mMethodMap) {
1199 mClients.put(client.asBinder(), new ClientState(client,
1200 inputContext, uid, pid));
1201 }
1202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
satoke7c6998e2011-06-03 17:57:59 +09001204 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001206 if (!calledFromValidUser()) {
1207 return;
1208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001210 ClientState cs = mClients.remove(client.asBinder());
1211 if (cs != null) {
1212 clearClientSessionLocked(cs);
1213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 void executeOrSendMessage(IInterface target, Message msg) {
1218 if (target.asBinder() instanceof Binder) {
1219 mCaller.sendMessage(msg);
1220 } else {
1221 handleMessage(msg);
1222 msg.recycle();
1223 }
1224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001226 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 + mCurClient.client.asBinder());
1230 if (mBoundToMethod) {
1231 mBoundToMethod = false;
1232 if (mCurMethod != null) {
1233 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1234 MSG_UNBIND_INPUT, mCurMethod));
1235 }
1236 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001237
1238 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1239 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1241 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1242 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project10592532009-03-18 17:39:46 -07001245 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 private int getImeShowFlags() {
1250 int flags = 0;
1251 if (mShowForced) {
1252 flags |= InputMethod.SHOW_FORCED
1253 | InputMethod.SHOW_EXPLICIT;
1254 } else if (mShowExplicitlyRequested) {
1255 flags |= InputMethod.SHOW_EXPLICIT;
1256 }
1257 return flags;
1258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 private int getAppShowFlags() {
1261 int flags = 0;
1262 if (mShowForced) {
1263 flags |= InputMethodManager.SHOW_FORCED;
1264 } else if (!mShowExplicitlyRequested) {
1265 flags |= InputMethodManager.SHOW_IMPLICIT;
1266 }
1267 return flags;
1268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
Dianne Hackborn7663d802012-02-24 13:08:49 -08001270 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 if (!mBoundToMethod) {
1272 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1273 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1274 mBoundToMethod = true;
1275 }
1276 final SessionState session = mCurClient.curSession;
1277 if (initial) {
1278 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1279 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1280 } else {
1281 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1282 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1283 }
1284 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001286 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001288 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001289 (session.channel != null ? session.channel.dup() : null),
1290 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 // If no method is currently selected, do nothing.
1296 if (mCurMethodId == null) {
1297 return mNoBinding;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 ClientState cs = mClients.get(client.asBinder());
1301 if (cs == null) {
1302 throw new IllegalArgumentException("unknown client "
1303 + client.asBinder());
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 try {
1307 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1308 // Check with the window manager to make sure this client actually
1309 // has a window with focus. If not, reject. This is thread safe
1310 // because if the focus changes some time before or after, the
1311 // next client receiving focus that has any interest in input will
1312 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001313 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1315 return null;
1316 }
1317 } catch (RemoteException e) {
1318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
Dianne Hackborn7663d802012-02-24 13:08:49 -08001320 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1321 }
1322
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001323 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001324 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001325 // If no method is currently selected, do nothing.
1326 if (mCurMethodId == null) {
1327 return mNoBinding;
1328 }
1329
Yohei Yukawad57ba672015-06-08 16:39:46 -07001330 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1331 attribute.packageName)) {
1332 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1333 + " uid=" + cs.uid + " package=" + attribute.packageName);
1334 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001335 }
1336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001338 // Was the keyguard locked when switching over to the new client?
1339 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // If the client is changing, we need to switch over to the new
1341 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001342 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001343 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001344 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345
1346 // If the screen is on, inform the new client it is active
1347 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001348 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1349 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 // Bump up the sequence for this client and attach it.
1354 mCurSeq++;
1355 if (mCurSeq <= 0) mCurSeq = 1;
1356 mCurClient = cs;
1357 mCurInputContext = inputContext;
1358 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 // Check if the input method is changing.
1361 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1362 if (cs.curSession != null) {
1363 // Fast case: if we are already connected to the input method,
1364 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001365 return attachNewInputLocked(
1366 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368 if (mHaveConnection) {
1369 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 // Return to client, and we will get back with it when
1371 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001372 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001373 return new InputBindResult(null, null, mCurId, mCurSeq,
1374 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 } else if (SystemClock.uptimeMillis()
1376 < (mLastBindTime+TIME_TO_RECONNECT)) {
1377 // In this case we have connected to the service, but
1378 // don't yet have its interface. If it hasn't been too
1379 // long since we did the connection, we'll return to
1380 // the client and wait to get the service interface so
1381 // we can report back. If it has been too long, we want
1382 // to fall through so we can try a disconnect/reconnect
1383 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001384 return new InputBindResult(null, null, mCurId, mCurSeq,
1385 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1388 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390 }
1391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001393 return startInputInnerLocked();
1394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001395
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001396 InputBindResult startInputInnerLocked() {
1397 if (mCurMethodId == null) {
1398 return mNoBinding;
1399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001401 if (!mSystemReady) {
1402 // If the system is not yet ready, we shouldn't be running third
1403 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001404 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1405 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1409 if (info == null) {
1410 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001412
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001413 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1416 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001417 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1418 com.android.internal.R.string.input_method_binding_label);
1419 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1420 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001421 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001422 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1423 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 mLastBindTime = SystemClock.uptimeMillis();
1425 mHaveConnection = true;
1426 mCurId = info.getId();
1427 mCurToken = new Binder();
1428 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001429 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 mIWindowManager.addWindowToken(mCurToken,
1431 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1432 } catch (RemoteException e) {
1433 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001434 return new InputBindResult(null, null, mCurId, mCurSeq,
1435 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 } else {
1437 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001438 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 + mCurIntent);
1440 }
1441 return null;
1442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
satoke7c6998e2011-06-03 17:57:59 +09001444 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001446 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001447 if (!calledFromValidUser()) {
1448 return null;
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 synchronized (mMethodMap) {
1451 final long ident = Binder.clearCallingIdentity();
1452 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001453 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 } finally {
1455 Binder.restoreCallingIdentity(ident);
1456 }
1457 }
1458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
satoke7c6998e2011-06-03 17:57:59 +09001460 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 public void finishInput(IInputMethodClient client) {
1462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
satoke7c6998e2011-06-03 17:57:59 +09001464 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 public void onServiceConnected(ComponentName name, IBinder service) {
1466 synchronized (mMethodMap) {
1467 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1468 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001469 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001470 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001471 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001472 return;
1473 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001474 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001475 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1476 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001478 clearClientSessionLocked(mCurClient);
1479 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 }
1482 }
1483 }
1484
Jeff Brownc28867a2013-03-26 15:42:39 -07001485 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1486 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 synchronized (mMethodMap) {
1488 if (mCurMethod != null && method != null
1489 && mCurMethod.asBinder() == method.asBinder()) {
1490 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001491 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001493 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001494 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 if (res.method != null) {
1496 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1497 MSG_BIND_METHOD, mCurClient.client, res));
1498 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001499 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501 }
1502 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001503
1504 // Session abandoned. Close its associated input channel.
1505 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001508 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001509 if (mVisibleBound) {
1510 mContext.unbindService(mVisibleConnection);
1511 mVisibleBound = false;
1512 }
1513
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001514 if (mHaveConnection) {
1515 mContext.unbindService(this);
1516 mHaveConnection = false;
1517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001518
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001519 if (mCurToken != null) {
1520 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001522 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001523 // The current IME is shown. Hence an IME switch (transition) is happening.
1524 mWindowManagerService.saveLastInputMethodWindowForTransition();
1525 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001526 mIWindowManager.removeWindowToken(mCurToken);
1527 } catch (RemoteException e) {
1528 }
1529 mCurToken = null;
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
The Android Open Source Project10592532009-03-18 17:39:46 -07001532 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001533 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001534
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001535 if (reportToClient && mCurClient != null) {
1536 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1537 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1538 }
1539 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001540
1541 void requestClientSessionLocked(ClientState cs) {
1542 if (!cs.sessionRequested) {
1543 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1544 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1545 cs.sessionRequested = true;
1546 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1547 MSG_CREATE_SESSION, mCurMethod, channels[1],
1548 new MethodCallback(this, mCurMethod, channels[0])));
1549 }
1550 }
1551
1552 void clearClientSessionLocked(ClientState cs) {
1553 finishSessionLocked(cs.curSession);
1554 cs.curSession = null;
1555 cs.sessionRequested = false;
1556 }
1557
1558 private void finishSessionLocked(SessionState sessionState) {
1559 if (sessionState != null) {
1560 if (sessionState.session != null) {
1561 try {
1562 sessionState.session.finishSession();
1563 } catch (RemoteException e) {
1564 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001565 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 }
1567 sessionState.session = null;
1568 }
1569 if (sessionState.channel != null) {
1570 sessionState.channel.dispose();
1571 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001572 }
1573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001576 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 if (mCurMethod != null) {
1578 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001579 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001581
Jeff Brownc28867a2013-03-26 15:42:39 -07001582 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001583 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 mCurMethod = null;
1585 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001586 if (mStatusBar != null) {
1587 mStatusBar.setIconVisibility("ime", false);
1588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
satoke7c6998e2011-06-03 17:57:59 +09001591 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 public void onServiceDisconnected(ComponentName name) {
1593 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 + " mCurIntent=" + mCurIntent);
1596 if (mCurMethod != null && mCurIntent != null
1597 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001598 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 // We consider this to be a new bind attempt, since the system
1600 // should now try to restart the service for us.
1601 mLastBindTime = SystemClock.uptimeMillis();
1602 mShowRequested = mInputShown;
1603 mInputShown = false;
1604 if (mCurClient != null) {
1605 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1606 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1607 }
1608 }
1609 }
1610 }
1611
satokf9f01002011-05-19 21:31:50 +09001612 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1614 long ident = Binder.clearCallingIdentity();
1615 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001617 if (!calledWithValidToken(token)) {
1618 final int uid = Binder.getCallingUid();
1619 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1620 + " token:" + token);
1621 return;
1622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001625 if (mStatusBar != null) {
1626 mStatusBar.setIconVisibility("ime", false);
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001629 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001630 CharSequence contentDescription = null;
1631 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001632 // Use PackageManager to load label
1633 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001634 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001635 mIPackageManager.getApplicationInfo(packageName, 0,
1636 mSettings.getCurrentUserId()));
1637 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001638 /* ignore */
1639 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001640 if (mStatusBar != null) {
1641 mStatusBar.setIcon("ime", packageName, iconId, 0,
1642 contentDescription != null
1643 ? contentDescription.toString() : null);
1644 mStatusBar.setIconVisibility("ime", true);
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 }
1648 } finally {
1649 Binder.restoreCallingIdentity(ident);
1650 }
1651 }
1652
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001653 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001654 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001655 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001656 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001657 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001658 if (mWindowManagerService.isHardKeyboardAvailable()) {
1659 // When physical keyboard is attached, we show the ime switcher (or notification if
1660 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1661 // exists in the IME switcher dialog. Might be OK to remove this condition once
1662 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1663 return true;
1664 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001665 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001666
1667 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1668 final int N = imis.size();
1669 if (N > 2) return true;
1670 if (N < 1) return false;
1671 int nonAuxCount = 0;
1672 int auxCount = 0;
1673 InputMethodSubtype nonAuxSubtype = null;
1674 InputMethodSubtype auxSubtype = null;
1675 for(int i = 0; i < N; ++i) {
1676 final InputMethodInfo imi = imis.get(i);
1677 final List<InputMethodSubtype> subtypes =
1678 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1679 final int subtypeCount = subtypes.size();
1680 if (subtypeCount == 0) {
1681 ++nonAuxCount;
1682 } else {
1683 for (int j = 0; j < subtypeCount; ++j) {
1684 final InputMethodSubtype subtype = subtypes.get(j);
1685 if (!subtype.isAuxiliary()) {
1686 ++nonAuxCount;
1687 nonAuxSubtype = subtype;
1688 } else {
1689 ++auxCount;
1690 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001691 }
1692 }
satok7cfc0ed2011-06-20 21:29:36 +09001693 }
1694 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001695 if (nonAuxCount > 1 || auxCount > 1) {
1696 return true;
1697 } else if (nonAuxCount == 1 && auxCount == 1) {
1698 if (nonAuxSubtype != null && auxSubtype != null
1699 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1700 || auxSubtype.overridesImplicitlyEnabledSubtype()
1701 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1702 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1703 return false;
1704 }
1705 return true;
1706 }
1707 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001708 }
1709
John Spurlocke0980502013-10-25 11:59:29 -04001710 private boolean isKeyguardLocked() {
1711 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1712 }
1713
satokdbf29502011-08-25 15:28:23 +09001714 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001715 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001716 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001717 if (!calledWithValidToken(token)) {
1718 final int uid = Binder.getCallingUid();
1719 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1720 + " token:" + token);
1721 return;
1722 }
1723
1724 synchronized (mMethodMap) {
1725 mImeWindowVis = vis;
1726 mBackDisposition = backDisposition;
1727 updateSystemUiLocked(token, vis, backDisposition);
1728 }
1729 }
1730
1731 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1732 synchronized (mMethodMap) {
1733 updateSystemUiLocked(token, vis, backDisposition);
1734 }
1735 }
1736
1737 // Caution! This method is called in this class. Handle multi-user carefully
1738 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1739 if (!calledWithValidToken(token)) {
1740 final int uid = Binder.getCallingUid();
1741 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1742 + " token:" + token);
1743 return;
1744 }
1745
1746 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1747 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001748 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001749 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001750 // apply policy for binder calls
1751 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1752 vis = 0;
satok06487a52010-10-29 11:37:18 +09001753 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001754 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1755 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1756 if (mStatusBar != null) {
1757 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1758 needsToShowImeSwitcher);
1759 }
1760 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1761 if (imi != null && needsToShowImeSwitcher) {
1762 // Used to load label
1763 final CharSequence title = mRes.getText(
1764 com.android.internal.R.string.select_input_method);
1765 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1766 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001767 mImeSwitcherNotification.setContentTitle(title)
1768 .setContentText(summary)
1769 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001770 if ((mNotificationManager != null)
1771 && !mWindowManagerService.hasNavigationBar()) {
1772 if (DEBUG) {
1773 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001774 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001775 mNotificationManager.notifyAsUser(null,
1776 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001777 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001778 mNotificationShown = true;
1779 }
1780 } else {
1781 if (mNotificationShown && mNotificationManager != null) {
1782 if (DEBUG) {
1783 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001784 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001785 mNotificationManager.cancelAsUser(null,
1786 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1787 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001788 }
satok06487a52010-10-29 11:37:18 +09001789 }
1790 } finally {
1791 Binder.restoreCallingIdentity(ident);
1792 }
1793 }
1794
satoke7c6998e2011-06-03 17:57:59 +09001795 @Override
satokf9f01002011-05-19 21:31:50 +09001796 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001797 if (!calledFromValidUser()) {
1798 return;
1799 }
satokf9f01002011-05-19 21:31:50 +09001800 synchronized (mMethodMap) {
1801 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1802 for (int i = 0; i < spans.length; ++i) {
1803 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001804 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001805 mSecureSuggestionSpans.put(ss, currentImi);
1806 }
1807 }
1808 }
1809 }
1810
satoke7c6998e2011-06-03 17:57:59 +09001811 @Override
satokf9f01002011-05-19 21:31:50 +09001812 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001813 if (!calledFromValidUser()) {
1814 return false;
1815 }
satokf9f01002011-05-19 21:31:50 +09001816 synchronized (mMethodMap) {
1817 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1818 // TODO: Do not send the intent if the process of the targetImi is already dead.
1819 if (targetImi != null) {
1820 final String[] suggestions = span.getSuggestions();
1821 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001822 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001823 final Intent intent = new Intent();
1824 // Ensures that only a class in the original IME package will receive the
1825 // notification.
satok42c5a162011-05-26 16:46:14 +09001826 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001827 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1828 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1829 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1830 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001831 final long ident = Binder.clearCallingIdentity();
1832 try {
1833 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1834 } finally {
1835 Binder.restoreCallingIdentity(ident);
1836 }
satokf9f01002011-05-19 21:31:50 +09001837 return true;
1838 }
1839 }
1840 return false;
1841 }
1842
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001843 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001844 updateInputMethodsFromSettingsLocked(enabledMayChange);
1845 updateKeyboardFromSettingsLocked();
1846 }
1847
1848 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001849 if (enabledMayChange) {
1850 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1851 for (int i=0; i<enabled.size(); i++) {
1852 // We allow the user to select "disabled until used" apps, so if they
1853 // are enabling one of those here we now need to make it enabled.
1854 InputMethodInfo imm = enabled.get(i);
1855 try {
1856 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1857 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1858 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001859 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001860 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001861 if (DEBUG) {
1862 Slog.d(TAG, "Update state(" + imm.getId()
1863 + "): DISABLED_UNTIL_USED -> DEFAULT");
1864 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001865 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1866 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001867 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1868 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001869 }
1870 } catch (RemoteException e) {
1871 }
1872 }
1873 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001874 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1875 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1876 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1877 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001878 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001879 // There is no input method selected, try to choose new applicable input method.
1880 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001881 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001882 }
1883 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001885 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001887 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001888 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001889 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
satokf3db1af2010-11-23 13:34:33 +09001891 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001892 } else {
1893 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001894 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001895 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001897 // Here is not the perfect place to reset the switching controller. Ideally
1898 // mSwitchingController and mSettings should be able to share the same state.
1899 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1900 // the same enabled IMEs list.
1901 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001902
1903 }
1904
1905 public void updateKeyboardFromSettingsLocked() {
1906 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1907 if (mSwitchingDialog != null
1908 && mSwitchingDialogTitleView != null
1909 && mSwitchingDialog.isShowing()) {
1910 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1911 com.android.internal.R.id.hard_keyboard_switch);
1912 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
satokab751aa2010-09-14 19:17:36 +09001916 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 InputMethodInfo info = mMethodMap.get(id);
1918 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001919 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001921
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001922 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001923 // We have already made sure that the package name belongs to the application's UID.
1924 // No further UID check is required.
1925 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1926 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001927 }
1928 }
1929
satokd81e9502012-05-21 12:58:45 +09001930 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001932 final int subtypeCount = info.getSubtypeCount();
1933 if (subtypeCount <= 0) {
1934 return;
satokcd7cd292010-11-20 15:46:23 +09001935 }
satokd81e9502012-05-21 12:58:45 +09001936 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1937 final InputMethodSubtype newSubtype;
1938 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1939 newSubtype = info.getSubtypeAt(subtypeId);
1940 } else {
1941 // If subtype is null, try to find the most applicable one from
1942 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001943 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001944 }
1945 if (newSubtype == null || oldSubtype == null) {
1946 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1947 + ", new subtype = " + newSubtype);
1948 return;
1949 }
1950 if (newSubtype != oldSubtype) {
1951 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1952 if (mCurMethod != null) {
1953 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001954 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001955 mCurMethod.changeInputMethodSubtype(newSubtype);
1956 } catch (RemoteException e) {
1957 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001958 }
1959 }
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 return;
1962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001963
satokd81e9502012-05-21 12:58:45 +09001964 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 final long ident = Binder.clearCallingIdentity();
1966 try {
satokab751aa2010-09-14 19:17:36 +09001967 // Set a subtype to this input method.
1968 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001969 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1970 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1971 // because mCurMethodId is stored as a history in
1972 // setSelectedInputMethodAndSubtypeLocked().
1973 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974
1975 if (ActivityManagerNative.isSystemReady()) {
1976 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001977 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001979 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001981 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 } finally {
1983 Binder.restoreCallingIdentity(ident);
1984 }
1985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001986
satok42c5a162011-05-26 16:46:14 +09001987 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 public boolean showSoftInput(IInputMethodClient client, int flags,
1989 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001990 if (!calledFromValidUser()) {
1991 return false;
1992 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001993 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 long ident = Binder.clearCallingIdentity();
1995 try {
1996 synchronized (mMethodMap) {
1997 if (mCurClient == null || client == null
1998 || mCurClient.client.asBinder() != client.asBinder()) {
1999 try {
2000 // We need to check if this is the current client with
2001 // focus in the window manager, to allow this call to
2002 // be made before input is started in it.
2003 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002004 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002005 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
2007 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002008 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002011
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002012 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002013 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
2015 } finally {
2016 Binder.restoreCallingIdentity(ident);
2017 }
2018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
The Android Open Source Project4df24232009-03-05 14:34:35 -08002020 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 mShowRequested = true;
2022 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2023 mShowExplicitlyRequested = true;
2024 }
2025 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2026 mShowExplicitlyRequested = true;
2027 mShowForced = true;
2028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
Dianne Hackborncc278702009-09-02 23:07:23 -07002030 if (!mSystemReady) {
2031 return false;
2032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002033
The Android Open Source Project4df24232009-03-05 14:34:35 -08002034 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002036 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002037 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2038 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2039 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002041 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002042 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002043 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002044 | Context.BIND_TREAT_LIKE_ACTIVITY
2045 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002046 mVisibleBound = true;
2047 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002048 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002050 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 // The client has asked to have the input method shown, but
2052 // we have been sitting here too long with a connection to the
2053 // service and no interface received, so let's disconnect/connect
2054 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002057 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002059 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002060 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002061 } else {
2062 if (DEBUG) {
2063 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2064 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002070
satok42c5a162011-05-26 16:46:14 +09002071 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002072 public boolean hideSoftInput(IInputMethodClient client, int flags,
2073 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002074 if (!calledFromValidUser()) {
2075 return false;
2076 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002077 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 long ident = Binder.clearCallingIdentity();
2079 try {
2080 synchronized (mMethodMap) {
2081 if (mCurClient == null || client == null
2082 || mCurClient.client.asBinder() != client.asBinder()) {
2083 try {
2084 // We need to check if this is the current client with
2085 // focus in the window manager, to allow this call to
2086 // be made before input is started in it.
2087 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002088 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2089 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002096
Joe Onorato8a9b2202010-02-26 18:56:32 -08002097 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002098 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100 } finally {
2101 Binder.restoreCallingIdentity(ident);
2102 }
2103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002104
The Android Open Source Project4df24232009-03-05 14:34:35 -08002105 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2107 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002108 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 -08002109 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002112 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 -08002113 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002115 boolean res;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002116 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2118 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2119 res = true;
2120 } else {
2121 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002123 if (mHaveConnection && mVisibleBound) {
2124 mContext.unbindService(mVisibleConnection);
2125 mVisibleBound = false;
2126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 mInputShown = false;
2128 mShowRequested = false;
2129 mShowExplicitlyRequested = false;
2130 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
satok42c5a162011-05-26 16:46:14 +09002134 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002135 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2136 int controlFlags, int softInputMode, int windowFlags,
2137 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002138 // Needs to check the validity before clearing calling identity
2139 final boolean calledFromValidUser = calledFromValidUser();
2140
Dianne Hackborn7663d802012-02-24 13:08:49 -08002141 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 long ident = Binder.clearCallingIdentity();
2143 try {
2144 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002146 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002148 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149
Dianne Hackborn7663d802012-02-24 13:08:49 -08002150 ClientState cs = mClients.get(client.asBinder());
2151 if (cs == null) {
2152 throw new IllegalArgumentException("unknown client "
2153 + client.asBinder());
2154 }
2155
2156 try {
2157 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2158 // Check with the window manager to make sure this client actually
2159 // has a window with focus. If not, reject. This is thread safe
2160 // because if the focus changes some time before or after, the
2161 // next client receiving focus that has any interest in input will
2162 // be calling through here after that change happens.
2163 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2164 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2165 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002167 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002169
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002170 if (!calledFromValidUser) {
2171 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2172 Slog.w(TAG, "If you want to interect with IME, you need "
2173 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2174 hideCurrentInputLocked(0, null);
2175 return null;
2176 }
2177
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002178 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002179 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002180 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002181 if (attribute != null) {
2182 return startInputUncheckedLocked(cs, inputContext, attribute,
2183 controlFlags);
2184 }
2185 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002186 }
2187 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002189 // Should we auto-show the IME even if the caller has not
2190 // specified what should be done with it?
2191 // We only do this automatically if the window can resize
2192 // to accommodate the IME (so what the user sees will give
2193 // them good context without input information being obscured
2194 // by the IME) or if running on a large screen where there
2195 // is more room for the target window + IME.
2196 final boolean doAutoShow =
2197 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2198 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2199 || mRes.getConfiguration().isLayoutSizeAtLeast(
2200 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002201 final boolean isTextEditor =
2202 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2203
2204 // We want to start input before showing the IME, but after closing
2205 // it. We want to do this after closing it to help the IME disappear
2206 // more quickly (not get stuck behind it initializing itself for the
2207 // new focused input, even if its window wants to hide the IME).
2208 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2211 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002212 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2214 // There is no focus view, and this window will
2215 // be behind any soft input window, so hide the
2216 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002217 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002220 } else if (isTextEditor && doAutoShow && (softInputMode &
2221 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 // There is a focus view, and we are navigating forward
2223 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002224 // We only do this automatically if the window can resize
2225 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002226 // them good context without input information being obscured
2227 // by the IME) or if running on a large screen where there
2228 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002229 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002230 if (attribute != null) {
2231 res = startInputUncheckedLocked(cs, inputContext, attribute,
2232 controlFlags);
2233 didStart = true;
2234 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
2237 break;
2238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2239 // Do nothing.
2240 break;
2241 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2242 if ((softInputMode &
2243 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002244 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002245 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
2247 break;
2248 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002249 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002250 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 break;
2252 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2253 if ((softInputMode &
2254 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002255 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002256 if (attribute != null) {
2257 res = startInputUncheckedLocked(cs, inputContext, attribute,
2258 controlFlags);
2259 didStart = true;
2260 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263 break;
2264 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002265 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 if (attribute != null) {
2267 res = startInputUncheckedLocked(cs, inputContext, attribute,
2268 controlFlags);
2269 didStart = true;
2270 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002271 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 break;
2273 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002274
2275 if (!didStart && attribute != null) {
2276 res = startInputUncheckedLocked(cs, inputContext, attribute,
2277 controlFlags);
2278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280 } finally {
2281 Binder.restoreCallingIdentity(ident);
2282 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283
2284 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
satok42c5a162011-05-26 16:46:14 +09002287 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002288 public void showInputMethodPickerFromClient(
2289 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002290 if (!calledFromValidUser()) {
2291 return;
2292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 synchronized (mMethodMap) {
2294 if (mCurClient == null || client == null
2295 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002296 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002297 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
2299
satok440aab52010-11-25 09:43:11 +09002300 // Always call subtype picker, because subtype picker is a superset of input method
2301 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002302 mHandler.sendMessage(mCaller.obtainMessageI(
2303 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002304 }
2305 }
2306
satok42c5a162011-05-26 16:46:14 +09002307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002309 if (!calledFromValidUser()) {
2310 return;
2311 }
satok28203512010-11-24 11:06:49 +09002312 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2313 }
2314
satok42c5a162011-05-26 16:46:14 +09002315 @Override
satok28203512010-11-24 11:06:49 +09002316 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 if (!calledFromValidUser()) {
2318 return;
2319 }
satok28203512010-11-24 11:06:49 +09002320 synchronized (mMethodMap) {
2321 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002322 setInputMethodWithSubtypeIdLocked(token, id,
2323 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2324 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002325 } else {
2326 setInputMethod(token, id);
2327 }
2328 }
satokab751aa2010-09-14 19:17:36 +09002329 }
2330
satok42c5a162011-05-26 16:46:14 +09002331 @Override
satokb416a712010-11-25 20:42:14 +09002332 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002333 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002334 if (!calledFromValidUser()) {
2335 return;
2336 }
satokb416a712010-11-25 20:42:14 +09002337 synchronized (mMethodMap) {
2338 if (mCurClient == null || client == null
2339 || mCurClient.client.asBinder() != client.asBinder()) {
2340 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2341 }
satok7fee71f2010-12-17 18:54:26 +09002342 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2343 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002344 }
2345 }
2346
satok4fc87d62011-05-20 16:13:43 +09002347 @Override
satok735cf382010-11-11 20:40:09 +09002348 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002349 if (!calledFromValidUser()) {
2350 return false;
2351 }
satok735cf382010-11-11 20:40:09 +09002352 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002353 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002354 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002355 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002356 lastImi = mMethodMap.get(lastIme.first);
2357 } else {
2358 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002359 }
satok4fc87d62011-05-20 16:13:43 +09002360 String targetLastImiId = null;
2361 int subtypeId = NOT_A_SUBTYPE_ID;
2362 if (lastIme != null && lastImi != null) {
2363 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2364 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2365 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2366 : mCurrentSubtype.hashCode();
2367 // If the last IME is the same as the current IME and the last subtype is not
2368 // defined, there is no need to switch to the last IME.
2369 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2370 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002371 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002372 }
2373 }
2374
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002375 if (TextUtils.isEmpty(targetLastImiId)
2376 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002377 // This is a safety net. If the currentSubtype can't be added to the history
2378 // and the framework couldn't find the last ime, we will make the last ime be
2379 // the most applicable enabled keyboard subtype of the system imes.
2380 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2381 if (enabled != null) {
2382 final int N = enabled.size();
2383 final String locale = mCurrentSubtype == null
2384 ? mRes.getConfiguration().locale.toString()
2385 : mCurrentSubtype.getLocale();
2386 for (int i = 0; i < N; ++i) {
2387 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002388 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002389 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002390 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2391 InputMethodUtils.getSubtypes(imi),
2392 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002393 if (keyboardSubtype != null) {
2394 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002395 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002396 imi, keyboardSubtype.hashCode());
2397 if(keyboardSubtype.getLocale().equals(locale)) {
2398 break;
2399 }
2400 }
2401 }
2402 }
2403 }
2404 }
2405
2406 if (!TextUtils.isEmpty(targetLastImiId)) {
2407 if (DEBUG) {
2408 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2409 + ", from: " + mCurMethodId + ", " + subtypeId);
2410 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002411 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002412 return true;
2413 } else {
2414 return false;
2415 }
satok735cf382010-11-11 20:40:09 +09002416 }
2417 }
2418
satoke7c6998e2011-06-03 17:57:59 +09002419 @Override
satok688bd472012-02-09 20:09:17 +09002420 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 if (!calledFromValidUser()) {
2422 return false;
2423 }
satok688bd472012-02-09 20:09:17 +09002424 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002425 if (!calledWithValidToken(token)) {
2426 final int uid = Binder.getCallingUid();
2427 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2428 + " token:" + token);
2429 return false;
2430 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002431 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002432 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2433 if (nextSubtype == null) {
2434 return false;
2435 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002436 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2437 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002438 return true;
2439 }
2440 }
2441
2442 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002443 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2444 if (!calledFromValidUser()) {
2445 return false;
2446 }
2447 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002448 if (!calledWithValidToken(token)) {
2449 final int uid = Binder.getCallingUid();
2450 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2451 + "token. uid:" + uid + " token:" + token);
2452 return false;
2453 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002454 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002455 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2456 if (nextSubtype == null) {
2457 return false;
2458 }
2459 return true;
2460 }
2461 }
2462
2463 @Override
satok68f1b782011-04-11 14:26:04 +09002464 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002465 if (!calledFromValidUser()) {
2466 return null;
2467 }
satok68f1b782011-04-11 14:26:04 +09002468 synchronized (mMethodMap) {
2469 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2470 // TODO: Handle the case of the last IME with no subtypes
2471 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2472 || TextUtils.isEmpty(lastIme.second)) return null;
2473 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2474 if (lastImi == null) return null;
2475 try {
2476 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002477 final int lastSubtypeId =
2478 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002479 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2480 return null;
2481 }
2482 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002483 } catch (NumberFormatException e) {
2484 return null;
2485 }
2486 }
2487 }
2488
satoke7c6998e2011-06-03 17:57:59 +09002489 @Override
satokee5e77c2011-09-02 18:50:15 +09002490 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002491 if (!calledFromValidUser()) {
2492 return;
2493 }
satok91e88122011-07-18 11:11:42 +09002494 // By this IPC call, only a process which shares the same uid with the IME can add
2495 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002496 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002497 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002498 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002499 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002500 final String[] packageInfos;
2501 try {
2502 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2503 } catch (RemoteException e) {
2504 Slog.e(TAG, "Failed to get package infos");
2505 return;
2506 }
satok91e88122011-07-18 11:11:42 +09002507 if (packageInfos != null) {
2508 final int packageNum = packageInfos.length;
2509 for (int i = 0; i < packageNum; ++i) {
2510 if (packageInfos[i].equals(imi.getPackageName())) {
2511 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002512 final long ident = Binder.clearCallingIdentity();
2513 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002514 buildInputMethodListLocked(mMethodList, mMethodMap,
2515 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002516 } finally {
2517 Binder.restoreCallingIdentity(ident);
2518 }
satokee5e77c2011-09-02 18:50:15 +09002519 return;
satok91e88122011-07-18 11:11:42 +09002520 }
2521 }
2522 }
satoke7c6998e2011-06-03 17:57:59 +09002523 }
satokee5e77c2011-09-02 18:50:15 +09002524 return;
satoke7c6998e2011-06-03 17:57:59 +09002525 }
2526
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002527 @Override
2528 public int getInputMethodWindowVisibleHeight() {
2529 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2530 }
2531
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002532 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002533 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002534 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002535 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002536 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002537 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002538 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2539 if (DEBUG) {
2540 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2541 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2542 + " actual: " + sequenceNumber);
2543 }
2544 return;
2545 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002546 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2547 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002548 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002549 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002550 }
2551 }
2552
satok28203512010-11-24 11:06:49 +09002553 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002555 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2556 }
2557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002559 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2560 if (token == null) {
2561 if (mContext.checkCallingOrSelfPermission(
2562 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2563 != PackageManager.PERMISSION_GRANTED) {
2564 throw new SecurityException(
2565 "Using null token requires permission "
2566 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002568 } else if (mCurToken != token) {
2569 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2570 + " token: " + token);
2571 return;
2572 }
2573
2574 final long ident = Binder.clearCallingIdentity();
2575 try {
2576 setInputMethodLocked(id, subtypeId);
2577 } finally {
2578 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580 }
2581
satok42c5a162011-05-26 16:46:14 +09002582 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002584 if (!calledFromValidUser()) {
2585 return;
2586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002588 if (!calledWithValidToken(token)) {
2589 final int uid = Binder.getCallingUid();
2590 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2591 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 return;
2593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 long ident = Binder.clearCallingIdentity();
2595 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002596 hideCurrentInputLocked(flags, null);
2597 } finally {
2598 Binder.restoreCallingIdentity(ident);
2599 }
2600 }
2601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002602
satok42c5a162011-05-26 16:46:14 +09002603 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002604 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002605 if (!calledFromValidUser()) {
2606 return;
2607 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002608 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002609 if (!calledWithValidToken(token)) {
2610 final int uid = Binder.getCallingUid();
2611 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2612 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002613 return;
2614 }
2615 long ident = Binder.clearCallingIdentity();
2616 try {
2617 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 } finally {
2619 Binder.restoreCallingIdentity(ident);
2620 }
2621 }
2622 }
2623
2624 void setEnabledSessionInMainThread(SessionState session) {
2625 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002626 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002628 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002629 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 } catch (RemoteException e) {
2631 }
2632 }
2633 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002634 if (mEnabledSession != null && mEnabledSession.session != null) {
2635 try {
2636 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2637 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2638 } catch (RemoteException e) {
2639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 }
2641 }
2642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002643
satok42c5a162011-05-26 16:46:14 +09002644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002646 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002648 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002649 final boolean showAuxSubtypes;
2650 switch (msg.arg1) {
2651 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2652 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2653 // implemented so that auxiliary subtypes will be excluded when the soft
2654 // keyboard is invisible.
2655 showAuxSubtypes = mInputShown;
2656 break;
2657 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2658 showAuxSubtypes = true;
2659 break;
2660 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2661 showAuxSubtypes = false;
2662 break;
2663 default:
2664 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2665 return false;
2666 }
2667 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002668 return true;
2669
satok47a44912010-10-06 16:03:58 +09002670 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002671 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002672 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 args.recycle();
satok217f5482010-12-15 05:19:19 +09002674 return true;
2675
2676 case MSG_SHOW_IM_CONFIG:
2677 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002678 return true;
2679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 case MSG_UNBIND_INPUT:
2683 try {
2684 ((IInputMethod)msg.obj).unbindInput();
2685 } catch (RemoteException e) {
2686 // There is nothing interesting about the method dying.
2687 }
2688 return true;
2689 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002690 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 try {
2692 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2693 } catch (RemoteException e) {
2694 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002695 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 return true;
2697 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002698 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002700 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002701 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002702 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 } catch (RemoteException e) {
2704 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002705 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 return true;
2707 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002708 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002710 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002711 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002712 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 } catch (RemoteException e) {
2714 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002715 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 return true;
2717 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002718 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002720 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2722 } catch (RemoteException e) {
2723 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002726 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002727 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002728 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002729 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002731 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002733 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002734 // Dispose the channel if the input method is not local to this process
2735 // because the remote proxy will get its own copy when unparceled.
2736 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002737 channel.dispose();
2738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002740 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002746 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 try {
2748 SessionState session = (SessionState)args.arg1;
2749 setEnabledSessionInMainThread(session);
2750 session.method.startInput((IInputContext)args.arg2,
2751 (EditorInfo)args.arg3);
2752 } catch (RemoteException e) {
2753 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002754 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 return true;
2756 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 try {
2759 SessionState session = (SessionState)args.arg1;
2760 setEnabledSessionInMainThread(session);
2761 session.method.restartInput((IInputContext)args.arg2,
2762 (EditorInfo)args.arg3);
2763 } catch (RemoteException e) {
2764 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002765 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 case MSG_UNBIND_METHOD:
2771 try {
2772 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2773 } catch (RemoteException e) {
2774 // There is nothing interesting about the last client dying.
2775 }
2776 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002777 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002778 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002779 IInputMethodClient client = (IInputMethodClient)args.arg1;
2780 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002782 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002784 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002785 } finally {
2786 // Dispose the channel if the input method is not local to this process
2787 // because the remote proxy will get its own copy when unparceled.
2788 if (res.channel != null && Binder.isProxy(client)) {
2789 res.channel.dispose();
2790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002792 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002794 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002795 case MSG_SET_ACTIVE:
2796 try {
2797 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2798 } catch (RemoteException e) {
2799 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2800 + ((ClientState)msg.obj).pid + " uid "
2801 + ((ClientState)msg.obj).uid);
2802 }
2803 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002804 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2805 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002806 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002807 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002808 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002809 } catch (RemoteException e) {
2810 Slog.w(TAG, "Got RemoteException sending "
2811 + "setUserActionNotificationSequenceNumber("
2812 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002813 + clientState.pid + " uid "
2814 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002815 }
2816 return true;
2817 }
satok01038492012-04-09 21:08:27 +09002818
2819 // --------------------------------------------------------------
2820 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002821 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002822 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 return false;
2825 }
2826
satokdc9ddae2011-10-06 12:22:36 +09002827 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002828 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2829 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002830 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002831 if (DEBUG) {
2832 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2833 }
satok723a27e2010-11-11 14:58:11 +09002834 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002835 return true;
2836 }
2837
2838 return false;
2839 }
2840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002842 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002843 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002844 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002845 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 list.clear();
2848 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002849
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002850 // Use for queryIntentServicesAsUser
2851 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002852 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002853 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002855 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002857 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2858 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002859
satoke7c6998e2011-06-03 17:57:59 +09002860 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2861 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 for (int i = 0; i < services.size(); ++i) {
2863 ResolveInfo ri = services.get(i);
2864 ServiceInfo si = ri.serviceInfo;
2865 ComponentName compName = new ComponentName(si.packageName, si.name);
2866 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2867 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002868 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 + ": it does not require the permission "
2870 + android.Manifest.permission.BIND_INPUT_METHOD);
2871 continue;
2872 }
2873
Joe Onorato8a9b2202010-02-26 18:56:32 -08002874 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
2876 try {
satoke7c6998e2011-06-03 17:57:59 +09002877 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002879 final String id = p.getId();
2880 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
2882 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002883 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002887 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002889 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 }
2891 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002892
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002893 if (resetDefaultEnabledIme) {
2894 final ArrayList<InputMethodInfo> defaultEnabledIme =
2895 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2896 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2897 final InputMethodInfo imi = defaultEnabledIme.get(i);
2898 if (DEBUG) {
2899 Slog.d(TAG, "--- enable ime = " + imi);
2900 }
2901 setInputMethodEnabledLocked(imi.getId(), true);
2902 }
2903 }
2904
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002905 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002906 if (!TextUtils.isEmpty(defaultImiId)) {
2907 if (!map.containsKey(defaultImiId)) {
2908 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2909 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002910 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002911 }
2912 } else {
2913 // Double check that the default IME is certainly enabled.
2914 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002915 }
2916 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002917 // Here is not the perfect place to reset the switching controller. Ideally
2918 // mSwitchingController and mSettings should be able to share the same state.
2919 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2920 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002921 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002925
satok217f5482010-12-15 05:19:19 +09002926 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002927 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002928 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002929 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2930 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002931 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002932 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002933 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002934 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002935 }
2936
2937 private void showConfigureInputMethods() {
2938 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2939 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2940 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2941 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002942 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002943 }
2944
satok2c93efc2012-04-02 19:33:47 +09002945 private boolean isScreenLocked() {
2946 return mKeyguardManager != null
2947 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2948 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002949
Seigo Nonaka14e13912015-05-06 21:04:13 -07002950 private void showInputMethodMenu(boolean showAuxSubtypes) {
2951 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002954 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002955
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002956 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002957 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002958 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002960 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002961 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002962 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2963 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002964 if (immis == null || immis.size() == 0) {
2965 return;
2966 }
2967
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002968 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969
satok688bd472012-02-09 20:09:17 +09002970 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002971 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002972 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002973
satokc3690562012-01-10 20:14:43 +09002974 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002975 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002976 if (currentSubtype != null) {
2977 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002978 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2979 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002980 }
2981 }
2982
Ken Wakasa761eb372011-03-04 19:06:18 +09002983 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002984 mIms = new InputMethodInfo[N];
2985 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002986 int checkedItem = 0;
2987 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002988 final ImeSubtypeListItem item = imList.get(i);
2989 mIms[i] = item.mImi;
2990 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002991 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002992 int subtypeId = mSubtypeIds[i];
2993 if ((subtypeId == NOT_A_SUBTYPE_ID)
2994 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2995 || (subtypeId == lastInputMethodSubtypeId)) {
2996 checkedItem = i;
2997 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003000
3001 final Context settingsContext = new ContextThemeWrapper(context,
3002 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3003
3004 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003005 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3006 @Override
3007 public void onCancel(DialogInterface dialog) {
3008 hideInputMethodMenu();
3009 }
3010 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003011
3012 final Context dialogContext = mDialogBuilder.getContext();
3013 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3014 com.android.internal.R.styleable.DialogPreference,
3015 com.android.internal.R.attr.alertDialogStyle, 0);
3016 final Drawable dialogIcon = a.getDrawable(
3017 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3018 a.recycle();
3019
3020 mDialogBuilder.setIcon(dialogIcon);
3021
3022 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3023 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003024 final View tv = inflater.inflate(
3025 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3026 mDialogBuilder.setCustomTitle(tv);
3027
3028 // Setup layout for a toggle switch of the hardware keyboard
3029 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003030 mSwitchingDialogTitleView
3031 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3032 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3033 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003034 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003035 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003036 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003037 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3038 @Override
3039 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003040 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003041 // Ensure that the input method dialog is dismissed when changing
3042 // the hardware keyboard state.
3043 hideInputMethodMenu();
3044 }
3045 });
3046
Alan Viverette505e3ab2014-11-24 15:22:11 -08003047 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003048 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3049 final OnClickListener choiceListener = new OnClickListener() {
3050 @Override
3051 public void onClick(final DialogInterface dialog, final int which) {
3052 synchronized (mMethodMap) {
3053 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3054 || mSubtypeIds.length <= which) {
3055 return;
satok01038492012-04-09 21:08:27 +09003056 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003057 final InputMethodInfo im = mIms[which];
3058 int subtypeId = mSubtypeIds[which];
3059 adapter.mCheckedItem = which;
3060 adapter.notifyDataSetChanged();
3061 hideInputMethodMenu();
3062 if (im != null) {
3063 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3064 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003065 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003066 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003068 }
3069 }
3070 };
3071 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003073 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003074 final OnClickListener positiveListener = new OnClickListener() {
3075 @Override
3076 public void onClick(DialogInterface dialog, int whichButton) {
3077 showConfigureInputMethods();
3078 }
3079 };
satok82beadf2010-12-27 19:03:06 +09003080 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003081 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003084 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 mSwitchingDialog.getWindow().setType(
3086 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003087 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3088 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003089 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003090 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 mSwitchingDialog.show();
3092 }
3093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003094
Ken Wakasa05dbb652011-08-22 15:22:43 +09003095 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3096 private final LayoutInflater mInflater;
3097 private final int mTextViewResourceId;
3098 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003099 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003100 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3101 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3102 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003103
Ken Wakasa05dbb652011-08-22 15:22:43 +09003104 mTextViewResourceId = textViewResourceId;
3105 mItemsList = itemsList;
3106 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003107 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003108 }
3109
3110 @Override
3111 public View getView(int position, View convertView, ViewGroup parent) {
3112 final View view = convertView != null ? convertView
3113 : mInflater.inflate(mTextViewResourceId, null);
3114 if (position < 0 || position >= mItemsList.size()) return view;
3115 final ImeSubtypeListItem item = mItemsList.get(position);
3116 final CharSequence imeName = item.mImeName;
3117 final CharSequence subtypeName = item.mSubtypeName;
3118 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3119 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3120 if (TextUtils.isEmpty(subtypeName)) {
3121 firstTextView.setText(imeName);
3122 secondTextView.setVisibility(View.GONE);
3123 } else {
3124 firstTextView.setText(subtypeName);
3125 secondTextView.setText(imeName);
3126 secondTextView.setVisibility(View.VISIBLE);
3127 }
3128 final RadioButton radioButton =
3129 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3130 radioButton.setChecked(position == mCheckedItem);
3131 return view;
3132 }
3133 }
3134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003136 synchronized (mMethodMap) {
3137 hideInputMethodMenuLocked();
3138 }
3139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003140
The Android Open Source Project10592532009-03-18 17:39:46 -07003141 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003142 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143
The Android Open Source Project10592532009-03-18 17:39:46 -07003144 if (mSwitchingDialog != null) {
3145 mSwitchingDialog.dismiss();
3146 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003148
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003149 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003150 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003151 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003155
satok42c5a162011-05-26 16:46:14 +09003156 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003158 // TODO: Make this work even for non-current users?
3159 if (!calledFromValidUser()) {
3160 return false;
3161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 synchronized (mMethodMap) {
3163 if (mContext.checkCallingOrSelfPermission(
3164 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3165 != PackageManager.PERMISSION_GRANTED) {
3166 throw new SecurityException(
3167 "Requires permission "
3168 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3169 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 long ident = Binder.clearCallingIdentity();
3172 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003173 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 } finally {
3175 Binder.restoreCallingIdentity(ident);
3176 }
3177 }
3178 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003179
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003180 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3181 // Make sure this is a valid input method.
3182 InputMethodInfo imm = mMethodMap.get(id);
3183 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003184 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003185 }
3186
satokd87c2592010-09-29 11:52:06 +09003187 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3188 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003189
satokd87c2592010-09-29 11:52:06 +09003190 if (enabled) {
3191 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3192 if (pair.first.equals(id)) {
3193 // We are enabling this input method, but it is already enabled.
3194 // Nothing to do. The previous state was enabled.
3195 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003196 }
3197 }
satokd87c2592010-09-29 11:52:06 +09003198 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3199 // Previous state was disabled.
3200 return false;
3201 } else {
3202 StringBuilder builder = new StringBuilder();
3203 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3204 builder, enabledInputMethodsList, id)) {
3205 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003206 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003207 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3208 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3209 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003210 }
3211 // Previous state was enabled.
3212 return true;
3213 } else {
3214 // We are disabling the input method but it is already disabled.
3215 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003216 return false;
3217 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003218 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003219 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003220
satok723a27e2010-11-11 14:58:11 +09003221 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3222 boolean setSubtypeOnly) {
3223 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003224 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003225
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003226 mCurUserActionNotificationSequenceNumber =
3227 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3228 if (DEBUG) {
3229 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3230 + mCurUserActionNotificationSequenceNumber);
3231 }
3232
3233 if (mCurClient != null && mCurClient.client != null) {
3234 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3235 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003236 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003237 }
3238
satok723a27e2010-11-11 14:58:11 +09003239 // Set Subtype here
3240 if (imi == null || subtypeId < 0) {
3241 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003242 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003243 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003244 if (subtypeId < imi.getSubtypeCount()) {
3245 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3246 mSettings.putSelectedSubtype(subtype.hashCode());
3247 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003248 } else {
3249 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003250 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003251 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003252 }
satokab751aa2010-09-14 19:17:36 +09003253 }
satok723a27e2010-11-11 14:58:11 +09003254
satok4c0e7152012-06-20 20:08:44 +09003255 // Workaround.
3256 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3257 // IMEs are not recognized and considered uninstalled.
3258 // Actually, we can't move everything after SystemReady because
3259 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3260 // the default IME here and try cheking the default IME again in systemReady().
3261 // TODO: Do nothing before system ready and implement a separated logic for
3262 // the encryption lock screen.
3263 // TODO: ASEC should be ready before IMMS is instantiated.
3264 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003265 // Set InputMethod here
3266 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3267 }
3268 }
3269
3270 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3271 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3272 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3273 // newDefaultIme is empty when there is no candidate for the selected IME.
3274 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3275 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3276 if (subtypeHashCode != null) {
3277 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003278 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003279 imi, Integer.valueOf(subtypeHashCode));
3280 } catch (NumberFormatException e) {
3281 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3282 }
3283 }
3284 }
3285 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003286 }
3287
satok4e4569d2010-11-19 18:45:53 +09003288 // If there are no selected shortcuts, tries finding the most applicable ones.
3289 private Pair<InputMethodInfo, InputMethodSubtype>
3290 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3291 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3292 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003293 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003294 boolean foundInSystemIME = false;
3295
3296 // Search applicable subtype for each InputMethodInfo
3297 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003298 final String imiId = imi.getId();
3299 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3300 continue;
3301 }
satokcd7cd292010-11-20 15:46:23 +09003302 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003303 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003304 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003305 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003306 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003307 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003308 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003309 }
satokdf31ae62011-01-15 06:19:44 +09003310 // 2. Search by the system locale from enabledSubtypes.
3311 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003312 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003313 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003314 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003315 }
satoka86f5e42011-09-02 17:12:42 +09003316 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003317 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003318 final ArrayList<InputMethodSubtype> subtypesForSearch =
3319 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003320 ? InputMethodUtils.getSubtypes(imi)
3321 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003322 // 4. Search by the current subtype's locale from all subtypes.
3323 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003324 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003325 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003326 }
3327 // 5. Search by the system locale from all subtypes.
3328 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003329 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003330 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003331 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003332 }
satokcd7cd292010-11-20 15:46:23 +09003333 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003334 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003335 // The current input method is the most applicable IME.
3336 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003337 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003338 break;
satok7599a7f2010-12-22 13:45:23 +09003339 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003340 // The system input method is 2nd applicable IME.
3341 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003342 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003343 if ((imi.getServiceInfo().applicationInfo.flags
3344 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3345 foundInSystemIME = true;
3346 }
satok4e4569d2010-11-19 18:45:53 +09003347 }
3348 }
3349 }
3350 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003351 if (mostApplicableIMI != null) {
3352 Slog.w(TAG, "Most applicable shortcut input method was:"
3353 + mostApplicableIMI.getId());
3354 if (mostApplicableSubtype != null) {
3355 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3356 + "," + mostApplicableSubtype.getMode() + ","
3357 + mostApplicableSubtype.getLocale());
3358 }
3359 }
satok4e4569d2010-11-19 18:45:53 +09003360 }
satokcd7cd292010-11-20 15:46:23 +09003361 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003362 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003363 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003364 } else {
3365 return null;
3366 }
3367 }
3368
satokab751aa2010-09-14 19:17:36 +09003369 /**
3370 * @return Return the current subtype of this input method.
3371 */
satok42c5a162011-05-26 16:46:14 +09003372 @Override
satokab751aa2010-09-14 19:17:36 +09003373 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003374 // TODO: Make this work even for non-current users?
3375 if (!calledFromValidUser()) {
3376 return null;
3377 }
3378 synchronized (mMethodMap) {
3379 return getCurrentInputMethodSubtypeLocked();
3380 }
3381 }
3382
3383 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003384 if (mCurMethodId == null) {
3385 return null;
3386 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003387 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003388 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3389 if (imi == null || imi.getSubtypeCount() == 0) {
3390 return null;
satok4e4569d2010-11-19 18:45:53 +09003391 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003392 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003393 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3394 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003395 if (subtypeId == NOT_A_SUBTYPE_ID) {
3396 // If there are no selected subtypes, the framework will try to find
3397 // the most applicable subtype from explicitly or implicitly enabled
3398 // subtypes.
3399 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003400 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003401 // If there is only one explicitly or implicitly enabled subtype,
3402 // just returns it.
3403 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3404 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3405 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003406 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003407 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003408 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003409 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003410 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3412 true);
satok4e4569d2010-11-19 18:45:53 +09003413 }
satok3ef8b292010-11-23 06:06:29 +09003414 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003415 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003416 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003417 }
3418 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003419 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003420 }
3421
satokf3db1af2010-11-23 13:34:33 +09003422 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3423 InputMethodSubtype subtype) {
3424 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3425 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3426 } else {
3427 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3428 subtypes.add(subtype);
3429 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3430 }
3431 }
3432
satok4e4569d2010-11-19 18:45:53 +09003433 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003434 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003435 @Override
satok4e4569d2010-11-19 18:45:53 +09003436 public List getShortcutInputMethodsAndSubtypes() {
3437 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003438 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003439 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003440 // If there are no selected shortcut subtypes, the framework will try to find
3441 // the most applicable subtype from all subtypes whose mode is
3442 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003443 Pair<InputMethodInfo, InputMethodSubtype> info =
3444 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003445 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003446 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003447 ret.add(info.first);
3448 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003449 }
satok3da92232011-01-11 22:46:30 +09003450 return ret;
satokf3db1af2010-11-23 13:34:33 +09003451 }
satokf3db1af2010-11-23 13:34:33 +09003452 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3453 ret.add(imi);
3454 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3455 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003456 }
3457 }
satokf3db1af2010-11-23 13:34:33 +09003458 return ret;
satok4e4569d2010-11-19 18:45:53 +09003459 }
3460 }
3461
satok42c5a162011-05-26 16:46:14 +09003462 @Override
satokb66d2872010-11-10 01:04:04 +09003463 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003464 // TODO: Make this work even for non-current users?
3465 if (!calledFromValidUser()) {
3466 return false;
3467 }
satokb66d2872010-11-10 01:04:04 +09003468 synchronized (mMethodMap) {
3469 if (subtype != null && mCurMethodId != null) {
3470 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003472 if (subtypeId != NOT_A_SUBTYPE_ID) {
3473 setInputMethodLocked(mCurMethodId, subtypeId);
3474 return true;
3475 }
3476 }
3477 return false;
3478 }
3479 }
3480
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003481 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003482 private static class InputMethodFileManager {
3483 private static final String SYSTEM_PATH = "system";
3484 private static final String INPUT_METHOD_PATH = "inputmethod";
3485 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3486 private static final String NODE_SUBTYPES = "subtypes";
3487 private static final String NODE_SUBTYPE = "subtype";
3488 private static final String NODE_IMI = "imi";
3489 private static final String ATTR_ID = "id";
3490 private static final String ATTR_LABEL = "label";
3491 private static final String ATTR_ICON = "icon";
3492 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3493 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3494 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3495 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3496 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3497 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003498 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003499 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003500 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003501 if (methodMap == null) {
3502 throw new NullPointerException("methodMap is null");
3503 }
3504 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003505 final File systemDir = userId == UserHandle.USER_OWNER
3506 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3507 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003508 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3509 if (!inputMethodDir.mkdirs()) {
3510 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3511 }
3512 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3513 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3514 if (!subtypeFile.exists()) {
3515 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003516 writeAdditionalInputMethodSubtypes(
3517 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003518 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003519 readAdditionalInputMethodSubtypes(
3520 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003521 }
3522 }
3523
3524 private void deleteAllInputMethodSubtypes(String imiId) {
3525 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003526 mAdditionalSubtypesMap.remove(imiId);
3527 writeAdditionalInputMethodSubtypes(
3528 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003529 }
3530 }
3531
3532 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003533 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003534 synchronized (mMethodMap) {
3535 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3536 final int N = additionalSubtypes.length;
3537 for (int i = 0; i < N; ++i) {
3538 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003539 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003540 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003541 } else {
3542 Slog.w(TAG, "Duplicated subtype definition found: "
3543 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003544 }
3545 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003546 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3547 writeAdditionalInputMethodSubtypes(
3548 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003549 }
3550 }
3551
3552 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3553 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003554 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003555 }
3556 }
3557
3558 private static void writeAdditionalInputMethodSubtypes(
3559 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3560 HashMap<String, InputMethodInfo> methodMap) {
3561 // Safety net for the case that this function is called before methodMap is set.
3562 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3563 FileOutputStream fos = null;
3564 try {
3565 fos = subtypesFile.startWrite();
3566 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003567 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003568 out.startDocument(null, true);
3569 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3570 out.startTag(null, NODE_SUBTYPES);
3571 for (String imiId : allSubtypes.keySet()) {
3572 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3573 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3574 continue;
3575 }
3576 out.startTag(null, NODE_IMI);
3577 out.attribute(null, ATTR_ID, imiId);
3578 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3579 final int N = subtypesList.size();
3580 for (int i = 0; i < N; ++i) {
3581 final InputMethodSubtype subtype = subtypesList.get(i);
3582 out.startTag(null, NODE_SUBTYPE);
3583 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3584 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3585 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3586 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3587 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3588 out.attribute(null, ATTR_IS_AUXILIARY,
3589 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3590 out.endTag(null, NODE_SUBTYPE);
3591 }
3592 out.endTag(null, NODE_IMI);
3593 }
3594 out.endTag(null, NODE_SUBTYPES);
3595 out.endDocument();
3596 subtypesFile.finishWrite(fos);
3597 } catch (java.io.IOException e) {
3598 Slog.w(TAG, "Error writing subtypes", e);
3599 if (fos != null) {
3600 subtypesFile.failWrite(fos);
3601 }
3602 }
3603 }
3604
3605 private static void readAdditionalInputMethodSubtypes(
3606 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3607 if (allSubtypes == null || subtypesFile == null) return;
3608 allSubtypes.clear();
3609 FileInputStream fis = null;
3610 try {
3611 fis = subtypesFile.openRead();
3612 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003613 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003614 int type = parser.getEventType();
3615 // Skip parsing until START_TAG
3616 while ((type = parser.next()) != XmlPullParser.START_TAG
3617 && type != XmlPullParser.END_DOCUMENT) {}
3618 String firstNodeName = parser.getName();
3619 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3620 throw new XmlPullParserException("Xml doesn't start with subtypes");
3621 }
3622 final int depth =parser.getDepth();
3623 String currentImiId = null;
3624 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3625 while (((type = parser.next()) != XmlPullParser.END_TAG
3626 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3627 if (type != XmlPullParser.START_TAG)
3628 continue;
3629 final String nodeName = parser.getName();
3630 if (NODE_IMI.equals(nodeName)) {
3631 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3632 if (TextUtils.isEmpty(currentImiId)) {
3633 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3634 continue;
3635 }
3636 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3637 allSubtypes.put(currentImiId, tempSubtypesArray);
3638 } else if (NODE_SUBTYPE.equals(nodeName)) {
3639 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3640 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3641 continue;
3642 }
3643 final int icon = Integer.valueOf(
3644 parser.getAttributeValue(null, ATTR_ICON));
3645 final int label = Integer.valueOf(
3646 parser.getAttributeValue(null, ATTR_LABEL));
3647 final String imeSubtypeLocale =
3648 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3649 final String imeSubtypeMode =
3650 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3651 final String imeSubtypeExtraValue =
3652 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003653 final boolean isAuxiliary = "1".equals(String.valueOf(
3654 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003655 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3656 .setSubtypeNameResId(label)
3657 .setSubtypeIconResId(icon)
3658 .setSubtypeLocale(imeSubtypeLocale)
3659 .setSubtypeMode(imeSubtypeMode)
3660 .setSubtypeExtraValue(imeSubtypeExtraValue)
3661 .setIsAuxiliary(isAuxiliary)
3662 .build();
satoke7c6998e2011-06-03 17:57:59 +09003663 tempSubtypesArray.add(subtype);
3664 }
3665 }
3666 } catch (XmlPullParserException e) {
3667 Slog.w(TAG, "Error reading subtypes: " + e);
3668 return;
3669 } catch (java.io.IOException e) {
3670 Slog.w(TAG, "Error reading subtypes: " + e);
3671 return;
3672 } catch (NumberFormatException e) {
3673 Slog.w(TAG, "Error reading subtypes: " + e);
3674 return;
3675 } finally {
3676 if (fis != null) {
3677 try {
3678 fis.close();
3679 } catch (java.io.IOException e1) {
3680 Slog.w(TAG, "Failed to close.");
3681 }
3682 }
3683 }
3684 }
3685 }
3686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 @Override
3688 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3689 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3690 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3693 + Binder.getCallingPid()
3694 + ", uid=" + Binder.getCallingUid());
3695 return;
3696 }
3697
3698 IInputMethod method;
3699 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 synchronized (mMethodMap) {
3704 p.println("Current Input Method Manager state:");
3705 int N = mMethodList.size();
3706 p.println(" Input Methods:");
3707 for (int i=0; i<N; i++) {
3708 InputMethodInfo info = mMethodList.get(i);
3709 p.println(" InputMethod #" + i + ":");
3710 info.dump(p, " ");
3711 }
3712 p.println(" Clients:");
3713 for (ClientState ci : mClients.values()) {
3714 p.println(" Client " + ci + ":");
3715 p.println(" client=" + ci.client);
3716 p.println(" inputContext=" + ci.inputContext);
3717 p.println(" sessionRequested=" + ci.sessionRequested);
3718 p.println(" curSession=" + ci.curSession);
3719 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003720 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003722 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3723 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3725 + " mBoundToMethod=" + mBoundToMethod);
3726 p.println(" mCurToken=" + mCurToken);
3727 p.println(" mCurIntent=" + mCurIntent);
3728 method = mCurMethod;
3729 p.println(" mCurMethod=" + mCurMethod);
3730 p.println(" mEnabledSession=" + mEnabledSession);
3731 p.println(" mShowRequested=" + mShowRequested
3732 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3733 + " mShowForced=" + mShowForced
3734 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003735 p.println(" mCurUserActionNotificationSequenceNumber="
3736 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003737 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
Yohei Yukawa81482972015-06-04 00:58:59 -07003738 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003739 p.println(" mSwitchingController:");
3740 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003742
Jeff Brownb88102f2010-09-08 11:49:43 -07003743 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 pw.flush();
3746 try {
3747 client.client.asBinder().dump(fd, args);
3748 } catch (RemoteException e) {
3749 p.println("Input method client dead: " + e);
3750 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003751 } else {
3752 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003754
Jeff Brownb88102f2010-09-08 11:49:43 -07003755 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 pw.flush();
3758 try {
3759 method.asBinder().dump(fd, args);
3760 } catch (RemoteException e) {
3761 p.println("Input method service dead: " + e);
3762 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003763 } else {
3764 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766 }
3767}