blob: ab0c8a97fa0f248589d39416ad9691c6418ad70e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090022import com.android.internal.inputmethod.InputMethodUtils;
23import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070025import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090026import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputContext;
28import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070029import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputMethodClient;
31import com.android.internal.view.IInputMethodManager;
32import com.android.internal.view.IInputMethodSession;
33import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090035import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090042import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090044import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090045import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090050import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.ComponentName;
52import android.content.ContentResolver;
53import android.content.Context;
54import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090056import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090058import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070060import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090061import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.pm.PackageManager;
63import android.content.pm.ResolveInfo;
64import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080065import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070066import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.res.Resources;
68import android.content.res.TypedArray;
69import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080070import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080071import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070072import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090074import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Handler;
76import android.os.IBinder;
77import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Parcel;
82import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080083import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.ServiceManager;
85import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090086import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080087import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.provider.Settings;
89import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070090import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090091import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070092import android.util.ArrayMap;
93import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070094import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090096import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090097import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.PrintWriterPrinter;
99import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900100import android.util.Slog;
101import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900102import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700104import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.view.LayoutInflater;
106import android.view.View;
107import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900109import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.inputmethod.InputBinding;
111import android.view.inputmethod.InputMethod;
112import android.view.inputmethod.InputMethodInfo;
113import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900114import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900115import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900116import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900117import android.widget.CompoundButton;
118import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900120import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
satoke7c6998e2011-06-03 17:57:59 +0900123import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900125import java.io.FileInputStream;
126import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.io.IOException;
128import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100129import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900131import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700133import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900135import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137/**
138 * This class provides a system service that manages input methods.
139 */
140public class InputMethodManagerService extends IInputMethodManager.Stub
141 implements ServiceConnection, Handler.Callback {
142 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700143 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700144 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 private static final char INPUT_METHOD_SEPARATOR = ':';
147 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
148
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900166 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
satok01038492012-04-09 21:08:27 +0900168 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
169
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700170 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
satokf9f01002011-05-19 21:31:50 +0900172 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
173
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900174 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900175 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176
satok4e4569d2010-11-19 18:45:53 +0900177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800179 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900181 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final IWindowManager mIWindowManager;
184 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700185 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900186 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900187 private final HardKeyboardListener mHardKeyboardListener;
188 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900189 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900191 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // All known input methods. mMethodMap also serves as the global
194 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900195 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
196 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900197 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
198 new LruCache<SuggestionSpan, InputMethodInfo>(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;
215 private Notification mImeSwitcherNotification;
216 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final HashMap<IBinder, ClientState> mClients
276 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Id of the currently selected input method.
285 */
286 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The current binding sequence number, incremented every time there is
290 * a new bind performed.
291 */
292 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The client that is currently bound to an input method.
296 */
297 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700300 * The last window token that gained focus.
301 */
302 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * The input context last provided by the current client.
306 */
307 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The attributes last provided by the current client.
311 */
312 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The input method ID of the input method service that we are currently
316 * connected to or in the process of connecting to.
317 */
318 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
satokab751aa2010-09-14 19:17:36 +0900321 * The current subtype of the current input method.
322 */
323 private InputMethodSubtype mCurrentSubtype;
324
satok4e4569d2010-11-19 18:45:53 +0900325 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900326 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
327 mShortcutInputMethodsAndSubtypes =
328 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900329
John Spurlocke0980502013-10-25 11:59:29 -0400330 // Was the keyguard locked when this client became current?
331 private boolean mCurClientInKeyguard;
332
satokab751aa2010-09-14 19:17:36 +0900333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 * Set to true if our ServiceConnection is currently actively bound to
335 * a service (whether or not we have gotten its IBinder back yet).
336 */
337 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * Set if the client has asked for the input method to be shown.
341 */
342 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * Set if we were explicitly told to show the input method.
346 */
347 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Set if we were forced to be shown.
351 */
352 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Set if we last told the input method to show itself.
356 */
357 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * The Intent used to connect to the current input method.
361 */
362 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * The token we have made for the currently active input method, to
366 * identify it in the future.
367 */
368 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * If non-null, this is the input method service we are currently connected
372 * to.
373 */
374 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * Time that we last initiated a bind to the input method, to determine
378 * if we should try to disconnect and reconnect to it.
379 */
380 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
383 * Have we called mCurMethod.bindInput()?
384 */
385 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Currently enabled session. Only touched by service thread, not
389 * protected by a lock.
390 */
391 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * True if the screen is on. The value is true initially.
395 */
396 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900398 int mCurUserActionNotificationSequenceNumber = 0;
399
Joe Onorato857fd9b2011-01-27 15:08:35 -0800400 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
401 int mImeWindowVis;
402
Ken Wakasa05dbb652011-08-22 15:22:43 +0900403 private AlertDialog.Builder mDialogBuilder;
404 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900405 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900406 private InputMethodInfo[] mIms;
407 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900408 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700409 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
411 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800414 String mLastEnabled = "";
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 SettingsObserver(Handler handler) {
417 super(handler);
418 ContentResolver resolver = mContext.getContentResolver();
419 resolver.registerContentObserver(Settings.Secure.getUriFor(
420 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900421 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900422 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
423 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900424 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700425 resolver.registerContentObserver(Settings.Secure.getUriFor(
426 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
Michael Wright7b5a96b2014-08-09 19:28:42 -0700429 @Override public void onChange(boolean selfChange, Uri uri) {
430 final Uri showImeUri =
431 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700433 if (showImeUri.equals(uri)) {
434 updateKeyboardFromSettingsLocked();
435 } else {
436 boolean enabledChanged = false;
437 String newEnabled = mSettings.getEnabledInputMethodsStr();
438 if (!mLastEnabled.equals(newEnabled)) {
439 mLastEnabled = newEnabled;
440 enabledChanged = true;
441 }
442 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900448 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
449 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700451 if (mCurClient != null && mCurClient.client != null) {
452 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
453 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456
457 @Override
458 public void onReceive(Context context, Intent intent) {
459 final String action = intent.getAction();
460 if (Intent.ACTION_SCREEN_ON.equals(action)) {
461 mScreenOn = true;
462 refreshImeWindowVisibilityLocked();
463 updateActive();
464 return;
465 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
466 mScreenOn = false;
467 setImeWindowVisibilityStatusHiddenLocked();
468 updateActive();
469 return;
470 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
471 hideInputMethodMenu();
472 // No need to updateActive
473 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800474 } else if (Intent.ACTION_USER_ADDED.equals(action)
475 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100476 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800477 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700478 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
479 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
480 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
481 final String prevValue = intent.getStringExtra(
482 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
483 final String newValue = intent.getStringExtra(
484 Intent.EXTRA_SETTING_NEW_VALUE);
485 restoreEnabledInputMethods(mContext, prevValue, newValue);
486 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 } else {
488 Slog.w(TAG, "Unexpected intent " + intent);
489 }
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700493 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
494 // does not attempt to validate on the fly with any installed device policy, so must only
495 // be run in the context of initial device setup.
496 //
497 // TODO: Move this method to InputMethodUtils with adding unit tests.
498 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
499 if (DEBUG_RESTORE) {
500 Slog.i(TAG, "Restoring enabled input methods:");
501 Slog.i(TAG, "prev=" + prevValue);
502 Slog.i(TAG, " new=" + newValue);
503 }
504 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
505 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
506 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
507
508 // Merge the restored ime+subtype enabled states into the live state
509 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
510 final String imeId = entry.getKey();
511 ArraySet<String> prevSubtypes = prevMap.get(imeId);
512 if (prevSubtypes == null) {
513 prevSubtypes = new ArraySet<String>(2);
514 prevMap.put(imeId, prevSubtypes);
515 }
516 prevSubtypes.addAll(entry.getValue());
517 }
518
519 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
520 if (DEBUG_RESTORE) {
521 Slog.i(TAG, "Merged IME string:");
522 Slog.i(TAG, " " + mergedImesAndSubtypesString);
523 }
524 Settings.Secure.putString(context.getContentResolver(),
525 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
526 }
527
528 // TODO: Move this method to InputMethodUtils with adding unit tests.
529 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
530 // we want to use the canonical InputMethodSettings implementation,
531 // so we convert data structures first.
532 List<Pair<String, ArrayList<String>>> imeMap =
533 new ArrayList<Pair<String, ArrayList<String>>>(4);
534 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
535 final String imeName = entry.getKey();
536 final ArraySet<String> subtypeSet = entry.getValue();
537 final ArrayList<String> subtypes = new ArrayList<String>(2);
538 if (subtypeSet != null) {
539 subtypes.addAll(subtypeSet);
540 }
541 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
542 }
543 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
544 }
545
546 // TODO: Move this method to InputMethodUtils with adding unit tests.
547 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
548 final String inputMethodsAndSubtypesString) {
549 final ArrayMap<String, ArraySet<String>> imeMap =
550 new ArrayMap<String, ArraySet<String>>();
551 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
552 return imeMap;
553 }
554
555 final SimpleStringSplitter typeSplitter =
556 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
557 final SimpleStringSplitter subtypeSplitter =
558 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
559 List<Pair<String, ArrayList<String>>> allImeSettings =
560 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
561 typeSplitter,
562 subtypeSplitter);
563 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
564 ArraySet<String> subtypes = new ArraySet<String>();
565 if (ime.second != null) {
566 subtypes.addAll(ime.second);
567 }
568 imeMap.put(ime.first, subtypes);
569 }
570 return imeMap;
571 }
572
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800573 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 private boolean isChangingPackagesOfCurrentUser() {
575 final int userId = getChangingUserId();
576 final boolean retval = userId == mSettings.getCurrentUserId();
577 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900578 if (!retval) {
579 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
580 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 }
582 return retval;
583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800586 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900587 if (!isChangingPackagesOfCurrentUser()) {
588 return false;
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900591 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 final int N = mMethodList.size();
593 if (curInputMethodId != null) {
594 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800595 InputMethodInfo imi = mMethodList.get(i);
596 if (imi.getId().equals(curInputMethodId)) {
597 for (String pkg : packages) {
598 if (imi.getPackageName().equals(pkg)) {
599 if (!doit) {
600 return true;
601 }
satok723a27e2010-11-11 14:58:11 +0900602 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800603 chooseNewDefaultIMELocked();
604 return true;
605 }
606 }
607 }
608 }
609 }
610 }
611 return false;
612 }
613
614 @Override
615 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900616 if (!isChangingPackagesOfCurrentUser()) {
617 return;
618 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 synchronized (mMethodMap) {
620 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800622 final int N = mMethodList.size();
623 if (curInputMethodId != null) {
624 for (int i=0; i<N; i++) {
625 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900626 final String imiId = imi.getId();
627 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 curIm = imi;
629 }
satoke7c6998e2011-06-03 17:57:59 +0900630
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800631 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900632 if (isPackageModified(imi.getPackageName())) {
633 mFileManager.deleteAllInputMethodSubtypes(imiId);
634 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800635 if (change == PACKAGE_TEMPORARY_CHANGE
636 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800638 + imi.getComponent());
639 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 }
642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900644 buildInputMethodListLocked(
645 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800649 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 int change = isPackageDisappearing(curIm.getPackageName());
651 if (change == PACKAGE_TEMPORARY_CHANGE
652 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800653 ServiceInfo si = null;
654 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 si = mIPackageManager.getServiceInfo(
656 curIm.getComponent(), 0, mSettings.getCurrentUserId());
657 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800658 }
659 if (si == null) {
660 // Uh oh, current input method is no longer around!
661 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900663 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800665 changed = true;
666 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800667 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900668 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
670 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800671 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 }
satokab751aa2010-09-14 19:17:36 +0900673
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 if (curIm == null) {
675 // We currently don't have a default input method... is
676 // one now available?
677 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700678 } else if (!changed && isPackageModified(curIm.getPackageName())) {
679 // Even if the current input method is still available, mCurrentSubtype could
680 // be obsolete when the package is modified in practice.
681 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800685 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687 }
688 }
689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
Jeff Brownc28867a2013-03-26 15:42:39 -0700691 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900692 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700693 private final IInputMethod mMethod;
694 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
Jeff Brownc28867a2013-03-26 15:42:39 -0700696 MethodCallback(InputMethodManagerService imms, IInputMethod method,
697 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900698 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700699 mMethod = method;
700 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800702
satoke7c6998e2011-06-03 17:57:59 +0900703 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700704 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700705 long ident = Binder.clearCallingIdentity();
706 try {
707 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
708 } finally {
709 Binder.restoreCallingIdentity(ident);
710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800713
satok01038492012-04-09 21:08:27 +0900714 private class HardKeyboardListener
715 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
716 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700717 public void onHardKeyboardStatusChange(boolean available) {
718 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
719 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900720 }
721
Michael Wright7b5a96b2014-08-09 19:28:42 -0700722 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900723 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700724 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900725 }
726 synchronized(mMethodMap) {
727 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
728 && mSwitchingDialog.isShowing()) {
729 mSwitchingDialogTitleView.findViewById(
730 com.android.internal.R.id.hard_keyboard_section).setVisibility(
731 available ? View.VISIBLE : View.GONE);
732 }
733 }
734 }
735 }
736
737 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900738 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800740 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mHandler = new Handler(this);
742 mIWindowManager = IWindowManager.Stub.asInterface(
743 ServiceManager.getService(Context.WINDOW_SERVICE));
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*/);
satok01038492012-04-09 21:08:27 +0900750 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900751 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900752 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700753 mHasFeature = context.getPackageManager().hasSystemFeature(
754 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900755
satok7cfc0ed2011-06-20 21:29:36 +0900756 mImeSwitcherNotification = new Notification();
757 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
758 mImeSwitcherNotification.when = 0;
759 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
760 mImeSwitcherNotification.tickerText = null;
761 mImeSwitcherNotification.defaults = 0; // please be quiet
762 mImeSwitcherNotification.sound = null;
763 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400764
765 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400766 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
767 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400768
satok7cfc0ed2011-06-20 21:29:36 +0900769 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900770 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900771
772 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900773
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900774 final IntentFilter broadcastFilter = new IntentFilter();
775 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
776 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
777 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800778 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
779 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700780 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900781 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
satok7cfc0ed2011-06-20 21:29:36 +0900783 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900784 int userId = 0;
785 try {
786 ActivityManagerNative.getDefault().registerUserSwitchObserver(
787 new IUserSwitchObserver.Stub() {
788 @Override
789 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900790 synchronized(mMethodMap) {
791 switchUserLocked(newUserId);
792 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900793 if (reply != null) {
794 try {
795 reply.sendResult(null);
796 } catch (RemoteException e) {
797 }
798 }
799 }
800
801 @Override
802 public void onUserSwitchComplete(int newUserId) throws RemoteException {
803 }
Kenny Guy42979622015-04-13 18:03:05 +0000804
805 @Override
806 public void onForegroundProfileSwitch(int newProfileId) {
807 // Ignore.
808 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 });
810 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
811 } catch (RemoteException e) {
812 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
813 }
satok81f8b7c2012-12-04 20:42:56 +0900814 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900815
satokd87c2592010-09-29 11:52:06 +0900816 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900817 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100819 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900820 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900821 synchronized (mMethodMap) {
822 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
823 mSettings, context);
824 }
satok0a1bcf42012-05-16 19:26:31 +0900825
826 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900827 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900828 if (DEBUG) {
829 Slog.d(TAG, "Initial default ime = " + defaultImiId);
830 }
satok0a1bcf42012-05-16 19:26:31 +0900831 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
832
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900833 synchronized (mMethodMap) {
834 buildInputMethodListLocked(mMethodList, mMethodMap,
835 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
836 }
satokd87c2592010-09-29 11:52:06 +0900837 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
satok0a1bcf42012-05-16 19:26:31 +0900839 if (!mImeSelectedOnBoot) {
840 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900841 synchronized (mMethodMap) {
842 resetDefaultImeLocked(context);
843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900847 synchronized (mMethodMap) {
848 updateFromSettingsLocked(true);
849 }
satok5b927c432012-05-01 20:09:34 +0900850
851 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
852 // according to the new system locale.
853 final IntentFilter filter = new IntentFilter();
854 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
855 mContext.registerReceiver(
856 new BroadcastReceiver() {
857 @Override
858 public void onReceive(Context context, Intent intent) {
859 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900860 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900861 }
862 }
863 }, filter);
864 }
865
satok5b927c432012-05-01 20:09:34 +0900866 private void resetDefaultImeLocked(Context context) {
867 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900868 if (mCurMethodId != null
869 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900870 return;
871 }
872
873 InputMethodInfo defIm = null;
874 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900875 if (defIm == null && mSystemReady) {
876 final Locale systemLocale = context.getResources().getConfiguration().locale;
877 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
878 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
879 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900880 defIm = imi;
881 Slog.i(TAG, "Selected default: " + imi.getId());
882 }
883 }
884 }
885 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900886 defIm = InputMethodUtils.getMostApplicableDefaultIME(
887 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900888 if (defIm != null) {
889 Slog.i(TAG, "Default found, using " + defIm.getId());
890 } else {
891 Slog.i(TAG, "No default found");
892 }
satok5b927c432012-05-01 20:09:34 +0900893 }
894 if (defIm != null) {
895 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
896 }
897 }
898
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900899 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
900 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 if (!mSystemReady) {
902 // not system ready
903 return;
904 }
905 final Locale newLocale = mRes.getConfiguration().locale;
906 if (!updateOnlyWhenLocaleChanged
907 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
908 if (!updateOnlyWhenLocaleChanged) {
909 hideCurrentInputLocked(0, null);
910 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700911 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 }
913 if (DEBUG) {
914 Slog.i(TAG, "Locale has been changed to " + newLocale);
915 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900916 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900917 if (!updateOnlyWhenLocaleChanged) {
918 final String selectedImiId = mSettings.getSelectedInputMethod();
919 if (TextUtils.isEmpty(selectedImiId)) {
920 // This is the first time of the user switch and
921 // set the current ime to the proper one.
922 resetDefaultImeLocked(mContext);
923 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900924 } else {
925 // If the locale is changed, needs to reset the default ime
926 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800928 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 mLastSystemLocale = newLocale;
930 if (!updateOnlyWhenLocaleChanged) {
931 try {
932 startInputInnerLocked();
933 } catch (RuntimeException e) {
934 Slog.w(TAG, "Unexpected exception", e);
935 }
936 }
937 }
938 }
939
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900940 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900941 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
942 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 }
944
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900945 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100947 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900948 // InputMethodFileManager should be reset when the user is changed
949 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900950 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900951 // For secondary users, the list of enabled IMEs may not have been updated since the
952 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
953 // not be empty even if the IME has been uninstalled by the primary user.
954 // Even in such cases, IMMS works fine because it will find the most applicable
955 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900956 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900957 if (DEBUG) {
958 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
959 }
960 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900961 initialUserSwitch /* needsToResetDefaultIme */);
962 if (initialUserSwitch) {
963 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
964 mSettings.getEnabledInputMethodListLocked());
965 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 }
967
Kenny Guy2a764942014-04-02 13:29:20 +0100968 void updateCurrentProfileIds() {
969 List<UserInfo> profiles =
970 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
971 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
972 for (int i = 0; i < currentProfileIds.length; i++) {
973 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800974 }
Kenny Guy2a764942014-04-02 13:29:20 +0100975 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800976 }
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 @Override
979 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
980 throws RemoteException {
981 try {
982 return super.onTransact(code, data, reply, flags);
983 } catch (RuntimeException e) {
984 // The input method manager only throws security exceptions, so let's
985 // log all others.
986 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700987 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 throw e;
990 }
991 }
992
Svetoslav Ganova0027152013-06-25 14:59:53 -0700993 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700994 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 if (DEBUG) {
996 Slog.d(TAG, "--- systemReady");
997 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700998 if (!mSystemReady) {
999 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 mKeyguardManager =
1001 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001002 mNotificationManager = (NotificationManager)
1003 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1004 mStatusBar = statusBar;
1005 statusBar.setIconVisibility("ime", false);
1006 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +09001007 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1008 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001009 if (mShowOngoingImeSwitcherForPhones) {
1010 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1011 mHardKeyboardListener);
1012 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001013 buildInputMethodListLocked(mMethodList, mMethodMap,
1014 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001015 if (!mImeSelectedOnBoot) {
1016 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001017 resetStateIfCurrentLocaleChangedLocked();
1018 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1019 mContext.getPackageManager(),
1020 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001021 }
1022 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001023 try {
1024 startInputInnerLocked();
1025 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001027 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001028 }
1029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001031
satok15452a42011-10-28 17:58:28 +09001032 private void setImeWindowVisibilityStatusHiddenLocked() {
1033 mImeWindowVis = 0;
1034 updateImeWindowStatusLocked();
1035 }
1036
satok3afd6c02011-11-18 08:38:19 +09001037 private void refreshImeWindowVisibilityLocked() {
1038 final Configuration conf = mRes.getConfiguration();
1039 final boolean haveHardKeyboard = conf.keyboard
1040 != Configuration.KEYBOARD_NOKEYS;
1041 final boolean hardKeyShown = haveHardKeyboard
1042 && conf.hardKeyboardHidden
1043 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -04001044
1045 final boolean isScreenLocked = isKeyguardLocked();
1046 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -04001047 // We assume the softkeyboard is shown when the input is active as long as the
1048 // hard keyboard is not shown.
1049 final boolean inputVisible = inputActive && !hardKeyShown;
1050 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
1051 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +09001052 updateImeWindowStatusLocked();
1053 }
1054
satok15452a42011-10-28 17:58:28 +09001055 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +09001056 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001057 }
1058
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001059 // ---------------------------------------------------------------------------------------
1060 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1061 // 1) it comes from the system process
1062 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1063 private boolean calledFromValidUser() {
1064 final int uid = Binder.getCallingUid();
1065 final int userId = UserHandle.getUserId(uid);
1066 if (DEBUG) {
1067 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1068 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1069 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001070 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1071 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001072 }
Kenny Guy2a764942014-04-02 13:29:20 +01001073 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001074 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001075 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001076
1077 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1078 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1079 // must not manage background users' states in any functions.
1080 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1081 // by a token.
1082 if (mContext.checkCallingOrSelfPermission(
1083 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1084 == PackageManager.PERMISSION_GRANTED) {
1085 if (DEBUG) {
1086 Slog.d(TAG, "--- Access granted because the calling process has "
1087 + "the INTERACT_ACROSS_USERS_FULL permission");
1088 }
1089 return true;
1090 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001091 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1092 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001093 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001094 }
1095
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001096
1097 /**
1098 * Returns true iff the caller is identified to be the current input method with the token.
1099 * @param token The window token given to the input method when it was started.
1100 * @return true if and only if non-null valid token is specified.
1101 */
1102 private boolean calledWithValidToken(IBinder token) {
1103 if (token == null || mCurToken != token) {
1104 return false;
1105 }
1106 return true;
1107 }
1108
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 private boolean bindCurrentInputMethodService(
1110 Intent service, ServiceConnection conn, int flags) {
1111 if (service == null || conn == null) {
1112 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1113 return false;
1114 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001115 return mContext.bindServiceAsUser(service, conn, flags,
1116 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 }
1118
satoke7c6998e2011-06-03 17:57:59 +09001119 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001121 // TODO: Make this work even for non-current users?
1122 if (!calledFromValidUser()) {
1123 return Collections.emptyList();
1124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 synchronized (mMethodMap) {
1126 return new ArrayList<InputMethodInfo>(mMethodList);
1127 }
1128 }
1129
satoke7c6998e2011-06-03 17:57:59 +09001130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 // TODO: Make this work even for non-current users?
1133 if (!calledFromValidUser()) {
1134 return Collections.emptyList();
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001137 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
1140
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001141 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001142 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001143 * @return enabled subtypes of the specified imi
1144 */
satoke7c6998e2011-06-03 17:57:59 +09001145 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001146 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001147 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 // TODO: Make this work even for non-current users?
1149 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001150 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 }
satok67ddf9c2010-11-17 09:45:54 +09001152 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001153 final InputMethodInfo imi;
1154 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001155 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001156 } else {
1157 imi = mMethodMap.get(imiId);
1158 }
1159 if (imi == null) {
1160 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001161 }
1162 return mSettings.getEnabledInputMethodSubtypeListLocked(
1163 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001164 }
1165 }
1166
satoke7c6998e2011-06-03 17:57:59 +09001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public void addClient(IInputMethodClient client,
1169 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001170 if (!calledFromValidUser()) {
1171 return;
1172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 synchronized (mMethodMap) {
1174 mClients.put(client.asBinder(), new ClientState(client,
1175 inputContext, uid, pid));
1176 }
1177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
satoke7c6998e2011-06-03 17:57:59 +09001179 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 if (!calledFromValidUser()) {
1182 return;
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001185 ClientState cs = mClients.remove(client.asBinder());
1186 if (cs != null) {
1187 clearClientSessionLocked(cs);
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 void executeOrSendMessage(IInterface target, Message msg) {
1193 if (target.asBinder() instanceof Binder) {
1194 mCaller.sendMessage(msg);
1195 } else {
1196 handleMessage(msg);
1197 msg.recycle();
1198 }
1199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001201 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 + mCurClient.client.asBinder());
1205 if (mBoundToMethod) {
1206 mBoundToMethod = false;
1207 if (mCurMethod != null) {
1208 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1209 MSG_UNBIND_INPUT, mCurMethod));
1210 }
1211 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001212
1213 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1214 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1216 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1217 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
The Android Open Source Project10592532009-03-18 17:39:46 -07001220 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 private int getImeShowFlags() {
1225 int flags = 0;
1226 if (mShowForced) {
1227 flags |= InputMethod.SHOW_FORCED
1228 | InputMethod.SHOW_EXPLICIT;
1229 } else if (mShowExplicitlyRequested) {
1230 flags |= InputMethod.SHOW_EXPLICIT;
1231 }
1232 return flags;
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 private int getAppShowFlags() {
1236 int flags = 0;
1237 if (mShowForced) {
1238 flags |= InputMethodManager.SHOW_FORCED;
1239 } else if (!mShowExplicitlyRequested) {
1240 flags |= InputMethodManager.SHOW_IMPLICIT;
1241 }
1242 return flags;
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
Dianne Hackborn7663d802012-02-24 13:08:49 -08001245 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 if (!mBoundToMethod) {
1247 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1248 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1249 mBoundToMethod = true;
1250 }
1251 final SessionState session = mCurClient.curSession;
1252 if (initial) {
1253 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1254 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1255 } else {
1256 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1257 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1258 }
1259 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001261 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001263 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001264 (session.channel != null ? session.channel.dup() : null),
1265 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001269 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 // If no method is currently selected, do nothing.
1271 if (mCurMethodId == null) {
1272 return mNoBinding;
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 ClientState cs = mClients.get(client.asBinder());
1276 if (cs == null) {
1277 throw new IllegalArgumentException("unknown client "
1278 + client.asBinder());
1279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 try {
1282 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1283 // Check with the window manager to make sure this client actually
1284 // has a window with focus. If not, reject. This is thread safe
1285 // because if the focus changes some time before or after, the
1286 // next client receiving focus that has any interest in input will
1287 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1290 return null;
1291 }
1292 } catch (RemoteException e) {
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
Dianne Hackborn7663d802012-02-24 13:08:49 -08001295 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1296 }
1297
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001298 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001299 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1300 // If no method is currently selected, do nothing.
1301 if (mCurMethodId == null) {
1302 return mNoBinding;
1303 }
1304
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001305 if (attribute != null) {
1306 // We accept an empty package name as a valid data.
1307 if (!TextUtils.isEmpty(attribute.packageName) &&
1308 !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1309 attribute.packageName)) {
1310 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1311 + " uid=" + cs.uid + " package=" + attribute.packageName);
1312 return mNoBinding;
1313 }
1314 }
1315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001317 // Was the keyguard locked when switching over to the new client?
1318 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 // If the client is changing, we need to switch over to the new
1320 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001321 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001322 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001323 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324
1325 // If the screen is on, inform the new client it is active
1326 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001327 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1328 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 // Bump up the sequence for this client and attach it.
1333 mCurSeq++;
1334 if (mCurSeq <= 0) mCurSeq = 1;
1335 mCurClient = cs;
1336 mCurInputContext = inputContext;
1337 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 // Check if the input method is changing.
1340 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1341 if (cs.curSession != null) {
1342 // Fast case: if we are already connected to the input method,
1343 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001344 return attachNewInputLocked(
1345 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
1347 if (mHaveConnection) {
1348 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 // Return to client, and we will get back with it when
1350 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001351 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001352 return new InputBindResult(null, null, mCurId, mCurSeq,
1353 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 } else if (SystemClock.uptimeMillis()
1355 < (mLastBindTime+TIME_TO_RECONNECT)) {
1356 // In this case we have connected to the service, but
1357 // don't yet have its interface. If it hasn't been too
1358 // long since we did the connection, we'll return to
1359 // the client and wait to get the service interface so
1360 // we can report back. If it has been too long, we want
1361 // to fall through so we can try a disconnect/reconnect
1362 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001363 return new InputBindResult(null, null, mCurId, mCurSeq,
1364 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1367 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 }
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001372 return startInputInnerLocked();
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001375 InputBindResult startInputInnerLocked() {
1376 if (mCurMethodId == null) {
1377 return mNoBinding;
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001380 if (!mSystemReady) {
1381 // If the system is not yet ready, we shouldn't be running third
1382 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001383 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1384 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1388 if (info == null) {
1389 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001392 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1395 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001396 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1397 com.android.internal.R.string.input_method_binding_label);
1398 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1399 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001400 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001401 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1402 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 mLastBindTime = SystemClock.uptimeMillis();
1404 mHaveConnection = true;
1405 mCurId = info.getId();
1406 mCurToken = new Binder();
1407 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001408 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 mIWindowManager.addWindowToken(mCurToken,
1410 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1411 } catch (RemoteException e) {
1412 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001413 return new InputBindResult(null, null, mCurId, mCurSeq,
1414 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 } else {
1416 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 + mCurIntent);
1419 }
1420 return null;
1421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
satoke7c6998e2011-06-03 17:57:59 +09001423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001425 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001426 if (!calledFromValidUser()) {
1427 return null;
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 synchronized (mMethodMap) {
1430 final long ident = Binder.clearCallingIdentity();
1431 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001432 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 } finally {
1434 Binder.restoreCallingIdentity(ident);
1435 }
1436 }
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
satoke7c6998e2011-06-03 17:57:59 +09001439 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 public void finishInput(IInputMethodClient client) {
1441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
satoke7c6998e2011-06-03 17:57:59 +09001443 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 public void onServiceConnected(ComponentName name, IBinder service) {
1445 synchronized (mMethodMap) {
1446 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1447 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001448 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001450 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001451 return;
1452 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001453 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001454 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1455 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001457 clearClientSessionLocked(mCurClient);
1458 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
1460 }
1461 }
1462 }
1463
Jeff Brownc28867a2013-03-26 15:42:39 -07001464 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1465 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 synchronized (mMethodMap) {
1467 if (mCurMethod != null && method != null
1468 && mCurMethod.asBinder() == method.asBinder()) {
1469 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001470 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001472 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001473 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 if (res.method != null) {
1475 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1476 MSG_BIND_METHOD, mCurClient.client, res));
1477 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001478 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480 }
1481 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001482
1483 // Session abandoned. Close its associated input channel.
1484 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001486
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001487 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001488 if (mVisibleBound) {
1489 mContext.unbindService(mVisibleConnection);
1490 mVisibleBound = false;
1491 }
1492
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001493 if (mHaveConnection) {
1494 mContext.unbindService(this);
1495 mHaveConnection = false;
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001498 if (mCurToken != null) {
1499 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001501 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001502 // The current IME is shown. Hence an IME switch (transition) is happening.
1503 mWindowManagerService.saveLastInputMethodWindowForTransition();
1504 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001505 mIWindowManager.removeWindowToken(mCurToken);
1506 } catch (RemoteException e) {
1507 }
1508 mCurToken = null;
1509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
The Android Open Source Project10592532009-03-18 17:39:46 -07001511 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001512 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001514 if (reportToClient && mCurClient != null) {
1515 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1516 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1517 }
1518 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001519
1520 void requestClientSessionLocked(ClientState cs) {
1521 if (!cs.sessionRequested) {
1522 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1523 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1524 cs.sessionRequested = true;
1525 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1526 MSG_CREATE_SESSION, mCurMethod, channels[1],
1527 new MethodCallback(this, mCurMethod, channels[0])));
1528 }
1529 }
1530
1531 void clearClientSessionLocked(ClientState cs) {
1532 finishSessionLocked(cs.curSession);
1533 cs.curSession = null;
1534 cs.sessionRequested = false;
1535 }
1536
1537 private void finishSessionLocked(SessionState sessionState) {
1538 if (sessionState != null) {
1539 if (sessionState.session != null) {
1540 try {
1541 sessionState.session.finishSession();
1542 } catch (RemoteException e) {
1543 Slog.w(TAG, "Session failed to close due to remote exception", e);
1544 setImeWindowVisibilityStatusHiddenLocked();
1545 }
1546 sessionState.session = null;
1547 }
1548 if (sessionState.channel != null) {
1549 sessionState.channel.dispose();
1550 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001551 }
1552 }
1553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001554
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001555 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 if (mCurMethod != null) {
1557 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001560
Jeff Brownc28867a2013-03-26 15:42:39 -07001561 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001562 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 mCurMethod = null;
1564 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001565 if (mStatusBar != null) {
1566 mStatusBar.setIconVisibility("ime", false);
1567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001569
satoke7c6998e2011-06-03 17:57:59 +09001570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 public void onServiceDisconnected(ComponentName name) {
1572 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 + " mCurIntent=" + mCurIntent);
1575 if (mCurMethod != null && mCurIntent != null
1576 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 // We consider this to be a new bind attempt, since the system
1579 // should now try to restart the service for us.
1580 mLastBindTime = SystemClock.uptimeMillis();
1581 mShowRequested = mInputShown;
1582 mInputShown = false;
1583 if (mCurClient != null) {
1584 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1585 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1586 }
1587 }
1588 }
1589 }
1590
satokf9f01002011-05-19 21:31:50 +09001591 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1593 long ident = Binder.clearCallingIdentity();
1594 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001596 if (!calledWithValidToken(token)) {
1597 final int uid = Binder.getCallingUid();
1598 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1599 + " token:" + token);
1600 return;
1601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001604 if (mStatusBar != null) {
1605 mStatusBar.setIconVisibility("ime", false);
1606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001608 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001609 CharSequence contentDescription = null;
1610 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001611 // Use PackageManager to load label
1612 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001613 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001614 mIPackageManager.getApplicationInfo(packageName, 0,
1615 mSettings.getCurrentUserId()));
1616 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001617 /* ignore */
1618 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001619 if (mStatusBar != null) {
1620 mStatusBar.setIcon("ime", packageName, iconId, 0,
1621 contentDescription != null
1622 ? contentDescription.toString() : null);
1623 mStatusBar.setIconVisibility("ime", true);
1624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626 }
1627 } finally {
1628 Binder.restoreCallingIdentity(ident);
1629 }
1630 }
1631
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001632 private boolean shouldShowImeSwitcherLocked() {
satok7cfc0ed2011-06-20 21:29:36 +09001633 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001634 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001635 if (isScreenLocked()) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001636 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) == 0) return false;
1637 if (mWindowManagerService.isHardKeyboardAvailable()) {
1638 // When physical keyboard is attached, we show the ime switcher (or notification if
1639 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1640 // exists in the IME switcher dialog. Might be OK to remove this condition once
1641 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1642 return true;
1643 }
1644 if ((mImeWindowVis & InputMethodService.IME_VISIBLE) == 0) return false;
1645
1646 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1647 final int N = imis.size();
1648 if (N > 2) return true;
1649 if (N < 1) return false;
1650 int nonAuxCount = 0;
1651 int auxCount = 0;
1652 InputMethodSubtype nonAuxSubtype = null;
1653 InputMethodSubtype auxSubtype = null;
1654 for(int i = 0; i < N; ++i) {
1655 final InputMethodInfo imi = imis.get(i);
1656 final List<InputMethodSubtype> subtypes =
1657 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1658 final int subtypeCount = subtypes.size();
1659 if (subtypeCount == 0) {
1660 ++nonAuxCount;
1661 } else {
1662 for (int j = 0; j < subtypeCount; ++j) {
1663 final InputMethodSubtype subtype = subtypes.get(j);
1664 if (!subtype.isAuxiliary()) {
1665 ++nonAuxCount;
1666 nonAuxSubtype = subtype;
1667 } else {
1668 ++auxCount;
1669 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001670 }
1671 }
satok7cfc0ed2011-06-20 21:29:36 +09001672 }
1673 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001674 if (nonAuxCount > 1 || auxCount > 1) {
1675 return true;
1676 } else if (nonAuxCount == 1 && auxCount == 1) {
1677 if (nonAuxSubtype != null && auxSubtype != null
1678 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1679 || auxSubtype.overridesImplicitlyEnabledSubtype()
1680 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1681 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1682 return false;
1683 }
1684 return true;
1685 }
1686 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001687 }
1688
John Spurlocke0980502013-10-25 11:59:29 -04001689 private boolean isKeyguardLocked() {
1690 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1691 }
1692
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001693 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001694 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001695 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001696 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001697 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001698 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001699 if (!calledWithValidToken(token)) {
1700 final int uid = Binder.getCallingUid();
1701 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1702 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001703 return;
1704 }
satok06487a52010-10-29 11:37:18 +09001705 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001706 // apply policy for binder calls
1707 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1708 vis = 0;
1709 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001710 mImeWindowVis = vis;
1711 mBackDisposition = backDisposition;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001712 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1713 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked();
Jason Monkb605fec2014-05-02 17:04:10 -04001714 if (mStatusBar != null) {
1715 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1716 needsToShowImeSwitcher);
1717 }
satok5bc8e732011-07-22 21:07:23 +09001718 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001719 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001720 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001721 final CharSequence title = mRes.getText(
1722 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001723 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1724 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001725
Alan Viverette4a357cd2015-03-18 18:37:18 -07001726 mImeSwitcherNotification.color = mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +02001727 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001728 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001729 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001730 if ((mNotificationManager != null)
1731 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001732 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001733 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001734 }
1735 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001736 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001737 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001738 mNotificationShown = true;
1739 }
satok7cfc0ed2011-06-20 21:29:36 +09001740 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001741 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001742 if (DEBUG) {
1743 Slog.d(TAG, "--- hide notification");
1744 }
1745 mNotificationManager.cancelAsUser(null,
1746 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001747 mNotificationShown = false;
1748 }
1749 }
satok06487a52010-10-29 11:37:18 +09001750 }
1751 } finally {
1752 Binder.restoreCallingIdentity(ident);
1753 }
1754 }
1755
satoke7c6998e2011-06-03 17:57:59 +09001756 @Override
satokf9f01002011-05-19 21:31:50 +09001757 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001758 if (!calledFromValidUser()) {
1759 return;
1760 }
satokf9f01002011-05-19 21:31:50 +09001761 synchronized (mMethodMap) {
1762 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1763 for (int i = 0; i < spans.length; ++i) {
1764 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001765 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001766 mSecureSuggestionSpans.put(ss, currentImi);
1767 }
1768 }
1769 }
1770 }
1771
satoke7c6998e2011-06-03 17:57:59 +09001772 @Override
satokf9f01002011-05-19 21:31:50 +09001773 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001774 if (!calledFromValidUser()) {
1775 return false;
1776 }
satokf9f01002011-05-19 21:31:50 +09001777 synchronized (mMethodMap) {
1778 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1779 // TODO: Do not send the intent if the process of the targetImi is already dead.
1780 if (targetImi != null) {
1781 final String[] suggestions = span.getSuggestions();
1782 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001783 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001784 final Intent intent = new Intent();
1785 // Ensures that only a class in the original IME package will receive the
1786 // notification.
satok42c5a162011-05-26 16:46:14 +09001787 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001788 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1789 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1790 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1791 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001792 final long ident = Binder.clearCallingIdentity();
1793 try {
1794 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1795 } finally {
1796 Binder.restoreCallingIdentity(ident);
1797 }
satokf9f01002011-05-19 21:31:50 +09001798 return true;
1799 }
1800 }
1801 return false;
1802 }
1803
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001804 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001805 updateInputMethodsFromSettingsLocked(enabledMayChange);
1806 updateKeyboardFromSettingsLocked();
1807 }
1808
1809 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001810 if (enabledMayChange) {
1811 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1812 for (int i=0; i<enabled.size(); i++) {
1813 // We allow the user to select "disabled until used" apps, so if they
1814 // are enabling one of those here we now need to make it enabled.
1815 InputMethodInfo imm = enabled.get(i);
1816 try {
1817 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1818 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1819 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001820 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001821 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001822 if (DEBUG) {
1823 Slog.d(TAG, "Update state(" + imm.getId()
1824 + "): DISABLED_UNTIL_USED -> DEFAULT");
1825 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001826 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1827 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001828 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1829 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001830 }
1831 } catch (RemoteException e) {
1832 }
1833 }
1834 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001835 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1836 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1837 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1838 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001839 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001840 // There is no input method selected, try to choose new applicable input method.
1841 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001842 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001843 }
1844 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001846 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001848 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001849 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001850 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 }
satokf3db1af2010-11-23 13:34:33 +09001852 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001853 } else {
1854 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001855 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001856 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001858 // Here is not the perfect place to reset the switching controller. Ideally
1859 // mSwitchingController and mSettings should be able to share the same state.
1860 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1861 // the same enabled IMEs list.
1862 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001863
1864 }
1865
1866 public void updateKeyboardFromSettingsLocked() {
1867 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1868 if (mSwitchingDialog != null
1869 && mSwitchingDialogTitleView != null
1870 && mSwitchingDialog.isShowing()) {
1871 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1872 com.android.internal.R.id.hard_keyboard_switch);
1873 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001876
satokab751aa2010-09-14 19:17:36 +09001877 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 InputMethodInfo info = mMethodMap.get(id);
1879 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001880 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001883 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001884 // We have already made sure that the package name belongs to the application's UID.
1885 // No further UID check is required.
1886 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1887 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001888 }
1889 }
1890
satokd81e9502012-05-21 12:58:45 +09001891 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001893 final int subtypeCount = info.getSubtypeCount();
1894 if (subtypeCount <= 0) {
1895 return;
satokcd7cd292010-11-20 15:46:23 +09001896 }
satokd81e9502012-05-21 12:58:45 +09001897 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1898 final InputMethodSubtype newSubtype;
1899 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1900 newSubtype = info.getSubtypeAt(subtypeId);
1901 } else {
1902 // If subtype is null, try to find the most applicable one from
1903 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001904 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001905 }
1906 if (newSubtype == null || oldSubtype == null) {
1907 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1908 + ", new subtype = " + newSubtype);
1909 return;
1910 }
1911 if (newSubtype != oldSubtype) {
1912 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1913 if (mCurMethod != null) {
1914 try {
1915 refreshImeWindowVisibilityLocked();
1916 mCurMethod.changeInputMethodSubtype(newSubtype);
1917 } catch (RemoteException e) {
1918 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001919 }
1920 }
1921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 return;
1923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001924
satokd81e9502012-05-21 12:58:45 +09001925 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 final long ident = Binder.clearCallingIdentity();
1927 try {
satokab751aa2010-09-14 19:17:36 +09001928 // Set a subtype to this input method.
1929 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001930 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1931 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1932 // because mCurMethodId is stored as a history in
1933 // setSelectedInputMethodAndSubtypeLocked().
1934 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935
1936 if (ActivityManagerNative.isSystemReady()) {
1937 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001938 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001940 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001942 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 } finally {
1944 Binder.restoreCallingIdentity(ident);
1945 }
1946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001947
satok42c5a162011-05-26 16:46:14 +09001948 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001949 public boolean showSoftInput(IInputMethodClient client, int flags,
1950 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001951 if (!calledFromValidUser()) {
1952 return false;
1953 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001954 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 long ident = Binder.clearCallingIdentity();
1956 try {
1957 synchronized (mMethodMap) {
1958 if (mCurClient == null || client == null
1959 || mCurClient.client.asBinder() != client.asBinder()) {
1960 try {
1961 // We need to check if this is the current client with
1962 // focus in the window manager, to allow this call to
1963 // be made before input is started in it.
1964 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001965 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001966 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001969 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001972
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001973 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001974 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
1976 } finally {
1977 Binder.restoreCallingIdentity(ident);
1978 }
1979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
The Android Open Source Project4df24232009-03-05 14:34:35 -08001981 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 mShowRequested = true;
1983 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1984 mShowExplicitlyRequested = true;
1985 }
1986 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1987 mShowExplicitlyRequested = true;
1988 mShowForced = true;
1989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
Dianne Hackborncc278702009-09-02 23:07:23 -07001991 if (!mSystemReady) {
1992 return false;
1993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001994
The Android Open Source Project4df24232009-03-05 14:34:35 -08001995 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001997 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001998 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1999 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2000 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002002 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002003 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002004 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002005 | Context.BIND_TREAT_LIKE_ACTIVITY
2006 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002007 mVisibleBound = true;
2008 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002009 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002011 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 // The client has asked to have the input method shown, but
2013 // we have been sitting here too long with a connection to the
2014 // service and no interface received, so let's disconnect/connect
2015 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002016 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002018 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002021 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002022 } else {
2023 if (DEBUG) {
2024 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2025 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002028
The Android Open Source Project4df24232009-03-05 14:34:35 -08002029 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002031
satok42c5a162011-05-26 16:46:14 +09002032 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002033 public boolean hideSoftInput(IInputMethodClient client, int flags,
2034 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002035 if (!calledFromValidUser()) {
2036 return false;
2037 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002038 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 long ident = Binder.clearCallingIdentity();
2040 try {
2041 synchronized (mMethodMap) {
2042 if (mCurClient == null || client == null
2043 || mCurClient.client.asBinder() != client.asBinder()) {
2044 try {
2045 // We need to check if this is the current client with
2046 // focus in the window manager, to allow this call to
2047 // be made before input is started in it.
2048 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002049 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2050 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
Joe Onorato8a9b2202010-02-26 18:56:32 -08002058 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002059 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061 } finally {
2062 Binder.restoreCallingIdentity(ident);
2063 }
2064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002065
The Android Open Source Project4df24232009-03-05 14:34:35 -08002066 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2068 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002069 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 -08002070 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
2072 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002073 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 -08002074 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2079 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2080 res = true;
2081 } else {
2082 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002084 if (mHaveConnection && mVisibleBound) {
2085 mContext.unbindService(mVisibleConnection);
2086 mVisibleBound = false;
2087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 mInputShown = false;
2089 mShowRequested = false;
2090 mShowExplicitlyRequested = false;
2091 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002094
satok42c5a162011-05-26 16:46:14 +09002095 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002096 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2097 int controlFlags, int softInputMode, int windowFlags,
2098 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002099 // Needs to check the validity before clearing calling identity
2100 final boolean calledFromValidUser = calledFromValidUser();
2101
Dianne Hackborn7663d802012-02-24 13:08:49 -08002102 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 long ident = Binder.clearCallingIdentity();
2104 try {
2105 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002106 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002107 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002109 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002110
Dianne Hackborn7663d802012-02-24 13:08:49 -08002111 ClientState cs = mClients.get(client.asBinder());
2112 if (cs == null) {
2113 throw new IllegalArgumentException("unknown client "
2114 + client.asBinder());
2115 }
2116
2117 try {
2118 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2119 // Check with the window manager to make sure this client actually
2120 // has a window with focus. If not, reject. This is thread safe
2121 // because if the focus changes some time before or after, the
2122 // next client receiving focus that has any interest in input will
2123 // be calling through here after that change happens.
2124 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2125 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2126 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002128 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002131 if (!calledFromValidUser) {
2132 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2133 Slog.w(TAG, "If you want to interect with IME, you need "
2134 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2135 hideCurrentInputLocked(0, null);
2136 return null;
2137 }
2138
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002139 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002140 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002141 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002142 if (attribute != null) {
2143 return startInputUncheckedLocked(cs, inputContext, attribute,
2144 controlFlags);
2145 }
2146 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002147 }
2148 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002150 // Should we auto-show the IME even if the caller has not
2151 // specified what should be done with it?
2152 // We only do this automatically if the window can resize
2153 // to accommodate the IME (so what the user sees will give
2154 // them good context without input information being obscured
2155 // by the IME) or if running on a large screen where there
2156 // is more room for the target window + IME.
2157 final boolean doAutoShow =
2158 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2159 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2160 || mRes.getConfiguration().isLayoutSizeAtLeast(
2161 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002162 final boolean isTextEditor =
2163 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2164
2165 // We want to start input before showing the IME, but after closing
2166 // it. We want to do this after closing it to help the IME disappear
2167 // more quickly (not get stuck behind it initializing itself for the
2168 // new focused input, even if its window wants to hide the IME).
2169 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2172 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002173 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2175 // There is no focus view, and this window will
2176 // be behind any soft input window, so hide the
2177 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002178 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002179 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002181 } else if (isTextEditor && doAutoShow && (softInputMode &
2182 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 // There is a focus view, and we are navigating forward
2184 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002185 // We only do this automatically if the window can resize
2186 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002187 // them good context without input information being obscured
2188 // by the IME) or if running on a large screen where there
2189 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002190 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002191 if (attribute != null) {
2192 res = startInputUncheckedLocked(cs, inputContext, attribute,
2193 controlFlags);
2194 didStart = true;
2195 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002196 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
2198 break;
2199 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2200 // Do nothing.
2201 break;
2202 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2203 if ((softInputMode &
2204 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002205 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002206 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 }
2208 break;
2209 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002210 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 break;
2213 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2214 if ((softInputMode &
2215 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002216 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002217 if (attribute != null) {
2218 res = startInputUncheckedLocked(cs, inputContext, attribute,
2219 controlFlags);
2220 didStart = true;
2221 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
2224 break;
2225 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002226 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002227 if (attribute != null) {
2228 res = startInputUncheckedLocked(cs, inputContext, attribute,
2229 controlFlags);
2230 didStart = true;
2231 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002232 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 break;
2234 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002235
2236 if (!didStart && attribute != null) {
2237 res = startInputUncheckedLocked(cs, inputContext, attribute,
2238 controlFlags);
2239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241 } finally {
2242 Binder.restoreCallingIdentity(ident);
2243 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002244
2245 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002247
satok42c5a162011-05-26 16:46:14 +09002248 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002249 public void showInputMethodPickerFromClient(
2250 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002251 if (!calledFromValidUser()) {
2252 return;
2253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 synchronized (mMethodMap) {
2255 if (mCurClient == null || client == null
2256 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002257 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002258 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 }
2260
satok440aab52010-11-25 09:43:11 +09002261 // Always call subtype picker, because subtype picker is a superset of input method
2262 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002263 mHandler.sendMessage(mCaller.obtainMessageI(
2264 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002265 }
2266 }
2267
satok42c5a162011-05-26 16:46:14 +09002268 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002270 if (!calledFromValidUser()) {
2271 return;
2272 }
satok28203512010-11-24 11:06:49 +09002273 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2274 }
2275
satok42c5a162011-05-26 16:46:14 +09002276 @Override
satok28203512010-11-24 11:06:49 +09002277 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002278 if (!calledFromValidUser()) {
2279 return;
2280 }
satok28203512010-11-24 11:06:49 +09002281 synchronized (mMethodMap) {
2282 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002283 setInputMethodWithSubtypeIdLocked(token, id,
2284 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2285 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002286 } else {
2287 setInputMethod(token, id);
2288 }
2289 }
satokab751aa2010-09-14 19:17:36 +09002290 }
2291
satok42c5a162011-05-26 16:46:14 +09002292 @Override
satokb416a712010-11-25 20:42:14 +09002293 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002294 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002295 if (!calledFromValidUser()) {
2296 return;
2297 }
satokb416a712010-11-25 20:42:14 +09002298 synchronized (mMethodMap) {
2299 if (mCurClient == null || client == null
2300 || mCurClient.client.asBinder() != client.asBinder()) {
2301 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2302 }
satok7fee71f2010-12-17 18:54:26 +09002303 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2304 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002305 }
2306 }
2307
satok4fc87d62011-05-20 16:13:43 +09002308 @Override
satok735cf382010-11-11 20:40:09 +09002309 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002310 if (!calledFromValidUser()) {
2311 return false;
2312 }
satok735cf382010-11-11 20:40:09 +09002313 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002314 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002315 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002316 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002317 lastImi = mMethodMap.get(lastIme.first);
2318 } else {
2319 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002320 }
satok4fc87d62011-05-20 16:13:43 +09002321 String targetLastImiId = null;
2322 int subtypeId = NOT_A_SUBTYPE_ID;
2323 if (lastIme != null && lastImi != null) {
2324 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2325 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2326 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2327 : mCurrentSubtype.hashCode();
2328 // If the last IME is the same as the current IME and the last subtype is not
2329 // defined, there is no need to switch to the last IME.
2330 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2331 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002332 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002333 }
2334 }
2335
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002336 if (TextUtils.isEmpty(targetLastImiId)
2337 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002338 // This is a safety net. If the currentSubtype can't be added to the history
2339 // and the framework couldn't find the last ime, we will make the last ime be
2340 // the most applicable enabled keyboard subtype of the system imes.
2341 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2342 if (enabled != null) {
2343 final int N = enabled.size();
2344 final String locale = mCurrentSubtype == null
2345 ? mRes.getConfiguration().locale.toString()
2346 : mCurrentSubtype.getLocale();
2347 for (int i = 0; i < N; ++i) {
2348 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002349 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002350 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002351 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2352 InputMethodUtils.getSubtypes(imi),
2353 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002354 if (keyboardSubtype != null) {
2355 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002356 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002357 imi, keyboardSubtype.hashCode());
2358 if(keyboardSubtype.getLocale().equals(locale)) {
2359 break;
2360 }
2361 }
2362 }
2363 }
2364 }
2365 }
2366
2367 if (!TextUtils.isEmpty(targetLastImiId)) {
2368 if (DEBUG) {
2369 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2370 + ", from: " + mCurMethodId + ", " + subtypeId);
2371 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002372 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002373 return true;
2374 } else {
2375 return false;
2376 }
satok735cf382010-11-11 20:40:09 +09002377 }
2378 }
2379
satoke7c6998e2011-06-03 17:57:59 +09002380 @Override
satok688bd472012-02-09 20:09:17 +09002381 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002382 if (!calledFromValidUser()) {
2383 return false;
2384 }
satok688bd472012-02-09 20:09:17 +09002385 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002386 if (!calledWithValidToken(token)) {
2387 final int uid = Binder.getCallingUid();
2388 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2389 + " token:" + token);
2390 return false;
2391 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002392 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002393 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2394 if (nextSubtype == null) {
2395 return false;
2396 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002397 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2398 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002399 return true;
2400 }
2401 }
2402
2403 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002404 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2405 if (!calledFromValidUser()) {
2406 return false;
2407 }
2408 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002409 if (!calledWithValidToken(token)) {
2410 final int uid = Binder.getCallingUid();
2411 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2412 + "token. uid:" + uid + " token:" + token);
2413 return false;
2414 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002415 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002416 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2417 if (nextSubtype == null) {
2418 return false;
2419 }
2420 return true;
2421 }
2422 }
2423
2424 @Override
satok68f1b782011-04-11 14:26:04 +09002425 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 if (!calledFromValidUser()) {
2427 return null;
2428 }
satok68f1b782011-04-11 14:26:04 +09002429 synchronized (mMethodMap) {
2430 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2431 // TODO: Handle the case of the last IME with no subtypes
2432 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2433 || TextUtils.isEmpty(lastIme.second)) return null;
2434 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2435 if (lastImi == null) return null;
2436 try {
2437 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002438 final int lastSubtypeId =
2439 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002440 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2441 return null;
2442 }
2443 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002444 } catch (NumberFormatException e) {
2445 return null;
2446 }
2447 }
2448 }
2449
satoke7c6998e2011-06-03 17:57:59 +09002450 @Override
satokee5e77c2011-09-02 18:50:15 +09002451 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002452 if (!calledFromValidUser()) {
2453 return;
2454 }
satok91e88122011-07-18 11:11:42 +09002455 // By this IPC call, only a process which shares the same uid with the IME can add
2456 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002457 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002458 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002459 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002460 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002461 final String[] packageInfos;
2462 try {
2463 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2464 } catch (RemoteException e) {
2465 Slog.e(TAG, "Failed to get package infos");
2466 return;
2467 }
satok91e88122011-07-18 11:11:42 +09002468 if (packageInfos != null) {
2469 final int packageNum = packageInfos.length;
2470 for (int i = 0; i < packageNum; ++i) {
2471 if (packageInfos[i].equals(imi.getPackageName())) {
2472 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002473 final long ident = Binder.clearCallingIdentity();
2474 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002475 buildInputMethodListLocked(mMethodList, mMethodMap,
2476 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002477 } finally {
2478 Binder.restoreCallingIdentity(ident);
2479 }
satokee5e77c2011-09-02 18:50:15 +09002480 return;
satok91e88122011-07-18 11:11:42 +09002481 }
2482 }
2483 }
satoke7c6998e2011-06-03 17:57:59 +09002484 }
satokee5e77c2011-09-02 18:50:15 +09002485 return;
satoke7c6998e2011-06-03 17:57:59 +09002486 }
2487
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002488 @Override
2489 public int getInputMethodWindowVisibleHeight() {
2490 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2491 }
2492
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002493 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002494 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002495 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002496 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002497 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002498 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002499 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2500 if (DEBUG) {
2501 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2502 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2503 + " actual: " + sequenceNumber);
2504 }
2505 return;
2506 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002507 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2508 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002509 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002510 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002511 }
2512 }
2513
satok28203512010-11-24 11:06:49 +09002514 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002516 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2517 }
2518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002520 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2521 if (token == null) {
2522 if (mContext.checkCallingOrSelfPermission(
2523 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2524 != PackageManager.PERMISSION_GRANTED) {
2525 throw new SecurityException(
2526 "Using null token requires permission "
2527 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002529 } else if (mCurToken != token) {
2530 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2531 + " token: " + token);
2532 return;
2533 }
2534
2535 final long ident = Binder.clearCallingIdentity();
2536 try {
2537 setInputMethodLocked(id, subtypeId);
2538 } finally {
2539 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
2541 }
2542
satok42c5a162011-05-26 16:46:14 +09002543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002545 if (!calledFromValidUser()) {
2546 return;
2547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002549 if (!calledWithValidToken(token)) {
2550 final int uid = Binder.getCallingUid();
2551 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2552 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 return;
2554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 long ident = Binder.clearCallingIdentity();
2556 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002557 hideCurrentInputLocked(flags, null);
2558 } finally {
2559 Binder.restoreCallingIdentity(ident);
2560 }
2561 }
2562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002563
satok42c5a162011-05-26 16:46:14 +09002564 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002565 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002566 if (!calledFromValidUser()) {
2567 return;
2568 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002569 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002570 if (!calledWithValidToken(token)) {
2571 final int uid = Binder.getCallingUid();
2572 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2573 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002574 return;
2575 }
2576 long ident = Binder.clearCallingIdentity();
2577 try {
2578 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 } finally {
2580 Binder.restoreCallingIdentity(ident);
2581 }
2582 }
2583 }
2584
2585 void setEnabledSessionInMainThread(SessionState session) {
2586 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002587 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002589 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002590 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 } catch (RemoteException e) {
2592 }
2593 }
2594 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002595 if (mEnabledSession != null && mEnabledSession.session != null) {
2596 try {
2597 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2598 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2599 } catch (RemoteException e) {
2600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 }
2602 }
2603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002604
satok42c5a162011-05-26 16:46:14 +09002605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002607 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002609 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002610 final boolean showAuxSubtypes;
2611 switch (msg.arg1) {
2612 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2613 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2614 // implemented so that auxiliary subtypes will be excluded when the soft
2615 // keyboard is invisible.
2616 showAuxSubtypes = mInputShown;
2617 break;
2618 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2619 showAuxSubtypes = true;
2620 break;
2621 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2622 showAuxSubtypes = false;
2623 break;
2624 default:
2625 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2626 return false;
2627 }
2628 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002629 return true;
2630
satok47a44912010-10-06 16:03:58 +09002631 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002632 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002633 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002634 args.recycle();
satok217f5482010-12-15 05:19:19 +09002635 return true;
2636
2637 case MSG_SHOW_IM_CONFIG:
2638 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002639 return true;
2640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 case MSG_UNBIND_INPUT:
2644 try {
2645 ((IInputMethod)msg.obj).unbindInput();
2646 } catch (RemoteException e) {
2647 // There is nothing interesting about the method dying.
2648 }
2649 return true;
2650 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002651 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 try {
2653 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2654 } catch (RemoteException e) {
2655 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002656 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 return true;
2658 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002659 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002661 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002662 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002663 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 } catch (RemoteException e) {
2665 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002666 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 return true;
2668 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002669 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002671 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002672 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002673 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 } catch (RemoteException e) {
2675 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002676 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 return true;
2678 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002679 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002681 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2683 } catch (RemoteException e) {
2684 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002685 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002687 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002688 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002689 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002690 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002692 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002694 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002695 // Dispose the channel if the input method is not local to this process
2696 // because the remote proxy will get its own copy when unparceled.
2697 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002698 channel.dispose();
2699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002701 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002707 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 try {
2709 SessionState session = (SessionState)args.arg1;
2710 setEnabledSessionInMainThread(session);
2711 session.method.startInput((IInputContext)args.arg2,
2712 (EditorInfo)args.arg3);
2713 } catch (RemoteException e) {
2714 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002715 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 return true;
2717 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002718 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 try {
2720 SessionState session = (SessionState)args.arg1;
2721 setEnabledSessionInMainThread(session);
2722 session.method.restartInput((IInputContext)args.arg2,
2723 (EditorInfo)args.arg3);
2724 } catch (RemoteException e) {
2725 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002726 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 case MSG_UNBIND_METHOD:
2732 try {
2733 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2734 } catch (RemoteException e) {
2735 // There is nothing interesting about the last client dying.
2736 }
2737 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002738 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002739 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002740 IInputMethodClient client = (IInputMethodClient)args.arg1;
2741 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002743 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002745 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002746 } finally {
2747 // Dispose the channel if the input method is not local to this process
2748 // because the remote proxy will get its own copy when unparceled.
2749 if (res.channel != null && Binder.isProxy(client)) {
2750 res.channel.dispose();
2751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002753 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002755 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002756 case MSG_SET_ACTIVE:
2757 try {
2758 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2759 } catch (RemoteException e) {
2760 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2761 + ((ClientState)msg.obj).pid + " uid "
2762 + ((ClientState)msg.obj).uid);
2763 }
2764 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002765 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2766 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002767 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002768 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002769 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002770 } catch (RemoteException e) {
2771 Slog.w(TAG, "Got RemoteException sending "
2772 + "setUserActionNotificationSequenceNumber("
2773 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002774 + clientState.pid + " uid "
2775 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002776 }
2777 return true;
2778 }
satok01038492012-04-09 21:08:27 +09002779
2780 // --------------------------------------------------------------
2781 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002782 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002783 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 return false;
2786 }
2787
satokdc9ddae2011-10-06 12:22:36 +09002788 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002789 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2790 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002791 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002792 if (DEBUG) {
2793 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2794 }
satok723a27e2010-11-11 14:58:11 +09002795 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002796 return true;
2797 }
2798
2799 return false;
2800 }
2801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002803 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002804 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002805 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002806 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 list.clear();
2809 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002810
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002811 // Use for queryIntentServicesAsUser
2812 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002813 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002814 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002816 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002818 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2819 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002820
satoke7c6998e2011-06-03 17:57:59 +09002821 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2822 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 for (int i = 0; i < services.size(); ++i) {
2824 ResolveInfo ri = services.get(i);
2825 ServiceInfo si = ri.serviceInfo;
2826 ComponentName compName = new ComponentName(si.packageName, si.name);
2827 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2828 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002829 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 + ": it does not require the permission "
2831 + android.Manifest.permission.BIND_INPUT_METHOD);
2832 continue;
2833 }
2834
Joe Onorato8a9b2202010-02-26 18:56:32 -08002835 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836
2837 try {
satoke7c6998e2011-06-03 17:57:59 +09002838 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002840 final String id = p.getId();
2841 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
2843 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002844 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002848 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002850 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 }
2852 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002853
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002854 if (resetDefaultEnabledIme) {
2855 final ArrayList<InputMethodInfo> defaultEnabledIme =
2856 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2857 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2858 final InputMethodInfo imi = defaultEnabledIme.get(i);
2859 if (DEBUG) {
2860 Slog.d(TAG, "--- enable ime = " + imi);
2861 }
2862 setInputMethodEnabledLocked(imi.getId(), true);
2863 }
2864 }
2865
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002866 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002867 if (!TextUtils.isEmpty(defaultImiId)) {
2868 if (!map.containsKey(defaultImiId)) {
2869 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2870 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002871 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002872 }
2873 } else {
2874 // Double check that the default IME is certainly enabled.
2875 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002876 }
2877 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002878 // Here is not the perfect place to reset the switching controller. Ideally
2879 // mSwitchingController and mSettings should be able to share the same state.
2880 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2881 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002882 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002886
satok217f5482010-12-15 05:19:19 +09002887 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002888 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002889 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002890 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2891 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002892 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002893 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002894 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002895 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002896 }
2897
2898 private void showConfigureInputMethods() {
2899 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2900 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2901 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2902 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002903 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002904 }
2905
satok2c93efc2012-04-02 19:33:47 +09002906 private boolean isScreenLocked() {
2907 return mKeyguardManager != null
2908 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2909 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002910
Seigo Nonaka14e13912015-05-06 21:04:13 -07002911 private void showInputMethodMenu(boolean showAuxSubtypes) {
2912 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002915 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002916
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002917 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002918 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002919 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002920
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002921 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002922 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002923 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2924 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002925 if (immis == null || immis.size() == 0) {
2926 return;
2927 }
2928
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002929 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930
satok688bd472012-02-09 20:09:17 +09002931 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002932 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002933 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002934
satokc3690562012-01-10 20:14:43 +09002935 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002936 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002937 if (currentSubtype != null) {
2938 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002939 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2940 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002941 }
2942 }
2943
Ken Wakasa761eb372011-03-04 19:06:18 +09002944 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002945 mIms = new InputMethodInfo[N];
2946 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002947 int checkedItem = 0;
2948 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002949 final ImeSubtypeListItem item = imList.get(i);
2950 mIms[i] = item.mImi;
2951 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002952 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002953 int subtypeId = mSubtypeIds[i];
2954 if ((subtypeId == NOT_A_SUBTYPE_ID)
2955 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2956 || (subtypeId == lastInputMethodSubtypeId)) {
2957 checkedItem = i;
2958 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002961
2962 final Context settingsContext = new ContextThemeWrapper(context,
2963 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2964
2965 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002966 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2967 @Override
2968 public void onCancel(DialogInterface dialog) {
2969 hideInputMethodMenu();
2970 }
2971 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002972
2973 final Context dialogContext = mDialogBuilder.getContext();
2974 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2975 com.android.internal.R.styleable.DialogPreference,
2976 com.android.internal.R.attr.alertDialogStyle, 0);
2977 final Drawable dialogIcon = a.getDrawable(
2978 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2979 a.recycle();
2980
2981 mDialogBuilder.setIcon(dialogIcon);
2982
2983 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2984 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002985 final View tv = inflater.inflate(
2986 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2987 mDialogBuilder.setCustomTitle(tv);
2988
2989 // Setup layout for a toggle switch of the hardware keyboard
2990 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002991 mSwitchingDialogTitleView
2992 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2993 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2994 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002995 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002996 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002997 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002998 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2999 @Override
3000 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003001 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003002 // Ensure that the input method dialog is dismissed when changing
3003 // the hardware keyboard state.
3004 hideInputMethodMenu();
3005 }
3006 });
3007
Alan Viverette505e3ab2014-11-24 15:22:11 -08003008 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003009 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3010 final OnClickListener choiceListener = new OnClickListener() {
3011 @Override
3012 public void onClick(final DialogInterface dialog, final int which) {
3013 synchronized (mMethodMap) {
3014 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3015 || mSubtypeIds.length <= which) {
3016 return;
satok01038492012-04-09 21:08:27 +09003017 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003018 final InputMethodInfo im = mIms[which];
3019 int subtypeId = mSubtypeIds[which];
3020 adapter.mCheckedItem = which;
3021 adapter.notifyDataSetChanged();
3022 hideInputMethodMenu();
3023 if (im != null) {
3024 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3025 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003026 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003027 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003029 }
3030 }
3031 };
3032 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003034 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003035 final OnClickListener positiveListener = new OnClickListener() {
3036 @Override
3037 public void onClick(DialogInterface dialog, int whichButton) {
3038 showConfigureInputMethods();
3039 }
3040 };
satok82beadf2010-12-27 19:03:06 +09003041 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003042 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003045 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 mSwitchingDialog.getWindow().setType(
3047 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003048 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3049 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003050 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04003051 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 mSwitchingDialog.show();
3053 }
3054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003055
Ken Wakasa05dbb652011-08-22 15:22:43 +09003056 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3057 private final LayoutInflater mInflater;
3058 private final int mTextViewResourceId;
3059 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003060 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003061 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3062 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3063 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003064
Ken Wakasa05dbb652011-08-22 15:22:43 +09003065 mTextViewResourceId = textViewResourceId;
3066 mItemsList = itemsList;
3067 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003068 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003069 }
3070
3071 @Override
3072 public View getView(int position, View convertView, ViewGroup parent) {
3073 final View view = convertView != null ? convertView
3074 : mInflater.inflate(mTextViewResourceId, null);
3075 if (position < 0 || position >= mItemsList.size()) return view;
3076 final ImeSubtypeListItem item = mItemsList.get(position);
3077 final CharSequence imeName = item.mImeName;
3078 final CharSequence subtypeName = item.mSubtypeName;
3079 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3080 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3081 if (TextUtils.isEmpty(subtypeName)) {
3082 firstTextView.setText(imeName);
3083 secondTextView.setVisibility(View.GONE);
3084 } else {
3085 firstTextView.setText(subtypeName);
3086 secondTextView.setText(imeName);
3087 secondTextView.setVisibility(View.VISIBLE);
3088 }
3089 final RadioButton radioButton =
3090 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3091 radioButton.setChecked(position == mCheckedItem);
3092 return view;
3093 }
3094 }
3095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003097 synchronized (mMethodMap) {
3098 hideInputMethodMenuLocked();
3099 }
3100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003101
The Android Open Source Project10592532009-03-18 17:39:46 -07003102 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003103 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104
The Android Open Source Project10592532009-03-18 17:39:46 -07003105 if (mSwitchingDialog != null) {
3106 mSwitchingDialog.dismiss();
3107 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003109
Jason Monk807ef762014-05-08 15:47:46 -04003110 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07003111 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003112 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003116
satok42c5a162011-05-26 16:46:14 +09003117 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003119 // TODO: Make this work even for non-current users?
3120 if (!calledFromValidUser()) {
3121 return false;
3122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 synchronized (mMethodMap) {
3124 if (mContext.checkCallingOrSelfPermission(
3125 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3126 != PackageManager.PERMISSION_GRANTED) {
3127 throw new SecurityException(
3128 "Requires permission "
3129 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3130 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 long ident = Binder.clearCallingIdentity();
3133 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003134 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 } finally {
3136 Binder.restoreCallingIdentity(ident);
3137 }
3138 }
3139 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003140
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003141 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3142 // Make sure this is a valid input method.
3143 InputMethodInfo imm = mMethodMap.get(id);
3144 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003145 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003146 }
3147
satokd87c2592010-09-29 11:52:06 +09003148 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3149 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003150
satokd87c2592010-09-29 11:52:06 +09003151 if (enabled) {
3152 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3153 if (pair.first.equals(id)) {
3154 // We are enabling this input method, but it is already enabled.
3155 // Nothing to do. The previous state was enabled.
3156 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003157 }
3158 }
satokd87c2592010-09-29 11:52:06 +09003159 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3160 // Previous state was disabled.
3161 return false;
3162 } else {
3163 StringBuilder builder = new StringBuilder();
3164 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3165 builder, enabledInputMethodsList, id)) {
3166 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003167 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003168 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3169 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3170 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003171 }
3172 // Previous state was enabled.
3173 return true;
3174 } else {
3175 // We are disabling the input method but it is already disabled.
3176 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003177 return false;
3178 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003179 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003180 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003181
satok723a27e2010-11-11 14:58:11 +09003182 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3183 boolean setSubtypeOnly) {
3184 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003185 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003186
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003187 mCurUserActionNotificationSequenceNumber =
3188 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3189 if (DEBUG) {
3190 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3191 + mCurUserActionNotificationSequenceNumber);
3192 }
3193
3194 if (mCurClient != null && mCurClient.client != null) {
3195 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3196 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003197 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003198 }
3199
satok723a27e2010-11-11 14:58:11 +09003200 // Set Subtype here
3201 if (imi == null || subtypeId < 0) {
3202 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003203 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003204 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003205 if (subtypeId < imi.getSubtypeCount()) {
3206 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3207 mSettings.putSelectedSubtype(subtype.hashCode());
3208 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003209 } else {
3210 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003211 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003212 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003213 }
satokab751aa2010-09-14 19:17:36 +09003214 }
satok723a27e2010-11-11 14:58:11 +09003215
satok4c0e7152012-06-20 20:08:44 +09003216 // Workaround.
3217 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3218 // IMEs are not recognized and considered uninstalled.
3219 // Actually, we can't move everything after SystemReady because
3220 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3221 // the default IME here and try cheking the default IME again in systemReady().
3222 // TODO: Do nothing before system ready and implement a separated logic for
3223 // the encryption lock screen.
3224 // TODO: ASEC should be ready before IMMS is instantiated.
3225 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003226 // Set InputMethod here
3227 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3228 }
3229 }
3230
3231 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3232 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3233 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3234 // newDefaultIme is empty when there is no candidate for the selected IME.
3235 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3236 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3237 if (subtypeHashCode != null) {
3238 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003239 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003240 imi, Integer.valueOf(subtypeHashCode));
3241 } catch (NumberFormatException e) {
3242 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3243 }
3244 }
3245 }
3246 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003247 }
3248
satok4e4569d2010-11-19 18:45:53 +09003249 // If there are no selected shortcuts, tries finding the most applicable ones.
3250 private Pair<InputMethodInfo, InputMethodSubtype>
3251 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3252 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3253 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003254 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003255 boolean foundInSystemIME = false;
3256
3257 // Search applicable subtype for each InputMethodInfo
3258 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003259 final String imiId = imi.getId();
3260 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3261 continue;
3262 }
satokcd7cd292010-11-20 15:46:23 +09003263 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003264 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003265 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003266 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003267 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003268 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003269 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003270 }
satokdf31ae62011-01-15 06:19:44 +09003271 // 2. Search by the system locale from enabledSubtypes.
3272 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003273 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003274 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003275 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003276 }
satoka86f5e42011-09-02 17:12:42 +09003277 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003278 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003279 final ArrayList<InputMethodSubtype> subtypesForSearch =
3280 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003281 ? InputMethodUtils.getSubtypes(imi)
3282 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003283 // 4. Search by the current subtype's locale from all subtypes.
3284 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003285 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003286 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003287 }
3288 // 5. Search by the system locale from all subtypes.
3289 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003290 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003291 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003292 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003293 }
satokcd7cd292010-11-20 15:46:23 +09003294 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003295 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003296 // The current input method is the most applicable IME.
3297 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003298 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003299 break;
satok7599a7f2010-12-22 13:45:23 +09003300 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003301 // The system input method is 2nd applicable IME.
3302 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003303 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003304 if ((imi.getServiceInfo().applicationInfo.flags
3305 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3306 foundInSystemIME = true;
3307 }
satok4e4569d2010-11-19 18:45:53 +09003308 }
3309 }
3310 }
3311 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003312 if (mostApplicableIMI != null) {
3313 Slog.w(TAG, "Most applicable shortcut input method was:"
3314 + mostApplicableIMI.getId());
3315 if (mostApplicableSubtype != null) {
3316 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3317 + "," + mostApplicableSubtype.getMode() + ","
3318 + mostApplicableSubtype.getLocale());
3319 }
3320 }
satok4e4569d2010-11-19 18:45:53 +09003321 }
satokcd7cd292010-11-20 15:46:23 +09003322 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003323 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003324 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003325 } else {
3326 return null;
3327 }
3328 }
3329
satokab751aa2010-09-14 19:17:36 +09003330 /**
3331 * @return Return the current subtype of this input method.
3332 */
satok42c5a162011-05-26 16:46:14 +09003333 @Override
satokab751aa2010-09-14 19:17:36 +09003334 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003335 // TODO: Make this work even for non-current users?
3336 if (!calledFromValidUser()) {
3337 return null;
3338 }
3339 synchronized (mMethodMap) {
3340 return getCurrentInputMethodSubtypeLocked();
3341 }
3342 }
3343
3344 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003345 if (mCurMethodId == null) {
3346 return null;
3347 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003348 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003349 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3350 if (imi == null || imi.getSubtypeCount() == 0) {
3351 return null;
satok4e4569d2010-11-19 18:45:53 +09003352 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003353 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003354 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3355 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003356 if (subtypeId == NOT_A_SUBTYPE_ID) {
3357 // If there are no selected subtypes, the framework will try to find
3358 // the most applicable subtype from explicitly or implicitly enabled
3359 // subtypes.
3360 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003361 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003362 // If there is only one explicitly or implicitly enabled subtype,
3363 // just returns it.
3364 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3365 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3366 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003367 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003368 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003369 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003370 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003371 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003372 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3373 true);
satok4e4569d2010-11-19 18:45:53 +09003374 }
satok3ef8b292010-11-23 06:06:29 +09003375 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003376 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003377 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003378 }
3379 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003380 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003381 }
3382
satokf3db1af2010-11-23 13:34:33 +09003383 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3384 InputMethodSubtype subtype) {
3385 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3386 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3387 } else {
3388 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3389 subtypes.add(subtype);
3390 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3391 }
3392 }
3393
satok4e4569d2010-11-19 18:45:53 +09003394 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003395 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003396 @Override
satok4e4569d2010-11-19 18:45:53 +09003397 public List getShortcutInputMethodsAndSubtypes() {
3398 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003399 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003400 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003401 // If there are no selected shortcut subtypes, the framework will try to find
3402 // the most applicable subtype from all subtypes whose mode is
3403 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003404 Pair<InputMethodInfo, InputMethodSubtype> info =
3405 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003406 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003407 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003408 ret.add(info.first);
3409 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003410 }
satok3da92232011-01-11 22:46:30 +09003411 return ret;
satokf3db1af2010-11-23 13:34:33 +09003412 }
satokf3db1af2010-11-23 13:34:33 +09003413 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3414 ret.add(imi);
3415 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3416 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003417 }
3418 }
satokf3db1af2010-11-23 13:34:33 +09003419 return ret;
satok4e4569d2010-11-19 18:45:53 +09003420 }
3421 }
3422
satok42c5a162011-05-26 16:46:14 +09003423 @Override
satokb66d2872010-11-10 01:04:04 +09003424 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003425 // TODO: Make this work even for non-current users?
3426 if (!calledFromValidUser()) {
3427 return false;
3428 }
satokb66d2872010-11-10 01:04:04 +09003429 synchronized (mMethodMap) {
3430 if (subtype != null && mCurMethodId != null) {
3431 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003432 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003433 if (subtypeId != NOT_A_SUBTYPE_ID) {
3434 setInputMethodLocked(mCurMethodId, subtypeId);
3435 return true;
3436 }
3437 }
3438 return false;
3439 }
3440 }
3441
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003442 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003443 private static class InputMethodFileManager {
3444 private static final String SYSTEM_PATH = "system";
3445 private static final String INPUT_METHOD_PATH = "inputmethod";
3446 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3447 private static final String NODE_SUBTYPES = "subtypes";
3448 private static final String NODE_SUBTYPE = "subtype";
3449 private static final String NODE_IMI = "imi";
3450 private static final String ATTR_ID = "id";
3451 private static final String ATTR_LABEL = "label";
3452 private static final String ATTR_ICON = "icon";
3453 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3454 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3455 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3456 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3457 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3458 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003459 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003460 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003461 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003462 if (methodMap == null) {
3463 throw new NullPointerException("methodMap is null");
3464 }
3465 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003466 final File systemDir = userId == UserHandle.USER_OWNER
3467 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3468 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003469 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3470 if (!inputMethodDir.mkdirs()) {
3471 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3472 }
3473 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3474 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3475 if (!subtypeFile.exists()) {
3476 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003477 writeAdditionalInputMethodSubtypes(
3478 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003479 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003480 readAdditionalInputMethodSubtypes(
3481 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003482 }
3483 }
3484
3485 private void deleteAllInputMethodSubtypes(String imiId) {
3486 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003487 mAdditionalSubtypesMap.remove(imiId);
3488 writeAdditionalInputMethodSubtypes(
3489 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003490 }
3491 }
3492
3493 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003494 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003495 synchronized (mMethodMap) {
3496 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3497 final int N = additionalSubtypes.length;
3498 for (int i = 0; i < N; ++i) {
3499 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003500 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003501 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003502 } else {
3503 Slog.w(TAG, "Duplicated subtype definition found: "
3504 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003505 }
3506 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003507 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3508 writeAdditionalInputMethodSubtypes(
3509 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003510 }
3511 }
3512
3513 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3514 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003515 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003516 }
3517 }
3518
3519 private static void writeAdditionalInputMethodSubtypes(
3520 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3521 HashMap<String, InputMethodInfo> methodMap) {
3522 // Safety net for the case that this function is called before methodMap is set.
3523 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3524 FileOutputStream fos = null;
3525 try {
3526 fos = subtypesFile.startWrite();
3527 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003528 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003529 out.startDocument(null, true);
3530 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3531 out.startTag(null, NODE_SUBTYPES);
3532 for (String imiId : allSubtypes.keySet()) {
3533 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3534 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3535 continue;
3536 }
3537 out.startTag(null, NODE_IMI);
3538 out.attribute(null, ATTR_ID, imiId);
3539 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3540 final int N = subtypesList.size();
3541 for (int i = 0; i < N; ++i) {
3542 final InputMethodSubtype subtype = subtypesList.get(i);
3543 out.startTag(null, NODE_SUBTYPE);
3544 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3545 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3546 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3547 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3548 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3549 out.attribute(null, ATTR_IS_AUXILIARY,
3550 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3551 out.endTag(null, NODE_SUBTYPE);
3552 }
3553 out.endTag(null, NODE_IMI);
3554 }
3555 out.endTag(null, NODE_SUBTYPES);
3556 out.endDocument();
3557 subtypesFile.finishWrite(fos);
3558 } catch (java.io.IOException e) {
3559 Slog.w(TAG, "Error writing subtypes", e);
3560 if (fos != null) {
3561 subtypesFile.failWrite(fos);
3562 }
3563 }
3564 }
3565
3566 private static void readAdditionalInputMethodSubtypes(
3567 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3568 if (allSubtypes == null || subtypesFile == null) return;
3569 allSubtypes.clear();
3570 FileInputStream fis = null;
3571 try {
3572 fis = subtypesFile.openRead();
3573 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003574 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003575 int type = parser.getEventType();
3576 // Skip parsing until START_TAG
3577 while ((type = parser.next()) != XmlPullParser.START_TAG
3578 && type != XmlPullParser.END_DOCUMENT) {}
3579 String firstNodeName = parser.getName();
3580 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3581 throw new XmlPullParserException("Xml doesn't start with subtypes");
3582 }
3583 final int depth =parser.getDepth();
3584 String currentImiId = null;
3585 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3586 while (((type = parser.next()) != XmlPullParser.END_TAG
3587 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3588 if (type != XmlPullParser.START_TAG)
3589 continue;
3590 final String nodeName = parser.getName();
3591 if (NODE_IMI.equals(nodeName)) {
3592 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3593 if (TextUtils.isEmpty(currentImiId)) {
3594 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3595 continue;
3596 }
3597 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3598 allSubtypes.put(currentImiId, tempSubtypesArray);
3599 } else if (NODE_SUBTYPE.equals(nodeName)) {
3600 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3601 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3602 continue;
3603 }
3604 final int icon = Integer.valueOf(
3605 parser.getAttributeValue(null, ATTR_ICON));
3606 final int label = Integer.valueOf(
3607 parser.getAttributeValue(null, ATTR_LABEL));
3608 final String imeSubtypeLocale =
3609 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3610 final String imeSubtypeMode =
3611 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3612 final String imeSubtypeExtraValue =
3613 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003614 final boolean isAuxiliary = "1".equals(String.valueOf(
3615 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003616 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3617 .setSubtypeNameResId(label)
3618 .setSubtypeIconResId(icon)
3619 .setSubtypeLocale(imeSubtypeLocale)
3620 .setSubtypeMode(imeSubtypeMode)
3621 .setSubtypeExtraValue(imeSubtypeExtraValue)
3622 .setIsAuxiliary(isAuxiliary)
3623 .build();
satoke7c6998e2011-06-03 17:57:59 +09003624 tempSubtypesArray.add(subtype);
3625 }
3626 }
3627 } catch (XmlPullParserException e) {
3628 Slog.w(TAG, "Error reading subtypes: " + e);
3629 return;
3630 } catch (java.io.IOException e) {
3631 Slog.w(TAG, "Error reading subtypes: " + e);
3632 return;
3633 } catch (NumberFormatException e) {
3634 Slog.w(TAG, "Error reading subtypes: " + e);
3635 return;
3636 } finally {
3637 if (fis != null) {
3638 try {
3639 fis.close();
3640 } catch (java.io.IOException e1) {
3641 Slog.w(TAG, "Failed to close.");
3642 }
3643 }
3644 }
3645 }
3646 }
3647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 @Override
3649 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3650 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3651 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3654 + Binder.getCallingPid()
3655 + ", uid=" + Binder.getCallingUid());
3656 return;
3657 }
3658
3659 IInputMethod method;
3660 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 synchronized (mMethodMap) {
3665 p.println("Current Input Method Manager state:");
3666 int N = mMethodList.size();
3667 p.println(" Input Methods:");
3668 for (int i=0; i<N; i++) {
3669 InputMethodInfo info = mMethodList.get(i);
3670 p.println(" InputMethod #" + i + ":");
3671 info.dump(p, " ");
3672 }
3673 p.println(" Clients:");
3674 for (ClientState ci : mClients.values()) {
3675 p.println(" Client " + ci + ":");
3676 p.println(" client=" + ci.client);
3677 p.println(" inputContext=" + ci.inputContext);
3678 p.println(" sessionRequested=" + ci.sessionRequested);
3679 p.println(" curSession=" + ci.curSession);
3680 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003681 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003683 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3684 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3686 + " mBoundToMethod=" + mBoundToMethod);
3687 p.println(" mCurToken=" + mCurToken);
3688 p.println(" mCurIntent=" + mCurIntent);
3689 method = mCurMethod;
3690 p.println(" mCurMethod=" + mCurMethod);
3691 p.println(" mEnabledSession=" + mEnabledSession);
3692 p.println(" mShowRequested=" + mShowRequested
3693 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3694 + " mShowForced=" + mShowForced
3695 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003696 p.println(" mCurUserActionNotificationSequenceNumber="
3697 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003698 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003700
Jeff Brownb88102f2010-09-08 11:49:43 -07003701 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 pw.flush();
3704 try {
3705 client.client.asBinder().dump(fd, args);
3706 } catch (RemoteException e) {
3707 p.println("Input method client dead: " + e);
3708 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003709 } else {
3710 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003712
Jeff Brownb88102f2010-09-08 11:49:43 -07003713 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 pw.flush();
3716 try {
3717 method.asBinder().dump(fd, args);
3718 } catch (RemoteException e) {
3719 p.println("Input method service dead: " + e);
3720 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003721 } else {
3722 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 }
3724 }
3725}