blob: 66a23084b8d0c09d58ced13563908914e9087d2a [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
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700148 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
149 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
150 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int MSG_UNBIND_INPUT = 1000;
153 static final int MSG_BIND_INPUT = 1010;
154 static final int MSG_SHOW_SOFT_INPUT = 1020;
155 static final int MSG_HIDE_SOFT_INPUT = 1030;
156 static final int MSG_ATTACH_TOKEN = 1040;
157 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 static final int MSG_START_INPUT = 2000;
160 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_UNBIND_METHOD = 3000;
163 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700164 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700165 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900166 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
satok01038492012-04-09 21:08:27 +0900168 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
169
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700170 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
satokf9f01002011-05-19 21:31:50 +0900172 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
173
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900174 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900175 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176
satok4e4569d2010-11-19 18:45:53 +0900177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800179 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900181 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final IWindowManager mIWindowManager;
184 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700185 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900186 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900187 private final HardKeyboardListener mHardKeyboardListener;
188 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900189 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900191 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // All known input methods. mMethodMap also serves as the global
194 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700195 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
196 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900197 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700198 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900199 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700201 // Used to bring IME service up to visible adjustment while it is being shown.
202 final ServiceConnection mVisibleConnection = new ServiceConnection() {
203 @Override public void onServiceConnected(ComponentName name, IBinder service) {
204 }
205
206 @Override public void onServiceDisconnected(ComponentName name) {
207 }
208 };
209 boolean mVisibleBound = false;
210
satok7cfc0ed2011-06-20 21:29:36 +0900211 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700212 private NotificationManager mNotificationManager;
213 private KeyguardManager mKeyguardManager;
214 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400215 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700216 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900217 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900218 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900219 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900220
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900221 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 final ClientState client;
223 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700224
225 IInputMethodSession session;
226 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 @Override
229 public String toString() {
230 return "SessionState{uid " + client.uid + " pid " + client.pid
231 + " method " + Integer.toHexString(
232 System.identityHashCode(method))
233 + " session " + Integer.toHexString(
234 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700235 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 + "}";
237 }
238
239 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 client = _client;
242 method = _method;
243 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700244 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
Jeff Brownc28867a2013-03-26 15:42:39 -0700248 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IInputMethodClient client;
250 final IInputContext inputContext;
251 final int uid;
252 final int pid;
253 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 boolean sessionRequested;
256 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 @Override
259 public String toString() {
260 return "ClientState{" + Integer.toHexString(
261 System.identityHashCode(this)) + " uid " + uid
262 + " pid " + pid + "}";
263 }
264
265 ClientState(IInputMethodClient _client, IInputContext _inputContext,
266 int _uid, int _pid) {
267 client = _client;
268 inputContext = _inputContext;
269 uid = _uid;
270 pid = _pid;
271 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
272 }
273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700275 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700278 * Set once the system is ready to run third party code.
279 */
280 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * Id of the currently selected input method.
284 */
285 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * The current binding sequence number, incremented every time there is
289 * a new bind performed.
290 */
291 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The client that is currently bound to an input method.
295 */
296 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700299 * The last window token that gained focus.
300 */
301 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * The input context last provided by the current client.
305 */
306 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The attributes last provided by the current client.
310 */
311 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * The input method ID of the input method service that we are currently
315 * connected to or in the process of connecting to.
316 */
317 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
satokab751aa2010-09-14 19:17:36 +0900320 * The current subtype of the current input method.
321 */
322 private InputMethodSubtype mCurrentSubtype;
323
satok4e4569d2010-11-19 18:45:53 +0900324 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900325 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700326 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900327
John Spurlocke0980502013-10-25 11:59:29 -0400328 // Was the keyguard locked when this client became current?
329 private boolean mCurClientInKeyguard;
330
satokab751aa2010-09-14 19:17:36 +0900331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 * Set to true if our ServiceConnection is currently actively bound to
333 * a service (whether or not we have gotten its IBinder back yet).
334 */
335 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * Set if the client has asked for the input method to be shown.
339 */
340 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Set if we were explicitly told to show the input method.
344 */
345 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * Set if we were forced to be shown.
349 */
350 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * Set if we last told the input method to show itself.
354 */
355 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * The Intent used to connect to the current input method.
359 */
360 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * The token we have made for the currently active input method, to
364 * identify it in the future.
365 */
366 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * If non-null, this is the input method service we are currently connected
370 * to.
371 */
372 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * Time that we last initiated a bind to the input method, to determine
376 * if we should try to disconnect and reconnect to it.
377 */
378 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
381 * Have we called mCurMethod.bindInput()?
382 */
383 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
386 * Currently enabled session. Only touched by service thread, not
387 * protected by a lock.
388 */
389 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700392 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700394 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900396 int mCurUserActionNotificationSequenceNumber = 0;
397
Joe Onorato857fd9b2011-01-27 15:08:35 -0800398 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900399
400 /**
401 * A set of status bits regarding the active IME.
402 *
403 * <p>This value is a combination of following two bits:</p>
404 * <dl>
405 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
406 * <dd>
407 * If this bit is ON, connected IME is ready to accept touch/key events.
408 * </dd>
409 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
410 * <dd>
411 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
412 * </dd>
413 * </dl>
414 * <em>Do not update this value outside of setImeWindowStatus.</em>
415 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800416 int mImeWindowVis;
417
Ken Wakasa05dbb652011-08-22 15:22:43 +0900418 private AlertDialog.Builder mDialogBuilder;
419 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900420 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900421 private InputMethodInfo[] mIms;
422 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900423 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700424 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900425 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
426 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700429 int mUserId;
430 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800431 String mLastEnabled = "";
432
Yohei Yukawa81482972015-06-04 00:58:59 -0700433 /**
434 * <em>This constructor must be called within the lock.</em>
435 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 SettingsObserver(Handler handler) {
437 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700438 }
439
440 public void registerContentObserverLocked(int userId) {
441 if (mRegistered && mUserId == userId) {
442 return;
443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700445 if (mRegistered) {
446 mContext.getContentResolver().unregisterContentObserver(this);
447 mRegistered = false;
448 }
449 if (mUserId != userId) {
450 mLastEnabled = "";
451 mUserId = userId;
452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700454 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900455 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700456 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900457 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700458 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700459 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700460 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
461 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Michael Wright7b5a96b2014-08-09 19:28:42 -0700464 @Override public void onChange(boolean selfChange, Uri uri) {
465 final Uri showImeUri =
466 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700468 if (showImeUri.equals(uri)) {
469 updateKeyboardFromSettingsLocked();
470 } else {
471 boolean enabledChanged = false;
472 String newEnabled = mSettings.getEnabledInputMethodsStr();
473 if (!mLastEnabled.equals(newEnabled)) {
474 mLastEnabled = newEnabled;
475 enabledChanged = true;
476 }
477 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700481
482 @Override
483 public String toString() {
484 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
485 + " mLastEnabled=" + mLastEnabled + "}";
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900489 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 @Override
491 public void onReceive(Context context, Intent intent) {
492 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700493 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900494 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700495 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900496 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800497 } else if (Intent.ACTION_USER_ADDED.equals(action)
498 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100499 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800500 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700501 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
502 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
503 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
504 final String prevValue = intent.getStringExtra(
505 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
506 final String newValue = intent.getStringExtra(
507 Intent.EXTRA_SETTING_NEW_VALUE);
508 restoreEnabledInputMethods(mContext, prevValue, newValue);
509 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900510 } else {
511 Slog.w(TAG, "Unexpected intent " + intent);
512 }
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700516 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
517 // does not attempt to validate on the fly with any installed device policy, so must only
518 // be run in the context of initial device setup.
519 //
520 // TODO: Move this method to InputMethodUtils with adding unit tests.
521 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
522 if (DEBUG_RESTORE) {
523 Slog.i(TAG, "Restoring enabled input methods:");
524 Slog.i(TAG, "prev=" + prevValue);
525 Slog.i(TAG, " new=" + newValue);
526 }
527 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900528 ArrayMap<String, ArraySet<String>> prevMap =
529 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
530 ArrayMap<String, ArraySet<String>> newMap =
531 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700532
533 // Merge the restored ime+subtype enabled states into the live state
534 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
535 final String imeId = entry.getKey();
536 ArraySet<String> prevSubtypes = prevMap.get(imeId);
537 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700538 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700539 prevMap.put(imeId, prevSubtypes);
540 }
541 prevSubtypes.addAll(entry.getValue());
542 }
543
544 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
545 if (DEBUG_RESTORE) {
546 Slog.i(TAG, "Merged IME string:");
547 Slog.i(TAG, " " + mergedImesAndSubtypesString);
548 }
549 Settings.Secure.putString(context.getContentResolver(),
550 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
551 }
552
553 // TODO: Move this method to InputMethodUtils with adding unit tests.
554 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
555 // we want to use the canonical InputMethodSettings implementation,
556 // so we convert data structures first.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700557 List<Pair<String, ArrayList<String>>> imeMap = new ArrayList<>(4);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700558 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
559 final String imeName = entry.getKey();
560 final ArraySet<String> subtypeSet = entry.getValue();
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700561 final ArrayList<String> subtypes = new ArrayList<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700562 if (subtypeSet != null) {
563 subtypes.addAll(subtypeSet);
564 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700565 imeMap.add(new Pair<>(imeName, subtypes));
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700566 }
567 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
568 }
569
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800570 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900571 private boolean isChangingPackagesOfCurrentUser() {
572 final int userId = getChangingUserId();
573 final boolean retval = userId == mSettings.getCurrentUserId();
574 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900575 if (!retval) {
576 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
577 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900578 }
579 return retval;
580 }
581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800583 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900584 if (!isChangingPackagesOfCurrentUser()) {
585 return false;
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900588 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 final int N = mMethodList.size();
590 if (curInputMethodId != null) {
591 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800592 InputMethodInfo imi = mMethodList.get(i);
593 if (imi.getId().equals(curInputMethodId)) {
594 for (String pkg : packages) {
595 if (imi.getPackageName().equals(pkg)) {
596 if (!doit) {
597 return true;
598 }
satok723a27e2010-11-11 14:58:11 +0900599 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800600 chooseNewDefaultIMELocked();
601 return true;
602 }
603 }
604 }
605 }
606 }
607 }
608 return false;
609 }
610
611 @Override
612 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900613 if (!isChangingPackagesOfCurrentUser()) {
614 return;
615 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800616 synchronized (mMethodMap) {
617 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900618 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 final int N = mMethodList.size();
620 if (curInputMethodId != null) {
621 for (int i=0; i<N; i++) {
622 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900623 final String imiId = imi.getId();
624 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800625 curIm = imi;
626 }
satoke7c6998e2011-06-03 17:57:59 +0900627
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900629 if (isPackageModified(imi.getPackageName())) {
630 mFileManager.deleteAllInputMethodSubtypes(imiId);
631 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800632 if (change == PACKAGE_TEMPORARY_CHANGE
633 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800634 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800635 + imi.getComponent());
636 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638 }
639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800640
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900641 buildInputMethodListLocked(
642 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800646 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 int change = isPackageDisappearing(curIm.getPackageName());
648 if (change == PACKAGE_TEMPORARY_CHANGE
649 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800650 ServiceInfo si = null;
651 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900652 si = mIPackageManager.getServiceInfo(
653 curIm.getComponent(), 0, mSettings.getCurrentUserId());
654 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800655 }
656 if (si == null) {
657 // Uh oh, current input method is no longer around!
658 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800659 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900660 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800661 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800662 changed = true;
663 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800664 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900665 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800666 }
667 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800668 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 }
satokab751aa2010-09-14 19:17:36 +0900670
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 if (curIm == null) {
672 // We currently don't have a default input method... is
673 // one now available?
674 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700675 } else if (!changed && isPackageModified(curIm.getPackageName())) {
676 // Even if the current input method is still available, mCurrentSubtype could
677 // be obsolete when the package is modified in practice.
678 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800682 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684 }
685 }
686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
Jeff Brownc28867a2013-03-26 15:42:39 -0700688 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900689 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700690 private final IInputMethod mMethod;
691 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692
Jeff Brownc28867a2013-03-26 15:42:39 -0700693 MethodCallback(InputMethodManagerService imms, IInputMethod method,
694 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900695 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700696 mMethod = method;
697 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800699
satoke7c6998e2011-06-03 17:57:59 +0900700 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700701 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700702 long ident = Binder.clearCallingIdentity();
703 try {
704 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
705 } finally {
706 Binder.restoreCallingIdentity(ident);
707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710
satok01038492012-04-09 21:08:27 +0900711 private class HardKeyboardListener
712 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
713 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700714 public void onHardKeyboardStatusChange(boolean available) {
715 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
716 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900717 }
718
Michael Wright7b5a96b2014-08-09 19:28:42 -0700719 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900720 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700721 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900722 }
723 synchronized(mMethodMap) {
724 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
725 && mSwitchingDialog.isShowing()) {
726 mSwitchingDialogTitleView.findViewById(
727 com.android.internal.R.id.hard_keyboard_section).setVisibility(
728 available ? View.VISIBLE : View.GONE);
729 }
730 }
731 }
732 }
733
734 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900735 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800737 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700739 // Note: SettingsObserver doesn't register observers in its constructor.
740 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mIWindowManager = IWindowManager.Stub.asInterface(
742 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800743 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 public void executeMessage(Message msg) {
746 handleMessage(msg);
747 }
Mita Yuned218c72012-12-06 17:18:25 -0800748 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900749 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900750 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900751 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700752 mHasFeature = context.getPackageManager().hasSystemFeature(
753 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900754
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400755 Bundle extras = new Bundle();
756 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
757 mImeSwitcherNotification = new Notification.Builder(mContext)
758 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
759 .setWhen(0)
760 .setOngoing(true)
761 .addExtras(extras)
762 .setCategory(Notification.CATEGORY_SYSTEM)
763 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400764
satok7cfc0ed2011-06-20 21:29:36 +0900765 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900766 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900767
768 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900769
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900770 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800772 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
773 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700774 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900775 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
satok7cfc0ed2011-06-20 21:29:36 +0900777 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900778 int userId = 0;
779 try {
780 ActivityManagerNative.getDefault().registerUserSwitchObserver(
781 new IUserSwitchObserver.Stub() {
782 @Override
783 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900784 synchronized(mMethodMap) {
785 switchUserLocked(newUserId);
786 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 if (reply != null) {
788 try {
789 reply.sendResult(null);
790 } catch (RemoteException e) {
791 }
792 }
793 }
794
795 @Override
796 public void onUserSwitchComplete(int newUserId) throws RemoteException {
797 }
Kenny Guy42979622015-04-13 18:03:05 +0000798
799 @Override
800 public void onForegroundProfileSwitch(int newProfileId) {
801 // Ignore.
802 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900803 });
804 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
805 } catch (RemoteException e) {
806 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
807 }
satok81f8b7c2012-12-04 20:42:56 +0900808 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900809
satokd87c2592010-09-29 11:52:06 +0900810 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900811 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900812 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700813
814 // Let the package manager query which are the default imes
815 // as they get certain permissions granted by default.
816 PackageManagerInternal packageManagerInternal = LocalServices.getService(
817 PackageManagerInternal.class);
818 packageManagerInternal.setImePackagesProvider(
819 new PackageManagerInternal.PackagesProvider() {
820 @Override
821 public String[] getPackages(int userId) {
822 synchronized (mMethodMap) {
823 final int currentUserId = mSettings.getCurrentUserId();
824 // TODO: We are switching the current user id in the settings
825 // object to query it and then revert the user id. Ideally, we
826 // should call a API in settings with the user id as an argument.
827 mSettings.setCurrentUserId(userId);
828 List<InputMethodInfo> imes = mSettings
829 .getEnabledInputMethodListLocked();
830 String[] packageNames = null;
831 if (imes != null) {
832 final int imeCount = imes.size();
833 packageNames = new String[imeCount];
834 for (int i = 0; i < imeCount; i++) {
835 InputMethodInfo ime = imes.get(i);
836 packageNames[i] = ime.getPackageName();
837 }
838 }
839 mSettings.setCurrentUserId(currentUserId);
840 return packageNames;
841 }
842 }
843 });
844
Kenny Guy2a764942014-04-02 13:29:20 +0100845 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900846 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900847 synchronized (mMethodMap) {
848 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
849 mSettings, context);
850 }
satok0a1bcf42012-05-16 19:26:31 +0900851
852 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900853 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900854 if (DEBUG) {
855 Slog.d(TAG, "Initial default ime = " + defaultImiId);
856 }
satok0a1bcf42012-05-16 19:26:31 +0900857 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
858
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900859 synchronized (mMethodMap) {
860 buildInputMethodListLocked(mMethodList, mMethodMap,
861 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
862 }
satokd87c2592010-09-29 11:52:06 +0900863 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864
satok0a1bcf42012-05-16 19:26:31 +0900865 if (!mImeSelectedOnBoot) {
866 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900867 synchronized (mMethodMap) {
868 resetDefaultImeLocked(context);
869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900872 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700873 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900874 updateFromSettingsLocked(true);
875 }
satok5b927c432012-05-01 20:09:34 +0900876
877 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
878 // according to the new system locale.
879 final IntentFilter filter = new IntentFilter();
880 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
881 mContext.registerReceiver(
882 new BroadcastReceiver() {
883 @Override
884 public void onReceive(Context context, Intent intent) {
885 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900886 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900887 }
888 }
889 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700890 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900891 }
892
satok5b927c432012-05-01 20:09:34 +0900893 private void resetDefaultImeLocked(Context context) {
894 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900895 if (mCurMethodId != null
896 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900897 return;
898 }
899
900 InputMethodInfo defIm = null;
901 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900902 if (defIm == null && mSystemReady) {
903 final Locale systemLocale = context.getResources().getConfiguration().locale;
904 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
905 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
906 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900907 defIm = imi;
908 Slog.i(TAG, "Selected default: " + imi.getId());
909 }
910 }
911 }
912 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900913 defIm = InputMethodUtils.getMostApplicableDefaultIME(
914 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900915 if (defIm != null) {
916 Slog.i(TAG, "Default found, using " + defIm.getId());
917 } else {
918 Slog.i(TAG, "No default found");
919 }
satok5b927c432012-05-01 20:09:34 +0900920 }
921 if (defIm != null) {
922 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
923 }
924 }
925
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900926 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
927 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 if (!mSystemReady) {
929 // not system ready
930 return;
931 }
932 final Locale newLocale = mRes.getConfiguration().locale;
933 if (!updateOnlyWhenLocaleChanged
934 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
935 if (!updateOnlyWhenLocaleChanged) {
936 hideCurrentInputLocked(0, null);
937 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700938 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 }
940 if (DEBUG) {
941 Slog.i(TAG, "Locale has been changed to " + newLocale);
942 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900943 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 if (!updateOnlyWhenLocaleChanged) {
945 final String selectedImiId = mSettings.getSelectedInputMethod();
946 if (TextUtils.isEmpty(selectedImiId)) {
947 // This is the first time of the user switch and
948 // set the current ime to the proper one.
949 resetDefaultImeLocked(mContext);
950 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900951 } else {
952 // If the locale is changed, needs to reset the default ime
953 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800955 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 mLastSystemLocale = newLocale;
957 if (!updateOnlyWhenLocaleChanged) {
958 try {
959 startInputInnerLocked();
960 } catch (RuntimeException e) {
961 Slog.w(TAG, "Unexpected exception", e);
962 }
963 }
964 }
965 }
966
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900967 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900968 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
969 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 }
971
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900972 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700973 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
974 + " currentUserId=" + mSettings.getCurrentUserId());
975
Yohei Yukawa81482972015-06-04 00:58:59 -0700976 // ContentObserver should be registered again when the user is changed
977 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100979 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900980 // InputMethodFileManager should be reset when the user is changed
981 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700983
984 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
985 + " defaultImiId=" + defaultImiId);
986
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900987 // For secondary users, the list of enabled IMEs may not have been updated since the
988 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
989 // not be empty even if the IME has been uninstalled by the primary user.
990 // Even in such cases, IMMS works fine because it will find the most applicable
991 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900992 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900993 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900994 initialUserSwitch /* needsToResetDefaultIme */);
995 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -0700996 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
997 mSettings.getEnabledInputMethodListLocked(), newUserId,
998 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900999 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001000
1001 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1002 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 }
1004
Kenny Guy2a764942014-04-02 13:29:20 +01001005 void updateCurrentProfileIds() {
1006 List<UserInfo> profiles =
1007 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1008 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1009 for (int i = 0; i < currentProfileIds.length; i++) {
1010 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001011 }
Kenny Guy2a764942014-04-02 13:29:20 +01001012 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001013 }
1014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 @Override
1016 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1017 throws RemoteException {
1018 try {
1019 return super.onTransact(code, data, reply, flags);
1020 } catch (RuntimeException e) {
1021 // The input method manager only throws security exceptions, so let's
1022 // log all others.
1023 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001024 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 throw e;
1027 }
1028 }
1029
Svetoslav Ganova0027152013-06-25 14:59:53 -07001030 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001031 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001032 if (DEBUG) {
1033 Slog.d(TAG, "--- systemReady");
1034 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001035 if (!mSystemReady) {
1036 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001037 mKeyguardManager =
1038 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001039 mNotificationManager = (NotificationManager)
1040 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1041 mStatusBar = statusBar;
1042 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001043 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001044 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1045 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001046 if (mShowOngoingImeSwitcherForPhones) {
1047 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1048 mHardKeyboardListener);
1049 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001050 buildInputMethodListLocked(mMethodList, mMethodMap,
1051 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001052 if (!mImeSelectedOnBoot) {
1053 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001055 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1056 mSettings.getEnabledInputMethodListLocked(),
1057 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001058 }
1059 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001060 try {
1061 startInputInnerLocked();
1062 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001064 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001065 }
1066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001069 // ---------------------------------------------------------------------------------------
1070 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1071 // 1) it comes from the system process
1072 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1073 private boolean calledFromValidUser() {
1074 final int uid = Binder.getCallingUid();
1075 final int userId = UserHandle.getUserId(uid);
1076 if (DEBUG) {
1077 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1078 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1079 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001080 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1081 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001082 }
Kenny Guy2a764942014-04-02 13:29:20 +01001083 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001084 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001086
1087 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1088 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1089 // must not manage background users' states in any functions.
1090 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1091 // by a token.
1092 if (mContext.checkCallingOrSelfPermission(
1093 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1094 == PackageManager.PERMISSION_GRANTED) {
1095 if (DEBUG) {
1096 Slog.d(TAG, "--- Access granted because the calling process has "
1097 + "the INTERACT_ACROSS_USERS_FULL permission");
1098 }
1099 return true;
1100 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001101 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1102 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001103 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001104 }
1105
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001106
1107 /**
1108 * Returns true iff the caller is identified to be the current input method with the token.
1109 * @param token The window token given to the input method when it was started.
1110 * @return true if and only if non-null valid token is specified.
1111 */
1112 private boolean calledWithValidToken(IBinder token) {
1113 if (token == null || mCurToken != token) {
1114 return false;
1115 }
1116 return true;
1117 }
1118
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 private boolean bindCurrentInputMethodService(
1120 Intent service, ServiceConnection conn, int flags) {
1121 if (service == null || conn == null) {
1122 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1123 return false;
1124 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001125 return mContext.bindServiceAsUser(service, conn, flags,
1126 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 }
1128
satoke7c6998e2011-06-03 17:57:59 +09001129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 // TODO: Make this work even for non-current users?
1132 if (!calledFromValidUser()) {
1133 return Collections.emptyList();
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001136 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 }
1139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 // TODO: Make this work even for non-current users?
1143 if (!calledFromValidUser()) {
1144 return Collections.emptyList();
1145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001147 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
1150
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001151 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001152 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001153 * @return enabled subtypes of the specified imi
1154 */
satoke7c6998e2011-06-03 17:57:59 +09001155 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001156 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001157 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001158 // TODO: Make this work even for non-current users?
1159 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001160 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 }
satok67ddf9c2010-11-17 09:45:54 +09001162 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001163 final InputMethodInfo imi;
1164 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001165 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001166 } else {
1167 imi = mMethodMap.get(imiId);
1168 }
1169 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001170 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001171 }
1172 return mSettings.getEnabledInputMethodSubtypeListLocked(
1173 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001174 }
1175 }
1176
satoke7c6998e2011-06-03 17:57:59 +09001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public void addClient(IInputMethodClient client,
1179 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 if (!calledFromValidUser()) {
1181 return;
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 synchronized (mMethodMap) {
1184 mClients.put(client.asBinder(), new ClientState(client,
1185 inputContext, uid, pid));
1186 }
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 if (!calledFromValidUser()) {
1192 return;
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001195 ClientState cs = mClients.remove(client.asBinder());
1196 if (cs != null) {
1197 clearClientSessionLocked(cs);
1198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 void executeOrSendMessage(IInterface target, Message msg) {
1203 if (target.asBinder() instanceof Binder) {
1204 mCaller.sendMessage(msg);
1205 } else {
1206 handleMessage(msg);
1207 msg.recycle();
1208 }
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001211 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 + mCurClient.client.asBinder());
1215 if (mBoundToMethod) {
1216 mBoundToMethod = false;
1217 if (mCurMethod != null) {
1218 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1219 MSG_UNBIND_INPUT, mCurMethod));
1220 }
1221 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001222
1223 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1224 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1226 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1227 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project10592532009-03-18 17:39:46 -07001230 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 private int getImeShowFlags() {
1235 int flags = 0;
1236 if (mShowForced) {
1237 flags |= InputMethod.SHOW_FORCED
1238 | InputMethod.SHOW_EXPLICIT;
1239 } else if (mShowExplicitlyRequested) {
1240 flags |= InputMethod.SHOW_EXPLICIT;
1241 }
1242 return flags;
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 private int getAppShowFlags() {
1246 int flags = 0;
1247 if (mShowForced) {
1248 flags |= InputMethodManager.SHOW_FORCED;
1249 } else if (!mShowExplicitlyRequested) {
1250 flags |= InputMethodManager.SHOW_IMPLICIT;
1251 }
1252 return flags;
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn7663d802012-02-24 13:08:49 -08001255 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (!mBoundToMethod) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1258 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1259 mBoundToMethod = true;
1260 }
1261 final SessionState session = mCurClient.curSession;
1262 if (initial) {
1263 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1264 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1265 } else {
1266 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1267 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1268 }
1269 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001271 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001273 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001274 (session.channel != null ? session.channel.dup() : null),
1275 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001279 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 // If no method is currently selected, do nothing.
1281 if (mCurMethodId == null) {
1282 return mNoBinding;
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 ClientState cs = mClients.get(client.asBinder());
1286 if (cs == null) {
1287 throw new IllegalArgumentException("unknown client "
1288 + client.asBinder());
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 try {
1292 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1293 // Check with the window manager to make sure this client actually
1294 // has a window with focus. If not, reject. This is thread safe
1295 // because if the focus changes some time before or after, the
1296 // next client receiving focus that has any interest in input will
1297 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1300 return null;
1301 }
1302 } catch (RemoteException e) {
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn7663d802012-02-24 13:08:49 -08001305 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1306 }
1307
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001308 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001309 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 // If no method is currently selected, do nothing.
1311 if (mCurMethodId == null) {
1312 return mNoBinding;
1313 }
1314
Yohei Yukawad57ba672015-06-08 16:39:46 -07001315 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1316 attribute.packageName)) {
1317 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1318 + " uid=" + cs.uid + " package=" + attribute.packageName);
1319 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001320 }
1321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001323 // Was the keyguard locked when switching over to the new client?
1324 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // If the client is changing, we need to switch over to the new
1326 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001327 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001329 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
1331 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001332 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001333 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001334 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // Bump up the sequence for this client and attach it.
1339 mCurSeq++;
1340 if (mCurSeq <= 0) mCurSeq = 1;
1341 mCurClient = cs;
1342 mCurInputContext = inputContext;
1343 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 // Check if the input method is changing.
1346 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1347 if (cs.curSession != null) {
1348 // Fast case: if we are already connected to the input method,
1349 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001350 return attachNewInputLocked(
1351 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 if (mHaveConnection) {
1354 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // Return to client, and we will get back with it when
1356 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001357 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001358 return new InputBindResult(null, null, mCurId, mCurSeq,
1359 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } else if (SystemClock.uptimeMillis()
1361 < (mLastBindTime+TIME_TO_RECONNECT)) {
1362 // In this case we have connected to the service, but
1363 // don't yet have its interface. If it hasn't been too
1364 // long since we did the connection, we'll return to
1365 // the client and wait to get the service interface so
1366 // we can report back. If it has been too long, we want
1367 // to fall through so we can try a disconnect/reconnect
1368 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001369 return new InputBindResult(null, null, mCurId, mCurSeq,
1370 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1373 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375 }
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001378 return startInputInnerLocked();
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001381 InputBindResult startInputInnerLocked() {
1382 if (mCurMethodId == null) {
1383 return mNoBinding;
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001386 if (!mSystemReady) {
1387 // If the system is not yet ready, we shouldn't be running third
1388 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001389 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1390 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1394 if (info == null) {
1395 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001398 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1401 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001402 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1403 com.android.internal.R.string.input_method_binding_label);
1404 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1405 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001406 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001407 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1408 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 mLastBindTime = SystemClock.uptimeMillis();
1410 mHaveConnection = true;
1411 mCurId = info.getId();
1412 mCurToken = new Binder();
1413 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001414 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 mIWindowManager.addWindowToken(mCurToken,
1416 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1417 } catch (RemoteException e) {
1418 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001419 return new InputBindResult(null, null, mCurId, mCurSeq,
1420 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 } else {
1422 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 + mCurIntent);
1425 }
1426 return null;
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
satoke7c6998e2011-06-03 17:57:59 +09001429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001431 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001432 if (!calledFromValidUser()) {
1433 return null;
1434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 synchronized (mMethodMap) {
1436 final long ident = Binder.clearCallingIdentity();
1437 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001438 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 } finally {
1440 Binder.restoreCallingIdentity(ident);
1441 }
1442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
satoke7c6998e2011-06-03 17:57:59 +09001445 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 public void finishInput(IInputMethodClient client) {
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
satoke7c6998e2011-06-03 17:57:59 +09001449 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 public void onServiceConnected(ComponentName name, IBinder service) {
1451 synchronized (mMethodMap) {
1452 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1453 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001454 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001456 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001457 return;
1458 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001459 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001460 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1461 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001463 clearClientSessionLocked(mCurClient);
1464 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467 }
1468 }
1469
Jeff Brownc28867a2013-03-26 15:42:39 -07001470 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1471 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 synchronized (mMethodMap) {
1473 if (mCurMethod != null && method != null
1474 && mCurMethod.asBinder() == method.asBinder()) {
1475 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001476 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001478 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001479 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 if (res.method != null) {
1481 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1482 MSG_BIND_METHOD, mCurClient.client, res));
1483 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001484 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486 }
1487 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001488
1489 // Session abandoned. Close its associated input channel.
1490 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001493 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001494 if (mVisibleBound) {
1495 mContext.unbindService(mVisibleConnection);
1496 mVisibleBound = false;
1497 }
1498
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001499 if (mHaveConnection) {
1500 mContext.unbindService(this);
1501 mHaveConnection = false;
1502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001504 if (mCurToken != null) {
1505 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001506 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001507 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001508 // The current IME is shown. Hence an IME switch (transition) is happening.
1509 mWindowManagerService.saveLastInputMethodWindowForTransition();
1510 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001511 mIWindowManager.removeWindowToken(mCurToken);
1512 } catch (RemoteException e) {
1513 }
1514 mCurToken = null;
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
The Android Open Source Project10592532009-03-18 17:39:46 -07001517 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001518 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001520 if (reportToClient && mCurClient != null) {
1521 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1522 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1523 }
1524 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001525
1526 void requestClientSessionLocked(ClientState cs) {
1527 if (!cs.sessionRequested) {
1528 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1529 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1530 cs.sessionRequested = true;
1531 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1532 MSG_CREATE_SESSION, mCurMethod, channels[1],
1533 new MethodCallback(this, mCurMethod, channels[0])));
1534 }
1535 }
1536
1537 void clearClientSessionLocked(ClientState cs) {
1538 finishSessionLocked(cs.curSession);
1539 cs.curSession = null;
1540 cs.sessionRequested = false;
1541 }
1542
1543 private void finishSessionLocked(SessionState sessionState) {
1544 if (sessionState != null) {
1545 if (sessionState.session != null) {
1546 try {
1547 sessionState.session.finishSession();
1548 } catch (RemoteException e) {
1549 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001550 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001551 }
1552 sessionState.session = null;
1553 }
1554 if (sessionState.channel != null) {
1555 sessionState.channel.dispose();
1556 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001557 }
1558 }
1559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001561 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 if (mCurMethod != null) {
1563 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001564 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001566
Jeff Brownc28867a2013-03-26 15:42:39 -07001567 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001568 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 mCurMethod = null;
1570 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001571 if (mStatusBar != null) {
1572 mStatusBar.setIconVisibility("ime", false);
1573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
satoke7c6998e2011-06-03 17:57:59 +09001576 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 public void onServiceDisconnected(ComponentName name) {
1578 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001579 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 + " mCurIntent=" + mCurIntent);
1581 if (mCurMethod != null && mCurIntent != null
1582 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001583 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 // We consider this to be a new bind attempt, since the system
1585 // should now try to restart the service for us.
1586 mLastBindTime = SystemClock.uptimeMillis();
1587 mShowRequested = mInputShown;
1588 mInputShown = false;
1589 if (mCurClient != null) {
1590 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1591 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1592 }
1593 }
1594 }
1595 }
1596
satokf9f01002011-05-19 21:31:50 +09001597 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1599 long ident = Binder.clearCallingIdentity();
1600 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001602 if (!calledWithValidToken(token)) {
1603 final int uid = Binder.getCallingUid();
1604 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1605 + " token:" + token);
1606 return;
1607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001609 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001610 if (mStatusBar != null) {
1611 mStatusBar.setIconVisibility("ime", false);
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001614 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001615 CharSequence contentDescription = null;
1616 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001617 // Use PackageManager to load label
1618 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001619 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001620 mIPackageManager.getApplicationInfo(packageName, 0,
1621 mSettings.getCurrentUserId()));
1622 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001623 /* ignore */
1624 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001625 if (mStatusBar != null) {
1626 mStatusBar.setIcon("ime", packageName, iconId, 0,
1627 contentDescription != null
1628 ? contentDescription.toString() : null);
1629 mStatusBar.setIconVisibility("ime", true);
1630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
1632 }
1633 } finally {
1634 Binder.restoreCallingIdentity(ident);
1635 }
1636 }
1637
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001638 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001639 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001640 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001641 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001642 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001643 if (mWindowManagerService.isHardKeyboardAvailable()) {
1644 // When physical keyboard is attached, we show the ime switcher (or notification if
1645 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1646 // exists in the IME switcher dialog. Might be OK to remove this condition once
1647 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1648 return true;
1649 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001650 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001651
1652 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1653 final int N = imis.size();
1654 if (N > 2) return true;
1655 if (N < 1) return false;
1656 int nonAuxCount = 0;
1657 int auxCount = 0;
1658 InputMethodSubtype nonAuxSubtype = null;
1659 InputMethodSubtype auxSubtype = null;
1660 for(int i = 0; i < N; ++i) {
1661 final InputMethodInfo imi = imis.get(i);
1662 final List<InputMethodSubtype> subtypes =
1663 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1664 final int subtypeCount = subtypes.size();
1665 if (subtypeCount == 0) {
1666 ++nonAuxCount;
1667 } else {
1668 for (int j = 0; j < subtypeCount; ++j) {
1669 final InputMethodSubtype subtype = subtypes.get(j);
1670 if (!subtype.isAuxiliary()) {
1671 ++nonAuxCount;
1672 nonAuxSubtype = subtype;
1673 } else {
1674 ++auxCount;
1675 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001676 }
1677 }
satok7cfc0ed2011-06-20 21:29:36 +09001678 }
1679 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001680 if (nonAuxCount > 1 || auxCount > 1) {
1681 return true;
1682 } else if (nonAuxCount == 1 && auxCount == 1) {
1683 if (nonAuxSubtype != null && auxSubtype != null
1684 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1685 || auxSubtype.overridesImplicitlyEnabledSubtype()
1686 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1687 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1688 return false;
1689 }
1690 return true;
1691 }
1692 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001693 }
1694
John Spurlocke0980502013-10-25 11:59:29 -04001695 private boolean isKeyguardLocked() {
1696 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1697 }
1698
satokdbf29502011-08-25 15:28:23 +09001699 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001700 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001701 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001702 if (!calledWithValidToken(token)) {
1703 final int uid = Binder.getCallingUid();
1704 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1705 + " token:" + token);
1706 return;
1707 }
1708
1709 synchronized (mMethodMap) {
1710 mImeWindowVis = vis;
1711 mBackDisposition = backDisposition;
1712 updateSystemUiLocked(token, vis, backDisposition);
1713 }
1714 }
1715
1716 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1717 synchronized (mMethodMap) {
1718 updateSystemUiLocked(token, vis, backDisposition);
1719 }
1720 }
1721
1722 // Caution! This method is called in this class. Handle multi-user carefully
1723 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1724 if (!calledWithValidToken(token)) {
1725 final int uid = Binder.getCallingUid();
1726 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1727 + " token:" + token);
1728 return;
1729 }
1730
1731 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1732 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001733 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001734 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001735 // apply policy for binder calls
1736 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1737 vis = 0;
satok06487a52010-10-29 11:37:18 +09001738 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001739 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1740 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1741 if (mStatusBar != null) {
1742 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1743 needsToShowImeSwitcher);
1744 }
1745 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1746 if (imi != null && needsToShowImeSwitcher) {
1747 // Used to load label
1748 final CharSequence title = mRes.getText(
1749 com.android.internal.R.string.select_input_method);
1750 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1751 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001752 mImeSwitcherNotification.setContentTitle(title)
1753 .setContentText(summary)
1754 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001755 if ((mNotificationManager != null)
1756 && !mWindowManagerService.hasNavigationBar()) {
1757 if (DEBUG) {
1758 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001759 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001760 mNotificationManager.notifyAsUser(null,
1761 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001762 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001763 mNotificationShown = true;
1764 }
1765 } else {
1766 if (mNotificationShown && mNotificationManager != null) {
1767 if (DEBUG) {
1768 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001769 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001770 mNotificationManager.cancelAsUser(null,
1771 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1772 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001773 }
satok06487a52010-10-29 11:37:18 +09001774 }
1775 } finally {
1776 Binder.restoreCallingIdentity(ident);
1777 }
1778 }
1779
satoke7c6998e2011-06-03 17:57:59 +09001780 @Override
satokf9f01002011-05-19 21:31:50 +09001781 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001782 if (!calledFromValidUser()) {
1783 return;
1784 }
satokf9f01002011-05-19 21:31:50 +09001785 synchronized (mMethodMap) {
1786 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1787 for (int i = 0; i < spans.length; ++i) {
1788 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001789 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001790 mSecureSuggestionSpans.put(ss, currentImi);
1791 }
1792 }
1793 }
1794 }
1795
satoke7c6998e2011-06-03 17:57:59 +09001796 @Override
satokf9f01002011-05-19 21:31:50 +09001797 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001798 if (!calledFromValidUser()) {
1799 return false;
1800 }
satokf9f01002011-05-19 21:31:50 +09001801 synchronized (mMethodMap) {
1802 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1803 // TODO: Do not send the intent if the process of the targetImi is already dead.
1804 if (targetImi != null) {
1805 final String[] suggestions = span.getSuggestions();
1806 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001807 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001808 final Intent intent = new Intent();
1809 // Ensures that only a class in the original IME package will receive the
1810 // notification.
satok42c5a162011-05-26 16:46:14 +09001811 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001812 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1813 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1814 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1815 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001816 final long ident = Binder.clearCallingIdentity();
1817 try {
1818 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1819 } finally {
1820 Binder.restoreCallingIdentity(ident);
1821 }
satokf9f01002011-05-19 21:31:50 +09001822 return true;
1823 }
1824 }
1825 return false;
1826 }
1827
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001828 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001829 updateInputMethodsFromSettingsLocked(enabledMayChange);
1830 updateKeyboardFromSettingsLocked();
1831 }
1832
1833 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001834 if (enabledMayChange) {
1835 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1836 for (int i=0; i<enabled.size(); i++) {
1837 // We allow the user to select "disabled until used" apps, so if they
1838 // are enabling one of those here we now need to make it enabled.
1839 InputMethodInfo imm = enabled.get(i);
1840 try {
1841 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1842 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1843 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001844 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001845 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001846 if (DEBUG) {
1847 Slog.d(TAG, "Update state(" + imm.getId()
1848 + "): DISABLED_UNTIL_USED -> DEFAULT");
1849 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001850 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1851 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001852 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1853 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001854 }
1855 } catch (RemoteException e) {
1856 }
1857 }
1858 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001859 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1860 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1861 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1862 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001863 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001864 // There is no input method selected, try to choose new applicable input method.
1865 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001866 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001867 }
1868 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001870 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001872 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001873 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001874 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
satokf3db1af2010-11-23 13:34:33 +09001876 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001877 } else {
1878 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001879 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001880 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001882 // Here is not the perfect place to reset the switching controller. Ideally
1883 // mSwitchingController and mSettings should be able to share the same state.
1884 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1885 // the same enabled IMEs list.
1886 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001887
1888 }
1889
1890 public void updateKeyboardFromSettingsLocked() {
1891 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1892 if (mSwitchingDialog != null
1893 && mSwitchingDialogTitleView != null
1894 && mSwitchingDialog.isShowing()) {
1895 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1896 com.android.internal.R.id.hard_keyboard_switch);
1897 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001900
satokab751aa2010-09-14 19:17:36 +09001901 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 InputMethodInfo info = mMethodMap.get(id);
1903 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001904 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001906
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001907 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001908 // We have already made sure that the package name belongs to the application's UID.
1909 // No further UID check is required.
1910 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1911 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001912 }
1913 }
1914
satokd81e9502012-05-21 12:58:45 +09001915 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001917 final int subtypeCount = info.getSubtypeCount();
1918 if (subtypeCount <= 0) {
1919 return;
satokcd7cd292010-11-20 15:46:23 +09001920 }
satokd81e9502012-05-21 12:58:45 +09001921 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1922 final InputMethodSubtype newSubtype;
1923 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1924 newSubtype = info.getSubtypeAt(subtypeId);
1925 } else {
1926 // If subtype is null, try to find the most applicable one from
1927 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001928 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001929 }
1930 if (newSubtype == null || oldSubtype == null) {
1931 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1932 + ", new subtype = " + newSubtype);
1933 return;
1934 }
1935 if (newSubtype != oldSubtype) {
1936 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1937 if (mCurMethod != null) {
1938 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001939 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001940 mCurMethod.changeInputMethodSubtype(newSubtype);
1941 } catch (RemoteException e) {
1942 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001943 }
1944 }
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 return;
1947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948
satokd81e9502012-05-21 12:58:45 +09001949 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 final long ident = Binder.clearCallingIdentity();
1951 try {
satokab751aa2010-09-14 19:17:36 +09001952 // Set a subtype to this input method.
1953 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001954 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1955 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1956 // because mCurMethodId is stored as a history in
1957 // setSelectedInputMethodAndSubtypeLocked().
1958 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959
1960 if (ActivityManagerNative.isSystemReady()) {
1961 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001962 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001964 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001966 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 } finally {
1968 Binder.restoreCallingIdentity(ident);
1969 }
1970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001971
satok42c5a162011-05-26 16:46:14 +09001972 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001973 public boolean showSoftInput(IInputMethodClient client, int flags,
1974 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001975 if (!calledFromValidUser()) {
1976 return false;
1977 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001978 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 long ident = Binder.clearCallingIdentity();
1980 try {
1981 synchronized (mMethodMap) {
1982 if (mCurClient == null || client == null
1983 || mCurClient.client.asBinder() != client.asBinder()) {
1984 try {
1985 // We need to check if this is the current client with
1986 // focus in the window manager, to allow this call to
1987 // be made before input is started in it.
1988 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001989 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001990 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001996
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001997 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001998 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
2000 } finally {
2001 Binder.restoreCallingIdentity(ident);
2002 }
2003 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002004
The Android Open Source Project4df24232009-03-05 14:34:35 -08002005 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 mShowRequested = true;
2007 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2008 mShowExplicitlyRequested = true;
2009 }
2010 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2011 mShowExplicitlyRequested = true;
2012 mShowForced = true;
2013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002014
Dianne Hackborncc278702009-09-02 23:07:23 -07002015 if (!mSystemReady) {
2016 return false;
2017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002018
The Android Open Source Project4df24232009-03-05 14:34:35 -08002019 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002021 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002022 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2023 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2024 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002026 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002027 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002028 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002029 | Context.BIND_TREAT_LIKE_ACTIVITY
2030 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002031 mVisibleBound = true;
2032 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002033 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002035 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 // The client has asked to have the input method shown, but
2037 // we have been sitting here too long with a connection to the
2038 // service and no interface received, so let's disconnect/connect
2039 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002040 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002042 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002044 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002045 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002046 } else {
2047 if (DEBUG) {
2048 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2049 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
The Android Open Source Project4df24232009-03-05 14:34:35 -08002053 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
satok42c5a162011-05-26 16:46:14 +09002056 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002057 public boolean hideSoftInput(IInputMethodClient client, int flags,
2058 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002059 if (!calledFromValidUser()) {
2060 return false;
2061 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002062 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 long ident = Binder.clearCallingIdentity();
2064 try {
2065 synchronized (mMethodMap) {
2066 if (mCurClient == null || client == null
2067 || mCurClient.client.asBinder() != client.asBinder()) {
2068 try {
2069 // We need to check if this is the current client with
2070 // focus in the window manager, to allow this call to
2071 // be made before input is started in it.
2072 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002073 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2074 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002075 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
2077 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002081
Joe Onorato8a9b2202010-02-26 18:56:32 -08002082 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002083 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
2085 } finally {
2086 Binder.restoreCallingIdentity(ident);
2087 }
2088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002089
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2092 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002093 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 -08002094 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002097 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 -08002098 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002100
2101 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2102 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2103 // to be updated with the new value sent from IME process. Even in such a transient state
2104 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2105 // application process as a valid request, and have even promised such a behavior with CTS
2106 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2107 // IMMS#InputShown indicates that the software keyboard is shown.
2108 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2109 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2110 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002112 if (shouldHideSoftInput) {
2113 // The IME will report its visible state again after the following message finally
2114 // delivered to the IME process as an IPC. Hence the inconsistency between
2115 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2116 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2118 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2119 res = true;
2120 } else {
2121 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002123 if (mHaveConnection && mVisibleBound) {
2124 mContext.unbindService(mVisibleConnection);
2125 mVisibleBound = false;
2126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 mInputShown = false;
2128 mShowRequested = false;
2129 mShowExplicitlyRequested = false;
2130 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
satok42c5a162011-05-26 16:46:14 +09002134 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002135 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2136 int controlFlags, int softInputMode, int windowFlags,
2137 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002138 // Needs to check the validity before clearing calling identity
2139 final boolean calledFromValidUser = calledFromValidUser();
2140
Dianne Hackborn7663d802012-02-24 13:08:49 -08002141 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 long ident = Binder.clearCallingIdentity();
2143 try {
2144 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002146 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002148 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149
Dianne Hackborn7663d802012-02-24 13:08:49 -08002150 ClientState cs = mClients.get(client.asBinder());
2151 if (cs == null) {
2152 throw new IllegalArgumentException("unknown client "
2153 + client.asBinder());
2154 }
2155
2156 try {
2157 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2158 // Check with the window manager to make sure this client actually
2159 // has a window with focus. If not, reject. This is thread safe
2160 // because if the focus changes some time before or after, the
2161 // next client receiving focus that has any interest in input will
2162 // be calling through here after that change happens.
2163 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2164 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2165 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002167 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002169
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002170 if (!calledFromValidUser) {
2171 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2172 Slog.w(TAG, "If you want to interect with IME, you need "
2173 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2174 hideCurrentInputLocked(0, null);
2175 return null;
2176 }
2177
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002178 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002179 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002180 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002181 if (attribute != null) {
2182 return startInputUncheckedLocked(cs, inputContext, attribute,
2183 controlFlags);
2184 }
2185 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002186 }
2187 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002189 // Should we auto-show the IME even if the caller has not
2190 // specified what should be done with it?
2191 // We only do this automatically if the window can resize
2192 // to accommodate the IME (so what the user sees will give
2193 // them good context without input information being obscured
2194 // by the IME) or if running on a large screen where there
2195 // is more room for the target window + IME.
2196 final boolean doAutoShow =
2197 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2198 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2199 || mRes.getConfiguration().isLayoutSizeAtLeast(
2200 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002201 final boolean isTextEditor =
2202 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2203
2204 // We want to start input before showing the IME, but after closing
2205 // it. We want to do this after closing it to help the IME disappear
2206 // more quickly (not get stuck behind it initializing itself for the
2207 // new focused input, even if its window wants to hide the IME).
2208 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2211 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002212 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2214 // There is no focus view, and this window will
2215 // be behind any soft input window, so hide the
2216 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002217 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002220 } else if (isTextEditor && doAutoShow && (softInputMode &
2221 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 // There is a focus view, and we are navigating forward
2223 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002224 // We only do this automatically if the window can resize
2225 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002226 // them good context without input information being obscured
2227 // by the IME) or if running on a large screen where there
2228 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002229 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002230 if (attribute != null) {
2231 res = startInputUncheckedLocked(cs, inputContext, attribute,
2232 controlFlags);
2233 didStart = true;
2234 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
2237 break;
2238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2239 // Do nothing.
2240 break;
2241 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2242 if ((softInputMode &
2243 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002244 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002245 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
2247 break;
2248 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002249 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002250 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 break;
2252 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2253 if ((softInputMode &
2254 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002255 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002256 if (attribute != null) {
2257 res = startInputUncheckedLocked(cs, inputContext, attribute,
2258 controlFlags);
2259 didStart = true;
2260 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263 break;
2264 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002265 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 if (attribute != null) {
2267 res = startInputUncheckedLocked(cs, inputContext, attribute,
2268 controlFlags);
2269 didStart = true;
2270 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002271 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 break;
2273 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002274
2275 if (!didStart && attribute != null) {
2276 res = startInputUncheckedLocked(cs, inputContext, attribute,
2277 controlFlags);
2278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280 } finally {
2281 Binder.restoreCallingIdentity(ident);
2282 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283
2284 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
satok42c5a162011-05-26 16:46:14 +09002287 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002288 public void showInputMethodPickerFromClient(
2289 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002290 if (!calledFromValidUser()) {
2291 return;
2292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 synchronized (mMethodMap) {
2294 if (mCurClient == null || client == null
2295 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002296 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002297 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
2299
satok440aab52010-11-25 09:43:11 +09002300 // Always call subtype picker, because subtype picker is a superset of input method
2301 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002302 mHandler.sendMessage(mCaller.obtainMessageI(
2303 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002304 }
2305 }
2306
satok42c5a162011-05-26 16:46:14 +09002307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002309 if (!calledFromValidUser()) {
2310 return;
2311 }
satok28203512010-11-24 11:06:49 +09002312 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2313 }
2314
satok42c5a162011-05-26 16:46:14 +09002315 @Override
satok28203512010-11-24 11:06:49 +09002316 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 if (!calledFromValidUser()) {
2318 return;
2319 }
satok28203512010-11-24 11:06:49 +09002320 synchronized (mMethodMap) {
2321 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002322 setInputMethodWithSubtypeIdLocked(token, id,
2323 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2324 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002325 } else {
2326 setInputMethod(token, id);
2327 }
2328 }
satokab751aa2010-09-14 19:17:36 +09002329 }
2330
satok42c5a162011-05-26 16:46:14 +09002331 @Override
satokb416a712010-11-25 20:42:14 +09002332 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002333 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002334 if (!calledFromValidUser()) {
2335 return;
2336 }
satokb416a712010-11-25 20:42:14 +09002337 synchronized (mMethodMap) {
2338 if (mCurClient == null || client == null
2339 || mCurClient.client.asBinder() != client.asBinder()) {
2340 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2341 }
satok7fee71f2010-12-17 18:54:26 +09002342 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2343 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002344 }
2345 }
2346
satok4fc87d62011-05-20 16:13:43 +09002347 @Override
satok735cf382010-11-11 20:40:09 +09002348 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002349 if (!calledFromValidUser()) {
2350 return false;
2351 }
satok735cf382010-11-11 20:40:09 +09002352 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002353 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002354 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002355 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002356 lastImi = mMethodMap.get(lastIme.first);
2357 } else {
2358 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002359 }
satok4fc87d62011-05-20 16:13:43 +09002360 String targetLastImiId = null;
2361 int subtypeId = NOT_A_SUBTYPE_ID;
2362 if (lastIme != null && lastImi != null) {
2363 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2364 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2365 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2366 : mCurrentSubtype.hashCode();
2367 // If the last IME is the same as the current IME and the last subtype is not
2368 // defined, there is no need to switch to the last IME.
2369 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2370 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002371 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002372 }
2373 }
2374
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002375 if (TextUtils.isEmpty(targetLastImiId)
2376 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002377 // This is a safety net. If the currentSubtype can't be added to the history
2378 // and the framework couldn't find the last ime, we will make the last ime be
2379 // the most applicable enabled keyboard subtype of the system imes.
2380 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2381 if (enabled != null) {
2382 final int N = enabled.size();
2383 final String locale = mCurrentSubtype == null
2384 ? mRes.getConfiguration().locale.toString()
2385 : mCurrentSubtype.getLocale();
2386 for (int i = 0; i < N; ++i) {
2387 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002388 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002389 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002390 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2391 InputMethodUtils.getSubtypes(imi),
2392 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002393 if (keyboardSubtype != null) {
2394 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002395 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002396 imi, keyboardSubtype.hashCode());
2397 if(keyboardSubtype.getLocale().equals(locale)) {
2398 break;
2399 }
2400 }
2401 }
2402 }
2403 }
2404 }
2405
2406 if (!TextUtils.isEmpty(targetLastImiId)) {
2407 if (DEBUG) {
2408 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2409 + ", from: " + mCurMethodId + ", " + subtypeId);
2410 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002411 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002412 return true;
2413 } else {
2414 return false;
2415 }
satok735cf382010-11-11 20:40:09 +09002416 }
2417 }
2418
satoke7c6998e2011-06-03 17:57:59 +09002419 @Override
satok688bd472012-02-09 20:09:17 +09002420 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 if (!calledFromValidUser()) {
2422 return false;
2423 }
satok688bd472012-02-09 20:09:17 +09002424 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002425 if (!calledWithValidToken(token)) {
2426 final int uid = Binder.getCallingUid();
2427 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2428 + " token:" + token);
2429 return false;
2430 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002431 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002432 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2433 if (nextSubtype == null) {
2434 return false;
2435 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002436 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2437 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002438 return true;
2439 }
2440 }
2441
2442 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002443 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2444 if (!calledFromValidUser()) {
2445 return false;
2446 }
2447 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002448 if (!calledWithValidToken(token)) {
2449 final int uid = Binder.getCallingUid();
2450 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2451 + "token. uid:" + uid + " token:" + token);
2452 return false;
2453 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002454 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002455 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2456 if (nextSubtype == null) {
2457 return false;
2458 }
2459 return true;
2460 }
2461 }
2462
2463 @Override
satok68f1b782011-04-11 14:26:04 +09002464 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002465 if (!calledFromValidUser()) {
2466 return null;
2467 }
satok68f1b782011-04-11 14:26:04 +09002468 synchronized (mMethodMap) {
2469 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2470 // TODO: Handle the case of the last IME with no subtypes
2471 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2472 || TextUtils.isEmpty(lastIme.second)) return null;
2473 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2474 if (lastImi == null) return null;
2475 try {
2476 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002477 final int lastSubtypeId =
2478 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002479 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2480 return null;
2481 }
2482 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002483 } catch (NumberFormatException e) {
2484 return null;
2485 }
2486 }
2487 }
2488
satoke7c6998e2011-06-03 17:57:59 +09002489 @Override
satokee5e77c2011-09-02 18:50:15 +09002490 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002491 if (!calledFromValidUser()) {
2492 return;
2493 }
satok91e88122011-07-18 11:11:42 +09002494 // By this IPC call, only a process which shares the same uid with the IME can add
2495 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002496 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002497 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002498 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002499 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002500 final String[] packageInfos;
2501 try {
2502 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2503 } catch (RemoteException e) {
2504 Slog.e(TAG, "Failed to get package infos");
2505 return;
2506 }
satok91e88122011-07-18 11:11:42 +09002507 if (packageInfos != null) {
2508 final int packageNum = packageInfos.length;
2509 for (int i = 0; i < packageNum; ++i) {
2510 if (packageInfos[i].equals(imi.getPackageName())) {
2511 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002512 final long ident = Binder.clearCallingIdentity();
2513 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002514 buildInputMethodListLocked(mMethodList, mMethodMap,
2515 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002516 } finally {
2517 Binder.restoreCallingIdentity(ident);
2518 }
satokee5e77c2011-09-02 18:50:15 +09002519 return;
satok91e88122011-07-18 11:11:42 +09002520 }
2521 }
2522 }
satoke7c6998e2011-06-03 17:57:59 +09002523 }
satokee5e77c2011-09-02 18:50:15 +09002524 return;
satoke7c6998e2011-06-03 17:57:59 +09002525 }
2526
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002527 @Override
2528 public int getInputMethodWindowVisibleHeight() {
2529 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2530 }
2531
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002532 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002533 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002534 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002535 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002536 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002537 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002538 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2539 if (DEBUG) {
2540 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2541 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2542 + " actual: " + sequenceNumber);
2543 }
2544 return;
2545 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002546 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2547 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002548 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002549 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002550 }
2551 }
2552
satok28203512010-11-24 11:06:49 +09002553 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002555 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2556 }
2557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002559 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2560 if (token == null) {
2561 if (mContext.checkCallingOrSelfPermission(
2562 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2563 != PackageManager.PERMISSION_GRANTED) {
2564 throw new SecurityException(
2565 "Using null token requires permission "
2566 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002568 } else if (mCurToken != token) {
2569 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2570 + " token: " + token);
2571 return;
2572 }
2573
2574 final long ident = Binder.clearCallingIdentity();
2575 try {
2576 setInputMethodLocked(id, subtypeId);
2577 } finally {
2578 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580 }
2581
satok42c5a162011-05-26 16:46:14 +09002582 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002584 if (!calledFromValidUser()) {
2585 return;
2586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002588 if (!calledWithValidToken(token)) {
2589 final int uid = Binder.getCallingUid();
2590 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2591 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 return;
2593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 long ident = Binder.clearCallingIdentity();
2595 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002596 hideCurrentInputLocked(flags, null);
2597 } finally {
2598 Binder.restoreCallingIdentity(ident);
2599 }
2600 }
2601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002602
satok42c5a162011-05-26 16:46:14 +09002603 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002604 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002605 if (!calledFromValidUser()) {
2606 return;
2607 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002608 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002609 if (!calledWithValidToken(token)) {
2610 final int uid = Binder.getCallingUid();
2611 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2612 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002613 return;
2614 }
2615 long ident = Binder.clearCallingIdentity();
2616 try {
2617 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 } finally {
2619 Binder.restoreCallingIdentity(ident);
2620 }
2621 }
2622 }
2623
2624 void setEnabledSessionInMainThread(SessionState session) {
2625 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002626 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002628 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002629 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 } catch (RemoteException e) {
2631 }
2632 }
2633 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002634 if (mEnabledSession != null && mEnabledSession.session != null) {
2635 try {
2636 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2637 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2638 } catch (RemoteException e) {
2639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 }
2641 }
2642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002643
satok42c5a162011-05-26 16:46:14 +09002644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002646 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002648 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002649 final boolean showAuxSubtypes;
2650 switch (msg.arg1) {
2651 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2652 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2653 // implemented so that auxiliary subtypes will be excluded when the soft
2654 // keyboard is invisible.
2655 showAuxSubtypes = mInputShown;
2656 break;
2657 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2658 showAuxSubtypes = true;
2659 break;
2660 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2661 showAuxSubtypes = false;
2662 break;
2663 default:
2664 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2665 return false;
2666 }
2667 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002668 return true;
2669
satok47a44912010-10-06 16:03:58 +09002670 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002671 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002672 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 args.recycle();
satok217f5482010-12-15 05:19:19 +09002674 return true;
2675
2676 case MSG_SHOW_IM_CONFIG:
2677 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002678 return true;
2679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 case MSG_UNBIND_INPUT:
2683 try {
2684 ((IInputMethod)msg.obj).unbindInput();
2685 } catch (RemoteException e) {
2686 // There is nothing interesting about the method dying.
2687 }
2688 return true;
2689 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002690 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 try {
2692 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2693 } catch (RemoteException e) {
2694 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002695 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 return true;
2697 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002698 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002700 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002701 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002702 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 } catch (RemoteException e) {
2704 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002705 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 return true;
2707 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002708 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002710 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002711 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002712 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 } catch (RemoteException e) {
2714 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002715 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 return true;
2717 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002718 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002720 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2722 } catch (RemoteException e) {
2723 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002726 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002727 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002728 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002729 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002731 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002733 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002734 // Dispose the channel if the input method is not local to this process
2735 // because the remote proxy will get its own copy when unparceled.
2736 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002737 channel.dispose();
2738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002740 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002746 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 try {
2748 SessionState session = (SessionState)args.arg1;
2749 setEnabledSessionInMainThread(session);
2750 session.method.startInput((IInputContext)args.arg2,
2751 (EditorInfo)args.arg3);
2752 } catch (RemoteException e) {
2753 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002754 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 return true;
2756 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 try {
2759 SessionState session = (SessionState)args.arg1;
2760 setEnabledSessionInMainThread(session);
2761 session.method.restartInput((IInputContext)args.arg2,
2762 (EditorInfo)args.arg3);
2763 } catch (RemoteException e) {
2764 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002765 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 case MSG_UNBIND_METHOD:
2771 try {
2772 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2773 } catch (RemoteException e) {
2774 // There is nothing interesting about the last client dying.
2775 }
2776 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002777 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002778 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002779 IInputMethodClient client = (IInputMethodClient)args.arg1;
2780 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002782 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002784 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002785 } finally {
2786 // Dispose the channel if the input method is not local to this process
2787 // because the remote proxy will get its own copy when unparceled.
2788 if (res.channel != null && Binder.isProxy(client)) {
2789 res.channel.dispose();
2790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002792 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002794 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002795 case MSG_SET_ACTIVE:
2796 try {
2797 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2798 } catch (RemoteException e) {
2799 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2800 + ((ClientState)msg.obj).pid + " uid "
2801 + ((ClientState)msg.obj).uid);
2802 }
2803 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002804 case MSG_SET_INTERACTIVE:
2805 handleSetInteractive(msg.arg1 != 0);
2806 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002807 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2808 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002809 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002810 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002811 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002812 } catch (RemoteException e) {
2813 Slog.w(TAG, "Got RemoteException sending "
2814 + "setUserActionNotificationSequenceNumber("
2815 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002816 + clientState.pid + " uid "
2817 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002818 }
2819 return true;
2820 }
satok01038492012-04-09 21:08:27 +09002821
2822 // --------------------------------------------------------------
2823 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002824 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002825 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827 return false;
2828 }
2829
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002830 private void handleSetInteractive(final boolean interactive) {
2831 synchronized (mMethodMap) {
2832 mIsInteractive = interactive;
2833 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2834
2835 // Inform the current client of the change in active status
2836 if (mCurClient != null && mCurClient.client != null) {
2837 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2838 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2839 }
2840 }
2841 }
2842
satokdc9ddae2011-10-06 12:22:36 +09002843 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002844 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2845 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002846 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002847 if (DEBUG) {
2848 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2849 }
satok723a27e2010-11-11 14:58:11 +09002850 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002851 return true;
2852 }
2853
2854 return false;
2855 }
2856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002858 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002859 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002860 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002861 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 list.clear();
2864 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002865
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002866 // Use for queryIntentServicesAsUser
2867 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002869 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002871 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2872 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002873
satoke7c6998e2011-06-03 17:57:59 +09002874 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2875 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 for (int i = 0; i < services.size(); ++i) {
2877 ResolveInfo ri = services.get(i);
2878 ServiceInfo si = ri.serviceInfo;
2879 ComponentName compName = new ComponentName(si.packageName, si.name);
2880 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2881 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002882 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 + ": it does not require the permission "
2884 + android.Manifest.permission.BIND_INPUT_METHOD);
2885 continue;
2886 }
2887
Joe Onorato8a9b2202010-02-26 18:56:32 -08002888 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
2890 try {
satoke7c6998e2011-06-03 17:57:59 +09002891 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002893 final String id = p.getId();
2894 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895
2896 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002897 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002899 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002900 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002903
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002904 if (resetDefaultEnabledIme) {
2905 final ArrayList<InputMethodInfo> defaultEnabledIme =
2906 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2907 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2908 final InputMethodInfo imi = defaultEnabledIme.get(i);
2909 if (DEBUG) {
2910 Slog.d(TAG, "--- enable ime = " + imi);
2911 }
2912 setInputMethodEnabledLocked(imi.getId(), true);
2913 }
2914 }
2915
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002916 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002917 if (!TextUtils.isEmpty(defaultImiId)) {
2918 if (!map.containsKey(defaultImiId)) {
2919 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2920 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002921 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002922 }
2923 } else {
2924 // Double check that the default IME is certainly enabled.
2925 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002926 }
2927 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002928 // Here is not the perfect place to reset the switching controller. Ideally
2929 // mSwitchingController and mSettings should be able to share the same state.
2930 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2931 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002932 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002936
satok217f5482010-12-15 05:19:19 +09002937 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002938 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002939 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002940 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2941 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002942 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002943 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002944 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002945 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002946 }
2947
2948 private void showConfigureInputMethods() {
2949 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2950 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2951 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2952 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002953 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002954 }
2955
satok2c93efc2012-04-02 19:33:47 +09002956 private boolean isScreenLocked() {
2957 return mKeyguardManager != null
2958 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2959 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002960
Seigo Nonaka14e13912015-05-06 21:04:13 -07002961 private void showInputMethodMenu(boolean showAuxSubtypes) {
2962 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002965 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002966
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002967 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002968 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002969 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002970
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002971 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002972 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002973 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2974 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002975 if (immis == null || immis.size() == 0) {
2976 return;
2977 }
2978
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002979 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980
satok688bd472012-02-09 20:09:17 +09002981 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002982 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002983 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002984
satokc3690562012-01-10 20:14:43 +09002985 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002986 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002987 if (currentSubtype != null) {
2988 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002989 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2990 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002991 }
2992 }
2993
Ken Wakasa761eb372011-03-04 19:06:18 +09002994 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002995 mIms = new InputMethodInfo[N];
2996 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002997 int checkedItem = 0;
2998 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002999 final ImeSubtypeListItem item = imList.get(i);
3000 mIms[i] = item.mImi;
3001 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003002 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003003 int subtypeId = mSubtypeIds[i];
3004 if ((subtypeId == NOT_A_SUBTYPE_ID)
3005 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3006 || (subtypeId == lastInputMethodSubtypeId)) {
3007 checkedItem = i;
3008 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003011
3012 final Context settingsContext = new ContextThemeWrapper(context,
3013 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3014
3015 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003016 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3017 @Override
3018 public void onCancel(DialogInterface dialog) {
3019 hideInputMethodMenu();
3020 }
3021 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003022
3023 final Context dialogContext = mDialogBuilder.getContext();
3024 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3025 com.android.internal.R.styleable.DialogPreference,
3026 com.android.internal.R.attr.alertDialogStyle, 0);
3027 final Drawable dialogIcon = a.getDrawable(
3028 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3029 a.recycle();
3030
3031 mDialogBuilder.setIcon(dialogIcon);
3032
3033 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3034 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003035 final View tv = inflater.inflate(
3036 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3037 mDialogBuilder.setCustomTitle(tv);
3038
3039 // Setup layout for a toggle switch of the hardware keyboard
3040 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003041 mSwitchingDialogTitleView
3042 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3043 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3044 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003045 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003046 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003047 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003048 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3049 @Override
3050 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003051 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003052 // Ensure that the input method dialog is dismissed when changing
3053 // the hardware keyboard state.
3054 hideInputMethodMenu();
3055 }
3056 });
3057
Alan Viverette505e3ab2014-11-24 15:22:11 -08003058 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003059 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3060 final OnClickListener choiceListener = new OnClickListener() {
3061 @Override
3062 public void onClick(final DialogInterface dialog, final int which) {
3063 synchronized (mMethodMap) {
3064 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3065 || mSubtypeIds.length <= which) {
3066 return;
satok01038492012-04-09 21:08:27 +09003067 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003068 final InputMethodInfo im = mIms[which];
3069 int subtypeId = mSubtypeIds[which];
3070 adapter.mCheckedItem = which;
3071 adapter.notifyDataSetChanged();
3072 hideInputMethodMenu();
3073 if (im != null) {
3074 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3075 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003076 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003077 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003079 }
3080 }
3081 };
3082 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003084 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003085 final OnClickListener positiveListener = new OnClickListener() {
3086 @Override
3087 public void onClick(DialogInterface dialog, int whichButton) {
3088 showConfigureInputMethods();
3089 }
3090 };
satok82beadf2010-12-27 19:03:06 +09003091 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003092 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003095 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 mSwitchingDialog.getWindow().setType(
3097 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003098 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3099 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003100 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003101 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 mSwitchingDialog.show();
3103 }
3104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003105
Ken Wakasa05dbb652011-08-22 15:22:43 +09003106 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3107 private final LayoutInflater mInflater;
3108 private final int mTextViewResourceId;
3109 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003110 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003111 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3112 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3113 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003114
Ken Wakasa05dbb652011-08-22 15:22:43 +09003115 mTextViewResourceId = textViewResourceId;
3116 mItemsList = itemsList;
3117 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003118 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003119 }
3120
3121 @Override
3122 public View getView(int position, View convertView, ViewGroup parent) {
3123 final View view = convertView != null ? convertView
3124 : mInflater.inflate(mTextViewResourceId, null);
3125 if (position < 0 || position >= mItemsList.size()) return view;
3126 final ImeSubtypeListItem item = mItemsList.get(position);
3127 final CharSequence imeName = item.mImeName;
3128 final CharSequence subtypeName = item.mSubtypeName;
3129 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3130 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3131 if (TextUtils.isEmpty(subtypeName)) {
3132 firstTextView.setText(imeName);
3133 secondTextView.setVisibility(View.GONE);
3134 } else {
3135 firstTextView.setText(subtypeName);
3136 secondTextView.setText(imeName);
3137 secondTextView.setVisibility(View.VISIBLE);
3138 }
3139 final RadioButton radioButton =
3140 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3141 radioButton.setChecked(position == mCheckedItem);
3142 return view;
3143 }
3144 }
3145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003147 synchronized (mMethodMap) {
3148 hideInputMethodMenuLocked();
3149 }
3150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003151
The Android Open Source Project10592532009-03-18 17:39:46 -07003152 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003153 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154
The Android Open Source Project10592532009-03-18 17:39:46 -07003155 if (mSwitchingDialog != null) {
3156 mSwitchingDialog.dismiss();
3157 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003159
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003160 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003161 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003162 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003166
satok42c5a162011-05-26 16:46:14 +09003167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003169 // TODO: Make this work even for non-current users?
3170 if (!calledFromValidUser()) {
3171 return false;
3172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 synchronized (mMethodMap) {
3174 if (mContext.checkCallingOrSelfPermission(
3175 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3176 != PackageManager.PERMISSION_GRANTED) {
3177 throw new SecurityException(
3178 "Requires permission "
3179 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3180 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 long ident = Binder.clearCallingIdentity();
3183 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003184 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 } finally {
3186 Binder.restoreCallingIdentity(ident);
3187 }
3188 }
3189 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003190
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003191 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3192 // Make sure this is a valid input method.
3193 InputMethodInfo imm = mMethodMap.get(id);
3194 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003195 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003196 }
3197
satokd87c2592010-09-29 11:52:06 +09003198 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3199 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003200
satokd87c2592010-09-29 11:52:06 +09003201 if (enabled) {
3202 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3203 if (pair.first.equals(id)) {
3204 // We are enabling this input method, but it is already enabled.
3205 // Nothing to do. The previous state was enabled.
3206 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003207 }
3208 }
satokd87c2592010-09-29 11:52:06 +09003209 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3210 // Previous state was disabled.
3211 return false;
3212 } else {
3213 StringBuilder builder = new StringBuilder();
3214 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3215 builder, enabledInputMethodsList, id)) {
3216 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003217 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003218 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3219 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3220 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003221 }
3222 // Previous state was enabled.
3223 return true;
3224 } else {
3225 // We are disabling the input method but it is already disabled.
3226 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003227 return false;
3228 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003229 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003230 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003231
satok723a27e2010-11-11 14:58:11 +09003232 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3233 boolean setSubtypeOnly) {
3234 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003235 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003236
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003237 mCurUserActionNotificationSequenceNumber =
3238 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3239 if (DEBUG) {
3240 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3241 + mCurUserActionNotificationSequenceNumber);
3242 }
3243
3244 if (mCurClient != null && mCurClient.client != null) {
3245 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3246 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003247 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003248 }
3249
satok723a27e2010-11-11 14:58:11 +09003250 // Set Subtype here
3251 if (imi == null || subtypeId < 0) {
3252 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003253 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003254 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003255 if (subtypeId < imi.getSubtypeCount()) {
3256 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3257 mSettings.putSelectedSubtype(subtype.hashCode());
3258 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003259 } else {
3260 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003261 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003262 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003263 }
satokab751aa2010-09-14 19:17:36 +09003264 }
satok723a27e2010-11-11 14:58:11 +09003265
satok4c0e7152012-06-20 20:08:44 +09003266 // Workaround.
3267 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3268 // IMEs are not recognized and considered uninstalled.
3269 // Actually, we can't move everything after SystemReady because
3270 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3271 // the default IME here and try cheking the default IME again in systemReady().
3272 // TODO: Do nothing before system ready and implement a separated logic for
3273 // the encryption lock screen.
3274 // TODO: ASEC should be ready before IMMS is instantiated.
3275 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003276 // Set InputMethod here
3277 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3278 }
3279 }
3280
3281 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3282 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3283 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3284 // newDefaultIme is empty when there is no candidate for the selected IME.
3285 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3286 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3287 if (subtypeHashCode != null) {
3288 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003289 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003290 imi, Integer.valueOf(subtypeHashCode));
3291 } catch (NumberFormatException e) {
3292 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3293 }
3294 }
3295 }
3296 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003297 }
3298
satok4e4569d2010-11-19 18:45:53 +09003299 // If there are no selected shortcuts, tries finding the most applicable ones.
3300 private Pair<InputMethodInfo, InputMethodSubtype>
3301 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3302 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3303 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003304 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003305 boolean foundInSystemIME = false;
3306
3307 // Search applicable subtype for each InputMethodInfo
3308 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003309 final String imiId = imi.getId();
3310 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3311 continue;
3312 }
satokcd7cd292010-11-20 15:46:23 +09003313 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003314 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003315 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003316 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003317 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003318 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003319 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003320 }
satokdf31ae62011-01-15 06:19:44 +09003321 // 2. Search by the system locale from enabledSubtypes.
3322 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003323 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003324 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003325 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003326 }
satoka86f5e42011-09-02 17:12:42 +09003327 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003328 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003329 final ArrayList<InputMethodSubtype> subtypesForSearch =
3330 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003331 ? InputMethodUtils.getSubtypes(imi)
3332 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003333 // 4. Search by the current subtype's locale from all subtypes.
3334 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003335 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003336 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003337 }
3338 // 5. Search by the system locale from all subtypes.
3339 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003340 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003341 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003342 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003343 }
satokcd7cd292010-11-20 15:46:23 +09003344 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003345 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003346 // The current input method is the most applicable IME.
3347 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003348 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003349 break;
satok7599a7f2010-12-22 13:45:23 +09003350 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003351 // The system input method is 2nd applicable IME.
3352 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003353 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003354 if ((imi.getServiceInfo().applicationInfo.flags
3355 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3356 foundInSystemIME = true;
3357 }
satok4e4569d2010-11-19 18:45:53 +09003358 }
3359 }
3360 }
3361 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003362 if (mostApplicableIMI != null) {
3363 Slog.w(TAG, "Most applicable shortcut input method was:"
3364 + mostApplicableIMI.getId());
3365 if (mostApplicableSubtype != null) {
3366 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3367 + "," + mostApplicableSubtype.getMode() + ","
3368 + mostApplicableSubtype.getLocale());
3369 }
3370 }
satok4e4569d2010-11-19 18:45:53 +09003371 }
satokcd7cd292010-11-20 15:46:23 +09003372 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003373 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003374 } else {
3375 return null;
3376 }
3377 }
3378
satokab751aa2010-09-14 19:17:36 +09003379 /**
3380 * @return Return the current subtype of this input method.
3381 */
satok42c5a162011-05-26 16:46:14 +09003382 @Override
satokab751aa2010-09-14 19:17:36 +09003383 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003384 // TODO: Make this work even for non-current users?
3385 if (!calledFromValidUser()) {
3386 return null;
3387 }
3388 synchronized (mMethodMap) {
3389 return getCurrentInputMethodSubtypeLocked();
3390 }
3391 }
3392
3393 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003394 if (mCurMethodId == null) {
3395 return null;
3396 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003397 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003398 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3399 if (imi == null || imi.getSubtypeCount() == 0) {
3400 return null;
satok4e4569d2010-11-19 18:45:53 +09003401 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003402 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003403 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3404 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003405 if (subtypeId == NOT_A_SUBTYPE_ID) {
3406 // If there are no selected subtypes, the framework will try to find
3407 // the most applicable subtype from explicitly or implicitly enabled
3408 // subtypes.
3409 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003410 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 // If there is only one explicitly or implicitly enabled subtype,
3412 // just returns it.
3413 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3414 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3415 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003416 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003417 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003418 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003419 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003420 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3422 true);
satok4e4569d2010-11-19 18:45:53 +09003423 }
satok3ef8b292010-11-23 06:06:29 +09003424 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003425 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003426 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003427 }
3428 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003430 }
3431
satok4e4569d2010-11-19 18:45:53 +09003432 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003433 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003434 @Override
satok4e4569d2010-11-19 18:45:53 +09003435 public List getShortcutInputMethodsAndSubtypes() {
3436 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003437 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003438 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003439 // If there are no selected shortcut subtypes, the framework will try to find
3440 // the most applicable subtype from all subtypes whose mode is
3441 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003442 Pair<InputMethodInfo, InputMethodSubtype> info =
3443 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003445 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003446 ret.add(info.first);
3447 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003448 }
satok3da92232011-01-11 22:46:30 +09003449 return ret;
satokf3db1af2010-11-23 13:34:33 +09003450 }
satokf3db1af2010-11-23 13:34:33 +09003451 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3452 ret.add(imi);
3453 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3454 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003455 }
3456 }
satokf3db1af2010-11-23 13:34:33 +09003457 return ret;
satok4e4569d2010-11-19 18:45:53 +09003458 }
3459 }
3460
satok42c5a162011-05-26 16:46:14 +09003461 @Override
satokb66d2872010-11-10 01:04:04 +09003462 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003463 // TODO: Make this work even for non-current users?
3464 if (!calledFromValidUser()) {
3465 return false;
3466 }
satokb66d2872010-11-10 01:04:04 +09003467 synchronized (mMethodMap) {
3468 if (subtype != null && mCurMethodId != null) {
3469 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003470 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003471 if (subtypeId != NOT_A_SUBTYPE_ID) {
3472 setInputMethodLocked(mCurMethodId, subtypeId);
3473 return true;
3474 }
3475 }
3476 return false;
3477 }
3478 }
3479
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003480 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003481 private static class InputMethodFileManager {
3482 private static final String SYSTEM_PATH = "system";
3483 private static final String INPUT_METHOD_PATH = "inputmethod";
3484 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3485 private static final String NODE_SUBTYPES = "subtypes";
3486 private static final String NODE_SUBTYPE = "subtype";
3487 private static final String NODE_IMI = "imi";
3488 private static final String ATTR_ID = "id";
3489 private static final String ATTR_LABEL = "label";
3490 private static final String ATTR_ICON = "icon";
3491 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3492 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3493 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3494 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3495 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3496 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003497 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003498 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003499 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003500 if (methodMap == null) {
3501 throw new NullPointerException("methodMap is null");
3502 }
3503 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003504 final File systemDir = userId == UserHandle.USER_OWNER
3505 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3506 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003507 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003508 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003509 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3510 }
3511 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3512 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3513 if (!subtypeFile.exists()) {
3514 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003515 writeAdditionalInputMethodSubtypes(
3516 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003517 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003518 readAdditionalInputMethodSubtypes(
3519 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003520 }
3521 }
3522
3523 private void deleteAllInputMethodSubtypes(String imiId) {
3524 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003525 mAdditionalSubtypesMap.remove(imiId);
3526 writeAdditionalInputMethodSubtypes(
3527 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003528 }
3529 }
3530
3531 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003532 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003533 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003534 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003535 final int N = additionalSubtypes.length;
3536 for (int i = 0; i < N; ++i) {
3537 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003538 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003539 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003540 } else {
3541 Slog.w(TAG, "Duplicated subtype definition found: "
3542 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003543 }
3544 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003545 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3546 writeAdditionalInputMethodSubtypes(
3547 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003548 }
3549 }
3550
3551 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3552 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003553 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003554 }
3555 }
3556
3557 private static void writeAdditionalInputMethodSubtypes(
3558 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3559 HashMap<String, InputMethodInfo> methodMap) {
3560 // Safety net for the case that this function is called before methodMap is set.
3561 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3562 FileOutputStream fos = null;
3563 try {
3564 fos = subtypesFile.startWrite();
3565 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003566 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003567 out.startDocument(null, true);
3568 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3569 out.startTag(null, NODE_SUBTYPES);
3570 for (String imiId : allSubtypes.keySet()) {
3571 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3572 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3573 continue;
3574 }
3575 out.startTag(null, NODE_IMI);
3576 out.attribute(null, ATTR_ID, imiId);
3577 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3578 final int N = subtypesList.size();
3579 for (int i = 0; i < N; ++i) {
3580 final InputMethodSubtype subtype = subtypesList.get(i);
3581 out.startTag(null, NODE_SUBTYPE);
3582 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3583 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3584 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3585 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3586 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3587 out.attribute(null, ATTR_IS_AUXILIARY,
3588 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3589 out.endTag(null, NODE_SUBTYPE);
3590 }
3591 out.endTag(null, NODE_IMI);
3592 }
3593 out.endTag(null, NODE_SUBTYPES);
3594 out.endDocument();
3595 subtypesFile.finishWrite(fos);
3596 } catch (java.io.IOException e) {
3597 Slog.w(TAG, "Error writing subtypes", e);
3598 if (fos != null) {
3599 subtypesFile.failWrite(fos);
3600 }
3601 }
3602 }
3603
3604 private static void readAdditionalInputMethodSubtypes(
3605 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3606 if (allSubtypes == null || subtypesFile == null) return;
3607 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003608 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003609 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003610 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003611 int type = parser.getEventType();
3612 // Skip parsing until START_TAG
3613 while ((type = parser.next()) != XmlPullParser.START_TAG
3614 && type != XmlPullParser.END_DOCUMENT) {}
3615 String firstNodeName = parser.getName();
3616 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3617 throw new XmlPullParserException("Xml doesn't start with subtypes");
3618 }
3619 final int depth =parser.getDepth();
3620 String currentImiId = null;
3621 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3622 while (((type = parser.next()) != XmlPullParser.END_TAG
3623 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3624 if (type != XmlPullParser.START_TAG)
3625 continue;
3626 final String nodeName = parser.getName();
3627 if (NODE_IMI.equals(nodeName)) {
3628 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3629 if (TextUtils.isEmpty(currentImiId)) {
3630 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3631 continue;
3632 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003633 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003634 allSubtypes.put(currentImiId, tempSubtypesArray);
3635 } else if (NODE_SUBTYPE.equals(nodeName)) {
3636 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3637 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3638 continue;
3639 }
3640 final int icon = Integer.valueOf(
3641 parser.getAttributeValue(null, ATTR_ICON));
3642 final int label = Integer.valueOf(
3643 parser.getAttributeValue(null, ATTR_LABEL));
3644 final String imeSubtypeLocale =
3645 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3646 final String imeSubtypeMode =
3647 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3648 final String imeSubtypeExtraValue =
3649 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003650 final boolean isAuxiliary = "1".equals(String.valueOf(
3651 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003652 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3653 .setSubtypeNameResId(label)
3654 .setSubtypeIconResId(icon)
3655 .setSubtypeLocale(imeSubtypeLocale)
3656 .setSubtypeMode(imeSubtypeMode)
3657 .setSubtypeExtraValue(imeSubtypeExtraValue)
3658 .setIsAuxiliary(isAuxiliary)
3659 .build();
satoke7c6998e2011-06-03 17:57:59 +09003660 tempSubtypesArray.add(subtype);
3661 }
3662 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003663 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3664 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003665 return;
satoke7c6998e2011-06-03 17:57:59 +09003666 }
3667 }
3668 }
3669
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003670 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3671 @NonNull
3672 private final Handler mHandler;
3673
3674 LocalServiceImpl(@NonNull final Handler handler) {
3675 mHandler = handler;
3676 }
3677
3678 @Override
3679 public void setInteractive(boolean interactive) {
3680 // Do everything in handler so as not to block the caller.
3681 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3682 interactive ? 1 : 0, 0));
3683 }
3684 }
3685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 @Override
3687 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3688 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3689 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3692 + Binder.getCallingPid()
3693 + ", uid=" + Binder.getCallingUid());
3694 return;
3695 }
3696
3697 IInputMethod method;
3698 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 synchronized (mMethodMap) {
3703 p.println("Current Input Method Manager state:");
3704 int N = mMethodList.size();
3705 p.println(" Input Methods:");
3706 for (int i=0; i<N; i++) {
3707 InputMethodInfo info = mMethodList.get(i);
3708 p.println(" InputMethod #" + i + ":");
3709 info.dump(p, " ");
3710 }
3711 p.println(" Clients:");
3712 for (ClientState ci : mClients.values()) {
3713 p.println(" Client " + ci + ":");
3714 p.println(" client=" + ci.client);
3715 p.println(" inputContext=" + ci.inputContext);
3716 p.println(" sessionRequested=" + ci.sessionRequested);
3717 p.println(" curSession=" + ci.curSession);
3718 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003719 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003721 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3722 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3724 + " mBoundToMethod=" + mBoundToMethod);
3725 p.println(" mCurToken=" + mCurToken);
3726 p.println(" mCurIntent=" + mCurIntent);
3727 method = mCurMethod;
3728 p.println(" mCurMethod=" + mCurMethod);
3729 p.println(" mEnabledSession=" + mEnabledSession);
3730 p.println(" mShowRequested=" + mShowRequested
3731 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3732 + " mShowForced=" + mShowForced
3733 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003734 p.println(" mCurUserActionNotificationSequenceNumber="
3735 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003736 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003737 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003738 p.println(" mSwitchingController:");
3739 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003741
Jeff Brownb88102f2010-09-08 11:49:43 -07003742 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 pw.flush();
3745 try {
3746 client.client.asBinder().dump(fd, args);
3747 } catch (RemoteException e) {
3748 p.println("Input method client dead: " + e);
3749 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003750 } else {
3751 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003753
Jeff Brownb88102f2010-09-08 11:49:43 -07003754 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 pw.flush();
3757 try {
3758 method.asBinder().dump(fd, args);
3759 } catch (RemoteException e) {
3760 p.println("Input method service dead: " + e);
3761 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003762 } else {
3763 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 }
3765 }
3766}