blob: f9f617ef4c267bea5effcc12dda04cbce8bc988b [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;
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070030import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
34import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080035import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090036import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090040import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090045import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090046import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080072import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070073import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040075import android.os.Bundle;
satoke7c6998e2011-06-03 17:57:59 +090076import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Handler;
78import android.os.IBinder;
79import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090082import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.Parcel;
84import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080085import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.ServiceManager;
87import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090088import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080089import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.provider.Settings;
91import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070092import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090098import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090099import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.PrintWriterPrinter;
101import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900102import android.util.Slog;
103import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900104import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700106import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.view.LayoutInflater;
108import android.view.View;
109import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900111import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.inputmethod.InputBinding;
113import android.view.inputmethod.InputMethod;
114import android.view.inputmethod.InputMethodInfo;
115import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700116import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900117import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900118import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900120import android.widget.CompoundButton;
121import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900123import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
satoke7c6998e2011-06-03 17:57:59 +0900126import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900128import java.io.FileInputStream;
129import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.IOException;
131import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100132import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900134import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.HashMap;
136import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900137import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139/**
140 * This class provides a system service that manages input methods.
141 */
142public class InputMethodManagerService extends IInputMethodManager.Stub
143 implements ServiceConnection, Handler.Callback {
144 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700145 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700146 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700148 private static final char INPUT_METHOD_SEPARATOR = ':';
149 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
150
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700151 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
152 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
153 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int MSG_UNBIND_INPUT = 1000;
156 static final int MSG_BIND_INPUT = 1010;
157 static final int MSG_SHOW_SOFT_INPUT = 1020;
158 static final int MSG_HIDE_SOFT_INPUT = 1030;
159 static final int MSG_ATTACH_TOKEN = 1040;
160 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_START_INPUT = 2000;
163 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 static final int MSG_UNBIND_METHOD = 3000;
166 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700167 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700168 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900169 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satok01038492012-04-09 21:08:27 +0900171 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
172
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700173 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satokf9f01002011-05-19 21:31:50 +0900175 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
176
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900178 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179
satok4e4569d2010-11-19 18:45:53 +0900180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800182 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900184 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final IWindowManager mIWindowManager;
187 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700188 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900189 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900190 private final HardKeyboardListener mHardKeyboardListener;
191 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900192 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // All known input methods. mMethodMap also serves as the global
197 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700198 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
199 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900200 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700201 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900202 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 // Used to bring IME service up to visible adjustment while it is being shown.
205 final ServiceConnection mVisibleConnection = new ServiceConnection() {
206 @Override public void onServiceConnected(ComponentName name, IBinder service) {
207 }
208
209 @Override public void onServiceDisconnected(ComponentName name) {
210 }
211 };
212 boolean mVisibleBound = false;
213
satok7cfc0ed2011-06-20 21:29:36 +0900214 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700215 private NotificationManager mNotificationManager;
216 private KeyguardManager mKeyguardManager;
217 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700219 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900220 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900221 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900222 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900223
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900224 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 final ClientState client;
226 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227
228 IInputMethodSession session;
229 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public String toString() {
233 return "SessionState{uid " + client.uid + " pid " + client.pid
234 + " method " + Integer.toHexString(
235 System.identityHashCode(method))
236 + " session " + Integer.toHexString(
237 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 + "}";
240 }
241
242 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 client = _client;
245 method = _method;
246 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
Jeff Brownc28867a2013-03-26 15:42:39 -0700251 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IInputMethodClient client;
253 final IInputContext inputContext;
254 final int uid;
255 final int pid;
256 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean sessionRequested;
259 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "ClientState{" + Integer.toHexString(
264 System.identityHashCode(this)) + " uid " + uid
265 + " pid " + pid + "}";
266 }
267
268 ClientState(IInputMethodClient _client, IInputContext _inputContext,
269 int _uid, int _pid) {
270 client = _client;
271 inputContext = _inputContext;
272 uid = _uid;
273 pid = _pid;
274 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
275 }
276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700278 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700281 * Set once the system is ready to run third party code.
282 */
283 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * Id of the currently selected input method.
287 */
288 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The current binding sequence number, incremented every time there is
292 * a new bind performed.
293 */
294 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
297 * The client that is currently bound to an input method.
298 */
299 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700302 * The last window token that gained focus.
303 */
304 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * The input context last provided by the current client.
308 */
309 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
312 * The attributes last provided by the current client.
313 */
314 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * The input method ID of the input method service that we are currently
318 * connected to or in the process of connecting to.
319 */
320 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
satokab751aa2010-09-14 19:17:36 +0900323 * The current subtype of the current input method.
324 */
325 private InputMethodSubtype mCurrentSubtype;
326
satok4e4569d2010-11-19 18:45:53 +0900327 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900328 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700329 mShortcutInputMethodsAndSubtypes = new HashMap<>();
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 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700395 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700397 boolean mIsInteractive = 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 {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900493 @Override
494 public void onReceive(Context context, Intent intent) {
495 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700496 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700498 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900499 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800500 } else if (Intent.ACTION_USER_ADDED.equals(action)
501 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100502 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800503 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700504 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
505 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
506 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
507 final String prevValue = intent.getStringExtra(
508 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
509 final String newValue = intent.getStringExtra(
510 Intent.EXTRA_SETTING_NEW_VALUE);
511 restoreEnabledInputMethods(mContext, prevValue, newValue);
512 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900513 } else {
514 Slog.w(TAG, "Unexpected intent " + intent);
515 }
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700519 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
520 // does not attempt to validate on the fly with any installed device policy, so must only
521 // be run in the context of initial device setup.
522 //
523 // TODO: Move this method to InputMethodUtils with adding unit tests.
524 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
525 if (DEBUG_RESTORE) {
526 Slog.i(TAG, "Restoring enabled input methods:");
527 Slog.i(TAG, "prev=" + prevValue);
528 Slog.i(TAG, " new=" + newValue);
529 }
530 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
531 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
532 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
533
534 // Merge the restored ime+subtype enabled states into the live state
535 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
536 final String imeId = entry.getKey();
537 ArraySet<String> prevSubtypes = prevMap.get(imeId);
538 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700539 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700540 prevMap.put(imeId, prevSubtypes);
541 }
542 prevSubtypes.addAll(entry.getValue());
543 }
544
545 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
546 if (DEBUG_RESTORE) {
547 Slog.i(TAG, "Merged IME string:");
548 Slog.i(TAG, " " + mergedImesAndSubtypesString);
549 }
550 Settings.Secure.putString(context.getContentResolver(),
551 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
552 }
553
554 // TODO: Move this method to InputMethodUtils with adding unit tests.
555 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
556 // we want to use the canonical InputMethodSettings implementation,
557 // so we convert data structures first.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700558 List<Pair<String, ArrayList<String>>> imeMap = new ArrayList<>(4);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700559 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
560 final String imeName = entry.getKey();
561 final ArraySet<String> subtypeSet = entry.getValue();
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700562 final ArrayList<String> subtypes = new ArrayList<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700563 if (subtypeSet != null) {
564 subtypes.addAll(subtypeSet);
565 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700566 imeMap.add(new Pair<>(imeName, subtypes));
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700567 }
568 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
569 }
570
571 // TODO: Move this method to InputMethodUtils with adding unit tests.
572 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
573 final String inputMethodsAndSubtypesString) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700574 final ArrayMap<String, ArraySet<String>> imeMap = new ArrayMap<>();
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700575 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
576 return imeMap;
577 }
578
579 final SimpleStringSplitter typeSplitter =
580 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
581 final SimpleStringSplitter subtypeSplitter =
582 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
583 List<Pair<String, ArrayList<String>>> allImeSettings =
584 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
585 typeSplitter,
586 subtypeSplitter);
587 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700588 ArraySet<String> subtypes = new ArraySet<>();
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700589 if (ime.second != null) {
590 subtypes.addAll(ime.second);
591 }
592 imeMap.put(ime.first, subtypes);
593 }
594 return imeMap;
595 }
596
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800597 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900598 private boolean isChangingPackagesOfCurrentUser() {
599 final int userId = getChangingUserId();
600 final boolean retval = userId == mSettings.getCurrentUserId();
601 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900602 if (!retval) {
603 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
604 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900605 }
606 return retval;
607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800610 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900611 if (!isChangingPackagesOfCurrentUser()) {
612 return false;
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900615 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 final int N = mMethodList.size();
617 if (curInputMethodId != null) {
618 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 InputMethodInfo imi = mMethodList.get(i);
620 if (imi.getId().equals(curInputMethodId)) {
621 for (String pkg : packages) {
622 if (imi.getPackageName().equals(pkg)) {
623 if (!doit) {
624 return true;
625 }
satok723a27e2010-11-11 14:58:11 +0900626 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800627 chooseNewDefaultIMELocked();
628 return true;
629 }
630 }
631 }
632 }
633 }
634 }
635 return false;
636 }
637
638 @Override
639 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 if (!isChangingPackagesOfCurrentUser()) {
641 return;
642 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800643 synchronized (mMethodMap) {
644 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646 final int N = mMethodList.size();
647 if (curInputMethodId != null) {
648 for (int i=0; i<N; i++) {
649 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900650 final String imiId = imi.getId();
651 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800652 curIm = imi;
653 }
satoke7c6998e2011-06-03 17:57:59 +0900654
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800655 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900656 if (isPackageModified(imi.getPackageName())) {
657 mFileManager.deleteAllInputMethodSubtypes(imiId);
658 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800659 if (change == PACKAGE_TEMPORARY_CHANGE
660 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800661 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 + imi.getComponent());
663 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800667
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900668 buildInputMethodListLocked(
669 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800673 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 int change = isPackageDisappearing(curIm.getPackageName());
675 if (change == PACKAGE_TEMPORARY_CHANGE
676 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800677 ServiceInfo si = null;
678 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 si = mIPackageManager.getServiceInfo(
680 curIm.getComponent(), 0, mSettings.getCurrentUserId());
681 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 }
683 if (si == null) {
684 // Uh oh, current input method is no longer around!
685 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900687 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800689 changed = true;
690 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900692 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800693 }
694 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 }
satokab751aa2010-09-14 19:17:36 +0900697
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 if (curIm == null) {
699 // We currently don't have a default input method... is
700 // one now available?
701 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700702 } else if (!changed && isPackageModified(curIm.getPackageName())) {
703 // Even if the current input method is still available, mCurrentSubtype could
704 // be obsolete when the package is modified in practice.
705 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800709 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 }
713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
Jeff Brownc28867a2013-03-26 15:42:39 -0700715 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900716 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700717 private final IInputMethod mMethod;
718 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
Jeff Brownc28867a2013-03-26 15:42:39 -0700720 MethodCallback(InputMethodManagerService imms, IInputMethod method,
721 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900722 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700723 mMethod = method;
724 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
satoke7c6998e2011-06-03 17:57:59 +0900727 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700728 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700729 long ident = Binder.clearCallingIdentity();
730 try {
731 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
732 } finally {
733 Binder.restoreCallingIdentity(ident);
734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
satok01038492012-04-09 21:08:27 +0900738 private class HardKeyboardListener
739 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
740 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700741 public void onHardKeyboardStatusChange(boolean available) {
742 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
743 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900744 }
745
Michael Wright7b5a96b2014-08-09 19:28:42 -0700746 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900747 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700748 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900749 }
750 synchronized(mMethodMap) {
751 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
752 && mSwitchingDialog.isShowing()) {
753 mSwitchingDialogTitleView.findViewById(
754 com.android.internal.R.id.hard_keyboard_section).setVisibility(
755 available ? View.VISIBLE : View.GONE);
756 }
757 }
758 }
759 }
760
761 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900762 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800764 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700766 // Note: SettingsObserver doesn't register observers in its constructor.
767 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 mIWindowManager = IWindowManager.Stub.asInterface(
769 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800770 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 public void executeMessage(Message msg) {
773 handleMessage(msg);
774 }
Mita Yuned218c72012-12-06 17:18:25 -0800775 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900776 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900777 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900778 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700779 mHasFeature = context.getPackageManager().hasSystemFeature(
780 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900781
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400782 Bundle extras = new Bundle();
783 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
784 mImeSwitcherNotification = new Notification.Builder(mContext)
785 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
786 .setWhen(0)
787 .setOngoing(true)
788 .addExtras(extras)
789 .setCategory(Notification.CATEGORY_SYSTEM)
790 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400791
satok7cfc0ed2011-06-20 21:29:36 +0900792 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900793 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900794
795 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900796
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900797 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800799 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
800 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700801 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900802 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803
satok7cfc0ed2011-06-20 21:29:36 +0900804 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900805 int userId = 0;
806 try {
807 ActivityManagerNative.getDefault().registerUserSwitchObserver(
808 new IUserSwitchObserver.Stub() {
809 @Override
810 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900811 synchronized(mMethodMap) {
812 switchUserLocked(newUserId);
813 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 if (reply != null) {
815 try {
816 reply.sendResult(null);
817 } catch (RemoteException e) {
818 }
819 }
820 }
821
822 @Override
823 public void onUserSwitchComplete(int newUserId) throws RemoteException {
824 }
Kenny Guy42979622015-04-13 18:03:05 +0000825
826 @Override
827 public void onForegroundProfileSwitch(int newProfileId) {
828 // Ignore.
829 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900830 });
831 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
832 } catch (RemoteException e) {
833 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
834 }
satok81f8b7c2012-12-04 20:42:56 +0900835 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900836
satokd87c2592010-09-29 11:52:06 +0900837 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900838 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900839 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700840
841 // Let the package manager query which are the default imes
842 // as they get certain permissions granted by default.
843 PackageManagerInternal packageManagerInternal = LocalServices.getService(
844 PackageManagerInternal.class);
845 packageManagerInternal.setImePackagesProvider(
846 new PackageManagerInternal.PackagesProvider() {
847 @Override
848 public String[] getPackages(int userId) {
849 synchronized (mMethodMap) {
850 final int currentUserId = mSettings.getCurrentUserId();
851 // TODO: We are switching the current user id in the settings
852 // object to query it and then revert the user id. Ideally, we
853 // should call a API in settings with the user id as an argument.
854 mSettings.setCurrentUserId(userId);
855 List<InputMethodInfo> imes = mSettings
856 .getEnabledInputMethodListLocked();
857 String[] packageNames = null;
858 if (imes != null) {
859 final int imeCount = imes.size();
860 packageNames = new String[imeCount];
861 for (int i = 0; i < imeCount; i++) {
862 InputMethodInfo ime = imes.get(i);
863 packageNames[i] = ime.getPackageName();
864 }
865 }
866 mSettings.setCurrentUserId(currentUserId);
867 return packageNames;
868 }
869 }
870 });
871
Kenny Guy2a764942014-04-02 13:29:20 +0100872 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900873 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900874 synchronized (mMethodMap) {
875 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
876 mSettings, context);
877 }
satok0a1bcf42012-05-16 19:26:31 +0900878
879 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900880 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900881 if (DEBUG) {
882 Slog.d(TAG, "Initial default ime = " + defaultImiId);
883 }
satok0a1bcf42012-05-16 19:26:31 +0900884 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
885
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900886 synchronized (mMethodMap) {
887 buildInputMethodListLocked(mMethodList, mMethodMap,
888 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
889 }
satokd87c2592010-09-29 11:52:06 +0900890 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
satok0a1bcf42012-05-16 19:26:31 +0900892 if (!mImeSelectedOnBoot) {
893 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900894 synchronized (mMethodMap) {
895 resetDefaultImeLocked(context);
896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900899 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700900 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900901 updateFromSettingsLocked(true);
902 }
satok5b927c432012-05-01 20:09:34 +0900903
904 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
905 // according to the new system locale.
906 final IntentFilter filter = new IntentFilter();
907 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
908 mContext.registerReceiver(
909 new BroadcastReceiver() {
910 @Override
911 public void onReceive(Context context, Intent intent) {
912 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900913 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900914 }
915 }
916 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700917 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900918 }
919
satok5b927c432012-05-01 20:09:34 +0900920 private void resetDefaultImeLocked(Context context) {
921 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900922 if (mCurMethodId != null
923 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900924 return;
925 }
926
927 InputMethodInfo defIm = null;
928 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900929 if (defIm == null && mSystemReady) {
930 final Locale systemLocale = context.getResources().getConfiguration().locale;
931 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
932 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
933 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900934 defIm = imi;
935 Slog.i(TAG, "Selected default: " + imi.getId());
936 }
937 }
938 }
939 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900940 defIm = InputMethodUtils.getMostApplicableDefaultIME(
941 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900942 if (defIm != null) {
943 Slog.i(TAG, "Default found, using " + defIm.getId());
944 } else {
945 Slog.i(TAG, "No default found");
946 }
satok5b927c432012-05-01 20:09:34 +0900947 }
948 if (defIm != null) {
949 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
950 }
951 }
952
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900953 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
954 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 if (!mSystemReady) {
956 // not system ready
957 return;
958 }
959 final Locale newLocale = mRes.getConfiguration().locale;
960 if (!updateOnlyWhenLocaleChanged
961 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
962 if (!updateOnlyWhenLocaleChanged) {
963 hideCurrentInputLocked(0, null);
964 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700965 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 }
967 if (DEBUG) {
968 Slog.i(TAG, "Locale has been changed to " + newLocale);
969 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900970 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 if (!updateOnlyWhenLocaleChanged) {
972 final String selectedImiId = mSettings.getSelectedInputMethod();
973 if (TextUtils.isEmpty(selectedImiId)) {
974 // This is the first time of the user switch and
975 // set the current ime to the proper one.
976 resetDefaultImeLocked(mContext);
977 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900978 } else {
979 // If the locale is changed, needs to reset the default ime
980 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800982 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 mLastSystemLocale = newLocale;
984 if (!updateOnlyWhenLocaleChanged) {
985 try {
986 startInputInnerLocked();
987 } catch (RuntimeException e) {
988 Slog.w(TAG, "Unexpected exception", e);
989 }
990 }
991 }
992 }
993
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900994 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900995 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
996 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900997 }
998
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900999 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001000 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1001 + " currentUserId=" + mSettings.getCurrentUserId());
1002
Yohei Yukawa81482972015-06-04 00:58:59 -07001003 // ContentObserver should be registered again when the user is changed
1004 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001005 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +01001006 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001007 // InputMethodFileManager should be reset when the user is changed
1008 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001009 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001010
1011 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1012 + " defaultImiId=" + defaultImiId);
1013
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001014 // For secondary users, the list of enabled IMEs may not have been updated since the
1015 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1016 // not be empty even if the IME has been uninstalled by the primary user.
1017 // Even in such cases, IMMS works fine because it will find the most applicable
1018 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001019 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001020 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001021 initialUserSwitch /* needsToResetDefaultIme */);
1022 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001023 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1024 mSettings.getEnabledInputMethodListLocked(), newUserId,
1025 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001026 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001027
1028 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1029 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001030 }
1031
Kenny Guy2a764942014-04-02 13:29:20 +01001032 void updateCurrentProfileIds() {
1033 List<UserInfo> profiles =
1034 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1035 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1036 for (int i = 0; i < currentProfileIds.length; i++) {
1037 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001038 }
Kenny Guy2a764942014-04-02 13:29:20 +01001039 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001040 }
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 @Override
1043 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1044 throws RemoteException {
1045 try {
1046 return super.onTransact(code, data, reply, flags);
1047 } catch (RuntimeException e) {
1048 // The input method manager only throws security exceptions, so let's
1049 // log all others.
1050 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001051 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 throw e;
1054 }
1055 }
1056
Svetoslav Ganova0027152013-06-25 14:59:53 -07001057 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001058 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001059 if (DEBUG) {
1060 Slog.d(TAG, "--- systemReady");
1061 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001062 if (!mSystemReady) {
1063 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001064 mKeyguardManager =
1065 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001066 mNotificationManager = (NotificationManager)
1067 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1068 mStatusBar = statusBar;
1069 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001070 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001071 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1072 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001073 if (mShowOngoingImeSwitcherForPhones) {
1074 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1075 mHardKeyboardListener);
1076 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001077 buildInputMethodListLocked(mMethodList, mMethodMap,
1078 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001079 if (!mImeSelectedOnBoot) {
1080 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001081 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001082 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1083 mSettings.getEnabledInputMethodListLocked(),
1084 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001085 }
1086 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001087 try {
1088 startInputInnerLocked();
1089 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001091 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001092 }
1093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001096 // ---------------------------------------------------------------------------------------
1097 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1098 // 1) it comes from the system process
1099 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1100 private boolean calledFromValidUser() {
1101 final int uid = Binder.getCallingUid();
1102 final int userId = UserHandle.getUserId(uid);
1103 if (DEBUG) {
1104 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1105 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1106 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001107 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1108 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 }
Kenny Guy2a764942014-04-02 13:29:20 +01001110 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001111 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001113
1114 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1115 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1116 // must not manage background users' states in any functions.
1117 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1118 // by a token.
1119 if (mContext.checkCallingOrSelfPermission(
1120 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1121 == PackageManager.PERMISSION_GRANTED) {
1122 if (DEBUG) {
1123 Slog.d(TAG, "--- Access granted because the calling process has "
1124 + "the INTERACT_ACROSS_USERS_FULL permission");
1125 }
1126 return true;
1127 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001128 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1129 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001130 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 }
1132
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001133
1134 /**
1135 * Returns true iff the caller is identified to be the current input method with the token.
1136 * @param token The window token given to the input method when it was started.
1137 * @return true if and only if non-null valid token is specified.
1138 */
1139 private boolean calledWithValidToken(IBinder token) {
1140 if (token == null || mCurToken != token) {
1141 return false;
1142 }
1143 return true;
1144 }
1145
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001146 private boolean bindCurrentInputMethodService(
1147 Intent service, ServiceConnection conn, int flags) {
1148 if (service == null || conn == null) {
1149 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1150 return false;
1151 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001152 return mContext.bindServiceAsUser(service, conn, flags,
1153 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001154 }
1155
satoke7c6998e2011-06-03 17:57:59 +09001156 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001158 // TODO: Make this work even for non-current users?
1159 if (!calledFromValidUser()) {
1160 return Collections.emptyList();
1161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001163 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
1166
satoke7c6998e2011-06-03 17:57:59 +09001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001169 // TODO: Make this work even for non-current users?
1170 if (!calledFromValidUser()) {
1171 return Collections.emptyList();
1172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001174 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
1177
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001178 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001179 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001180 * @return enabled subtypes of the specified imi
1181 */
satoke7c6998e2011-06-03 17:57:59 +09001182 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001183 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001184 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001185 // TODO: Make this work even for non-current users?
1186 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001187 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001188 }
satok67ddf9c2010-11-17 09:45:54 +09001189 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001190 final InputMethodInfo imi;
1191 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001192 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001193 } else {
1194 imi = mMethodMap.get(imiId);
1195 }
1196 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001197 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001198 }
1199 return mSettings.getEnabledInputMethodSubtypeListLocked(
1200 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001201 }
1202 }
1203
satoke7c6998e2011-06-03 17:57:59 +09001204 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 public void addClient(IInputMethodClient client,
1206 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 if (!calledFromValidUser()) {
1208 return;
1209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 synchronized (mMethodMap) {
1211 mClients.put(client.asBinder(), new ClientState(client,
1212 inputContext, uid, pid));
1213 }
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
satoke7c6998e2011-06-03 17:57:59 +09001216 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 if (!calledFromValidUser()) {
1219 return;
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001222 ClientState cs = mClients.remove(client.asBinder());
1223 if (cs != null) {
1224 clearClientSessionLocked(cs);
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 void executeOrSendMessage(IInterface target, Message msg) {
1230 if (target.asBinder() instanceof Binder) {
1231 mCaller.sendMessage(msg);
1232 } else {
1233 handleMessage(msg);
1234 msg.recycle();
1235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001238 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001240 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 + mCurClient.client.asBinder());
1242 if (mBoundToMethod) {
1243 mBoundToMethod = false;
1244 if (mCurMethod != null) {
1245 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1246 MSG_UNBIND_INPUT, mCurMethod));
1247 }
1248 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001249
1250 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1251 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1253 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1254 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001256
The Android Open Source Project10592532009-03-18 17:39:46 -07001257 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 private int getImeShowFlags() {
1262 int flags = 0;
1263 if (mShowForced) {
1264 flags |= InputMethod.SHOW_FORCED
1265 | InputMethod.SHOW_EXPLICIT;
1266 } else if (mShowExplicitlyRequested) {
1267 flags |= InputMethod.SHOW_EXPLICIT;
1268 }
1269 return flags;
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 private int getAppShowFlags() {
1273 int flags = 0;
1274 if (mShowForced) {
1275 flags |= InputMethodManager.SHOW_FORCED;
1276 } else if (!mShowExplicitlyRequested) {
1277 flags |= InputMethodManager.SHOW_IMPLICIT;
1278 }
1279 return flags;
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
Dianne Hackborn7663d802012-02-24 13:08:49 -08001282 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 if (!mBoundToMethod) {
1284 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1285 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1286 mBoundToMethod = true;
1287 }
1288 final SessionState session = mCurClient.curSession;
1289 if (initial) {
1290 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1291 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1292 } else {
1293 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1294 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1295 }
1296 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001297 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001298 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001300 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001301 (session.channel != null ? session.channel.dup() : null),
1302 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 // If no method is currently selected, do nothing.
1308 if (mCurMethodId == null) {
1309 return mNoBinding;
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 ClientState cs = mClients.get(client.asBinder());
1313 if (cs == null) {
1314 throw new IllegalArgumentException("unknown client "
1315 + client.asBinder());
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 try {
1319 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1320 // Check with the window manager to make sure this client actually
1321 // has a window with focus. If not, reject. This is thread safe
1322 // because if the focus changes some time before or after, the
1323 // next client receiving focus that has any interest in input will
1324 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001325 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1327 return null;
1328 }
1329 } catch (RemoteException e) {
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
Dianne Hackborn7663d802012-02-24 13:08:49 -08001332 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1333 }
1334
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001335 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001336 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001337 // If no method is currently selected, do nothing.
1338 if (mCurMethodId == null) {
1339 return mNoBinding;
1340 }
1341
Yohei Yukawad57ba672015-06-08 16:39:46 -07001342 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1343 attribute.packageName)) {
1344 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1345 + " uid=" + cs.uid + " package=" + attribute.packageName);
1346 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001347 }
1348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001350 // Was the keyguard locked when switching over to the new client?
1351 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 // If the client is changing, we need to switch over to the new
1353 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001354 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001355 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001356 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357
1358 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001359 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001360 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001361 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 // Bump up the sequence for this client and attach it.
1366 mCurSeq++;
1367 if (mCurSeq <= 0) mCurSeq = 1;
1368 mCurClient = cs;
1369 mCurInputContext = inputContext;
1370 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 // Check if the input method is changing.
1373 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1374 if (cs.curSession != null) {
1375 // Fast case: if we are already connected to the input method,
1376 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001377 return attachNewInputLocked(
1378 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 if (mHaveConnection) {
1381 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 // Return to client, and we will get back with it when
1383 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001384 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001385 return new InputBindResult(null, null, mCurId, mCurSeq,
1386 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 } else if (SystemClock.uptimeMillis()
1388 < (mLastBindTime+TIME_TO_RECONNECT)) {
1389 // In this case we have connected to the service, but
1390 // don't yet have its interface. If it hasn't been too
1391 // long since we did the connection, we'll return to
1392 // the client and wait to get the service interface so
1393 // we can report back. If it has been too long, we want
1394 // to fall through so we can try a disconnect/reconnect
1395 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001396 return new InputBindResult(null, null, mCurId, mCurSeq,
1397 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1400 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 }
1403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001405 return startInputInnerLocked();
1406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001408 InputBindResult startInputInnerLocked() {
1409 if (mCurMethodId == null) {
1410 return mNoBinding;
1411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001412
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001413 if (!mSystemReady) {
1414 // If the system is not yet ready, we shouldn't be running third
1415 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001416 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1417 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1421 if (info == null) {
1422 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001425 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1428 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001429 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1430 com.android.internal.R.string.input_method_binding_label);
1431 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1432 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001433 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001434 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1435 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 mLastBindTime = SystemClock.uptimeMillis();
1437 mHaveConnection = true;
1438 mCurId = info.getId();
1439 mCurToken = new Binder();
1440 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001441 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 mIWindowManager.addWindowToken(mCurToken,
1443 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1444 } catch (RemoteException e) {
1445 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001446 return new InputBindResult(null, null, mCurId, mCurSeq,
1447 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 } else {
1449 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001450 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 + mCurIntent);
1452 }
1453 return null;
1454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
satoke7c6998e2011-06-03 17:57:59 +09001456 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001458 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001459 if (!calledFromValidUser()) {
1460 return null;
1461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 synchronized (mMethodMap) {
1463 final long ident = Binder.clearCallingIdentity();
1464 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001465 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 } finally {
1467 Binder.restoreCallingIdentity(ident);
1468 }
1469 }
1470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
satoke7c6998e2011-06-03 17:57:59 +09001472 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 public void finishInput(IInputMethodClient client) {
1474 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001475
satoke7c6998e2011-06-03 17:57:59 +09001476 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 public void onServiceConnected(ComponentName name, IBinder service) {
1478 synchronized (mMethodMap) {
1479 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1480 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001481 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001483 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001484 return;
1485 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001486 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001487 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1488 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001490 clearClientSessionLocked(mCurClient);
1491 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
1493 }
1494 }
1495 }
1496
Jeff Brownc28867a2013-03-26 15:42:39 -07001497 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1498 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 synchronized (mMethodMap) {
1500 if (mCurMethod != null && method != null
1501 && mCurMethod.asBinder() == method.asBinder()) {
1502 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001503 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001505 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001506 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 if (res.method != null) {
1508 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1509 MSG_BIND_METHOD, mCurClient.client, res));
1510 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001511 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513 }
1514 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001515
1516 // Session abandoned. Close its associated input channel.
1517 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001520 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001521 if (mVisibleBound) {
1522 mContext.unbindService(mVisibleConnection);
1523 mVisibleBound = false;
1524 }
1525
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001526 if (mHaveConnection) {
1527 mContext.unbindService(this);
1528 mHaveConnection = false;
1529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001531 if (mCurToken != null) {
1532 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001534 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001535 // The current IME is shown. Hence an IME switch (transition) is happening.
1536 mWindowManagerService.saveLastInputMethodWindowForTransition();
1537 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001538 mIWindowManager.removeWindowToken(mCurToken);
1539 } catch (RemoteException e) {
1540 }
1541 mCurToken = null;
1542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
The Android Open Source Project10592532009-03-18 17:39:46 -07001544 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001545 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001547 if (reportToClient && mCurClient != null) {
1548 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1549 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1550 }
1551 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001552
1553 void requestClientSessionLocked(ClientState cs) {
1554 if (!cs.sessionRequested) {
1555 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1556 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1557 cs.sessionRequested = true;
1558 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1559 MSG_CREATE_SESSION, mCurMethod, channels[1],
1560 new MethodCallback(this, mCurMethod, channels[0])));
1561 }
1562 }
1563
1564 void clearClientSessionLocked(ClientState cs) {
1565 finishSessionLocked(cs.curSession);
1566 cs.curSession = null;
1567 cs.sessionRequested = false;
1568 }
1569
1570 private void finishSessionLocked(SessionState sessionState) {
1571 if (sessionState != null) {
1572 if (sessionState.session != null) {
1573 try {
1574 sessionState.session.finishSession();
1575 } catch (RemoteException e) {
1576 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001577 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001578 }
1579 sessionState.session = null;
1580 }
1581 if (sessionState.channel != null) {
1582 sessionState.channel.dispose();
1583 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001584 }
1585 }
1586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001587
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001588 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 if (mCurMethod != null) {
1590 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001591 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001593
Jeff Brownc28867a2013-03-26 15:42:39 -07001594 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001595 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 mCurMethod = null;
1597 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001598 if (mStatusBar != null) {
1599 mStatusBar.setIconVisibility("ime", false);
1600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
satoke7c6998e2011-06-03 17:57:59 +09001603 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 public void onServiceDisconnected(ComponentName name) {
1605 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001606 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 + " mCurIntent=" + mCurIntent);
1608 if (mCurMethod != null && mCurIntent != null
1609 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001610 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 // We consider this to be a new bind attempt, since the system
1612 // should now try to restart the service for us.
1613 mLastBindTime = SystemClock.uptimeMillis();
1614 mShowRequested = mInputShown;
1615 mInputShown = false;
1616 if (mCurClient != null) {
1617 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1618 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1619 }
1620 }
1621 }
1622 }
1623
satokf9f01002011-05-19 21:31:50 +09001624 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1626 long ident = Binder.clearCallingIdentity();
1627 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001629 if (!calledWithValidToken(token)) {
1630 final int uid = Binder.getCallingUid();
1631 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1632 + " token:" + token);
1633 return;
1634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001637 if (mStatusBar != null) {
1638 mStatusBar.setIconVisibility("ime", false);
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001642 CharSequence contentDescription = null;
1643 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001644 // Use PackageManager to load label
1645 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001646 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001647 mIPackageManager.getApplicationInfo(packageName, 0,
1648 mSettings.getCurrentUserId()));
1649 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001650 /* ignore */
1651 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001652 if (mStatusBar != null) {
1653 mStatusBar.setIcon("ime", packageName, iconId, 0,
1654 contentDescription != null
1655 ? contentDescription.toString() : null);
1656 mStatusBar.setIconVisibility("ime", true);
1657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659 }
1660 } finally {
1661 Binder.restoreCallingIdentity(ident);
1662 }
1663 }
1664
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001665 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001666 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001667 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001668 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001669 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001670 if (mWindowManagerService.isHardKeyboardAvailable()) {
1671 // When physical keyboard is attached, we show the ime switcher (or notification if
1672 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1673 // exists in the IME switcher dialog. Might be OK to remove this condition once
1674 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1675 return true;
1676 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001677 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001678
1679 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1680 final int N = imis.size();
1681 if (N > 2) return true;
1682 if (N < 1) return false;
1683 int nonAuxCount = 0;
1684 int auxCount = 0;
1685 InputMethodSubtype nonAuxSubtype = null;
1686 InputMethodSubtype auxSubtype = null;
1687 for(int i = 0; i < N; ++i) {
1688 final InputMethodInfo imi = imis.get(i);
1689 final List<InputMethodSubtype> subtypes =
1690 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1691 final int subtypeCount = subtypes.size();
1692 if (subtypeCount == 0) {
1693 ++nonAuxCount;
1694 } else {
1695 for (int j = 0; j < subtypeCount; ++j) {
1696 final InputMethodSubtype subtype = subtypes.get(j);
1697 if (!subtype.isAuxiliary()) {
1698 ++nonAuxCount;
1699 nonAuxSubtype = subtype;
1700 } else {
1701 ++auxCount;
1702 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001703 }
1704 }
satok7cfc0ed2011-06-20 21:29:36 +09001705 }
1706 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001707 if (nonAuxCount > 1 || auxCount > 1) {
1708 return true;
1709 } else if (nonAuxCount == 1 && auxCount == 1) {
1710 if (nonAuxSubtype != null && auxSubtype != null
1711 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1712 || auxSubtype.overridesImplicitlyEnabledSubtype()
1713 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1714 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1715 return false;
1716 }
1717 return true;
1718 }
1719 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001720 }
1721
John Spurlocke0980502013-10-25 11:59:29 -04001722 private boolean isKeyguardLocked() {
1723 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1724 }
1725
satokdbf29502011-08-25 15:28:23 +09001726 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001727 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001728 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001729 if (!calledWithValidToken(token)) {
1730 final int uid = Binder.getCallingUid();
1731 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1732 + " token:" + token);
1733 return;
1734 }
1735
1736 synchronized (mMethodMap) {
1737 mImeWindowVis = vis;
1738 mBackDisposition = backDisposition;
1739 updateSystemUiLocked(token, vis, backDisposition);
1740 }
1741 }
1742
1743 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1744 synchronized (mMethodMap) {
1745 updateSystemUiLocked(token, vis, backDisposition);
1746 }
1747 }
1748
1749 // Caution! This method is called in this class. Handle multi-user carefully
1750 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1751 if (!calledWithValidToken(token)) {
1752 final int uid = Binder.getCallingUid();
1753 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1754 + " token:" + token);
1755 return;
1756 }
1757
1758 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1759 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001760 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001761 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001762 // apply policy for binder calls
1763 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1764 vis = 0;
satok06487a52010-10-29 11:37:18 +09001765 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001766 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1767 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1768 if (mStatusBar != null) {
1769 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1770 needsToShowImeSwitcher);
1771 }
1772 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1773 if (imi != null && needsToShowImeSwitcher) {
1774 // Used to load label
1775 final CharSequence title = mRes.getText(
1776 com.android.internal.R.string.select_input_method);
1777 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1778 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001779 mImeSwitcherNotification.setContentTitle(title)
1780 .setContentText(summary)
1781 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001782 if ((mNotificationManager != null)
1783 && !mWindowManagerService.hasNavigationBar()) {
1784 if (DEBUG) {
1785 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001786 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001787 mNotificationManager.notifyAsUser(null,
1788 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001789 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001790 mNotificationShown = true;
1791 }
1792 } else {
1793 if (mNotificationShown && mNotificationManager != null) {
1794 if (DEBUG) {
1795 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001796 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001797 mNotificationManager.cancelAsUser(null,
1798 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1799 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001800 }
satok06487a52010-10-29 11:37:18 +09001801 }
1802 } finally {
1803 Binder.restoreCallingIdentity(ident);
1804 }
1805 }
1806
satoke7c6998e2011-06-03 17:57:59 +09001807 @Override
satokf9f01002011-05-19 21:31:50 +09001808 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001809 if (!calledFromValidUser()) {
1810 return;
1811 }
satokf9f01002011-05-19 21:31:50 +09001812 synchronized (mMethodMap) {
1813 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1814 for (int i = 0; i < spans.length; ++i) {
1815 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001816 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001817 mSecureSuggestionSpans.put(ss, currentImi);
1818 }
1819 }
1820 }
1821 }
1822
satoke7c6998e2011-06-03 17:57:59 +09001823 @Override
satokf9f01002011-05-19 21:31:50 +09001824 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001825 if (!calledFromValidUser()) {
1826 return false;
1827 }
satokf9f01002011-05-19 21:31:50 +09001828 synchronized (mMethodMap) {
1829 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1830 // TODO: Do not send the intent if the process of the targetImi is already dead.
1831 if (targetImi != null) {
1832 final String[] suggestions = span.getSuggestions();
1833 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001834 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001835 final Intent intent = new Intent();
1836 // Ensures that only a class in the original IME package will receive the
1837 // notification.
satok42c5a162011-05-26 16:46:14 +09001838 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001839 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1840 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1841 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1842 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001843 final long ident = Binder.clearCallingIdentity();
1844 try {
1845 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1846 } finally {
1847 Binder.restoreCallingIdentity(ident);
1848 }
satokf9f01002011-05-19 21:31:50 +09001849 return true;
1850 }
1851 }
1852 return false;
1853 }
1854
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001855 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001856 updateInputMethodsFromSettingsLocked(enabledMayChange);
1857 updateKeyboardFromSettingsLocked();
1858 }
1859
1860 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001861 if (enabledMayChange) {
1862 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1863 for (int i=0; i<enabled.size(); i++) {
1864 // We allow the user to select "disabled until used" apps, so if they
1865 // are enabling one of those here we now need to make it enabled.
1866 InputMethodInfo imm = enabled.get(i);
1867 try {
1868 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1869 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1870 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001871 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001872 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001873 if (DEBUG) {
1874 Slog.d(TAG, "Update state(" + imm.getId()
1875 + "): DISABLED_UNTIL_USED -> DEFAULT");
1876 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001877 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1878 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001879 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1880 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001881 }
1882 } catch (RemoteException e) {
1883 }
1884 }
1885 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001886 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1887 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1888 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1889 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001890 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001891 // There is no input method selected, try to choose new applicable input method.
1892 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001893 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001894 }
1895 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001897 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001899 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001900 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001901 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
satokf3db1af2010-11-23 13:34:33 +09001903 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001904 } else {
1905 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001906 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001907 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001909 // Here is not the perfect place to reset the switching controller. Ideally
1910 // mSwitchingController and mSettings should be able to share the same state.
1911 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1912 // the same enabled IMEs list.
1913 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001914
1915 }
1916
1917 public void updateKeyboardFromSettingsLocked() {
1918 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1919 if (mSwitchingDialog != null
1920 && mSwitchingDialogTitleView != null
1921 && mSwitchingDialog.isShowing()) {
1922 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1923 com.android.internal.R.id.hard_keyboard_switch);
1924 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001927
satokab751aa2010-09-14 19:17:36 +09001928 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 InputMethodInfo info = mMethodMap.get(id);
1930 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001931 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001934 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001935 // We have already made sure that the package name belongs to the application's UID.
1936 // No further UID check is required.
1937 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1938 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001939 }
1940 }
1941
satokd81e9502012-05-21 12:58:45 +09001942 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001944 final int subtypeCount = info.getSubtypeCount();
1945 if (subtypeCount <= 0) {
1946 return;
satokcd7cd292010-11-20 15:46:23 +09001947 }
satokd81e9502012-05-21 12:58:45 +09001948 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1949 final InputMethodSubtype newSubtype;
1950 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1951 newSubtype = info.getSubtypeAt(subtypeId);
1952 } else {
1953 // If subtype is null, try to find the most applicable one from
1954 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001955 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001956 }
1957 if (newSubtype == null || oldSubtype == null) {
1958 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1959 + ", new subtype = " + newSubtype);
1960 return;
1961 }
1962 if (newSubtype != oldSubtype) {
1963 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1964 if (mCurMethod != null) {
1965 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001966 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001967 mCurMethod.changeInputMethodSubtype(newSubtype);
1968 } catch (RemoteException e) {
1969 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001970 }
1971 }
1972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 return;
1974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
satokd81e9502012-05-21 12:58:45 +09001976 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 final long ident = Binder.clearCallingIdentity();
1978 try {
satokab751aa2010-09-14 19:17:36 +09001979 // Set a subtype to this input method.
1980 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001981 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1982 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1983 // because mCurMethodId is stored as a history in
1984 // setSelectedInputMethodAndSubtypeLocked().
1985 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986
1987 if (ActivityManagerNative.isSystemReady()) {
1988 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001989 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001991 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001993 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 } finally {
1995 Binder.restoreCallingIdentity(ident);
1996 }
1997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001998
satok42c5a162011-05-26 16:46:14 +09001999 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002000 public boolean showSoftInput(IInputMethodClient client, int flags,
2001 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002002 if (!calledFromValidUser()) {
2003 return false;
2004 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002005 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 long ident = Binder.clearCallingIdentity();
2007 try {
2008 synchronized (mMethodMap) {
2009 if (mCurClient == null || client == null
2010 || mCurClient.client.asBinder() != client.asBinder()) {
2011 try {
2012 // We need to check if this is the current client with
2013 // focus in the window manager, to allow this call to
2014 // be made before input is started in it.
2015 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002016 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002017 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
2019 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002020 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002024 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002025 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027 } finally {
2028 Binder.restoreCallingIdentity(ident);
2029 }
2030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002031
The Android Open Source Project4df24232009-03-05 14:34:35 -08002032 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 mShowRequested = true;
2034 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2035 mShowExplicitlyRequested = true;
2036 }
2037 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2038 mShowExplicitlyRequested = true;
2039 mShowForced = true;
2040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041
Dianne Hackborncc278702009-09-02 23:07:23 -07002042 if (!mSystemReady) {
2043 return false;
2044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002045
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002048 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002049 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2050 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2051 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002053 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002054 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002055 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002056 | Context.BIND_TREAT_LIKE_ACTIVITY
2057 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002058 mVisibleBound = true;
2059 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002060 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002062 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 // The client has asked to have the input method shown, but
2064 // we have been sitting here too long with a connection to the
2065 // service and no interface received, so let's disconnect/connect
2066 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002069 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002071 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002072 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002073 } else {
2074 if (DEBUG) {
2075 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2076 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002079
The Android Open Source Project4df24232009-03-05 14:34:35 -08002080 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
satok42c5a162011-05-26 16:46:14 +09002083 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 public boolean hideSoftInput(IInputMethodClient client, int flags,
2085 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002086 if (!calledFromValidUser()) {
2087 return false;
2088 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002089 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 long ident = Binder.clearCallingIdentity();
2091 try {
2092 synchronized (mMethodMap) {
2093 if (mCurClient == null || client == null
2094 || mCurClient.client.asBinder() != client.asBinder()) {
2095 try {
2096 // We need to check if this is the current client with
2097 // focus in the window manager, to allow this call to
2098 // be made before input is started in it.
2099 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002100 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2101 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
2104 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002105 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002108
Joe Onorato8a9b2202010-02-26 18:56:32 -08002109 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002110 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112 } finally {
2113 Binder.restoreCallingIdentity(ident);
2114 }
2115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002116
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2119 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002120 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 -08002121 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002124 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 -08002125 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002127
2128 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2129 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2130 // to be updated with the new value sent from IME process. Even in such a transient state
2131 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2132 // application process as a valid request, and have even promised such a behavior with CTS
2133 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2134 // IMMS#InputShown indicates that the software keyboard is shown.
2135 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2136 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2137 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002138 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002139 if (shouldHideSoftInput) {
2140 // The IME will report its visible state again after the following message finally
2141 // delivered to the IME process as an IPC. Hence the inconsistency between
2142 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2143 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2145 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2146 res = true;
2147 } else {
2148 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002150 if (mHaveConnection && mVisibleBound) {
2151 mContext.unbindService(mVisibleConnection);
2152 mVisibleBound = false;
2153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 mInputShown = false;
2155 mShowRequested = false;
2156 mShowExplicitlyRequested = false;
2157 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002158 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002160
satok42c5a162011-05-26 16:46:14 +09002161 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002162 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2163 int controlFlags, int softInputMode, int windowFlags,
2164 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002165 // Needs to check the validity before clearing calling identity
2166 final boolean calledFromValidUser = calledFromValidUser();
2167
Dianne Hackborn7663d802012-02-24 13:08:49 -08002168 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 long ident = Binder.clearCallingIdentity();
2170 try {
2171 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002172 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002173 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002175 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002176
Dianne Hackborn7663d802012-02-24 13:08:49 -08002177 ClientState cs = mClients.get(client.asBinder());
2178 if (cs == null) {
2179 throw new IllegalArgumentException("unknown client "
2180 + client.asBinder());
2181 }
2182
2183 try {
2184 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2185 // Check with the window manager to make sure this client actually
2186 // has a window with focus. If not, reject. This is thread safe
2187 // because if the focus changes some time before or after, the
2188 // next client receiving focus that has any interest in input will
2189 // be calling through here after that change happens.
2190 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2191 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2192 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002194 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002196
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002197 if (!calledFromValidUser) {
2198 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2199 Slog.w(TAG, "If you want to interect with IME, you need "
2200 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2201 hideCurrentInputLocked(0, null);
2202 return null;
2203 }
2204
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002205 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002206 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002207 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002208 if (attribute != null) {
2209 return startInputUncheckedLocked(cs, inputContext, attribute,
2210 controlFlags);
2211 }
2212 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002213 }
2214 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002215
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002216 // Should we auto-show the IME even if the caller has not
2217 // specified what should be done with it?
2218 // We only do this automatically if the window can resize
2219 // to accommodate the IME (so what the user sees will give
2220 // them good context without input information being obscured
2221 // by the IME) or if running on a large screen where there
2222 // is more room for the target window + IME.
2223 final boolean doAutoShow =
2224 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2225 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2226 || mRes.getConfiguration().isLayoutSizeAtLeast(
2227 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002228 final boolean isTextEditor =
2229 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2230
2231 // We want to start input before showing the IME, but after closing
2232 // it. We want to do this after closing it to help the IME disappear
2233 // more quickly (not get stuck behind it initializing itself for the
2234 // new focused input, even if its window wants to hide the IME).
2235 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002239 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2241 // There is no focus view, and this window will
2242 // be behind any soft input window, so hide the
2243 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002244 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002245 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002247 } else if (isTextEditor && doAutoShow && (softInputMode &
2248 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 // There is a focus view, and we are navigating forward
2250 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002251 // We only do this automatically if the window can resize
2252 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002253 // them good context without input information being obscured
2254 // by the IME) or if running on a large screen where there
2255 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002256 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002257 if (attribute != null) {
2258 res = startInputUncheckedLocked(cs, inputContext, attribute,
2259 controlFlags);
2260 didStart = true;
2261 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002262 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 }
2264 break;
2265 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2266 // Do nothing.
2267 break;
2268 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2269 if ((softInputMode &
2270 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002271 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002272 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
2274 break;
2275 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002276 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002277 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 break;
2279 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2280 if ((softInputMode &
2281 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 if (attribute != null) {
2284 res = startInputUncheckedLocked(cs, inputContext, attribute,
2285 controlFlags);
2286 didStart = true;
2287 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002288 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
2290 break;
2291 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002292 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002293 if (attribute != null) {
2294 res = startInputUncheckedLocked(cs, inputContext, attribute,
2295 controlFlags);
2296 didStart = true;
2297 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002298 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 break;
2300 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301
2302 if (!didStart && attribute != null) {
2303 res = startInputUncheckedLocked(cs, inputContext, attribute,
2304 controlFlags);
2305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 }
2307 } finally {
2308 Binder.restoreCallingIdentity(ident);
2309 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002310
2311 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002313
satok42c5a162011-05-26 16:46:14 +09002314 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002315 public void showInputMethodPickerFromClient(
2316 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 if (!calledFromValidUser()) {
2318 return;
2319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 synchronized (mMethodMap) {
2321 if (mCurClient == null || client == null
2322 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002323 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002324 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 }
2326
satok440aab52010-11-25 09:43:11 +09002327 // Always call subtype picker, because subtype picker is a superset of input method
2328 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002329 mHandler.sendMessage(mCaller.obtainMessageI(
2330 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002331 }
2332 }
2333
satok42c5a162011-05-26 16:46:14 +09002334 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002336 if (!calledFromValidUser()) {
2337 return;
2338 }
satok28203512010-11-24 11:06:49 +09002339 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2340 }
2341
satok42c5a162011-05-26 16:46:14 +09002342 @Override
satok28203512010-11-24 11:06:49 +09002343 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002344 if (!calledFromValidUser()) {
2345 return;
2346 }
satok28203512010-11-24 11:06:49 +09002347 synchronized (mMethodMap) {
2348 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002349 setInputMethodWithSubtypeIdLocked(token, id,
2350 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2351 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002352 } else {
2353 setInputMethod(token, id);
2354 }
2355 }
satokab751aa2010-09-14 19:17:36 +09002356 }
2357
satok42c5a162011-05-26 16:46:14 +09002358 @Override
satokb416a712010-11-25 20:42:14 +09002359 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002360 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002361 if (!calledFromValidUser()) {
2362 return;
2363 }
satokb416a712010-11-25 20:42:14 +09002364 synchronized (mMethodMap) {
2365 if (mCurClient == null || client == null
2366 || mCurClient.client.asBinder() != client.asBinder()) {
2367 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2368 }
satok7fee71f2010-12-17 18:54:26 +09002369 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2370 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002371 }
2372 }
2373
satok4fc87d62011-05-20 16:13:43 +09002374 @Override
satok735cf382010-11-11 20:40:09 +09002375 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002376 if (!calledFromValidUser()) {
2377 return false;
2378 }
satok735cf382010-11-11 20:40:09 +09002379 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002380 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002381 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002382 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002383 lastImi = mMethodMap.get(lastIme.first);
2384 } else {
2385 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002386 }
satok4fc87d62011-05-20 16:13:43 +09002387 String targetLastImiId = null;
2388 int subtypeId = NOT_A_SUBTYPE_ID;
2389 if (lastIme != null && lastImi != null) {
2390 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2391 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2392 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2393 : mCurrentSubtype.hashCode();
2394 // If the last IME is the same as the current IME and the last subtype is not
2395 // defined, there is no need to switch to the last IME.
2396 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2397 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002398 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002399 }
2400 }
2401
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002402 if (TextUtils.isEmpty(targetLastImiId)
2403 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002404 // This is a safety net. If the currentSubtype can't be added to the history
2405 // and the framework couldn't find the last ime, we will make the last ime be
2406 // the most applicable enabled keyboard subtype of the system imes.
2407 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2408 if (enabled != null) {
2409 final int N = enabled.size();
2410 final String locale = mCurrentSubtype == null
2411 ? mRes.getConfiguration().locale.toString()
2412 : mCurrentSubtype.getLocale();
2413 for (int i = 0; i < N; ++i) {
2414 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002415 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002416 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002417 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2418 InputMethodUtils.getSubtypes(imi),
2419 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002420 if (keyboardSubtype != null) {
2421 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002422 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002423 imi, keyboardSubtype.hashCode());
2424 if(keyboardSubtype.getLocale().equals(locale)) {
2425 break;
2426 }
2427 }
2428 }
2429 }
2430 }
2431 }
2432
2433 if (!TextUtils.isEmpty(targetLastImiId)) {
2434 if (DEBUG) {
2435 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2436 + ", from: " + mCurMethodId + ", " + subtypeId);
2437 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002438 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002439 return true;
2440 } else {
2441 return false;
2442 }
satok735cf382010-11-11 20:40:09 +09002443 }
2444 }
2445
satoke7c6998e2011-06-03 17:57:59 +09002446 @Override
satok688bd472012-02-09 20:09:17 +09002447 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002448 if (!calledFromValidUser()) {
2449 return false;
2450 }
satok688bd472012-02-09 20:09:17 +09002451 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002452 if (!calledWithValidToken(token)) {
2453 final int uid = Binder.getCallingUid();
2454 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2455 + " token:" + token);
2456 return false;
2457 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002458 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002459 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2460 if (nextSubtype == null) {
2461 return false;
2462 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002463 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2464 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002465 return true;
2466 }
2467 }
2468
2469 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002470 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2471 if (!calledFromValidUser()) {
2472 return false;
2473 }
2474 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002475 if (!calledWithValidToken(token)) {
2476 final int uid = Binder.getCallingUid();
2477 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2478 + "token. uid:" + uid + " token:" + token);
2479 return false;
2480 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002481 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002482 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2483 if (nextSubtype == null) {
2484 return false;
2485 }
2486 return true;
2487 }
2488 }
2489
2490 @Override
satok68f1b782011-04-11 14:26:04 +09002491 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002492 if (!calledFromValidUser()) {
2493 return null;
2494 }
satok68f1b782011-04-11 14:26:04 +09002495 synchronized (mMethodMap) {
2496 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2497 // TODO: Handle the case of the last IME with no subtypes
2498 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2499 || TextUtils.isEmpty(lastIme.second)) return null;
2500 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2501 if (lastImi == null) return null;
2502 try {
2503 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002504 final int lastSubtypeId =
2505 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002506 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2507 return null;
2508 }
2509 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002510 } catch (NumberFormatException e) {
2511 return null;
2512 }
2513 }
2514 }
2515
satoke7c6998e2011-06-03 17:57:59 +09002516 @Override
satokee5e77c2011-09-02 18:50:15 +09002517 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002518 if (!calledFromValidUser()) {
2519 return;
2520 }
satok91e88122011-07-18 11:11:42 +09002521 // By this IPC call, only a process which shares the same uid with the IME can add
2522 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002523 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002524 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002525 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002526 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002527 final String[] packageInfos;
2528 try {
2529 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2530 } catch (RemoteException e) {
2531 Slog.e(TAG, "Failed to get package infos");
2532 return;
2533 }
satok91e88122011-07-18 11:11:42 +09002534 if (packageInfos != null) {
2535 final int packageNum = packageInfos.length;
2536 for (int i = 0; i < packageNum; ++i) {
2537 if (packageInfos[i].equals(imi.getPackageName())) {
2538 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002539 final long ident = Binder.clearCallingIdentity();
2540 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002541 buildInputMethodListLocked(mMethodList, mMethodMap,
2542 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002543 } finally {
2544 Binder.restoreCallingIdentity(ident);
2545 }
satokee5e77c2011-09-02 18:50:15 +09002546 return;
satok91e88122011-07-18 11:11:42 +09002547 }
2548 }
2549 }
satoke7c6998e2011-06-03 17:57:59 +09002550 }
satokee5e77c2011-09-02 18:50:15 +09002551 return;
satoke7c6998e2011-06-03 17:57:59 +09002552 }
2553
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002554 @Override
2555 public int getInputMethodWindowVisibleHeight() {
2556 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2557 }
2558
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002559 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002560 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002561 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002562 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002563 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002564 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002565 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2566 if (DEBUG) {
2567 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2568 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2569 + " actual: " + sequenceNumber);
2570 }
2571 return;
2572 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002573 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2574 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002575 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002576 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002577 }
2578 }
2579
satok28203512010-11-24 11:06:49 +09002580 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002582 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2583 }
2584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002586 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2587 if (token == null) {
2588 if (mContext.checkCallingOrSelfPermission(
2589 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2590 != PackageManager.PERMISSION_GRANTED) {
2591 throw new SecurityException(
2592 "Using null token requires permission "
2593 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002595 } else if (mCurToken != token) {
2596 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2597 + " token: " + token);
2598 return;
2599 }
2600
2601 final long ident = Binder.clearCallingIdentity();
2602 try {
2603 setInputMethodLocked(id, subtypeId);
2604 } finally {
2605 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 }
2607 }
2608
satok42c5a162011-05-26 16:46:14 +09002609 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002611 if (!calledFromValidUser()) {
2612 return;
2613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002615 if (!calledWithValidToken(token)) {
2616 final int uid = Binder.getCallingUid();
2617 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2618 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 return;
2620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 long ident = Binder.clearCallingIdentity();
2622 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002623 hideCurrentInputLocked(flags, null);
2624 } finally {
2625 Binder.restoreCallingIdentity(ident);
2626 }
2627 }
2628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002629
satok42c5a162011-05-26 16:46:14 +09002630 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002631 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002632 if (!calledFromValidUser()) {
2633 return;
2634 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002635 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002636 if (!calledWithValidToken(token)) {
2637 final int uid = Binder.getCallingUid();
2638 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2639 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002640 return;
2641 }
2642 long ident = Binder.clearCallingIdentity();
2643 try {
2644 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 } finally {
2646 Binder.restoreCallingIdentity(ident);
2647 }
2648 }
2649 }
2650
2651 void setEnabledSessionInMainThread(SessionState session) {
2652 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002653 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002655 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002656 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 } catch (RemoteException e) {
2658 }
2659 }
2660 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002661 if (mEnabledSession != null && mEnabledSession.session != null) {
2662 try {
2663 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2664 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2665 } catch (RemoteException e) {
2666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
2668 }
2669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002670
satok42c5a162011-05-26 16:46:14 +09002671 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002675 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002676 final boolean showAuxSubtypes;
2677 switch (msg.arg1) {
2678 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2679 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2680 // implemented so that auxiliary subtypes will be excluded when the soft
2681 // keyboard is invisible.
2682 showAuxSubtypes = mInputShown;
2683 break;
2684 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2685 showAuxSubtypes = true;
2686 break;
2687 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2688 showAuxSubtypes = false;
2689 break;
2690 default:
2691 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2692 return false;
2693 }
2694 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002695 return true;
2696
satok47a44912010-10-06 16:03:58 +09002697 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002698 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002699 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002700 args.recycle();
satok217f5482010-12-15 05:19:19 +09002701 return true;
2702
2703 case MSG_SHOW_IM_CONFIG:
2704 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002705 return true;
2706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 case MSG_UNBIND_INPUT:
2710 try {
2711 ((IInputMethod)msg.obj).unbindInput();
2712 } catch (RemoteException e) {
2713 // There is nothing interesting about the method dying.
2714 }
2715 return true;
2716 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002717 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 try {
2719 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2720 } catch (RemoteException e) {
2721 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002722 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 return true;
2724 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002725 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002727 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002728 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002729 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 } catch (RemoteException e) {
2731 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002732 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 return true;
2734 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002735 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002737 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002738 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002739 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 } catch (RemoteException e) {
2741 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002742 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 return true;
2744 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002745 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002747 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2749 } catch (RemoteException e) {
2750 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002751 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002753 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002754 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002755 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002756 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002758 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002760 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002761 // Dispose the channel if the input method is not local to this process
2762 // because the remote proxy will get its own copy when unparceled.
2763 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002764 channel.dispose();
2765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002767 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002773 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 try {
2775 SessionState session = (SessionState)args.arg1;
2776 setEnabledSessionInMainThread(session);
2777 session.method.startInput((IInputContext)args.arg2,
2778 (EditorInfo)args.arg3);
2779 } catch (RemoteException e) {
2780 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002781 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 return true;
2783 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002784 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 try {
2786 SessionState session = (SessionState)args.arg1;
2787 setEnabledSessionInMainThread(session);
2788 session.method.restartInput((IInputContext)args.arg2,
2789 (EditorInfo)args.arg3);
2790 } catch (RemoteException e) {
2791 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002792 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 case MSG_UNBIND_METHOD:
2798 try {
2799 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2800 } catch (RemoteException e) {
2801 // There is nothing interesting about the last client dying.
2802 }
2803 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002804 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002805 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002806 IInputMethodClient client = (IInputMethodClient)args.arg1;
2807 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002809 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002811 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002812 } finally {
2813 // Dispose the channel if the input method is not local to this process
2814 // because the remote proxy will get its own copy when unparceled.
2815 if (res.channel != null && Binder.isProxy(client)) {
2816 res.channel.dispose();
2817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002819 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002821 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002822 case MSG_SET_ACTIVE:
2823 try {
2824 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2825 } catch (RemoteException e) {
2826 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2827 + ((ClientState)msg.obj).pid + " uid "
2828 + ((ClientState)msg.obj).uid);
2829 }
2830 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002831 case MSG_SET_INTERACTIVE:
2832 handleSetInteractive(msg.arg1 != 0);
2833 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002834 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2835 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002836 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002837 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002838 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002839 } catch (RemoteException e) {
2840 Slog.w(TAG, "Got RemoteException sending "
2841 + "setUserActionNotificationSequenceNumber("
2842 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002843 + clientState.pid + " uid "
2844 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002845 }
2846 return true;
2847 }
satok01038492012-04-09 21:08:27 +09002848
2849 // --------------------------------------------------------------
2850 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002851 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002852 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 }
2854 return false;
2855 }
2856
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002857 private void handleSetInteractive(final boolean interactive) {
2858 synchronized (mMethodMap) {
2859 mIsInteractive = interactive;
2860 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2861
2862 // Inform the current client of the change in active status
2863 if (mCurClient != null && mCurClient.client != null) {
2864 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2865 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2866 }
2867 }
2868 }
2869
satokdc9ddae2011-10-06 12:22:36 +09002870 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002871 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2872 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002873 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002874 if (DEBUG) {
2875 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2876 }
satok723a27e2010-11-11 14:58:11 +09002877 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002878 return true;
2879 }
2880
2881 return false;
2882 }
2883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002885 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002886 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002887 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002888 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 list.clear();
2891 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002892
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002893 // Use for queryIntentServicesAsUser
2894 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002896 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002898 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2899 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002900
satoke7c6998e2011-06-03 17:57:59 +09002901 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2902 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 for (int i = 0; i < services.size(); ++i) {
2904 ResolveInfo ri = services.get(i);
2905 ServiceInfo si = ri.serviceInfo;
2906 ComponentName compName = new ComponentName(si.packageName, si.name);
2907 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2908 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002909 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 + ": it does not require the permission "
2911 + android.Manifest.permission.BIND_INPUT_METHOD);
2912 continue;
2913 }
2914
Joe Onorato8a9b2202010-02-26 18:56:32 -08002915 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916
2917 try {
satoke7c6998e2011-06-03 17:57:59 +09002918 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002920 final String id = p.getId();
2921 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
2923 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002924 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002926 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002927 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002930
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002931 if (resetDefaultEnabledIme) {
2932 final ArrayList<InputMethodInfo> defaultEnabledIme =
2933 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2934 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2935 final InputMethodInfo imi = defaultEnabledIme.get(i);
2936 if (DEBUG) {
2937 Slog.d(TAG, "--- enable ime = " + imi);
2938 }
2939 setInputMethodEnabledLocked(imi.getId(), true);
2940 }
2941 }
2942
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002943 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002944 if (!TextUtils.isEmpty(defaultImiId)) {
2945 if (!map.containsKey(defaultImiId)) {
2946 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2947 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002948 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002949 }
2950 } else {
2951 // Double check that the default IME is certainly enabled.
2952 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002953 }
2954 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002955 // Here is not the perfect place to reset the switching controller. Ideally
2956 // mSwitchingController and mSettings should be able to share the same state.
2957 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2958 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002959 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002963
satok217f5482010-12-15 05:19:19 +09002964 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002965 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002966 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002967 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2968 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002969 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002970 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002971 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002972 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002973 }
2974
2975 private void showConfigureInputMethods() {
2976 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2977 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2978 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2979 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002980 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002981 }
2982
satok2c93efc2012-04-02 19:33:47 +09002983 private boolean isScreenLocked() {
2984 return mKeyguardManager != null
2985 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2986 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002987
Seigo Nonaka14e13912015-05-06 21:04:13 -07002988 private void showInputMethodMenu(boolean showAuxSubtypes) {
2989 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002992 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002993
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002994 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002995 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002996 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002997
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002998 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002999 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003000 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3001 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003002 if (immis == null || immis.size() == 0) {
3003 return;
3004 }
3005
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003006 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007
satok688bd472012-02-09 20:09:17 +09003008 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003009 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07003010 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003011
satokc3690562012-01-10 20:14:43 +09003012 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003013 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003014 if (currentSubtype != null) {
3015 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003016 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3017 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003018 }
3019 }
3020
Ken Wakasa761eb372011-03-04 19:06:18 +09003021 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003022 mIms = new InputMethodInfo[N];
3023 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003024 int checkedItem = 0;
3025 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003026 final ImeSubtypeListItem item = imList.get(i);
3027 mIms[i] = item.mImi;
3028 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003029 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003030 int subtypeId = mSubtypeIds[i];
3031 if ((subtypeId == NOT_A_SUBTYPE_ID)
3032 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3033 || (subtypeId == lastInputMethodSubtypeId)) {
3034 checkedItem = i;
3035 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003038
3039 final Context settingsContext = new ContextThemeWrapper(context,
3040 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3041
3042 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003043 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3044 @Override
3045 public void onCancel(DialogInterface dialog) {
3046 hideInputMethodMenu();
3047 }
3048 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003049
3050 final Context dialogContext = mDialogBuilder.getContext();
3051 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3052 com.android.internal.R.styleable.DialogPreference,
3053 com.android.internal.R.attr.alertDialogStyle, 0);
3054 final Drawable dialogIcon = a.getDrawable(
3055 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3056 a.recycle();
3057
3058 mDialogBuilder.setIcon(dialogIcon);
3059
3060 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3061 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003062 final View tv = inflater.inflate(
3063 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3064 mDialogBuilder.setCustomTitle(tv);
3065
3066 // Setup layout for a toggle switch of the hardware keyboard
3067 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003068 mSwitchingDialogTitleView
3069 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3070 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3071 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003072 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003073 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003074 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003075 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3076 @Override
3077 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003078 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003079 // Ensure that the input method dialog is dismissed when changing
3080 // the hardware keyboard state.
3081 hideInputMethodMenu();
3082 }
3083 });
3084
Alan Viverette505e3ab2014-11-24 15:22:11 -08003085 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003086 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3087 final OnClickListener choiceListener = new OnClickListener() {
3088 @Override
3089 public void onClick(final DialogInterface dialog, final int which) {
3090 synchronized (mMethodMap) {
3091 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3092 || mSubtypeIds.length <= which) {
3093 return;
satok01038492012-04-09 21:08:27 +09003094 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003095 final InputMethodInfo im = mIms[which];
3096 int subtypeId = mSubtypeIds[which];
3097 adapter.mCheckedItem = which;
3098 adapter.notifyDataSetChanged();
3099 hideInputMethodMenu();
3100 if (im != null) {
3101 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3102 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003103 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003104 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003106 }
3107 }
3108 };
3109 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003111 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003112 final OnClickListener positiveListener = new OnClickListener() {
3113 @Override
3114 public void onClick(DialogInterface dialog, int whichButton) {
3115 showConfigureInputMethods();
3116 }
3117 };
satok82beadf2010-12-27 19:03:06 +09003118 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003119 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003122 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 mSwitchingDialog.getWindow().setType(
3124 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003125 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3126 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003127 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003128 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 mSwitchingDialog.show();
3130 }
3131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003132
Ken Wakasa05dbb652011-08-22 15:22:43 +09003133 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3134 private final LayoutInflater mInflater;
3135 private final int mTextViewResourceId;
3136 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003137 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003138 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3139 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3140 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003141
Ken Wakasa05dbb652011-08-22 15:22:43 +09003142 mTextViewResourceId = textViewResourceId;
3143 mItemsList = itemsList;
3144 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003145 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003146 }
3147
3148 @Override
3149 public View getView(int position, View convertView, ViewGroup parent) {
3150 final View view = convertView != null ? convertView
3151 : mInflater.inflate(mTextViewResourceId, null);
3152 if (position < 0 || position >= mItemsList.size()) return view;
3153 final ImeSubtypeListItem item = mItemsList.get(position);
3154 final CharSequence imeName = item.mImeName;
3155 final CharSequence subtypeName = item.mSubtypeName;
3156 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3157 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3158 if (TextUtils.isEmpty(subtypeName)) {
3159 firstTextView.setText(imeName);
3160 secondTextView.setVisibility(View.GONE);
3161 } else {
3162 firstTextView.setText(subtypeName);
3163 secondTextView.setText(imeName);
3164 secondTextView.setVisibility(View.VISIBLE);
3165 }
3166 final RadioButton radioButton =
3167 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3168 radioButton.setChecked(position == mCheckedItem);
3169 return view;
3170 }
3171 }
3172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003174 synchronized (mMethodMap) {
3175 hideInputMethodMenuLocked();
3176 }
3177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003178
The Android Open Source Project10592532009-03-18 17:39:46 -07003179 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003180 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181
The Android Open Source Project10592532009-03-18 17:39:46 -07003182 if (mSwitchingDialog != null) {
3183 mSwitchingDialog.dismiss();
3184 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003186
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003187 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003188 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003189 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003193
satok42c5a162011-05-26 16:46:14 +09003194 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003196 // TODO: Make this work even for non-current users?
3197 if (!calledFromValidUser()) {
3198 return false;
3199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 synchronized (mMethodMap) {
3201 if (mContext.checkCallingOrSelfPermission(
3202 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3203 != PackageManager.PERMISSION_GRANTED) {
3204 throw new SecurityException(
3205 "Requires permission "
3206 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3207 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 long ident = Binder.clearCallingIdentity();
3210 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003211 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 } finally {
3213 Binder.restoreCallingIdentity(ident);
3214 }
3215 }
3216 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003217
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003218 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3219 // Make sure this is a valid input method.
3220 InputMethodInfo imm = mMethodMap.get(id);
3221 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003222 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003223 }
3224
satokd87c2592010-09-29 11:52:06 +09003225 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3226 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003227
satokd87c2592010-09-29 11:52:06 +09003228 if (enabled) {
3229 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3230 if (pair.first.equals(id)) {
3231 // We are enabling this input method, but it is already enabled.
3232 // Nothing to do. The previous state was enabled.
3233 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003234 }
3235 }
satokd87c2592010-09-29 11:52:06 +09003236 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3237 // Previous state was disabled.
3238 return false;
3239 } else {
3240 StringBuilder builder = new StringBuilder();
3241 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3242 builder, enabledInputMethodsList, id)) {
3243 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003244 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003245 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3246 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3247 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003248 }
3249 // Previous state was enabled.
3250 return true;
3251 } else {
3252 // We are disabling the input method but it is already disabled.
3253 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003254 return false;
3255 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003256 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003257 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003258
satok723a27e2010-11-11 14:58:11 +09003259 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3260 boolean setSubtypeOnly) {
3261 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003262 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003263
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003264 mCurUserActionNotificationSequenceNumber =
3265 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3266 if (DEBUG) {
3267 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3268 + mCurUserActionNotificationSequenceNumber);
3269 }
3270
3271 if (mCurClient != null && mCurClient.client != null) {
3272 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3273 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003274 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003275 }
3276
satok723a27e2010-11-11 14:58:11 +09003277 // Set Subtype here
3278 if (imi == null || subtypeId < 0) {
3279 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003280 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003281 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003282 if (subtypeId < imi.getSubtypeCount()) {
3283 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3284 mSettings.putSelectedSubtype(subtype.hashCode());
3285 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003286 } else {
3287 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003288 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003289 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003290 }
satokab751aa2010-09-14 19:17:36 +09003291 }
satok723a27e2010-11-11 14:58:11 +09003292
satok4c0e7152012-06-20 20:08:44 +09003293 // Workaround.
3294 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3295 // IMEs are not recognized and considered uninstalled.
3296 // Actually, we can't move everything after SystemReady because
3297 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3298 // the default IME here and try cheking the default IME again in systemReady().
3299 // TODO: Do nothing before system ready and implement a separated logic for
3300 // the encryption lock screen.
3301 // TODO: ASEC should be ready before IMMS is instantiated.
3302 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003303 // Set InputMethod here
3304 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3305 }
3306 }
3307
3308 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3309 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3310 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3311 // newDefaultIme is empty when there is no candidate for the selected IME.
3312 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3313 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3314 if (subtypeHashCode != null) {
3315 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003316 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003317 imi, Integer.valueOf(subtypeHashCode));
3318 } catch (NumberFormatException e) {
3319 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3320 }
3321 }
3322 }
3323 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003324 }
3325
satok4e4569d2010-11-19 18:45:53 +09003326 // If there are no selected shortcuts, tries finding the most applicable ones.
3327 private Pair<InputMethodInfo, InputMethodSubtype>
3328 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3329 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3330 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003331 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003332 boolean foundInSystemIME = false;
3333
3334 // Search applicable subtype for each InputMethodInfo
3335 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003336 final String imiId = imi.getId();
3337 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3338 continue;
3339 }
satokcd7cd292010-11-20 15:46:23 +09003340 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003341 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003342 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003343 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003344 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003345 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003346 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003347 }
satokdf31ae62011-01-15 06:19:44 +09003348 // 2. Search by the system locale from enabledSubtypes.
3349 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003350 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003351 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003352 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003353 }
satoka86f5e42011-09-02 17:12:42 +09003354 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003355 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003356 final ArrayList<InputMethodSubtype> subtypesForSearch =
3357 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003358 ? InputMethodUtils.getSubtypes(imi)
3359 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003360 // 4. Search by the current subtype's locale from all subtypes.
3361 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003362 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003363 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003364 }
3365 // 5. Search by the system locale from all subtypes.
3366 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003367 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003368 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003369 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003370 }
satokcd7cd292010-11-20 15:46:23 +09003371 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003372 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003373 // The current input method is the most applicable IME.
3374 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003375 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003376 break;
satok7599a7f2010-12-22 13:45:23 +09003377 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003378 // The system input method is 2nd applicable IME.
3379 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003380 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003381 if ((imi.getServiceInfo().applicationInfo.flags
3382 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3383 foundInSystemIME = true;
3384 }
satok4e4569d2010-11-19 18:45:53 +09003385 }
3386 }
3387 }
3388 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003389 if (mostApplicableIMI != null) {
3390 Slog.w(TAG, "Most applicable shortcut input method was:"
3391 + mostApplicableIMI.getId());
3392 if (mostApplicableSubtype != null) {
3393 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3394 + "," + mostApplicableSubtype.getMode() + ","
3395 + mostApplicableSubtype.getLocale());
3396 }
3397 }
satok4e4569d2010-11-19 18:45:53 +09003398 }
satokcd7cd292010-11-20 15:46:23 +09003399 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003400 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003401 } else {
3402 return null;
3403 }
3404 }
3405
satokab751aa2010-09-14 19:17:36 +09003406 /**
3407 * @return Return the current subtype of this input method.
3408 */
satok42c5a162011-05-26 16:46:14 +09003409 @Override
satokab751aa2010-09-14 19:17:36 +09003410 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 // TODO: Make this work even for non-current users?
3412 if (!calledFromValidUser()) {
3413 return null;
3414 }
3415 synchronized (mMethodMap) {
3416 return getCurrentInputMethodSubtypeLocked();
3417 }
3418 }
3419
3420 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003421 if (mCurMethodId == null) {
3422 return null;
3423 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003424 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003425 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3426 if (imi == null || imi.getSubtypeCount() == 0) {
3427 return null;
satok4e4569d2010-11-19 18:45:53 +09003428 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003430 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3431 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003432 if (subtypeId == NOT_A_SUBTYPE_ID) {
3433 // If there are no selected subtypes, the framework will try to find
3434 // the most applicable subtype from explicitly or implicitly enabled
3435 // subtypes.
3436 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003437 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003438 // If there is only one explicitly or implicitly enabled subtype,
3439 // just returns it.
3440 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3441 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3442 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003443 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003444 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003445 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003446 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003447 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003448 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3449 true);
satok4e4569d2010-11-19 18:45:53 +09003450 }
satok3ef8b292010-11-23 06:06:29 +09003451 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003452 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003453 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003454 }
3455 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003456 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003457 }
3458
satok4e4569d2010-11-19 18:45:53 +09003459 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003460 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003461 @Override
satok4e4569d2010-11-19 18:45:53 +09003462 public List getShortcutInputMethodsAndSubtypes() {
3463 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003464 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003465 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003466 // If there are no selected shortcut subtypes, the framework will try to find
3467 // the most applicable subtype from all subtypes whose mode is
3468 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003469 Pair<InputMethodInfo, InputMethodSubtype> info =
3470 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003472 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003473 ret.add(info.first);
3474 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003475 }
satok3da92232011-01-11 22:46:30 +09003476 return ret;
satokf3db1af2010-11-23 13:34:33 +09003477 }
satokf3db1af2010-11-23 13:34:33 +09003478 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3479 ret.add(imi);
3480 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3481 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003482 }
3483 }
satokf3db1af2010-11-23 13:34:33 +09003484 return ret;
satok4e4569d2010-11-19 18:45:53 +09003485 }
3486 }
3487
satok42c5a162011-05-26 16:46:14 +09003488 @Override
satokb66d2872010-11-10 01:04:04 +09003489 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003490 // TODO: Make this work even for non-current users?
3491 if (!calledFromValidUser()) {
3492 return false;
3493 }
satokb66d2872010-11-10 01:04:04 +09003494 synchronized (mMethodMap) {
3495 if (subtype != null && mCurMethodId != null) {
3496 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003497 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003498 if (subtypeId != NOT_A_SUBTYPE_ID) {
3499 setInputMethodLocked(mCurMethodId, subtypeId);
3500 return true;
3501 }
3502 }
3503 return false;
3504 }
3505 }
3506
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003507 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003508 private static class InputMethodFileManager {
3509 private static final String SYSTEM_PATH = "system";
3510 private static final String INPUT_METHOD_PATH = "inputmethod";
3511 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3512 private static final String NODE_SUBTYPES = "subtypes";
3513 private static final String NODE_SUBTYPE = "subtype";
3514 private static final String NODE_IMI = "imi";
3515 private static final String ATTR_ID = "id";
3516 private static final String ATTR_LABEL = "label";
3517 private static final String ATTR_ICON = "icon";
3518 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3519 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3520 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3521 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3522 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3523 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003524 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003525 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003526 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003527 if (methodMap == null) {
3528 throw new NullPointerException("methodMap is null");
3529 }
3530 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003531 final File systemDir = userId == UserHandle.USER_OWNER
3532 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3533 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003534 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003535 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003536 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3537 }
3538 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3539 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3540 if (!subtypeFile.exists()) {
3541 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003542 writeAdditionalInputMethodSubtypes(
3543 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003544 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003545 readAdditionalInputMethodSubtypes(
3546 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003547 }
3548 }
3549
3550 private void deleteAllInputMethodSubtypes(String imiId) {
3551 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003552 mAdditionalSubtypesMap.remove(imiId);
3553 writeAdditionalInputMethodSubtypes(
3554 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003555 }
3556 }
3557
3558 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003559 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003560 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003561 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003562 final int N = additionalSubtypes.length;
3563 for (int i = 0; i < N; ++i) {
3564 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003565 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003566 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003567 } else {
3568 Slog.w(TAG, "Duplicated subtype definition found: "
3569 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003570 }
3571 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003572 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3573 writeAdditionalInputMethodSubtypes(
3574 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003575 }
3576 }
3577
3578 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3579 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003580 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003581 }
3582 }
3583
3584 private static void writeAdditionalInputMethodSubtypes(
3585 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3586 HashMap<String, InputMethodInfo> methodMap) {
3587 // Safety net for the case that this function is called before methodMap is set.
3588 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3589 FileOutputStream fos = null;
3590 try {
3591 fos = subtypesFile.startWrite();
3592 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003593 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003594 out.startDocument(null, true);
3595 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3596 out.startTag(null, NODE_SUBTYPES);
3597 for (String imiId : allSubtypes.keySet()) {
3598 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3599 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3600 continue;
3601 }
3602 out.startTag(null, NODE_IMI);
3603 out.attribute(null, ATTR_ID, imiId);
3604 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3605 final int N = subtypesList.size();
3606 for (int i = 0; i < N; ++i) {
3607 final InputMethodSubtype subtype = subtypesList.get(i);
3608 out.startTag(null, NODE_SUBTYPE);
3609 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3610 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3611 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3612 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3613 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3614 out.attribute(null, ATTR_IS_AUXILIARY,
3615 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3616 out.endTag(null, NODE_SUBTYPE);
3617 }
3618 out.endTag(null, NODE_IMI);
3619 }
3620 out.endTag(null, NODE_SUBTYPES);
3621 out.endDocument();
3622 subtypesFile.finishWrite(fos);
3623 } catch (java.io.IOException e) {
3624 Slog.w(TAG, "Error writing subtypes", e);
3625 if (fos != null) {
3626 subtypesFile.failWrite(fos);
3627 }
3628 }
3629 }
3630
3631 private static void readAdditionalInputMethodSubtypes(
3632 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3633 if (allSubtypes == null || subtypesFile == null) return;
3634 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003635 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003636 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003637 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003638 int type = parser.getEventType();
3639 // Skip parsing until START_TAG
3640 while ((type = parser.next()) != XmlPullParser.START_TAG
3641 && type != XmlPullParser.END_DOCUMENT) {}
3642 String firstNodeName = parser.getName();
3643 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3644 throw new XmlPullParserException("Xml doesn't start with subtypes");
3645 }
3646 final int depth =parser.getDepth();
3647 String currentImiId = null;
3648 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3649 while (((type = parser.next()) != XmlPullParser.END_TAG
3650 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3651 if (type != XmlPullParser.START_TAG)
3652 continue;
3653 final String nodeName = parser.getName();
3654 if (NODE_IMI.equals(nodeName)) {
3655 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3656 if (TextUtils.isEmpty(currentImiId)) {
3657 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3658 continue;
3659 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003660 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003661 allSubtypes.put(currentImiId, tempSubtypesArray);
3662 } else if (NODE_SUBTYPE.equals(nodeName)) {
3663 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3664 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3665 continue;
3666 }
3667 final int icon = Integer.valueOf(
3668 parser.getAttributeValue(null, ATTR_ICON));
3669 final int label = Integer.valueOf(
3670 parser.getAttributeValue(null, ATTR_LABEL));
3671 final String imeSubtypeLocale =
3672 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3673 final String imeSubtypeMode =
3674 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3675 final String imeSubtypeExtraValue =
3676 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003677 final boolean isAuxiliary = "1".equals(String.valueOf(
3678 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003679 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3680 .setSubtypeNameResId(label)
3681 .setSubtypeIconResId(icon)
3682 .setSubtypeLocale(imeSubtypeLocale)
3683 .setSubtypeMode(imeSubtypeMode)
3684 .setSubtypeExtraValue(imeSubtypeExtraValue)
3685 .setIsAuxiliary(isAuxiliary)
3686 .build();
satoke7c6998e2011-06-03 17:57:59 +09003687 tempSubtypesArray.add(subtype);
3688 }
3689 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003690 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3691 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003692 return;
satoke7c6998e2011-06-03 17:57:59 +09003693 }
3694 }
3695 }
3696
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003697 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3698 @NonNull
3699 private final Handler mHandler;
3700
3701 LocalServiceImpl(@NonNull final Handler handler) {
3702 mHandler = handler;
3703 }
3704
3705 @Override
3706 public void setInteractive(boolean interactive) {
3707 // Do everything in handler so as not to block the caller.
3708 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3709 interactive ? 1 : 0, 0));
3710 }
3711 }
3712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 @Override
3714 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3715 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3716 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3719 + Binder.getCallingPid()
3720 + ", uid=" + Binder.getCallingUid());
3721 return;
3722 }
3723
3724 IInputMethod method;
3725 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 synchronized (mMethodMap) {
3730 p.println("Current Input Method Manager state:");
3731 int N = mMethodList.size();
3732 p.println(" Input Methods:");
3733 for (int i=0; i<N; i++) {
3734 InputMethodInfo info = mMethodList.get(i);
3735 p.println(" InputMethod #" + i + ":");
3736 info.dump(p, " ");
3737 }
3738 p.println(" Clients:");
3739 for (ClientState ci : mClients.values()) {
3740 p.println(" Client " + ci + ":");
3741 p.println(" client=" + ci.client);
3742 p.println(" inputContext=" + ci.inputContext);
3743 p.println(" sessionRequested=" + ci.sessionRequested);
3744 p.println(" curSession=" + ci.curSession);
3745 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003746 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003748 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3749 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3751 + " mBoundToMethod=" + mBoundToMethod);
3752 p.println(" mCurToken=" + mCurToken);
3753 p.println(" mCurIntent=" + mCurIntent);
3754 method = mCurMethod;
3755 p.println(" mCurMethod=" + mCurMethod);
3756 p.println(" mEnabledSession=" + mEnabledSession);
3757 p.println(" mShowRequested=" + mShowRequested
3758 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3759 + " mShowForced=" + mShowForced
3760 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003761 p.println(" mCurUserActionNotificationSequenceNumber="
3762 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003763 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003764 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003765 p.println(" mSwitchingController:");
3766 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003768
Jeff Brownb88102f2010-09-08 11:49:43 -07003769 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 pw.flush();
3772 try {
3773 client.client.asBinder().dump(fd, args);
3774 } catch (RemoteException e) {
3775 p.println("Input method client dead: " + e);
3776 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003777 } else {
3778 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003780
Jeff Brownb88102f2010-09-08 11:49:43 -07003781 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 pw.flush();
3784 try {
3785 method.asBinder().dump(fd, args);
3786 } catch (RemoteException e) {
3787 p.println("Input method service dead: " + e);
3788 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003789 } else {
3790 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792 }
3793}