blob: b418abaae4d4dc63904b73435e1940cc8eb2bd77 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
satoke7c6998e2011-06-03 17:57:59 +090035import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import android.annotation.NonNull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.app.ActivityManagerNative;
41import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070042import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090043import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090044import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090045import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090046import android.app.Notification;
47import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070048import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090049import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.ComponentName;
51import android.content.ContentResolver;
52import android.content.Context;
53import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090055import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090057import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070059import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090060import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.pm.PackageManager;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070062import android.content.pm.PackageManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.ResolveInfo;
64import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080065import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070066import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.res.Resources;
68import android.content.res.TypedArray;
69import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080070import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080071import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070072import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040074import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070075import android.os.Debug;
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;
82import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070083import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import 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;
satokf9f01002011-05-19 21:31:50 +090092import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070093import android.util.ArrayMap;
94import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070095import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090097import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090098import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.PrintWriterPrinter;
100import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900101import android.util.Slog;
102import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900103import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700105import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.view.LayoutInflater;
107import android.view.View;
108import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700110import android.view.WindowManagerInternal;
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;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700184 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700186 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900187 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900188 private final HardKeyboardListener mHardKeyboardListener;
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
Seigo Nonaka7309b122015-08-17 18:34:13 -0700712 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900713 @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
Seigo Nonaka7309b122015-08-17 18:34:13 -0700734 public InputMethodManagerService(Context context) {
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));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700743 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800744 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900745 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public void executeMessage(Message msg) {
747 handleMessage(msg);
748 }
Mita Yuned218c72012-12-06 17:18:25 -0800749 }, true /*asyncHandler*/);
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) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001047 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001048 mHardKeyboardListener);
1049 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001050 buildInputMethodListLocked(mMethodList, mMethodMap,
1051 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001052 if (!mImeSelectedOnBoot) {
1053 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001055 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1056 mSettings.getEnabledInputMethodListLocked(),
1057 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001058 }
1059 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001060 try {
1061 startInputInnerLocked();
1062 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001064 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001065 }
1066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001069 // ---------------------------------------------------------------------------------------
1070 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1071 // 1) it comes from the system process
1072 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1073 private boolean calledFromValidUser() {
1074 final int uid = Binder.getCallingUid();
1075 final int userId = UserHandle.getUserId(uid);
1076 if (DEBUG) {
1077 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1078 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1079 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001080 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1081 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001082 }
Kenny Guy2a764942014-04-02 13:29:20 +01001083 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001084 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001086
1087 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1088 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1089 // must not manage background users' states in any functions.
1090 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1091 // by a token.
1092 if (mContext.checkCallingOrSelfPermission(
1093 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1094 == PackageManager.PERMISSION_GRANTED) {
1095 if (DEBUG) {
1096 Slog.d(TAG, "--- Access granted because the calling process has "
1097 + "the INTERACT_ACROSS_USERS_FULL permission");
1098 }
1099 return true;
1100 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001101 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1102 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001103 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001104 }
1105
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001106
1107 /**
1108 * Returns true iff the caller is identified to be the current input method with the token.
1109 * @param token The window token given to the input method when it was started.
1110 * @return true if and only if non-null valid token is specified.
1111 */
1112 private boolean calledWithValidToken(IBinder token) {
1113 if (token == null || mCurToken != token) {
1114 return false;
1115 }
1116 return true;
1117 }
1118
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 private boolean bindCurrentInputMethodService(
1120 Intent service, ServiceConnection conn, int flags) {
1121 if (service == null || conn == null) {
1122 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1123 return false;
1124 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001125 return mContext.bindServiceAsUser(service, conn, flags,
1126 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 }
1128
satoke7c6998e2011-06-03 17:57:59 +09001129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 // TODO: Make this work even for non-current users?
1132 if (!calledFromValidUser()) {
1133 return Collections.emptyList();
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001136 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 }
1139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 // TODO: Make this work even for non-current users?
1143 if (!calledFromValidUser()) {
1144 return Collections.emptyList();
1145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001147 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
1150
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001151 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001152 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001153 * @return enabled subtypes of the specified imi
1154 */
satoke7c6998e2011-06-03 17:57:59 +09001155 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001156 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001157 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001158 // TODO: Make this work even for non-current users?
1159 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001160 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 }
satok67ddf9c2010-11-17 09:45:54 +09001162 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001163 final InputMethodInfo imi;
1164 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001165 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001166 } else {
1167 imi = mMethodMap.get(imiId);
1168 }
1169 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001170 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001171 }
1172 return mSettings.getEnabledInputMethodSubtypeListLocked(
1173 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001174 }
1175 }
1176
satoke7c6998e2011-06-03 17:57:59 +09001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public void addClient(IInputMethodClient client,
1179 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 if (!calledFromValidUser()) {
1181 return;
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 synchronized (mMethodMap) {
1184 mClients.put(client.asBinder(), new ClientState(client,
1185 inputContext, uid, pid));
1186 }
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 if (!calledFromValidUser()) {
1192 return;
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001195 ClientState cs = mClients.remove(client.asBinder());
1196 if (cs != null) {
1197 clearClientSessionLocked(cs);
1198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 void executeOrSendMessage(IInterface target, Message msg) {
1203 if (target.asBinder() instanceof Binder) {
1204 mCaller.sendMessage(msg);
1205 } else {
1206 handleMessage(msg);
1207 msg.recycle();
1208 }
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001211 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 + mCurClient.client.asBinder());
1215 if (mBoundToMethod) {
1216 mBoundToMethod = false;
1217 if (mCurMethod != null) {
1218 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1219 MSG_UNBIND_INPUT, mCurMethod));
1220 }
1221 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001222
1223 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1224 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1226 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1227 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project10592532009-03-18 17:39:46 -07001230 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 private int getImeShowFlags() {
1235 int flags = 0;
1236 if (mShowForced) {
1237 flags |= InputMethod.SHOW_FORCED
1238 | InputMethod.SHOW_EXPLICIT;
1239 } else if (mShowExplicitlyRequested) {
1240 flags |= InputMethod.SHOW_EXPLICIT;
1241 }
1242 return flags;
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 private int getAppShowFlags() {
1246 int flags = 0;
1247 if (mShowForced) {
1248 flags |= InputMethodManager.SHOW_FORCED;
1249 } else if (!mShowExplicitlyRequested) {
1250 flags |= InputMethodManager.SHOW_IMPLICIT;
1251 }
1252 return flags;
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn7663d802012-02-24 13:08:49 -08001255 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (!mBoundToMethod) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1258 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1259 mBoundToMethod = true;
1260 }
1261 final SessionState session = mCurClient.curSession;
1262 if (initial) {
1263 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1264 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1265 } else {
1266 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1267 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1268 }
1269 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001271 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001273 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001274 (session.channel != null ? session.channel.dup() : null),
1275 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001279 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 // If no method is currently selected, do nothing.
1281 if (mCurMethodId == null) {
1282 return mNoBinding;
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 ClientState cs = mClients.get(client.asBinder());
1286 if (cs == null) {
1287 throw new IllegalArgumentException("unknown client "
1288 + client.asBinder());
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 try {
1292 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1293 // Check with the window manager to make sure this client actually
1294 // has a window with focus. If not, reject. This is thread safe
1295 // because if the focus changes some time before or after, the
1296 // next client receiving focus that has any interest in input will
1297 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1300 return null;
1301 }
1302 } catch (RemoteException e) {
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn7663d802012-02-24 13:08:49 -08001305 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1306 }
1307
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001308 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001309 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001310 // If no method is currently selected, do nothing.
1311 if (mCurMethodId == null) {
1312 return mNoBinding;
1313 }
1314
Yohei Yukawad57ba672015-06-08 16:39:46 -07001315 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1316 attribute.packageName)) {
1317 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1318 + " uid=" + cs.uid + " package=" + attribute.packageName);
1319 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001320 }
1321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001323 // Was the keyguard locked when switching over to the new client?
1324 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // If the client is changing, we need to switch over to the new
1326 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001327 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001329 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
1331 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001332 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001333 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001334 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // Bump up the sequence for this client and attach it.
1339 mCurSeq++;
1340 if (mCurSeq <= 0) mCurSeq = 1;
1341 mCurClient = cs;
1342 mCurInputContext = inputContext;
1343 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 // Check if the input method is changing.
1346 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1347 if (cs.curSession != null) {
1348 // Fast case: if we are already connected to the input method,
1349 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001350 return attachNewInputLocked(
1351 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 if (mHaveConnection) {
1354 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // Return to client, and we will get back with it when
1356 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001357 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001358 return new InputBindResult(null, null, mCurId, mCurSeq,
1359 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } else if (SystemClock.uptimeMillis()
1361 < (mLastBindTime+TIME_TO_RECONNECT)) {
1362 // In this case we have connected to the service, but
1363 // don't yet have its interface. If it hasn't been too
1364 // long since we did the connection, we'll return to
1365 // the client and wait to get the service interface so
1366 // we can report back. If it has been too long, we want
1367 // to fall through so we can try a disconnect/reconnect
1368 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001369 return new InputBindResult(null, null, mCurId, mCurSeq,
1370 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1373 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375 }
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001378 return startInputInnerLocked();
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001381 InputBindResult startInputInnerLocked() {
1382 if (mCurMethodId == null) {
1383 return mNoBinding;
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001386 if (!mSystemReady) {
1387 // If the system is not yet ready, we shouldn't be running third
1388 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001389 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1390 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1394 if (info == null) {
1395 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
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.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001509 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001510 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001511 mIWindowManager.removeWindowToken(mCurToken);
1512 } catch (RemoteException e) {
1513 }
1514 mCurToken = null;
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
The Android Open Source Project10592532009-03-18 17:39:46 -07001517 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001518 clearCurMethodLocked();
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;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001643 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001644 // 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 Nonaka7309b122015-08-17 18:34:13 -07001755 try {
1756 if ((mNotificationManager != null)
1757 && !mIWindowManager.hasNavigationBar()) {
1758 if (DEBUG) {
1759 Slog.d(TAG, "--- show notification: label = " + summary);
1760 }
1761 mNotificationManager.notifyAsUser(null,
1762 com.android.internal.R.string.select_input_method,
1763 mImeSwitcherNotification.build(), UserHandle.ALL);
1764 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001765 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001766 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001767 }
1768 } else {
1769 if (mNotificationShown && mNotificationManager != null) {
1770 if (DEBUG) {
1771 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001772 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001773 mNotificationManager.cancelAsUser(null,
1774 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1775 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001776 }
satok06487a52010-10-29 11:37:18 +09001777 }
1778 } finally {
1779 Binder.restoreCallingIdentity(ident);
1780 }
1781 }
1782
satoke7c6998e2011-06-03 17:57:59 +09001783 @Override
satokf9f01002011-05-19 21:31:50 +09001784 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001785 if (!calledFromValidUser()) {
1786 return;
1787 }
satokf9f01002011-05-19 21:31:50 +09001788 synchronized (mMethodMap) {
1789 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1790 for (int i = 0; i < spans.length; ++i) {
1791 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001792 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001793 mSecureSuggestionSpans.put(ss, currentImi);
1794 }
1795 }
1796 }
1797 }
1798
satoke7c6998e2011-06-03 17:57:59 +09001799 @Override
satokf9f01002011-05-19 21:31:50 +09001800 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001801 if (!calledFromValidUser()) {
1802 return false;
1803 }
satokf9f01002011-05-19 21:31:50 +09001804 synchronized (mMethodMap) {
1805 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1806 // TODO: Do not send the intent if the process of the targetImi is already dead.
1807 if (targetImi != null) {
1808 final String[] suggestions = span.getSuggestions();
1809 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001810 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001811 final Intent intent = new Intent();
1812 // Ensures that only a class in the original IME package will receive the
1813 // notification.
satok42c5a162011-05-26 16:46:14 +09001814 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001815 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1816 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1817 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1818 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001819 final long ident = Binder.clearCallingIdentity();
1820 try {
1821 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1822 } finally {
1823 Binder.restoreCallingIdentity(ident);
1824 }
satokf9f01002011-05-19 21:31:50 +09001825 return true;
1826 }
1827 }
1828 return false;
1829 }
1830
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001831 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001832 updateInputMethodsFromSettingsLocked(enabledMayChange);
1833 updateKeyboardFromSettingsLocked();
1834 }
1835
1836 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001837 if (enabledMayChange) {
1838 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1839 for (int i=0; i<enabled.size(); i++) {
1840 // We allow the user to select "disabled until used" apps, so if they
1841 // are enabling one of those here we now need to make it enabled.
1842 InputMethodInfo imm = enabled.get(i);
1843 try {
1844 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1845 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1846 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001847 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001848 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001849 if (DEBUG) {
1850 Slog.d(TAG, "Update state(" + imm.getId()
1851 + "): DISABLED_UNTIL_USED -> DEFAULT");
1852 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001853 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1854 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001855 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1856 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001857 }
1858 } catch (RemoteException e) {
1859 }
1860 }
1861 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001862 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1863 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1864 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1865 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001866 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001867 // There is no input method selected, try to choose new applicable input method.
1868 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001869 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001870 }
1871 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001873 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001875 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001876 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001877 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
satokf3db1af2010-11-23 13:34:33 +09001879 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001880 } else {
1881 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001882 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001883 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001885 // Here is not the perfect place to reset the switching controller. Ideally
1886 // mSwitchingController and mSettings should be able to share the same state.
1887 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1888 // the same enabled IMEs list.
1889 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001890
1891 }
1892
1893 public void updateKeyboardFromSettingsLocked() {
1894 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1895 if (mSwitchingDialog != null
1896 && mSwitchingDialogTitleView != null
1897 && mSwitchingDialog.isShowing()) {
1898 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1899 com.android.internal.R.id.hard_keyboard_switch);
1900 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001903
satokab751aa2010-09-14 19:17:36 +09001904 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 InputMethodInfo info = mMethodMap.get(id);
1906 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001907 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001910 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001911 // We have already made sure that the package name belongs to the application's UID.
1912 // No further UID check is required.
1913 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1914 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001915 }
1916 }
1917
satokd81e9502012-05-21 12:58:45 +09001918 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001920 final int subtypeCount = info.getSubtypeCount();
1921 if (subtypeCount <= 0) {
1922 return;
satokcd7cd292010-11-20 15:46:23 +09001923 }
satokd81e9502012-05-21 12:58:45 +09001924 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1925 final InputMethodSubtype newSubtype;
1926 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1927 newSubtype = info.getSubtypeAt(subtypeId);
1928 } else {
1929 // If subtype is null, try to find the most applicable one from
1930 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001931 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001932 }
1933 if (newSubtype == null || oldSubtype == null) {
1934 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1935 + ", new subtype = " + newSubtype);
1936 return;
1937 }
1938 if (newSubtype != oldSubtype) {
1939 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1940 if (mCurMethod != null) {
1941 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001942 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001943 mCurMethod.changeInputMethodSubtype(newSubtype);
1944 } catch (RemoteException e) {
1945 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001946 }
1947 }
1948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 return;
1950 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001951
satokd81e9502012-05-21 12:58:45 +09001952 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 final long ident = Binder.clearCallingIdentity();
1954 try {
satokab751aa2010-09-14 19:17:36 +09001955 // Set a subtype to this input method.
1956 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001957 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1958 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1959 // because mCurMethodId is stored as a history in
1960 // setSelectedInputMethodAndSubtypeLocked().
1961 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962
1963 if (ActivityManagerNative.isSystemReady()) {
1964 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001965 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001967 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001969 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 } finally {
1971 Binder.restoreCallingIdentity(ident);
1972 }
1973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001974
satok42c5a162011-05-26 16:46:14 +09001975 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001976 public boolean showSoftInput(IInputMethodClient client, int flags,
1977 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001978 if (!calledFromValidUser()) {
1979 return false;
1980 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001981 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 long ident = Binder.clearCallingIdentity();
1983 try {
1984 synchronized (mMethodMap) {
1985 if (mCurClient == null || client == null
1986 || mCurClient.client.asBinder() != client.asBinder()) {
1987 try {
1988 // We need to check if this is the current client with
1989 // focus in the window manager, to allow this call to
1990 // be made before input is started in it.
1991 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001992 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001996 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
1998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001999
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002000 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002001 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 } finally {
2004 Binder.restoreCallingIdentity(ident);
2005 }
2006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007
The Android Open Source Project4df24232009-03-05 14:34:35 -08002008 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 mShowRequested = true;
2010 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2011 mShowExplicitlyRequested = true;
2012 }
2013 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2014 mShowExplicitlyRequested = true;
2015 mShowForced = true;
2016 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002017
Dianne Hackborncc278702009-09-02 23:07:23 -07002018 if (!mSystemReady) {
2019 return false;
2020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002021
The Android Open Source Project4df24232009-03-05 14:34:35 -08002022 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002024 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002025 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2026 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2027 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002029 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002030 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002031 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002032 | Context.BIND_TREAT_LIKE_ACTIVITY
2033 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002034 mVisibleBound = true;
2035 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002036 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002038 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 // The client has asked to have the input method shown, but
2040 // we have been sitting here too long with a connection to the
2041 // service and no interface received, so let's disconnect/connect
2042 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002043 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002045 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002047 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002048 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002049 } else {
2050 if (DEBUG) {
2051 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2052 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
The Android Open Source Project4df24232009-03-05 14:34:35 -08002056 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002058
satok42c5a162011-05-26 16:46:14 +09002059 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002060 public boolean hideSoftInput(IInputMethodClient client, int flags,
2061 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002062 if (!calledFromValidUser()) {
2063 return false;
2064 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002065 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 long ident = Binder.clearCallingIdentity();
2067 try {
2068 synchronized (mMethodMap) {
2069 if (mCurClient == null || client == null
2070 || mCurClient.client.asBinder() != client.asBinder()) {
2071 try {
2072 // We need to check if this is the current client with
2073 // focus in the window manager, to allow this call to
2074 // be made before input is started in it.
2075 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002076 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2077 + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002081 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002084
Joe Onorato8a9b2202010-02-26 18:56:32 -08002085 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 } finally {
2089 Binder.restoreCallingIdentity(ident);
2090 }
2091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2095 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002096 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 -08002097 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002100 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 -08002101 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002103
2104 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2105 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2106 // to be updated with the new value sent from IME process. Even in such a transient state
2107 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2108 // application process as a valid request, and have even promised such a behavior with CTS
2109 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2110 // IMMS#InputShown indicates that the software keyboard is shown.
2111 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2112 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2113 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002114 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002115 if (shouldHideSoftInput) {
2116 // The IME will report its visible state again after the following message finally
2117 // delivered to the IME process as an IPC. Hence the inconsistency between
2118 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2119 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002120 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2121 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2122 res = true;
2123 } else {
2124 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002126 if (mHaveConnection && mVisibleBound) {
2127 mContext.unbindService(mVisibleConnection);
2128 mVisibleBound = false;
2129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 mInputShown = false;
2131 mShowRequested = false;
2132 mShowExplicitlyRequested = false;
2133 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002134 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002136
satok42c5a162011-05-26 16:46:14 +09002137 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002138 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2139 int controlFlags, int softInputMode, int windowFlags,
2140 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002141 // Needs to check the validity before clearing calling identity
2142 final boolean calledFromValidUser = calledFromValidUser();
2143
Dianne Hackborn7663d802012-02-24 13:08:49 -08002144 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 long ident = Binder.clearCallingIdentity();
2146 try {
2147 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002148 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002149 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002151 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
Dianne Hackborn7663d802012-02-24 13:08:49 -08002153 ClientState cs = mClients.get(client.asBinder());
2154 if (cs == null) {
2155 throw new IllegalArgumentException("unknown client "
2156 + client.asBinder());
2157 }
2158
2159 try {
2160 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2161 // Check with the window manager to make sure this client actually
2162 // has a window with focus. If not, reject. This is thread safe
2163 // because if the focus changes some time before or after, the
2164 // next client receiving focus that has any interest in input will
2165 // be calling through here after that change happens.
2166 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2167 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2168 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002170 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002172
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002173 if (!calledFromValidUser) {
2174 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2175 Slog.w(TAG, "If you want to interect with IME, you need "
2176 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2177 hideCurrentInputLocked(0, null);
2178 return null;
2179 }
2180
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002181 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002182 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002183 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002184 if (attribute != null) {
2185 return startInputUncheckedLocked(cs, inputContext, attribute,
2186 controlFlags);
2187 }
2188 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002189 }
2190 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002191
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002192 // Should we auto-show the IME even if the caller has not
2193 // specified what should be done with it?
2194 // We only do this automatically if the window can resize
2195 // to accommodate the IME (so what the user sees will give
2196 // them good context without input information being obscured
2197 // by the IME) or if running on a large screen where there
2198 // is more room for the target window + IME.
2199 final boolean doAutoShow =
2200 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2201 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2202 || mRes.getConfiguration().isLayoutSizeAtLeast(
2203 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002204 final boolean isTextEditor =
2205 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2206
2207 // We want to start input before showing the IME, but after closing
2208 // it. We want to do this after closing it to help the IME disappear
2209 // more quickly (not get stuck behind it initializing itself for the
2210 // new focused input, even if its window wants to hide the IME).
2211 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2214 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002215 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2217 // There is no focus view, and this window will
2218 // be behind any soft input window, so hide the
2219 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002220 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002221 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002223 } else if (isTextEditor && doAutoShow && (softInputMode &
2224 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 // There is a focus view, and we are navigating forward
2226 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002227 // We only do this automatically if the window can resize
2228 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002229 // them good context without input information being obscured
2230 // by the IME) or if running on a large screen where there
2231 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002232 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002233 if (attribute != null) {
2234 res = startInputUncheckedLocked(cs, inputContext, attribute,
2235 controlFlags);
2236 didStart = true;
2237 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002238 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
2240 break;
2241 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2242 // Do nothing.
2243 break;
2244 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2245 if ((softInputMode &
2246 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002247 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002248 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 break;
2251 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002253 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 break;
2255 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2256 if ((softInputMode &
2257 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002259 if (attribute != null) {
2260 res = startInputUncheckedLocked(cs, inputContext, attribute,
2261 controlFlags);
2262 didStart = true;
2263 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002264 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
2266 break;
2267 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002268 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002269 if (attribute != null) {
2270 res = startInputUncheckedLocked(cs, inputContext, attribute,
2271 controlFlags);
2272 didStart = true;
2273 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002274 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 break;
2276 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002277
2278 if (!didStart && attribute != null) {
2279 res = startInputUncheckedLocked(cs, inputContext, attribute,
2280 controlFlags);
2281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
2283 } finally {
2284 Binder.restoreCallingIdentity(ident);
2285 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002286
2287 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
satok42c5a162011-05-26 16:46:14 +09002290 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002291 public void showInputMethodPickerFromClient(
2292 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002293 if (!calledFromValidUser()) {
2294 return;
2295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 synchronized (mMethodMap) {
2297 if (mCurClient == null || client == null
2298 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002299 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002300 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302
satok440aab52010-11-25 09:43:11 +09002303 // Always call subtype picker, because subtype picker is a superset of input method
2304 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002305 mHandler.sendMessage(mCaller.obtainMessageI(
2306 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002307 }
2308 }
2309
satok42c5a162011-05-26 16:46:14 +09002310 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002312 if (!calledFromValidUser()) {
2313 return;
2314 }
satok28203512010-11-24 11:06:49 +09002315 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2316 }
2317
satok42c5a162011-05-26 16:46:14 +09002318 @Override
satok28203512010-11-24 11:06:49 +09002319 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002320 if (!calledFromValidUser()) {
2321 return;
2322 }
satok28203512010-11-24 11:06:49 +09002323 synchronized (mMethodMap) {
2324 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002325 setInputMethodWithSubtypeIdLocked(token, id,
2326 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2327 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002328 } else {
2329 setInputMethod(token, id);
2330 }
2331 }
satokab751aa2010-09-14 19:17:36 +09002332 }
2333
satok42c5a162011-05-26 16:46:14 +09002334 @Override
satokb416a712010-11-25 20:42:14 +09002335 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002336 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002337 if (!calledFromValidUser()) {
2338 return;
2339 }
satokb416a712010-11-25 20:42:14 +09002340 synchronized (mMethodMap) {
2341 if (mCurClient == null || client == null
2342 || mCurClient.client.asBinder() != client.asBinder()) {
2343 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2344 }
satok7fee71f2010-12-17 18:54:26 +09002345 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2346 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002347 }
2348 }
2349
satok4fc87d62011-05-20 16:13:43 +09002350 @Override
satok735cf382010-11-11 20:40:09 +09002351 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002352 if (!calledFromValidUser()) {
2353 return false;
2354 }
satok735cf382010-11-11 20:40:09 +09002355 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002356 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002357 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002358 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002359 lastImi = mMethodMap.get(lastIme.first);
2360 } else {
2361 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002362 }
satok4fc87d62011-05-20 16:13:43 +09002363 String targetLastImiId = null;
2364 int subtypeId = NOT_A_SUBTYPE_ID;
2365 if (lastIme != null && lastImi != null) {
2366 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2367 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2368 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2369 : mCurrentSubtype.hashCode();
2370 // If the last IME is the same as the current IME and the last subtype is not
2371 // defined, there is no need to switch to the last IME.
2372 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2373 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002374 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002375 }
2376 }
2377
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002378 if (TextUtils.isEmpty(targetLastImiId)
2379 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002380 // This is a safety net. If the currentSubtype can't be added to the history
2381 // and the framework couldn't find the last ime, we will make the last ime be
2382 // the most applicable enabled keyboard subtype of the system imes.
2383 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2384 if (enabled != null) {
2385 final int N = enabled.size();
2386 final String locale = mCurrentSubtype == null
2387 ? mRes.getConfiguration().locale.toString()
2388 : mCurrentSubtype.getLocale();
2389 for (int i = 0; i < N; ++i) {
2390 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002391 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002392 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002393 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2394 InputMethodUtils.getSubtypes(imi),
2395 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002396 if (keyboardSubtype != null) {
2397 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002398 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002399 imi, keyboardSubtype.hashCode());
2400 if(keyboardSubtype.getLocale().equals(locale)) {
2401 break;
2402 }
2403 }
2404 }
2405 }
2406 }
2407 }
2408
2409 if (!TextUtils.isEmpty(targetLastImiId)) {
2410 if (DEBUG) {
2411 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2412 + ", from: " + mCurMethodId + ", " + subtypeId);
2413 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002414 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002415 return true;
2416 } else {
2417 return false;
2418 }
satok735cf382010-11-11 20:40:09 +09002419 }
2420 }
2421
satoke7c6998e2011-06-03 17:57:59 +09002422 @Override
satok688bd472012-02-09 20:09:17 +09002423 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002424 if (!calledFromValidUser()) {
2425 return false;
2426 }
satok688bd472012-02-09 20:09:17 +09002427 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002428 if (!calledWithValidToken(token)) {
2429 final int uid = Binder.getCallingUid();
2430 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2431 + " token:" + token);
2432 return false;
2433 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002434 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002435 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2436 if (nextSubtype == null) {
2437 return false;
2438 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002439 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2440 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002441 return true;
2442 }
2443 }
2444
2445 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002446 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2447 if (!calledFromValidUser()) {
2448 return false;
2449 }
2450 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002451 if (!calledWithValidToken(token)) {
2452 final int uid = Binder.getCallingUid();
2453 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2454 + "token. uid:" + uid + " token:" + token);
2455 return false;
2456 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002457 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002458 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2459 if (nextSubtype == null) {
2460 return false;
2461 }
2462 return true;
2463 }
2464 }
2465
2466 @Override
satok68f1b782011-04-11 14:26:04 +09002467 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002468 if (!calledFromValidUser()) {
2469 return null;
2470 }
satok68f1b782011-04-11 14:26:04 +09002471 synchronized (mMethodMap) {
2472 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2473 // TODO: Handle the case of the last IME with no subtypes
2474 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2475 || TextUtils.isEmpty(lastIme.second)) return null;
2476 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2477 if (lastImi == null) return null;
2478 try {
2479 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002480 final int lastSubtypeId =
2481 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002482 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2483 return null;
2484 }
2485 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002486 } catch (NumberFormatException e) {
2487 return null;
2488 }
2489 }
2490 }
2491
satoke7c6998e2011-06-03 17:57:59 +09002492 @Override
satokee5e77c2011-09-02 18:50:15 +09002493 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002494 if (!calledFromValidUser()) {
2495 return;
2496 }
satok91e88122011-07-18 11:11:42 +09002497 // By this IPC call, only a process which shares the same uid with the IME can add
2498 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002499 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002500 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002501 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002502 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002503 final String[] packageInfos;
2504 try {
2505 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2506 } catch (RemoteException e) {
2507 Slog.e(TAG, "Failed to get package infos");
2508 return;
2509 }
satok91e88122011-07-18 11:11:42 +09002510 if (packageInfos != null) {
2511 final int packageNum = packageInfos.length;
2512 for (int i = 0; i < packageNum; ++i) {
2513 if (packageInfos[i].equals(imi.getPackageName())) {
2514 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002515 final long ident = Binder.clearCallingIdentity();
2516 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002517 buildInputMethodListLocked(mMethodList, mMethodMap,
2518 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002519 } finally {
2520 Binder.restoreCallingIdentity(ident);
2521 }
satokee5e77c2011-09-02 18:50:15 +09002522 return;
satok91e88122011-07-18 11:11:42 +09002523 }
2524 }
2525 }
satoke7c6998e2011-06-03 17:57:59 +09002526 }
satokee5e77c2011-09-02 18:50:15 +09002527 return;
satoke7c6998e2011-06-03 17:57:59 +09002528 }
2529
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002530 @Override
2531 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002532 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002533 }
2534
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002535 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002536 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002537 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002538 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002539 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002540 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002541 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2542 if (DEBUG) {
2543 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2544 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2545 + " actual: " + sequenceNumber);
2546 }
2547 return;
2548 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002549 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2550 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002551 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002552 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002553 }
2554 }
2555
satok28203512010-11-24 11:06:49 +09002556 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002558 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2559 }
2560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002562 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2563 if (token == null) {
2564 if (mContext.checkCallingOrSelfPermission(
2565 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2566 != PackageManager.PERMISSION_GRANTED) {
2567 throw new SecurityException(
2568 "Using null token requires permission "
2569 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002571 } else if (mCurToken != token) {
2572 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2573 + " token: " + token);
2574 return;
2575 }
2576
2577 final long ident = Binder.clearCallingIdentity();
2578 try {
2579 setInputMethodLocked(id, subtypeId);
2580 } finally {
2581 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
2583 }
2584
satok42c5a162011-05-26 16:46:14 +09002585 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002587 if (!calledFromValidUser()) {
2588 return;
2589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002591 if (!calledWithValidToken(token)) {
2592 final int uid = Binder.getCallingUid();
2593 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2594 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 return;
2596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 long ident = Binder.clearCallingIdentity();
2598 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002599 hideCurrentInputLocked(flags, null);
2600 } finally {
2601 Binder.restoreCallingIdentity(ident);
2602 }
2603 }
2604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002605
satok42c5a162011-05-26 16:46:14 +09002606 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002607 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 if (!calledFromValidUser()) {
2609 return;
2610 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002611 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002612 if (!calledWithValidToken(token)) {
2613 final int uid = Binder.getCallingUid();
2614 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2615 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002616 return;
2617 }
2618 long ident = Binder.clearCallingIdentity();
2619 try {
2620 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 } finally {
2622 Binder.restoreCallingIdentity(ident);
2623 }
2624 }
2625 }
2626
2627 void setEnabledSessionInMainThread(SessionState session) {
2628 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002629 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002631 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002632 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 } catch (RemoteException e) {
2634 }
2635 }
2636 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002637 if (mEnabledSession != null && mEnabledSession.session != null) {
2638 try {
2639 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2640 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2641 } catch (RemoteException e) {
2642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
2644 }
2645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002646
satok42c5a162011-05-26 16:46:14 +09002647 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002649 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002651 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002652 final boolean showAuxSubtypes;
2653 switch (msg.arg1) {
2654 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2655 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2656 // implemented so that auxiliary subtypes will be excluded when the soft
2657 // keyboard is invisible.
2658 showAuxSubtypes = mInputShown;
2659 break;
2660 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2661 showAuxSubtypes = true;
2662 break;
2663 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2664 showAuxSubtypes = false;
2665 break;
2666 default:
2667 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2668 return false;
2669 }
2670 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002671 return true;
2672
satok47a44912010-10-06 16:03:58 +09002673 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002674 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002675 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002676 args.recycle();
satok217f5482010-12-15 05:19:19 +09002677 return true;
2678
2679 case MSG_SHOW_IM_CONFIG:
2680 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002681 return true;
2682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 case MSG_UNBIND_INPUT:
2686 try {
2687 ((IInputMethod)msg.obj).unbindInput();
2688 } catch (RemoteException e) {
2689 // There is nothing interesting about the method dying.
2690 }
2691 return true;
2692 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002693 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 try {
2695 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2696 } catch (RemoteException e) {
2697 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002698 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 return true;
2700 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002701 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002703 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002704 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002705 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 } catch (RemoteException e) {
2707 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002708 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 return true;
2710 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002711 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002713 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002714 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002715 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 } catch (RemoteException e) {
2717 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002718 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 return true;
2720 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002721 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002723 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2725 } catch (RemoteException e) {
2726 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002727 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002729 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002730 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002731 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002732 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002734 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002736 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002737 // Dispose the channel if the input method is not local to this process
2738 // because the remote proxy will get its own copy when unparceled.
2739 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002740 channel.dispose();
2741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002743 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002749 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 try {
2751 SessionState session = (SessionState)args.arg1;
2752 setEnabledSessionInMainThread(session);
2753 session.method.startInput((IInputContext)args.arg2,
2754 (EditorInfo)args.arg3);
2755 } catch (RemoteException e) {
2756 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 return true;
2759 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002760 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 try {
2762 SessionState session = (SessionState)args.arg1;
2763 setEnabledSessionInMainThread(session);
2764 session.method.restartInput((IInputContext)args.arg2,
2765 (EditorInfo)args.arg3);
2766 } catch (RemoteException e) {
2767 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002768 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 case MSG_UNBIND_METHOD:
2774 try {
2775 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2776 } catch (RemoteException e) {
2777 // There is nothing interesting about the last client dying.
2778 }
2779 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002780 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002781 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002782 IInputMethodClient client = (IInputMethodClient)args.arg1;
2783 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002785 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002787 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002788 } finally {
2789 // Dispose the channel if the input method is not local to this process
2790 // because the remote proxy will get its own copy when unparceled.
2791 if (res.channel != null && Binder.isProxy(client)) {
2792 res.channel.dispose();
2793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002795 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002797 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002798 case MSG_SET_ACTIVE:
2799 try {
2800 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2801 } catch (RemoteException e) {
2802 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2803 + ((ClientState)msg.obj).pid + " uid "
2804 + ((ClientState)msg.obj).uid);
2805 }
2806 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002807 case MSG_SET_INTERACTIVE:
2808 handleSetInteractive(msg.arg1 != 0);
2809 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002810 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2811 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002812 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002813 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002814 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002815 } catch (RemoteException e) {
2816 Slog.w(TAG, "Got RemoteException sending "
2817 + "setUserActionNotificationSequenceNumber("
2818 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002819 + clientState.pid + " uid "
2820 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002821 }
2822 return true;
2823 }
satok01038492012-04-09 21:08:27 +09002824
2825 // --------------------------------------------------------------
2826 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002827 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002828 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
2830 return false;
2831 }
2832
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002833 private void handleSetInteractive(final boolean interactive) {
2834 synchronized (mMethodMap) {
2835 mIsInteractive = interactive;
2836 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2837
2838 // Inform the current client of the change in active status
2839 if (mCurClient != null && mCurClient.client != null) {
2840 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2841 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2842 }
2843 }
2844 }
2845
satokdc9ddae2011-10-06 12:22:36 +09002846 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002847 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2848 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002849 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002850 if (DEBUG) {
2851 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2852 }
satok723a27e2010-11-11 14:58:11 +09002853 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002854 return true;
2855 }
2856
2857 return false;
2858 }
2859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002861 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002862 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002863 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002864 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 list.clear();
2867 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002868
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002869 // Use for queryIntentServicesAsUser
2870 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002872 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002874 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2875 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002876
satoke7c6998e2011-06-03 17:57:59 +09002877 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2878 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 for (int i = 0; i < services.size(); ++i) {
2880 ResolveInfo ri = services.get(i);
2881 ServiceInfo si = ri.serviceInfo;
2882 ComponentName compName = new ComponentName(si.packageName, si.name);
2883 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2884 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002885 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 + ": it does not require the permission "
2887 + android.Manifest.permission.BIND_INPUT_METHOD);
2888 continue;
2889 }
2890
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892
2893 try {
satoke7c6998e2011-06-03 17:57:59 +09002894 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002896 final String id = p.getId();
2897 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
2899 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002900 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002902 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002903 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
2905 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002906
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002907 if (resetDefaultEnabledIme) {
2908 final ArrayList<InputMethodInfo> defaultEnabledIme =
2909 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2910 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2911 final InputMethodInfo imi = defaultEnabledIme.get(i);
2912 if (DEBUG) {
2913 Slog.d(TAG, "--- enable ime = " + imi);
2914 }
2915 setInputMethodEnabledLocked(imi.getId(), true);
2916 }
2917 }
2918
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002919 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002920 if (!TextUtils.isEmpty(defaultImiId)) {
2921 if (!map.containsKey(defaultImiId)) {
2922 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2923 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002924 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002925 }
2926 } else {
2927 // Double check that the default IME is certainly enabled.
2928 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002929 }
2930 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002931 // Here is not the perfect place to reset the switching controller. Ideally
2932 // mSwitchingController and mSettings should be able to share the same state.
2933 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2934 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002935 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002939
satok217f5482010-12-15 05:19:19 +09002940 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002941 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002942 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002943 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2944 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002945 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002946 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002947 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002948 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002949 }
2950
2951 private void showConfigureInputMethods() {
2952 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2953 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2954 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2955 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002956 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002957 }
2958
satok2c93efc2012-04-02 19:33:47 +09002959 private boolean isScreenLocked() {
2960 return mKeyguardManager != null
2961 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2962 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002963
Seigo Nonaka14e13912015-05-06 21:04:13 -07002964 private void showInputMethodMenu(boolean showAuxSubtypes) {
2965 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002968 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002969
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002970 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002971 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002972 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002973
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002974 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002975 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002976 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2977 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002978 if (immis == null || immis.size() == 0) {
2979 return;
2980 }
2981
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002982 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983
satok688bd472012-02-09 20:09:17 +09002984 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002985 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002986 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002987
satokc3690562012-01-10 20:14:43 +09002988 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002989 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002990 if (currentSubtype != null) {
2991 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002992 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2993 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002994 }
2995 }
2996
Ken Wakasa761eb372011-03-04 19:06:18 +09002997 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002998 mIms = new InputMethodInfo[N];
2999 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003000 int checkedItem = 0;
3001 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003002 final ImeSubtypeListItem item = imList.get(i);
3003 mIms[i] = item.mImi;
3004 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003005 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003006 int subtypeId = mSubtypeIds[i];
3007 if ((subtypeId == NOT_A_SUBTYPE_ID)
3008 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3009 || (subtypeId == lastInputMethodSubtypeId)) {
3010 checkedItem = i;
3011 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003014
3015 final Context settingsContext = new ContextThemeWrapper(context,
3016 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3017
3018 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003019 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3020 @Override
3021 public void onCancel(DialogInterface dialog) {
3022 hideInputMethodMenu();
3023 }
3024 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003025
3026 final Context dialogContext = mDialogBuilder.getContext();
3027 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3028 com.android.internal.R.styleable.DialogPreference,
3029 com.android.internal.R.attr.alertDialogStyle, 0);
3030 final Drawable dialogIcon = a.getDrawable(
3031 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3032 a.recycle();
3033
3034 mDialogBuilder.setIcon(dialogIcon);
3035
3036 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3037 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003038 final View tv = inflater.inflate(
3039 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3040 mDialogBuilder.setCustomTitle(tv);
3041
3042 // Setup layout for a toggle switch of the hardware keyboard
3043 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003044 mSwitchingDialogTitleView
3045 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003046 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003047 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003048 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003049 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003050 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003051 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3052 @Override
3053 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003054 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003055 // Ensure that the input method dialog is dismissed when changing
3056 // the hardware keyboard state.
3057 hideInputMethodMenu();
3058 }
3059 });
3060
Alan Viverette505e3ab2014-11-24 15:22:11 -08003061 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003062 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3063 final OnClickListener choiceListener = new OnClickListener() {
3064 @Override
3065 public void onClick(final DialogInterface dialog, final int which) {
3066 synchronized (mMethodMap) {
3067 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3068 || mSubtypeIds.length <= which) {
3069 return;
satok01038492012-04-09 21:08:27 +09003070 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003071 final InputMethodInfo im = mIms[which];
3072 int subtypeId = mSubtypeIds[which];
3073 adapter.mCheckedItem = which;
3074 adapter.notifyDataSetChanged();
3075 hideInputMethodMenu();
3076 if (im != null) {
3077 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3078 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003079 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003080 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003082 }
3083 }
3084 };
3085 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003087 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003088 final OnClickListener positiveListener = new OnClickListener() {
3089 @Override
3090 public void onClick(DialogInterface dialog, int whichButton) {
3091 showConfigureInputMethods();
3092 }
3093 };
satok82beadf2010-12-27 19:03:06 +09003094 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003095 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003098 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 mSwitchingDialog.getWindow().setType(
3100 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003101 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3102 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003103 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003104 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 mSwitchingDialog.show();
3106 }
3107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003108
Ken Wakasa05dbb652011-08-22 15:22:43 +09003109 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3110 private final LayoutInflater mInflater;
3111 private final int mTextViewResourceId;
3112 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003113 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003114 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3115 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3116 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003117
Ken Wakasa05dbb652011-08-22 15:22:43 +09003118 mTextViewResourceId = textViewResourceId;
3119 mItemsList = itemsList;
3120 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003121 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003122 }
3123
3124 @Override
3125 public View getView(int position, View convertView, ViewGroup parent) {
3126 final View view = convertView != null ? convertView
3127 : mInflater.inflate(mTextViewResourceId, null);
3128 if (position < 0 || position >= mItemsList.size()) return view;
3129 final ImeSubtypeListItem item = mItemsList.get(position);
3130 final CharSequence imeName = item.mImeName;
3131 final CharSequence subtypeName = item.mSubtypeName;
3132 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3133 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3134 if (TextUtils.isEmpty(subtypeName)) {
3135 firstTextView.setText(imeName);
3136 secondTextView.setVisibility(View.GONE);
3137 } else {
3138 firstTextView.setText(subtypeName);
3139 secondTextView.setText(imeName);
3140 secondTextView.setVisibility(View.VISIBLE);
3141 }
3142 final RadioButton radioButton =
3143 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3144 radioButton.setChecked(position == mCheckedItem);
3145 return view;
3146 }
3147 }
3148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003150 synchronized (mMethodMap) {
3151 hideInputMethodMenuLocked();
3152 }
3153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003154
The Android Open Source Project10592532009-03-18 17:39:46 -07003155 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003156 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157
The Android Open Source Project10592532009-03-18 17:39:46 -07003158 if (mSwitchingDialog != null) {
3159 mSwitchingDialog.dismiss();
3160 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003162
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003163 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003164 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003165 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003169
satok42c5a162011-05-26 16:46:14 +09003170 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003172 // TODO: Make this work even for non-current users?
3173 if (!calledFromValidUser()) {
3174 return false;
3175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 synchronized (mMethodMap) {
3177 if (mContext.checkCallingOrSelfPermission(
3178 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3179 != PackageManager.PERMISSION_GRANTED) {
3180 throw new SecurityException(
3181 "Requires permission "
3182 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3183 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 long ident = Binder.clearCallingIdentity();
3186 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003187 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 } finally {
3189 Binder.restoreCallingIdentity(ident);
3190 }
3191 }
3192 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003193
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003194 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3195 // Make sure this is a valid input method.
3196 InputMethodInfo imm = mMethodMap.get(id);
3197 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003198 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003199 }
3200
satokd87c2592010-09-29 11:52:06 +09003201 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3202 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003203
satokd87c2592010-09-29 11:52:06 +09003204 if (enabled) {
3205 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3206 if (pair.first.equals(id)) {
3207 // We are enabling this input method, but it is already enabled.
3208 // Nothing to do. The previous state was enabled.
3209 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003210 }
3211 }
satokd87c2592010-09-29 11:52:06 +09003212 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3213 // Previous state was disabled.
3214 return false;
3215 } else {
3216 StringBuilder builder = new StringBuilder();
3217 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3218 builder, enabledInputMethodsList, id)) {
3219 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003220 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003221 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3222 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3223 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003224 }
3225 // Previous state was enabled.
3226 return true;
3227 } else {
3228 // We are disabling the input method but it is already disabled.
3229 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003230 return false;
3231 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003232 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003233 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003234
satok723a27e2010-11-11 14:58:11 +09003235 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3236 boolean setSubtypeOnly) {
3237 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003238 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003239
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003240 mCurUserActionNotificationSequenceNumber =
3241 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3242 if (DEBUG) {
3243 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3244 + mCurUserActionNotificationSequenceNumber);
3245 }
3246
3247 if (mCurClient != null && mCurClient.client != null) {
3248 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3249 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003250 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003251 }
3252
satok723a27e2010-11-11 14:58:11 +09003253 // Set Subtype here
3254 if (imi == null || subtypeId < 0) {
3255 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003256 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003257 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003258 if (subtypeId < imi.getSubtypeCount()) {
3259 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3260 mSettings.putSelectedSubtype(subtype.hashCode());
3261 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003262 } else {
3263 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003264 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003265 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003266 }
satokab751aa2010-09-14 19:17:36 +09003267 }
satok723a27e2010-11-11 14:58:11 +09003268
satok4c0e7152012-06-20 20:08:44 +09003269 // Workaround.
3270 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3271 // IMEs are not recognized and considered uninstalled.
3272 // Actually, we can't move everything after SystemReady because
3273 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3274 // the default IME here and try cheking the default IME again in systemReady().
3275 // TODO: Do nothing before system ready and implement a separated logic for
3276 // the encryption lock screen.
3277 // TODO: ASEC should be ready before IMMS is instantiated.
3278 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003279 // Set InputMethod here
3280 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3281 }
3282 }
3283
3284 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3285 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3286 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3287 // newDefaultIme is empty when there is no candidate for the selected IME.
3288 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3289 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3290 if (subtypeHashCode != null) {
3291 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003292 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003293 imi, Integer.valueOf(subtypeHashCode));
3294 } catch (NumberFormatException e) {
3295 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3296 }
3297 }
3298 }
3299 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003300 }
3301
satok4e4569d2010-11-19 18:45:53 +09003302 // If there are no selected shortcuts, tries finding the most applicable ones.
3303 private Pair<InputMethodInfo, InputMethodSubtype>
3304 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3305 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3306 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003307 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003308 boolean foundInSystemIME = false;
3309
3310 // Search applicable subtype for each InputMethodInfo
3311 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003312 final String imiId = imi.getId();
3313 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3314 continue;
3315 }
satokcd7cd292010-11-20 15:46:23 +09003316 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003317 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003318 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003319 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003320 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003321 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003322 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003323 }
satokdf31ae62011-01-15 06:19:44 +09003324 // 2. Search by the system locale from enabledSubtypes.
3325 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003326 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003327 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003328 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003329 }
satoka86f5e42011-09-02 17:12:42 +09003330 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003331 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003332 final ArrayList<InputMethodSubtype> subtypesForSearch =
3333 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003334 ? InputMethodUtils.getSubtypes(imi)
3335 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003336 // 4. Search by the current subtype's locale from all subtypes.
3337 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003338 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003339 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003340 }
3341 // 5. Search by the system locale from all subtypes.
3342 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003343 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003344 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003345 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003346 }
satokcd7cd292010-11-20 15:46:23 +09003347 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003348 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003349 // The current input method is the most applicable IME.
3350 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003351 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003352 break;
satok7599a7f2010-12-22 13:45:23 +09003353 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003354 // The system input method is 2nd applicable IME.
3355 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003356 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003357 if ((imi.getServiceInfo().applicationInfo.flags
3358 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3359 foundInSystemIME = true;
3360 }
satok4e4569d2010-11-19 18:45:53 +09003361 }
3362 }
3363 }
3364 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003365 if (mostApplicableIMI != null) {
3366 Slog.w(TAG, "Most applicable shortcut input method was:"
3367 + mostApplicableIMI.getId());
3368 if (mostApplicableSubtype != null) {
3369 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3370 + "," + mostApplicableSubtype.getMode() + ","
3371 + mostApplicableSubtype.getLocale());
3372 }
3373 }
satok4e4569d2010-11-19 18:45:53 +09003374 }
satokcd7cd292010-11-20 15:46:23 +09003375 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003376 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003377 } else {
3378 return null;
3379 }
3380 }
3381
satokab751aa2010-09-14 19:17:36 +09003382 /**
3383 * @return Return the current subtype of this input method.
3384 */
satok42c5a162011-05-26 16:46:14 +09003385 @Override
satokab751aa2010-09-14 19:17:36 +09003386 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003387 // TODO: Make this work even for non-current users?
3388 if (!calledFromValidUser()) {
3389 return null;
3390 }
3391 synchronized (mMethodMap) {
3392 return getCurrentInputMethodSubtypeLocked();
3393 }
3394 }
3395
3396 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003397 if (mCurMethodId == null) {
3398 return null;
3399 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003400 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003401 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3402 if (imi == null || imi.getSubtypeCount() == 0) {
3403 return null;
satok4e4569d2010-11-19 18:45:53 +09003404 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003405 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003406 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3407 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003408 if (subtypeId == NOT_A_SUBTYPE_ID) {
3409 // If there are no selected subtypes, the framework will try to find
3410 // the most applicable subtype from explicitly or implicitly enabled
3411 // subtypes.
3412 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003413 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003414 // If there is only one explicitly or implicitly enabled subtype,
3415 // just returns it.
3416 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3417 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3418 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003419 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003421 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003422 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003424 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3425 true);
satok4e4569d2010-11-19 18:45:53 +09003426 }
satok3ef8b292010-11-23 06:06:29 +09003427 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003428 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003429 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003430 }
3431 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003432 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003433 }
3434
satok4e4569d2010-11-19 18:45:53 +09003435 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003436 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003437 @Override
satok4e4569d2010-11-19 18:45:53 +09003438 public List getShortcutInputMethodsAndSubtypes() {
3439 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003440 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003441 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003442 // If there are no selected shortcut subtypes, the framework will try to find
3443 // the most applicable subtype from all subtypes whose mode is
3444 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003445 Pair<InputMethodInfo, InputMethodSubtype> info =
3446 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003447 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003448 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003449 ret.add(info.first);
3450 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003451 }
satok3da92232011-01-11 22:46:30 +09003452 return ret;
satokf3db1af2010-11-23 13:34:33 +09003453 }
satokf3db1af2010-11-23 13:34:33 +09003454 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3455 ret.add(imi);
3456 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3457 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003458 }
3459 }
satokf3db1af2010-11-23 13:34:33 +09003460 return ret;
satok4e4569d2010-11-19 18:45:53 +09003461 }
3462 }
3463
satok42c5a162011-05-26 16:46:14 +09003464 @Override
satokb66d2872010-11-10 01:04:04 +09003465 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003466 // TODO: Make this work even for non-current users?
3467 if (!calledFromValidUser()) {
3468 return false;
3469 }
satokb66d2872010-11-10 01:04:04 +09003470 synchronized (mMethodMap) {
3471 if (subtype != null && mCurMethodId != null) {
3472 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003473 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003474 if (subtypeId != NOT_A_SUBTYPE_ID) {
3475 setInputMethodLocked(mCurMethodId, subtypeId);
3476 return true;
3477 }
3478 }
3479 return false;
3480 }
3481 }
3482
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003483 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003484 private static class InputMethodFileManager {
3485 private static final String SYSTEM_PATH = "system";
3486 private static final String INPUT_METHOD_PATH = "inputmethod";
3487 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3488 private static final String NODE_SUBTYPES = "subtypes";
3489 private static final String NODE_SUBTYPE = "subtype";
3490 private static final String NODE_IMI = "imi";
3491 private static final String ATTR_ID = "id";
3492 private static final String ATTR_LABEL = "label";
3493 private static final String ATTR_ICON = "icon";
3494 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3495 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3496 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3497 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3498 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3499 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003500 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003501 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003502 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003503 if (methodMap == null) {
3504 throw new NullPointerException("methodMap is null");
3505 }
3506 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003507 final File systemDir = userId == UserHandle.USER_OWNER
3508 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3509 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003510 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003511 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003512 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3513 }
3514 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3515 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3516 if (!subtypeFile.exists()) {
3517 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003518 writeAdditionalInputMethodSubtypes(
3519 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003520 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003521 readAdditionalInputMethodSubtypes(
3522 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003523 }
3524 }
3525
3526 private void deleteAllInputMethodSubtypes(String imiId) {
3527 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003528 mAdditionalSubtypesMap.remove(imiId);
3529 writeAdditionalInputMethodSubtypes(
3530 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003531 }
3532 }
3533
3534 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003535 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003536 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003537 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003538 final int N = additionalSubtypes.length;
3539 for (int i = 0; i < N; ++i) {
3540 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003541 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003542 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003543 } else {
3544 Slog.w(TAG, "Duplicated subtype definition found: "
3545 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003546 }
3547 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003548 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3549 writeAdditionalInputMethodSubtypes(
3550 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003551 }
3552 }
3553
3554 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3555 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003556 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003557 }
3558 }
3559
3560 private static void writeAdditionalInputMethodSubtypes(
3561 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3562 HashMap<String, InputMethodInfo> methodMap) {
3563 // Safety net for the case that this function is called before methodMap is set.
3564 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3565 FileOutputStream fos = null;
3566 try {
3567 fos = subtypesFile.startWrite();
3568 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003569 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003570 out.startDocument(null, true);
3571 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3572 out.startTag(null, NODE_SUBTYPES);
3573 for (String imiId : allSubtypes.keySet()) {
3574 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3575 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3576 continue;
3577 }
3578 out.startTag(null, NODE_IMI);
3579 out.attribute(null, ATTR_ID, imiId);
3580 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3581 final int N = subtypesList.size();
3582 for (int i = 0; i < N; ++i) {
3583 final InputMethodSubtype subtype = subtypesList.get(i);
3584 out.startTag(null, NODE_SUBTYPE);
3585 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3586 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3587 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3588 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3589 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3590 out.attribute(null, ATTR_IS_AUXILIARY,
3591 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3592 out.endTag(null, NODE_SUBTYPE);
3593 }
3594 out.endTag(null, NODE_IMI);
3595 }
3596 out.endTag(null, NODE_SUBTYPES);
3597 out.endDocument();
3598 subtypesFile.finishWrite(fos);
3599 } catch (java.io.IOException e) {
3600 Slog.w(TAG, "Error writing subtypes", e);
3601 if (fos != null) {
3602 subtypesFile.failWrite(fos);
3603 }
3604 }
3605 }
3606
3607 private static void readAdditionalInputMethodSubtypes(
3608 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3609 if (allSubtypes == null || subtypesFile == null) return;
3610 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003611 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003612 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003613 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003614 int type = parser.getEventType();
3615 // Skip parsing until START_TAG
3616 while ((type = parser.next()) != XmlPullParser.START_TAG
3617 && type != XmlPullParser.END_DOCUMENT) {}
3618 String firstNodeName = parser.getName();
3619 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3620 throw new XmlPullParserException("Xml doesn't start with subtypes");
3621 }
3622 final int depth =parser.getDepth();
3623 String currentImiId = null;
3624 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3625 while (((type = parser.next()) != XmlPullParser.END_TAG
3626 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3627 if (type != XmlPullParser.START_TAG)
3628 continue;
3629 final String nodeName = parser.getName();
3630 if (NODE_IMI.equals(nodeName)) {
3631 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3632 if (TextUtils.isEmpty(currentImiId)) {
3633 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3634 continue;
3635 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003636 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003637 allSubtypes.put(currentImiId, tempSubtypesArray);
3638 } else if (NODE_SUBTYPE.equals(nodeName)) {
3639 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3640 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3641 continue;
3642 }
3643 final int icon = Integer.valueOf(
3644 parser.getAttributeValue(null, ATTR_ICON));
3645 final int label = Integer.valueOf(
3646 parser.getAttributeValue(null, ATTR_LABEL));
3647 final String imeSubtypeLocale =
3648 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3649 final String imeSubtypeMode =
3650 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3651 final String imeSubtypeExtraValue =
3652 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003653 final boolean isAuxiliary = "1".equals(String.valueOf(
3654 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003655 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3656 .setSubtypeNameResId(label)
3657 .setSubtypeIconResId(icon)
3658 .setSubtypeLocale(imeSubtypeLocale)
3659 .setSubtypeMode(imeSubtypeMode)
3660 .setSubtypeExtraValue(imeSubtypeExtraValue)
3661 .setIsAuxiliary(isAuxiliary)
3662 .build();
satoke7c6998e2011-06-03 17:57:59 +09003663 tempSubtypesArray.add(subtype);
3664 }
3665 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003666 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3667 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003668 return;
satoke7c6998e2011-06-03 17:57:59 +09003669 }
3670 }
3671 }
3672
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003673 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3674 @NonNull
3675 private final Handler mHandler;
3676
3677 LocalServiceImpl(@NonNull final Handler handler) {
3678 mHandler = handler;
3679 }
3680
3681 @Override
3682 public void setInteractive(boolean interactive) {
3683 // Do everything in handler so as not to block the caller.
3684 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3685 interactive ? 1 : 0, 0));
3686 }
3687 }
3688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 @Override
3690 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3691 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3692 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3695 + Binder.getCallingPid()
3696 + ", uid=" + Binder.getCallingUid());
3697 return;
3698 }
3699
3700 IInputMethod method;
3701 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 synchronized (mMethodMap) {
3706 p.println("Current Input Method Manager state:");
3707 int N = mMethodList.size();
3708 p.println(" Input Methods:");
3709 for (int i=0; i<N; i++) {
3710 InputMethodInfo info = mMethodList.get(i);
3711 p.println(" InputMethod #" + i + ":");
3712 info.dump(p, " ");
3713 }
3714 p.println(" Clients:");
3715 for (ClientState ci : mClients.values()) {
3716 p.println(" Client " + ci + ":");
3717 p.println(" client=" + ci.client);
3718 p.println(" inputContext=" + ci.inputContext);
3719 p.println(" sessionRequested=" + ci.sessionRequested);
3720 p.println(" curSession=" + ci.curSession);
3721 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003722 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003724 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3725 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3727 + " mBoundToMethod=" + mBoundToMethod);
3728 p.println(" mCurToken=" + mCurToken);
3729 p.println(" mCurIntent=" + mCurIntent);
3730 method = mCurMethod;
3731 p.println(" mCurMethod=" + mCurMethod);
3732 p.println(" mEnabledSession=" + mEnabledSession);
3733 p.println(" mShowRequested=" + mShowRequested
3734 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3735 + " mShowForced=" + mShowForced
3736 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003737 p.println(" mCurUserActionNotificationSequenceNumber="
3738 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003739 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003740 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003741 p.println(" mSwitchingController:");
3742 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003744
Jeff Brownb88102f2010-09-08 11:49:43 -07003745 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 pw.flush();
3748 try {
3749 client.client.asBinder().dump(fd, args);
3750 } catch (RemoteException e) {
3751 p.println("Input method client dead: " + e);
3752 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003753 } else {
3754 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003756
Jeff Brownb88102f2010-09-08 11:49:43 -07003757 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 pw.flush();
3760 try {
3761 method.asBinder().dump(fd, args);
3762 } catch (RemoteException e) {
3763 p.println("Input method service dead: " + e);
3764 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003765 } else {
3766 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
3768 }
3769}