blob: 45909db9255ba5558676727161cb950632dd8032 [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;
129import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900130import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700132import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900134import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136/**
137 * This class provides a system service that manages input methods.
138 */
139public class InputMethodManagerService extends IInputMethodManager.Stub
140 implements ServiceConnection, Handler.Callback {
141 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700142 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700143 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700145 private static final char INPUT_METHOD_SEPARATOR = ':';
146 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
147
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700148 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
149 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
150 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int MSG_UNBIND_INPUT = 1000;
153 static final int MSG_BIND_INPUT = 1010;
154 static final int MSG_SHOW_SOFT_INPUT = 1020;
155 static final int MSG_HIDE_SOFT_INPUT = 1030;
156 static final int MSG_ATTACH_TOKEN = 1040;
157 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 static final int MSG_START_INPUT = 2000;
160 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_UNBIND_METHOD = 3000;
163 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700164 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900165 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
satok01038492012-04-09 21:08:27 +0900167 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
168
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700169 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satokf9f01002011-05-19 21:31:50 +0900171 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
172
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900173 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900174 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900175
satok4e4569d2010-11-19 18:45:53 +0900176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800178 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900180 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final IWindowManager mIWindowManager;
183 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700184 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900185 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900186 private final HardKeyboardListener mHardKeyboardListener;
187 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900188 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900190 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 // All known input methods. mMethodMap also serves as the global
193 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900194 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
195 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900196 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
197 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900198 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700200 // Used to bring IME service up to visible adjustment while it is being shown.
201 final ServiceConnection mVisibleConnection = new ServiceConnection() {
202 @Override public void onServiceConnected(ComponentName name, IBinder service) {
203 }
204
205 @Override public void onServiceDisconnected(ComponentName name) {
206 }
207 };
208 boolean mVisibleBound = false;
209
satok7cfc0ed2011-06-20 21:29:36 +0900210 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700211 private NotificationManager mNotificationManager;
212 private KeyguardManager mKeyguardManager;
213 private StatusBarManagerService mStatusBar;
214 private Notification mImeSwitcherNotification;
215 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900216 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900217 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900218 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900219
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900220 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 final ClientState client;
222 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700223
224 IInputMethodSession session;
225 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 @Override
228 public String toString() {
229 return "SessionState{uid " + client.uid + " pid " + client.pid
230 + " method " + Integer.toHexString(
231 System.identityHashCode(method))
232 + " session " + Integer.toHexString(
233 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700234 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 + "}";
236 }
237
238 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700239 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 client = _client;
241 method = _method;
242 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final IInputMethodClient client;
249 final IInputContext inputContext;
250 final int uid;
251 final int pid;
252 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 boolean sessionRequested;
255 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 @Override
258 public String toString() {
259 return "ClientState{" + Integer.toHexString(
260 System.identityHashCode(this)) + " uid " + uid
261 + " pid " + pid + "}";
262 }
263
264 ClientState(IInputMethodClient _client, IInputContext _inputContext,
265 int _uid, int _pid) {
266 client = _client;
267 inputContext = _inputContext;
268 uid = _uid;
269 pid = _pid;
270 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
271 }
272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 final HashMap<IBinder, ClientState> mClients
275 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700278 * Set once the system is ready to run third party code.
279 */
280 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * Id of the currently selected input method.
284 */
285 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * The current binding sequence number, incremented every time there is
289 * a new bind performed.
290 */
291 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The client that is currently bound to an input method.
295 */
296 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700299 * The last window token that gained focus.
300 */
301 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * The input context last provided by the current client.
305 */
306 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The attributes last provided by the current client.
310 */
311 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * The input method ID of the input method service that we are currently
315 * connected to or in the process of connecting to.
316 */
317 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
satokab751aa2010-09-14 19:17:36 +0900320 * The current subtype of the current input method.
321 */
322 private InputMethodSubtype mCurrentSubtype;
323
satok4e4569d2010-11-19 18:45:53 +0900324 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900325 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
326 mShortcutInputMethodsAndSubtypes =
327 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900328
John Spurlocke0980502013-10-25 11:59:29 -0400329 // Was the keyguard locked when this client became current?
330 private boolean mCurClientInKeyguard;
331
satokab751aa2010-09-14 19:17:36 +0900332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * Set to true if our ServiceConnection is currently actively bound to
334 * a service (whether or not we have gotten its IBinder back yet).
335 */
336 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Set if the client has asked for the input method to be shown.
340 */
341 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * Set if we were explicitly told to show the input method.
345 */
346 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * Set if we were forced to be shown.
350 */
351 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * Set if we last told the input method to show itself.
355 */
356 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * The Intent used to connect to the current input method.
360 */
361 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * The token we have made for the currently active input method, to
365 * identify it in the future.
366 */
367 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * If non-null, this is the input method service we are currently connected
371 * to.
372 */
373 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * Time that we last initiated a bind to the input method, to determine
377 * if we should try to disconnect and reconnect to it.
378 */
379 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Have we called mCurMethod.bindInput()?
383 */
384 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Currently enabled session. Only touched by service thread, not
388 * protected by a lock.
389 */
390 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * True if the screen is on. The value is true initially.
394 */
395 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900397 int mCurUserActionNotificationSequenceNumber = 0;
398
Joe Onorato857fd9b2011-01-27 15:08:35 -0800399 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
400 int mImeWindowVis;
401
Ken Wakasa05dbb652011-08-22 15:22:43 +0900402 private AlertDialog.Builder mDialogBuilder;
403 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900404 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900405 private InputMethodInfo[] mIms;
406 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900407 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700408 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900409 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
410 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800413 String mLastEnabled = "";
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 SettingsObserver(Handler handler) {
416 super(handler);
417 ContentResolver resolver = mContext.getContentResolver();
418 resolver.registerContentObserver(Settings.Secure.getUriFor(
419 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900420 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900421 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
422 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900423 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700424 resolver.registerContentObserver(Settings.Secure.getUriFor(
425 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
Michael Wright7b5a96b2014-08-09 19:28:42 -0700428 @Override public void onChange(boolean selfChange, Uri uri) {
429 final Uri showImeUri =
430 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700432 if (showImeUri.equals(uri)) {
433 updateKeyboardFromSettingsLocked();
434 } else {
435 boolean enabledChanged = false;
436 String newEnabled = mSettings.getEnabledInputMethodsStr();
437 if (!mLastEnabled.equals(newEnabled)) {
438 mLastEnabled = newEnabled;
439 enabledChanged = true;
440 }
441 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444 }
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900447 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
448 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700450 if (mCurClient != null && mCurClient.client != null) {
451 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
452 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900455
456 @Override
457 public void onReceive(Context context, Intent intent) {
458 final String action = intent.getAction();
459 if (Intent.ACTION_SCREEN_ON.equals(action)) {
460 mScreenOn = true;
461 refreshImeWindowVisibilityLocked();
462 updateActive();
463 return;
464 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
465 mScreenOn = false;
466 setImeWindowVisibilityStatusHiddenLocked();
467 updateActive();
468 return;
469 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
470 hideInputMethodMenu();
471 // No need to updateActive
472 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800473 } else if (Intent.ACTION_USER_ADDED.equals(action)
474 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100475 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800476 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700477 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
478 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
479 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
480 final String prevValue = intent.getStringExtra(
481 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
482 final String newValue = intent.getStringExtra(
483 Intent.EXTRA_SETTING_NEW_VALUE);
484 restoreEnabledInputMethods(mContext, prevValue, newValue);
485 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900486 } else {
487 Slog.w(TAG, "Unexpected intent " + intent);
488 }
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700492 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
493 // does not attempt to validate on the fly with any installed device policy, so must only
494 // be run in the context of initial device setup.
495 //
496 // TODO: Move this method to InputMethodUtils with adding unit tests.
497 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
498 if (DEBUG_RESTORE) {
499 Slog.i(TAG, "Restoring enabled input methods:");
500 Slog.i(TAG, "prev=" + prevValue);
501 Slog.i(TAG, " new=" + newValue);
502 }
503 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
504 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
505 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
506
507 // Merge the restored ime+subtype enabled states into the live state
508 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
509 final String imeId = entry.getKey();
510 ArraySet<String> prevSubtypes = prevMap.get(imeId);
511 if (prevSubtypes == null) {
512 prevSubtypes = new ArraySet<String>(2);
513 prevMap.put(imeId, prevSubtypes);
514 }
515 prevSubtypes.addAll(entry.getValue());
516 }
517
518 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
519 if (DEBUG_RESTORE) {
520 Slog.i(TAG, "Merged IME string:");
521 Slog.i(TAG, " " + mergedImesAndSubtypesString);
522 }
523 Settings.Secure.putString(context.getContentResolver(),
524 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
525 }
526
527 // TODO: Move this method to InputMethodUtils with adding unit tests.
528 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
529 // we want to use the canonical InputMethodSettings implementation,
530 // so we convert data structures first.
531 List<Pair<String, ArrayList<String>>> imeMap =
532 new ArrayList<Pair<String, ArrayList<String>>>(4);
533 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
534 final String imeName = entry.getKey();
535 final ArraySet<String> subtypeSet = entry.getValue();
536 final ArrayList<String> subtypes = new ArrayList<String>(2);
537 if (subtypeSet != null) {
538 subtypes.addAll(subtypeSet);
539 }
540 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
541 }
542 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
543 }
544
545 // TODO: Move this method to InputMethodUtils with adding unit tests.
546 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
547 final String inputMethodsAndSubtypesString) {
548 final ArrayMap<String, ArraySet<String>> imeMap =
549 new ArrayMap<String, ArraySet<String>>();
550 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
551 return imeMap;
552 }
553
554 final SimpleStringSplitter typeSplitter =
555 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
556 final SimpleStringSplitter subtypeSplitter =
557 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
558 List<Pair<String, ArrayList<String>>> allImeSettings =
559 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
560 typeSplitter,
561 subtypeSplitter);
562 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
563 ArraySet<String> subtypes = new ArraySet<String>();
564 if (ime.second != null) {
565 subtypes.addAll(ime.second);
566 }
567 imeMap.put(ime.first, subtypes);
568 }
569 return imeMap;
570 }
571
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800572 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900573 private boolean isChangingPackagesOfCurrentUser() {
574 final int userId = getChangingUserId();
575 final boolean retval = userId == mSettings.getCurrentUserId();
576 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900577 if (!retval) {
578 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
579 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900580 }
581 return retval;
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800585 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900586 if (!isChangingPackagesOfCurrentUser()) {
587 return false;
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900590 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 final int N = mMethodList.size();
592 if (curInputMethodId != null) {
593 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800594 InputMethodInfo imi = mMethodList.get(i);
595 if (imi.getId().equals(curInputMethodId)) {
596 for (String pkg : packages) {
597 if (imi.getPackageName().equals(pkg)) {
598 if (!doit) {
599 return true;
600 }
satok723a27e2010-11-11 14:58:11 +0900601 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800602 chooseNewDefaultIMELocked();
603 return true;
604 }
605 }
606 }
607 }
608 }
609 }
610 return false;
611 }
612
613 @Override
614 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900615 if (!isChangingPackagesOfCurrentUser()) {
616 return;
617 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800618 synchronized (mMethodMap) {
619 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900620 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800621 final int N = mMethodList.size();
622 if (curInputMethodId != null) {
623 for (int i=0; i<N; i++) {
624 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900625 final String imiId = imi.getId();
626 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800627 curIm = imi;
628 }
satoke7c6998e2011-06-03 17:57:59 +0900629
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800630 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900631 if (isPackageModified(imi.getPackageName())) {
632 mFileManager.deleteAllInputMethodSubtypes(imiId);
633 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 if (change == PACKAGE_TEMPORARY_CHANGE
635 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800636 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 + imi.getComponent());
638 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640 }
641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800642
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900643 buildInputMethodListLocked(
644 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800648 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800649 int change = isPackageDisappearing(curIm.getPackageName());
650 if (change == PACKAGE_TEMPORARY_CHANGE
651 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800652 ServiceInfo si = null;
653 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900654 si = mIPackageManager.getServiceInfo(
655 curIm.getComponent(), 0, mSettings.getCurrentUserId());
656 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800657 }
658 if (si == null) {
659 // Uh oh, current input method is no longer around!
660 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800661 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900662 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800664 changed = true;
665 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800666 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900667 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800668 }
669 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800670 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 }
satokab751aa2010-09-14 19:17:36 +0900672
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800673 if (curIm == null) {
674 // We currently don't have a default input method... is
675 // one now available?
676 changed = chooseNewDefaultIMELocked();
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800680 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
683 }
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
Jeff Brownc28867a2013-03-26 15:42:39 -0700686 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900687 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700688 private final IInputMethod mMethod;
689 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
Jeff Brownc28867a2013-03-26 15:42:39 -0700691 MethodCallback(InputMethodManagerService imms, IInputMethod method,
692 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900693 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700694 mMethod = method;
695 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
satoke7c6998e2011-06-03 17:57:59 +0900698 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700699 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700700 long ident = Binder.clearCallingIdentity();
701 try {
702 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
703 } finally {
704 Binder.restoreCallingIdentity(ident);
705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
satok01038492012-04-09 21:08:27 +0900709 private class HardKeyboardListener
710 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
711 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700712 public void onHardKeyboardStatusChange(boolean available) {
713 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
714 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900715 }
716
Michael Wright7b5a96b2014-08-09 19:28:42 -0700717 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900718 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700719 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900720 }
721 synchronized(mMethodMap) {
722 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
723 && mSwitchingDialog.isShowing()) {
724 mSwitchingDialogTitleView.findViewById(
725 com.android.internal.R.id.hard_keyboard_section).setVisibility(
726 available ? View.VISIBLE : View.GONE);
727 }
728 }
729 }
730 }
731
732 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900733 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800735 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 mHandler = new Handler(this);
737 mIWindowManager = IWindowManager.Stub.asInterface(
738 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800739 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900740 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 public void executeMessage(Message msg) {
742 handleMessage(msg);
743 }
Mita Yuned218c72012-12-06 17:18:25 -0800744 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900745 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900746 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900747 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700748 mHasFeature = context.getPackageManager().hasSystemFeature(
749 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900750
satok7cfc0ed2011-06-20 21:29:36 +0900751 mImeSwitcherNotification = new Notification();
752 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
753 mImeSwitcherNotification.when = 0;
754 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
755 mImeSwitcherNotification.tickerText = null;
756 mImeSwitcherNotification.defaults = 0; // please be quiet
757 mImeSwitcherNotification.sound = null;
758 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400759
760 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
762 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400763
satok7cfc0ed2011-06-20 21:29:36 +0900764 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900765 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900766
767 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900768
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900769 final IntentFilter broadcastFilter = new IntentFilter();
770 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
771 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
772 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800773 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
774 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700775 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
satok7cfc0ed2011-06-20 21:29:36 +0900778 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 int userId = 0;
780 try {
781 ActivityManagerNative.getDefault().registerUserSwitchObserver(
782 new IUserSwitchObserver.Stub() {
783 @Override
784 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900785 synchronized(mMethodMap) {
786 switchUserLocked(newUserId);
787 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 if (reply != null) {
789 try {
790 reply.sendResult(null);
791 } catch (RemoteException e) {
792 }
793 }
794 }
795
796 @Override
797 public void onUserSwitchComplete(int newUserId) throws RemoteException {
798 }
Kenny Guy42979622015-04-13 18:03:05 +0000799
800 @Override
801 public void onForegroundProfileSwitch(int newProfileId) {
802 // Ignore.
803 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 });
805 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
806 } catch (RemoteException e) {
807 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
808 }
satok81f8b7c2012-12-04 20:42:56 +0900809 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900810
satokd87c2592010-09-29 11:52:06 +0900811 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900812 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100814 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900815 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900816 synchronized (mMethodMap) {
817 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
818 mSettings, context);
819 }
satok0a1bcf42012-05-16 19:26:31 +0900820
821 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900822 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900823 if (DEBUG) {
824 Slog.d(TAG, "Initial default ime = " + defaultImiId);
825 }
satok0a1bcf42012-05-16 19:26:31 +0900826 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
827
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900828 synchronized (mMethodMap) {
829 buildInputMethodListLocked(mMethodList, mMethodMap,
830 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
831 }
satokd87c2592010-09-29 11:52:06 +0900832 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
satok0a1bcf42012-05-16 19:26:31 +0900834 if (!mImeSelectedOnBoot) {
835 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900836 synchronized (mMethodMap) {
837 resetDefaultImeLocked(context);
838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900842 synchronized (mMethodMap) {
843 updateFromSettingsLocked(true);
844 }
satok5b927c432012-05-01 20:09:34 +0900845
846 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
847 // according to the new system locale.
848 final IntentFilter filter = new IntentFilter();
849 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
850 mContext.registerReceiver(
851 new BroadcastReceiver() {
852 @Override
853 public void onReceive(Context context, Intent intent) {
854 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900855 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900856 }
857 }
858 }, filter);
859 }
860
satok5b927c432012-05-01 20:09:34 +0900861 private void resetDefaultImeLocked(Context context) {
862 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900863 if (mCurMethodId != null
864 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900865 return;
866 }
867
868 InputMethodInfo defIm = null;
869 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900870 if (defIm == null && mSystemReady) {
871 final Locale systemLocale = context.getResources().getConfiguration().locale;
872 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
873 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
874 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900875 defIm = imi;
876 Slog.i(TAG, "Selected default: " + imi.getId());
877 }
878 }
879 }
880 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900881 defIm = InputMethodUtils.getMostApplicableDefaultIME(
882 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900883 if (defIm != null) {
884 Slog.i(TAG, "Default found, using " + defIm.getId());
885 } else {
886 Slog.i(TAG, "No default found");
887 }
satok5b927c432012-05-01 20:09:34 +0900888 }
889 if (defIm != null) {
890 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
891 }
892 }
893
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900894 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
895 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900896 if (!mSystemReady) {
897 // not system ready
898 return;
899 }
900 final Locale newLocale = mRes.getConfiguration().locale;
901 if (!updateOnlyWhenLocaleChanged
902 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
903 if (!updateOnlyWhenLocaleChanged) {
904 hideCurrentInputLocked(0, null);
905 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700906 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
908 if (DEBUG) {
909 Slog.i(TAG, "Locale has been changed to " + newLocale);
910 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900911 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 if (!updateOnlyWhenLocaleChanged) {
913 final String selectedImiId = mSettings.getSelectedInputMethod();
914 if (TextUtils.isEmpty(selectedImiId)) {
915 // This is the first time of the user switch and
916 // set the current ime to the proper one.
917 resetDefaultImeLocked(mContext);
918 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900919 } else {
920 // If the locale is changed, needs to reset the default ime
921 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900922 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800923 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900924 mLastSystemLocale = newLocale;
925 if (!updateOnlyWhenLocaleChanged) {
926 try {
927 startInputInnerLocked();
928 } catch (RuntimeException e) {
929 Slog.w(TAG, "Unexpected exception", e);
930 }
931 }
932 }
933 }
934
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900935 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900936 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
937 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900938 }
939
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900940 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900941 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100942 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900943 // InputMethodFileManager should be reset when the user is changed
944 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900945 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900946 // For secondary users, the list of enabled IMEs may not have been updated since the
947 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
948 // not be empty even if the IME has been uninstalled by the primary user.
949 // Even in such cases, IMMS works fine because it will find the most applicable
950 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900951 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900952 if (DEBUG) {
953 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
954 }
955 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900956 initialUserSwitch /* needsToResetDefaultIme */);
957 if (initialUserSwitch) {
958 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
959 mSettings.getEnabledInputMethodListLocked());
960 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900961 }
962
Kenny Guy2a764942014-04-02 13:29:20 +0100963 void updateCurrentProfileIds() {
964 List<UserInfo> profiles =
965 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
966 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
967 for (int i = 0; i < currentProfileIds.length; i++) {
968 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800969 }
Kenny Guy2a764942014-04-02 13:29:20 +0100970 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800971 }
972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 @Override
974 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
975 throws RemoteException {
976 try {
977 return super.onTransact(code, data, reply, flags);
978 } catch (RuntimeException e) {
979 // The input method manager only throws security exceptions, so let's
980 // log all others.
981 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700982 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 throw e;
985 }
986 }
987
Svetoslav Ganova0027152013-06-25 14:59:53 -0700988 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700989 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 if (DEBUG) {
991 Slog.d(TAG, "--- systemReady");
992 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700993 if (!mSystemReady) {
994 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 mKeyguardManager =
996 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700997 mNotificationManager = (NotificationManager)
998 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
999 mStatusBar = statusBar;
1000 statusBar.setIconVisibility("ime", false);
1001 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +09001002 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1003 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001004 if (mShowOngoingImeSwitcherForPhones) {
1005 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1006 mHardKeyboardListener);
1007 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001008 buildInputMethodListLocked(mMethodList, mMethodMap,
1009 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001010 if (!mImeSelectedOnBoot) {
1011 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001012 resetStateIfCurrentLocaleChangedLocked();
1013 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1014 mContext.getPackageManager(),
1015 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001016 }
1017 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001018 try {
1019 startInputInnerLocked();
1020 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001021 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001022 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001023 }
1024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
satok15452a42011-10-28 17:58:28 +09001027 private void setImeWindowVisibilityStatusHiddenLocked() {
1028 mImeWindowVis = 0;
1029 updateImeWindowStatusLocked();
1030 }
1031
satok3afd6c02011-11-18 08:38:19 +09001032 private void refreshImeWindowVisibilityLocked() {
1033 final Configuration conf = mRes.getConfiguration();
1034 final boolean haveHardKeyboard = conf.keyboard
1035 != Configuration.KEYBOARD_NOKEYS;
1036 final boolean hardKeyShown = haveHardKeyboard
1037 && conf.hardKeyboardHidden
1038 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -04001039
1040 final boolean isScreenLocked = isKeyguardLocked();
1041 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -04001042 // We assume the softkeyboard is shown when the input is active as long as the
1043 // hard keyboard is not shown.
1044 final boolean inputVisible = inputActive && !hardKeyShown;
1045 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
1046 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +09001047 updateImeWindowStatusLocked();
1048 }
1049
satok15452a42011-10-28 17:58:28 +09001050 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +09001051 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001052 }
1053
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001054 // ---------------------------------------------------------------------------------------
1055 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1056 // 1) it comes from the system process
1057 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1058 private boolean calledFromValidUser() {
1059 final int uid = Binder.getCallingUid();
1060 final int userId = UserHandle.getUserId(uid);
1061 if (DEBUG) {
1062 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1063 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1064 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001065 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1066 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001067 }
Kenny Guy2a764942014-04-02 13:29:20 +01001068 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001069 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001071
1072 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1073 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1074 // must not manage background users' states in any functions.
1075 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1076 // by a token.
1077 if (mContext.checkCallingOrSelfPermission(
1078 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1079 == PackageManager.PERMISSION_GRANTED) {
1080 if (DEBUG) {
1081 Slog.d(TAG, "--- Access granted because the calling process has "
1082 + "the INTERACT_ACROSS_USERS_FULL permission");
1083 }
1084 return true;
1085 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001086 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1087 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001088 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001089 }
1090
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001091
1092 /**
1093 * Returns true iff the caller is identified to be the current input method with the token.
1094 * @param token The window token given to the input method when it was started.
1095 * @return true if and only if non-null valid token is specified.
1096 */
1097 private boolean calledWithValidToken(IBinder token) {
1098 if (token == null || mCurToken != token) {
1099 return false;
1100 }
1101 return true;
1102 }
1103
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001104 private boolean bindCurrentInputMethodService(
1105 Intent service, ServiceConnection conn, int flags) {
1106 if (service == null || conn == null) {
1107 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1108 return false;
1109 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001110 return mContext.bindServiceAsUser(service, conn, flags,
1111 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 }
1113
satoke7c6998e2011-06-03 17:57:59 +09001114 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001116 // TODO: Make this work even for non-current users?
1117 if (!calledFromValidUser()) {
1118 return Collections.emptyList();
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 synchronized (mMethodMap) {
1121 return new ArrayList<InputMethodInfo>(mMethodList);
1122 }
1123 }
1124
satoke7c6998e2011-06-03 17:57:59 +09001125 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 // TODO: Make this work even for non-current users?
1128 if (!calledFromValidUser()) {
1129 return Collections.emptyList();
1130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001132 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 }
1135
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001136 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001137 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001138 * @return enabled subtypes of the specified imi
1139 */
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001141 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001142 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001143 // TODO: Make this work even for non-current users?
1144 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001145 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001146 }
satok67ddf9c2010-11-17 09:45:54 +09001147 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001148 final InputMethodInfo imi;
1149 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001150 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001151 } else {
1152 imi = mMethodMap.get(imiId);
1153 }
1154 if (imi == null) {
1155 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001156 }
1157 return mSettings.getEnabledInputMethodSubtypeListLocked(
1158 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001159 }
1160 }
1161
satoke7c6998e2011-06-03 17:57:59 +09001162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 public void addClient(IInputMethodClient client,
1164 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 if (!calledFromValidUser()) {
1166 return;
1167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 synchronized (mMethodMap) {
1169 mClients.put(client.asBinder(), new ClientState(client,
1170 inputContext, uid, pid));
1171 }
1172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173
satoke7c6998e2011-06-03 17:57:59 +09001174 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 if (!calledFromValidUser()) {
1177 return;
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001180 ClientState cs = mClients.remove(client.asBinder());
1181 if (cs != null) {
1182 clearClientSessionLocked(cs);
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 void executeOrSendMessage(IInterface target, Message msg) {
1188 if (target.asBinder() instanceof Binder) {
1189 mCaller.sendMessage(msg);
1190 } else {
1191 handleMessage(msg);
1192 msg.recycle();
1193 }
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001196 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001198 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 + mCurClient.client.asBinder());
1200 if (mBoundToMethod) {
1201 mBoundToMethod = false;
1202 if (mCurMethod != null) {
1203 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1204 MSG_UNBIND_INPUT, mCurMethod));
1205 }
1206 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001207
1208 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1209 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1211 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1212 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
The Android Open Source Project10592532009-03-18 17:39:46 -07001215 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 private int getImeShowFlags() {
1220 int flags = 0;
1221 if (mShowForced) {
1222 flags |= InputMethod.SHOW_FORCED
1223 | InputMethod.SHOW_EXPLICIT;
1224 } else if (mShowExplicitlyRequested) {
1225 flags |= InputMethod.SHOW_EXPLICIT;
1226 }
1227 return flags;
1228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 private int getAppShowFlags() {
1231 int flags = 0;
1232 if (mShowForced) {
1233 flags |= InputMethodManager.SHOW_FORCED;
1234 } else if (!mShowExplicitlyRequested) {
1235 flags |= InputMethodManager.SHOW_IMPLICIT;
1236 }
1237 return flags;
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
Dianne Hackborn7663d802012-02-24 13:08:49 -08001240 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 if (!mBoundToMethod) {
1242 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1243 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1244 mBoundToMethod = true;
1245 }
1246 final SessionState session = mCurClient.curSession;
1247 if (initial) {
1248 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1249 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1250 } else {
1251 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1252 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1253 }
1254 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001255 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001256 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001258 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001259 (session.channel != null ? session.channel.dup() : null),
1260 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001264 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 // If no method is currently selected, do nothing.
1266 if (mCurMethodId == null) {
1267 return mNoBinding;
1268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 ClientState cs = mClients.get(client.asBinder());
1271 if (cs == null) {
1272 throw new IllegalArgumentException("unknown client "
1273 + client.asBinder());
1274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 try {
1277 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1278 // Check with the window manager to make sure this client actually
1279 // has a window with focus. If not, reject. This is thread safe
1280 // because if the focus changes some time before or after, the
1281 // next client receiving focus that has any interest in input will
1282 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001283 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1285 return null;
1286 }
1287 } catch (RemoteException e) {
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
Dianne Hackborn7663d802012-02-24 13:08:49 -08001290 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1291 }
1292
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001293 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1295 // If no method is currently selected, do nothing.
1296 if (mCurMethodId == null) {
1297 return mNoBinding;
1298 }
1299
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001300 if (attribute != null) {
1301 // We accept an empty package name as a valid data.
1302 if (!TextUtils.isEmpty(attribute.packageName) &&
1303 !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1304 attribute.packageName)) {
1305 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1306 + " uid=" + cs.uid + " package=" + attribute.packageName);
1307 return mNoBinding;
1308 }
1309 }
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001312 // Was the keyguard locked when switching over to the new client?
1313 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 // If the client is changing, we need to switch over to the new
1315 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001317 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001318 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319
1320 // If the screen is on, inform the new client it is active
1321 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001322 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1323 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 // Bump up the sequence for this client and attach it.
1328 mCurSeq++;
1329 if (mCurSeq <= 0) mCurSeq = 1;
1330 mCurClient = cs;
1331 mCurInputContext = inputContext;
1332 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 // Check if the input method is changing.
1335 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1336 if (cs.curSession != null) {
1337 // Fast case: if we are already connected to the input method,
1338 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001339 return attachNewInputLocked(
1340 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
1342 if (mHaveConnection) {
1343 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 // Return to client, and we will get back with it when
1345 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001346 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001347 return new InputBindResult(null, null, mCurId, mCurSeq,
1348 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 } else if (SystemClock.uptimeMillis()
1350 < (mLastBindTime+TIME_TO_RECONNECT)) {
1351 // In this case we have connected to the service, but
1352 // don't yet have its interface. If it hasn't been too
1353 // long since we did the connection, we'll return to
1354 // the client and wait to get the service interface so
1355 // we can report back. If it has been too long, we want
1356 // to fall through so we can try a disconnect/reconnect
1357 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001358 return new InputBindResult(null, null, mCurId, mCurSeq,
1359 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001361 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1362 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001367 return startInputInnerLocked();
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001370 InputBindResult startInputInnerLocked() {
1371 if (mCurMethodId == null) {
1372 return mNoBinding;
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001375 if (!mSystemReady) {
1376 // If the system is not yet ready, we shouldn't be running third
1377 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001378 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1379 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1383 if (info == null) {
1384 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001387 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1390 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001391 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1392 com.android.internal.R.string.input_method_binding_label);
1393 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1394 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001395 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001396 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1397 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 mLastBindTime = SystemClock.uptimeMillis();
1399 mHaveConnection = true;
1400 mCurId = info.getId();
1401 mCurToken = new Binder();
1402 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001403 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 mIWindowManager.addWindowToken(mCurToken,
1405 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1406 } catch (RemoteException e) {
1407 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001408 return new InputBindResult(null, null, mCurId, mCurSeq,
1409 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } else {
1411 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001412 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 + mCurIntent);
1414 }
1415 return null;
1416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
satoke7c6998e2011-06-03 17:57:59 +09001418 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001420 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001421 if (!calledFromValidUser()) {
1422 return null;
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 synchronized (mMethodMap) {
1425 final long ident = Binder.clearCallingIdentity();
1426 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001427 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 } finally {
1429 Binder.restoreCallingIdentity(ident);
1430 }
1431 }
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
satoke7c6998e2011-06-03 17:57:59 +09001434 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 public void finishInput(IInputMethodClient client) {
1436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
satoke7c6998e2011-06-03 17:57:59 +09001438 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 public void onServiceConnected(ComponentName name, IBinder service) {
1440 synchronized (mMethodMap) {
1441 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1442 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001443 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001444 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001445 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001446 return;
1447 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001448 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001449 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1450 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001452 clearClientSessionLocked(mCurClient);
1453 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 }
1456 }
1457 }
1458
Jeff Brownc28867a2013-03-26 15:42:39 -07001459 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1460 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 synchronized (mMethodMap) {
1462 if (mCurMethod != null && method != null
1463 && mCurMethod.asBinder() == method.asBinder()) {
1464 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001465 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001467 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001468 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (res.method != null) {
1470 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1471 MSG_BIND_METHOD, mCurClient.client, res));
1472 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001473 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475 }
1476 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001477
1478 // Session abandoned. Close its associated input channel.
1479 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001482 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001483 if (mVisibleBound) {
1484 mContext.unbindService(mVisibleConnection);
1485 mVisibleBound = false;
1486 }
1487
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001488 if (mHaveConnection) {
1489 mContext.unbindService(this);
1490 mHaveConnection = false;
1491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001493 if (mCurToken != null) {
1494 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001496 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001497 // The current IME is shown. Hence an IME switch (transition) is happening.
1498 mWindowManagerService.saveLastInputMethodWindowForTransition();
1499 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001500 mIWindowManager.removeWindowToken(mCurToken);
1501 } catch (RemoteException e) {
1502 }
1503 mCurToken = null;
1504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
The Android Open Source Project10592532009-03-18 17:39:46 -07001506 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001507 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001509 if (reportToClient && mCurClient != null) {
1510 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1511 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1512 }
1513 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001514
1515 void requestClientSessionLocked(ClientState cs) {
1516 if (!cs.sessionRequested) {
1517 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1518 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1519 cs.sessionRequested = true;
1520 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1521 MSG_CREATE_SESSION, mCurMethod, channels[1],
1522 new MethodCallback(this, mCurMethod, channels[0])));
1523 }
1524 }
1525
1526 void clearClientSessionLocked(ClientState cs) {
1527 finishSessionLocked(cs.curSession);
1528 cs.curSession = null;
1529 cs.sessionRequested = false;
1530 }
1531
1532 private void finishSessionLocked(SessionState sessionState) {
1533 if (sessionState != null) {
1534 if (sessionState.session != null) {
1535 try {
1536 sessionState.session.finishSession();
1537 } catch (RemoteException e) {
1538 Slog.w(TAG, "Session failed to close due to remote exception", e);
1539 setImeWindowVisibilityStatusHiddenLocked();
1540 }
1541 sessionState.session = null;
1542 }
1543 if (sessionState.channel != null) {
1544 sessionState.channel.dispose();
1545 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001546 }
1547 }
1548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001549
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001550 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (mCurMethod != null) {
1552 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001555
Jeff Brownc28867a2013-03-26 15:42:39 -07001556 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001557 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 mCurMethod = null;
1559 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001560 if (mStatusBar != null) {
1561 mStatusBar.setIconVisibility("ime", false);
1562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001564
satoke7c6998e2011-06-03 17:57:59 +09001565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 public void onServiceDisconnected(ComponentName name) {
1567 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001568 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 + " mCurIntent=" + mCurIntent);
1570 if (mCurMethod != null && mCurIntent != null
1571 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001572 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 // We consider this to be a new bind attempt, since the system
1574 // should now try to restart the service for us.
1575 mLastBindTime = SystemClock.uptimeMillis();
1576 mShowRequested = mInputShown;
1577 mInputShown = false;
1578 if (mCurClient != null) {
1579 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1580 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1581 }
1582 }
1583 }
1584 }
1585
satokf9f01002011-05-19 21:31:50 +09001586 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1588 long ident = Binder.clearCallingIdentity();
1589 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001591 if (!calledWithValidToken(token)) {
1592 final int uid = Binder.getCallingUid();
1593 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1594 + " token:" + token);
1595 return;
1596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001599 if (mStatusBar != null) {
1600 mStatusBar.setIconVisibility("ime", false);
1601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001604 CharSequence contentDescription = null;
1605 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001606 // Use PackageManager to load label
1607 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001608 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001609 mIPackageManager.getApplicationInfo(packageName, 0,
1610 mSettings.getCurrentUserId()));
1611 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001612 /* ignore */
1613 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001614 if (mStatusBar != null) {
1615 mStatusBar.setIcon("ime", packageName, iconId, 0,
1616 contentDescription != null
1617 ? contentDescription.toString() : null);
1618 mStatusBar.setIconVisibility("ime", true);
1619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
1621 }
1622 } finally {
1623 Binder.restoreCallingIdentity(ident);
1624 }
1625 }
1626
satok7cfc0ed2011-06-20 21:29:36 +09001627 private boolean needsToShowImeSwitchOngoingNotification() {
1628 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001629 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001630 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001631 synchronized (mMethodMap) {
1632 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1633 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001634 if (N > 2) return true;
1635 if (N < 1) return false;
1636 int nonAuxCount = 0;
1637 int auxCount = 0;
1638 InputMethodSubtype nonAuxSubtype = null;
1639 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001640 for(int i = 0; i < N; ++i) {
1641 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001642 final List<InputMethodSubtype> subtypes =
1643 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001644 final int subtypeCount = subtypes.size();
1645 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001646 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001647 } else {
1648 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001649 final InputMethodSubtype subtype = subtypes.get(j);
1650 if (!subtype.isAuxiliary()) {
1651 ++nonAuxCount;
1652 nonAuxSubtype = subtype;
1653 } else {
1654 ++auxCount;
1655 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001656 }
1657 }
1658 }
satok7cfc0ed2011-06-20 21:29:36 +09001659 }
satokb6359412011-06-28 17:47:41 +09001660 if (nonAuxCount > 1 || auxCount > 1) {
1661 return true;
1662 } else if (nonAuxCount == 1 && auxCount == 1) {
1663 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001664 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1665 || auxSubtype.overridesImplicitlyEnabledSubtype()
1666 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001667 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1668 return false;
1669 }
1670 return true;
1671 }
1672 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001673 }
satok7cfc0ed2011-06-20 21:29:36 +09001674 }
1675
John Spurlocke0980502013-10-25 11:59:29 -04001676 private boolean isKeyguardLocked() {
1677 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1678 }
1679
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001680 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001681 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001682 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001683 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001684 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001685 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001686 if (!calledWithValidToken(token)) {
1687 final int uid = Binder.getCallingUid();
1688 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1689 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001690 return;
1691 }
satok06487a52010-10-29 11:37:18 +09001692 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001693 // apply policy for binder calls
1694 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1695 vis = 0;
1696 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001697 mImeWindowVis = vis;
1698 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001699 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1700 && (mWindowManagerService.isHardKeyboardAvailable()
1701 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001702 final boolean needsToShowImeSwitcher = iconVisibility
1703 && needsToShowImeSwitchOngoingNotification();
1704 if (mStatusBar != null) {
1705 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1706 needsToShowImeSwitcher);
1707 }
satok5bc8e732011-07-22 21:07:23 +09001708 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001709 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001710 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001711 final CharSequence title = mRes.getText(
1712 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001713 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1714 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001715
Alan Viverette4a357cd2015-03-18 18:37:18 -07001716 mImeSwitcherNotification.color = mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +02001717 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001718 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001719 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001720 if ((mNotificationManager != null)
1721 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001722 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001723 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001724 }
1725 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001726 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001727 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001728 mNotificationShown = true;
1729 }
satok7cfc0ed2011-06-20 21:29:36 +09001730 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001731 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001732 if (DEBUG) {
1733 Slog.d(TAG, "--- hide notification");
1734 }
1735 mNotificationManager.cancelAsUser(null,
1736 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001737 mNotificationShown = false;
1738 }
1739 }
satok06487a52010-10-29 11:37:18 +09001740 }
1741 } finally {
1742 Binder.restoreCallingIdentity(ident);
1743 }
1744 }
1745
satoke7c6998e2011-06-03 17:57:59 +09001746 @Override
satokf9f01002011-05-19 21:31:50 +09001747 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001748 if (!calledFromValidUser()) {
1749 return;
1750 }
satokf9f01002011-05-19 21:31:50 +09001751 synchronized (mMethodMap) {
1752 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1753 for (int i = 0; i < spans.length; ++i) {
1754 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001755 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001756 mSecureSuggestionSpans.put(ss, currentImi);
1757 }
1758 }
1759 }
1760 }
1761
satoke7c6998e2011-06-03 17:57:59 +09001762 @Override
satokf9f01002011-05-19 21:31:50 +09001763 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001764 if (!calledFromValidUser()) {
1765 return false;
1766 }
satokf9f01002011-05-19 21:31:50 +09001767 synchronized (mMethodMap) {
1768 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1769 // TODO: Do not send the intent if the process of the targetImi is already dead.
1770 if (targetImi != null) {
1771 final String[] suggestions = span.getSuggestions();
1772 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001773 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001774 final Intent intent = new Intent();
1775 // Ensures that only a class in the original IME package will receive the
1776 // notification.
satok42c5a162011-05-26 16:46:14 +09001777 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001778 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1779 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1780 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1781 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001782 final long ident = Binder.clearCallingIdentity();
1783 try {
1784 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1785 } finally {
1786 Binder.restoreCallingIdentity(ident);
1787 }
satokf9f01002011-05-19 21:31:50 +09001788 return true;
1789 }
1790 }
1791 return false;
1792 }
1793
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001794 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001795 updateInputMethodsFromSettingsLocked(enabledMayChange);
1796 updateKeyboardFromSettingsLocked();
1797 }
1798
1799 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001800 if (enabledMayChange) {
1801 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1802 for (int i=0; i<enabled.size(); i++) {
1803 // We allow the user to select "disabled until used" apps, so if they
1804 // are enabling one of those here we now need to make it enabled.
1805 InputMethodInfo imm = enabled.get(i);
1806 try {
1807 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1808 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1809 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001810 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001811 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001812 if (DEBUG) {
1813 Slog.d(TAG, "Update state(" + imm.getId()
1814 + "): DISABLED_UNTIL_USED -> DEFAULT");
1815 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001816 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1817 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001818 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1819 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001820 }
1821 } catch (RemoteException e) {
1822 }
1823 }
1824 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001825 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1826 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1827 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1828 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001829 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001830 // There is no input method selected, try to choose new applicable input method.
1831 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001832 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001833 }
1834 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001836 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001838 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001839 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001840 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
satokf3db1af2010-11-23 13:34:33 +09001842 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001843 } else {
1844 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001845 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001846 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001848 // Here is not the perfect place to reset the switching controller. Ideally
1849 // mSwitchingController and mSettings should be able to share the same state.
1850 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1851 // the same enabled IMEs list.
1852 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001853
1854 }
1855
1856 public void updateKeyboardFromSettingsLocked() {
1857 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1858 if (mSwitchingDialog != null
1859 && mSwitchingDialogTitleView != null
1860 && mSwitchingDialog.isShowing()) {
1861 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1862 com.android.internal.R.id.hard_keyboard_switch);
1863 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001866
satokab751aa2010-09-14 19:17:36 +09001867 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 InputMethodInfo info = mMethodMap.get(id);
1869 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001870 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001873 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001874 // We have already made sure that the package name belongs to the application's UID.
1875 // No further UID check is required.
1876 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1877 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001878 }
1879 }
1880
satokd81e9502012-05-21 12:58:45 +09001881 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001883 final int subtypeCount = info.getSubtypeCount();
1884 if (subtypeCount <= 0) {
1885 return;
satokcd7cd292010-11-20 15:46:23 +09001886 }
satokd81e9502012-05-21 12:58:45 +09001887 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1888 final InputMethodSubtype newSubtype;
1889 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1890 newSubtype = info.getSubtypeAt(subtypeId);
1891 } else {
1892 // If subtype is null, try to find the most applicable one from
1893 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001894 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001895 }
1896 if (newSubtype == null || oldSubtype == null) {
1897 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1898 + ", new subtype = " + newSubtype);
1899 return;
1900 }
1901 if (newSubtype != oldSubtype) {
1902 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1903 if (mCurMethod != null) {
1904 try {
1905 refreshImeWindowVisibilityLocked();
1906 mCurMethod.changeInputMethodSubtype(newSubtype);
1907 } catch (RemoteException e) {
1908 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001909 }
1910 }
1911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 return;
1913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001914
satokd81e9502012-05-21 12:58:45 +09001915 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 final long ident = Binder.clearCallingIdentity();
1917 try {
satokab751aa2010-09-14 19:17:36 +09001918 // Set a subtype to this input method.
1919 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001920 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1921 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1922 // because mCurMethodId is stored as a history in
1923 // setSelectedInputMethodAndSubtypeLocked().
1924 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925
1926 if (ActivityManagerNative.isSystemReady()) {
1927 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001928 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001930 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001932 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 } finally {
1934 Binder.restoreCallingIdentity(ident);
1935 }
1936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001937
satok42c5a162011-05-26 16:46:14 +09001938 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001939 public boolean showSoftInput(IInputMethodClient client, int flags,
1940 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001941 if (!calledFromValidUser()) {
1942 return false;
1943 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001944 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 long ident = Binder.clearCallingIdentity();
1946 try {
1947 synchronized (mMethodMap) {
1948 if (mCurClient == null || client == null
1949 || mCurClient.client.asBinder() != client.asBinder()) {
1950 try {
1951 // We need to check if this is the current client with
1952 // focus in the window manager, to allow this call to
1953 // be made before input is started in it.
1954 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001955 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001956 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001959 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
1961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001963 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001964 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 } finally {
1967 Binder.restoreCallingIdentity(ident);
1968 }
1969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001970
The Android Open Source Project4df24232009-03-05 14:34:35 -08001971 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 mShowRequested = true;
1973 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1974 mShowExplicitlyRequested = true;
1975 }
1976 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1977 mShowExplicitlyRequested = true;
1978 mShowForced = true;
1979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
Dianne Hackborncc278702009-09-02 23:07:23 -07001981 if (!mSystemReady) {
1982 return false;
1983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
The Android Open Source Project4df24232009-03-05 14:34:35 -08001985 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001987 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1989 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1990 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001992 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001993 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001994 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07001995 | Context.BIND_TREAT_LIKE_ACTIVITY
1996 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001997 mVisibleBound = true;
1998 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001999 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002001 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 // The client has asked to have the input method shown, but
2003 // we have been sitting here too long with a connection to the
2004 // service and no interface received, so let's disconnect/connect
2005 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002008 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002010 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002011 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002012 } else {
2013 if (DEBUG) {
2014 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2015 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002018
The Android Open Source Project4df24232009-03-05 14:34:35 -08002019 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002021
satok42c5a162011-05-26 16:46:14 +09002022 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002023 public boolean hideSoftInput(IInputMethodClient client, int flags,
2024 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002025 if (!calledFromValidUser()) {
2026 return false;
2027 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002028 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 long ident = Binder.clearCallingIdentity();
2030 try {
2031 synchronized (mMethodMap) {
2032 if (mCurClient == null || client == null
2033 || mCurClient.client.asBinder() != client.asBinder()) {
2034 try {
2035 // We need to check if this is the current client with
2036 // focus in the window manager, to allow this call to
2037 // be made before input is started in it.
2038 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002039 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2040 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09002041 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002042 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09002045 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049
Joe Onorato8a9b2202010-02-26 18:56:32 -08002050 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053 } finally {
2054 Binder.restoreCallingIdentity(ident);
2055 }
2056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
The Android Open Source Project4df24232009-03-05 14:34:35 -08002058 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2060 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002061 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 -08002062 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002065 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 -08002066 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2071 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2072 res = true;
2073 } else {
2074 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002076 if (mHaveConnection && mVisibleBound) {
2077 mContext.unbindService(mVisibleConnection);
2078 mVisibleBound = false;
2079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 mInputShown = false;
2081 mShowRequested = false;
2082 mShowExplicitlyRequested = false;
2083 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002086
satok42c5a162011-05-26 16:46:14 +09002087 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002088 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2089 int controlFlags, int softInputMode, int windowFlags,
2090 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002091 // Needs to check the validity before clearing calling identity
2092 final boolean calledFromValidUser = calledFromValidUser();
2093
Dianne Hackborn7663d802012-02-24 13:08:49 -08002094 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 long ident = Binder.clearCallingIdentity();
2096 try {
2097 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002098 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002099 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002101 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
Dianne Hackborn7663d802012-02-24 13:08:49 -08002103 ClientState cs = mClients.get(client.asBinder());
2104 if (cs == null) {
2105 throw new IllegalArgumentException("unknown client "
2106 + client.asBinder());
2107 }
2108
2109 try {
2110 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2111 // Check with the window manager to make sure this client actually
2112 // has a window with focus. If not, reject. This is thread safe
2113 // because if the focus changes some time before or after, the
2114 // next client receiving focus that has any interest in input will
2115 // be calling through here after that change happens.
2116 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2117 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2118 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002120 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002122
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002123 if (!calledFromValidUser) {
2124 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2125 Slog.w(TAG, "If you want to interect with IME, you need "
2126 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2127 hideCurrentInputLocked(0, null);
2128 return null;
2129 }
2130
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002131 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002132 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002133 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002134 if (attribute != null) {
2135 return startInputUncheckedLocked(cs, inputContext, attribute,
2136 controlFlags);
2137 }
2138 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002139 }
2140 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002141
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002142 // Should we auto-show the IME even if the caller has not
2143 // specified what should be done with it?
2144 // We only do this automatically if the window can resize
2145 // to accommodate the IME (so what the user sees will give
2146 // them good context without input information being obscured
2147 // by the IME) or if running on a large screen where there
2148 // is more room for the target window + IME.
2149 final boolean doAutoShow =
2150 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2151 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2152 || mRes.getConfiguration().isLayoutSizeAtLeast(
2153 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002154 final boolean isTextEditor =
2155 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2156
2157 // We want to start input before showing the IME, but after closing
2158 // it. We want to do this after closing it to help the IME disappear
2159 // more quickly (not get stuck behind it initializing itself for the
2160 // new focused input, even if its window wants to hide the IME).
2161 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2164 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002165 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2167 // There is no focus view, and this window will
2168 // be behind any soft input window, so hide the
2169 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002170 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002171 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002173 } else if (isTextEditor && doAutoShow && (softInputMode &
2174 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 // There is a focus view, and we are navigating forward
2176 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002177 // We only do this automatically if the window can resize
2178 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002179 // them good context without input information being obscured
2180 // by the IME) or if running on a large screen where there
2181 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002182 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002183 if (attribute != null) {
2184 res = startInputUncheckedLocked(cs, inputContext, attribute,
2185 controlFlags);
2186 didStart = true;
2187 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002188 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
2190 break;
2191 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2192 // Do nothing.
2193 break;
2194 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2195 if ((softInputMode &
2196 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002197 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 break;
2201 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002202 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002203 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 break;
2205 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2206 if ((softInputMode &
2207 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002208 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002209 if (attribute != null) {
2210 res = startInputUncheckedLocked(cs, inputContext, attribute,
2211 controlFlags);
2212 didStart = true;
2213 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002214 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
2216 break;
2217 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002218 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002219 if (attribute != null) {
2220 res = startInputUncheckedLocked(cs, inputContext, attribute,
2221 controlFlags);
2222 didStart = true;
2223 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002224 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 break;
2226 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002227
2228 if (!didStart && attribute != null) {
2229 res = startInputUncheckedLocked(cs, inputContext, attribute,
2230 controlFlags);
2231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
2233 } finally {
2234 Binder.restoreCallingIdentity(ident);
2235 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002236
2237 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002239
satok42c5a162011-05-26 16:46:14 +09002240 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002242 if (!calledFromValidUser()) {
2243 return;
2244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 synchronized (mMethodMap) {
2246 if (mCurClient == null || client == null
2247 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002248 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002249 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251
satok440aab52010-11-25 09:43:11 +09002252 // Always call subtype picker, because subtype picker is a superset of input method
2253 // picker.
satokab751aa2010-09-14 19:17:36 +09002254 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2255 }
2256 }
2257
satok42c5a162011-05-26 16:46:14 +09002258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002260 if (!calledFromValidUser()) {
2261 return;
2262 }
satok28203512010-11-24 11:06:49 +09002263 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2264 }
2265
satok42c5a162011-05-26 16:46:14 +09002266 @Override
satok28203512010-11-24 11:06:49 +09002267 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002268 if (!calledFromValidUser()) {
2269 return;
2270 }
satok28203512010-11-24 11:06:49 +09002271 synchronized (mMethodMap) {
2272 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002273 setInputMethodWithSubtypeIdLocked(token, id,
2274 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2275 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002276 } else {
2277 setInputMethod(token, id);
2278 }
2279 }
satokab751aa2010-09-14 19:17:36 +09002280 }
2281
satok42c5a162011-05-26 16:46:14 +09002282 @Override
satokb416a712010-11-25 20:42:14 +09002283 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002284 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002285 if (!calledFromValidUser()) {
2286 return;
2287 }
satokb416a712010-11-25 20:42:14 +09002288 synchronized (mMethodMap) {
2289 if (mCurClient == null || client == null
2290 || mCurClient.client.asBinder() != client.asBinder()) {
2291 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2292 }
satok7fee71f2010-12-17 18:54:26 +09002293 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2294 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002295 }
2296 }
2297
satok4fc87d62011-05-20 16:13:43 +09002298 @Override
satok735cf382010-11-11 20:40:09 +09002299 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002300 if (!calledFromValidUser()) {
2301 return false;
2302 }
satok735cf382010-11-11 20:40:09 +09002303 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002304 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002305 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002306 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002307 lastImi = mMethodMap.get(lastIme.first);
2308 } else {
2309 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002310 }
satok4fc87d62011-05-20 16:13:43 +09002311 String targetLastImiId = null;
2312 int subtypeId = NOT_A_SUBTYPE_ID;
2313 if (lastIme != null && lastImi != null) {
2314 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2315 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2316 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2317 : mCurrentSubtype.hashCode();
2318 // If the last IME is the same as the current IME and the last subtype is not
2319 // defined, there is no need to switch to the last IME.
2320 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2321 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002322 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002323 }
2324 }
2325
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002326 if (TextUtils.isEmpty(targetLastImiId)
2327 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002328 // This is a safety net. If the currentSubtype can't be added to the history
2329 // and the framework couldn't find the last ime, we will make the last ime be
2330 // the most applicable enabled keyboard subtype of the system imes.
2331 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2332 if (enabled != null) {
2333 final int N = enabled.size();
2334 final String locale = mCurrentSubtype == null
2335 ? mRes.getConfiguration().locale.toString()
2336 : mCurrentSubtype.getLocale();
2337 for (int i = 0; i < N; ++i) {
2338 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002339 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002340 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002341 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2342 InputMethodUtils.getSubtypes(imi),
2343 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002344 if (keyboardSubtype != null) {
2345 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002346 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002347 imi, keyboardSubtype.hashCode());
2348 if(keyboardSubtype.getLocale().equals(locale)) {
2349 break;
2350 }
2351 }
2352 }
2353 }
2354 }
2355 }
2356
2357 if (!TextUtils.isEmpty(targetLastImiId)) {
2358 if (DEBUG) {
2359 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2360 + ", from: " + mCurMethodId + ", " + subtypeId);
2361 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002362 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002363 return true;
2364 } else {
2365 return false;
2366 }
satok735cf382010-11-11 20:40:09 +09002367 }
2368 }
2369
satoke7c6998e2011-06-03 17:57:59 +09002370 @Override
satok688bd472012-02-09 20:09:17 +09002371 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002372 if (!calledFromValidUser()) {
2373 return false;
2374 }
satok688bd472012-02-09 20:09:17 +09002375 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002376 if (!calledWithValidToken(token)) {
2377 final int uid = Binder.getCallingUid();
2378 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2379 + " token:" + token);
2380 return false;
2381 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002382 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002383 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2384 if (nextSubtype == null) {
2385 return false;
2386 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002387 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2388 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002389 return true;
2390 }
2391 }
2392
2393 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002394 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2395 if (!calledFromValidUser()) {
2396 return false;
2397 }
2398 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002399 if (!calledWithValidToken(token)) {
2400 final int uid = Binder.getCallingUid();
2401 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2402 + "token. uid:" + uid + " token:" + token);
2403 return false;
2404 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002405 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002406 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2407 if (nextSubtype == null) {
2408 return false;
2409 }
2410 return true;
2411 }
2412 }
2413
2414 @Override
satok68f1b782011-04-11 14:26:04 +09002415 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 if (!calledFromValidUser()) {
2417 return null;
2418 }
satok68f1b782011-04-11 14:26:04 +09002419 synchronized (mMethodMap) {
2420 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2421 // TODO: Handle the case of the last IME with no subtypes
2422 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2423 || TextUtils.isEmpty(lastIme.second)) return null;
2424 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2425 if (lastImi == null) return null;
2426 try {
2427 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002428 final int lastSubtypeId =
2429 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002430 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2431 return null;
2432 }
2433 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002434 } catch (NumberFormatException e) {
2435 return null;
2436 }
2437 }
2438 }
2439
satoke7c6998e2011-06-03 17:57:59 +09002440 @Override
satokee5e77c2011-09-02 18:50:15 +09002441 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002442 if (!calledFromValidUser()) {
2443 return;
2444 }
satok91e88122011-07-18 11:11:42 +09002445 // By this IPC call, only a process which shares the same uid with the IME can add
2446 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002447 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002448 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002449 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002450 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002451 final String[] packageInfos;
2452 try {
2453 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2454 } catch (RemoteException e) {
2455 Slog.e(TAG, "Failed to get package infos");
2456 return;
2457 }
satok91e88122011-07-18 11:11:42 +09002458 if (packageInfos != null) {
2459 final int packageNum = packageInfos.length;
2460 for (int i = 0; i < packageNum; ++i) {
2461 if (packageInfos[i].equals(imi.getPackageName())) {
2462 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002463 final long ident = Binder.clearCallingIdentity();
2464 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002465 buildInputMethodListLocked(mMethodList, mMethodMap,
2466 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002467 } finally {
2468 Binder.restoreCallingIdentity(ident);
2469 }
satokee5e77c2011-09-02 18:50:15 +09002470 return;
satok91e88122011-07-18 11:11:42 +09002471 }
2472 }
2473 }
satoke7c6998e2011-06-03 17:57:59 +09002474 }
satokee5e77c2011-09-02 18:50:15 +09002475 return;
satoke7c6998e2011-06-03 17:57:59 +09002476 }
2477
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002478 @Override
2479 public int getInputMethodWindowVisibleHeight() {
2480 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2481 }
2482
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002483 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002484 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002485 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002486 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002487 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002488 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002489 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2490 if (DEBUG) {
2491 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2492 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2493 + " actual: " + sequenceNumber);
2494 }
2495 return;
2496 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002497 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2498 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002499 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002500 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002501 }
2502 }
2503
satok28203512010-11-24 11:06:49 +09002504 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002506 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2507 }
2508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002510 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2511 if (token == null) {
2512 if (mContext.checkCallingOrSelfPermission(
2513 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2514 != PackageManager.PERMISSION_GRANTED) {
2515 throw new SecurityException(
2516 "Using null token requires permission "
2517 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002519 } else if (mCurToken != token) {
2520 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2521 + " token: " + token);
2522 return;
2523 }
2524
2525 final long ident = Binder.clearCallingIdentity();
2526 try {
2527 setInputMethodLocked(id, subtypeId);
2528 } finally {
2529 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 }
2531 }
2532
satok42c5a162011-05-26 16:46:14 +09002533 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002535 if (!calledFromValidUser()) {
2536 return;
2537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002539 if (!calledWithValidToken(token)) {
2540 final int uid = Binder.getCallingUid();
2541 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2542 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 return;
2544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 long ident = Binder.clearCallingIdentity();
2546 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002547 hideCurrentInputLocked(flags, null);
2548 } finally {
2549 Binder.restoreCallingIdentity(ident);
2550 }
2551 }
2552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002553
satok42c5a162011-05-26 16:46:14 +09002554 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002555 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002556 if (!calledFromValidUser()) {
2557 return;
2558 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002559 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002560 if (!calledWithValidToken(token)) {
2561 final int uid = Binder.getCallingUid();
2562 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2563 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002564 return;
2565 }
2566 long ident = Binder.clearCallingIdentity();
2567 try {
2568 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 } finally {
2570 Binder.restoreCallingIdentity(ident);
2571 }
2572 }
2573 }
2574
2575 void setEnabledSessionInMainThread(SessionState session) {
2576 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002577 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002579 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002580 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 } catch (RemoteException e) {
2582 }
2583 }
2584 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002585 if (mEnabledSession != null && mEnabledSession.session != null) {
2586 try {
2587 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2588 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2589 } catch (RemoteException e) {
2590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 }
2592 }
2593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002594
satok42c5a162011-05-26 16:46:14 +09002595 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002597 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002599 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonakad4474cb2015-05-04 16:53:24 -07002600 showInputMethodMenu();
satokab751aa2010-09-14 19:17:36 +09002601 return true;
2602
satok47a44912010-10-06 16:03:58 +09002603 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002604 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002605 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002606 args.recycle();
satok217f5482010-12-15 05:19:19 +09002607 return true;
2608
2609 case MSG_SHOW_IM_CONFIG:
2610 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002611 return true;
2612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 case MSG_UNBIND_INPUT:
2616 try {
2617 ((IInputMethod)msg.obj).unbindInput();
2618 } catch (RemoteException e) {
2619 // There is nothing interesting about the method dying.
2620 }
2621 return true;
2622 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002623 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 try {
2625 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2626 } catch (RemoteException e) {
2627 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002628 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 return true;
2630 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002631 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002633 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002634 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002635 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 } catch (RemoteException e) {
2637 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002638 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 return true;
2640 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002641 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002643 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002644 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002645 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 } catch (RemoteException e) {
2647 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002648 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 return true;
2650 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002651 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002653 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2655 } catch (RemoteException e) {
2656 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002657 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002659 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002660 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002661 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002662 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002664 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002666 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002667 // Dispose the channel if the input method is not local to this process
2668 // because the remote proxy will get its own copy when unparceled.
2669 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002670 channel.dispose();
2671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002679 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 try {
2681 SessionState session = (SessionState)args.arg1;
2682 setEnabledSessionInMainThread(session);
2683 session.method.startInput((IInputContext)args.arg2,
2684 (EditorInfo)args.arg3);
2685 } catch (RemoteException e) {
2686 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002687 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 return true;
2689 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002690 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 try {
2692 SessionState session = (SessionState)args.arg1;
2693 setEnabledSessionInMainThread(session);
2694 session.method.restartInput((IInputContext)args.arg2,
2695 (EditorInfo)args.arg3);
2696 } catch (RemoteException e) {
2697 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002698 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 case MSG_UNBIND_METHOD:
2704 try {
2705 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2706 } catch (RemoteException e) {
2707 // There is nothing interesting about the last client dying.
2708 }
2709 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002710 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002711 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002712 IInputMethodClient client = (IInputMethodClient)args.arg1;
2713 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002715 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002717 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002718 } finally {
2719 // Dispose the channel if the input method is not local to this process
2720 // because the remote proxy will get its own copy when unparceled.
2721 if (res.channel != null && Binder.isProxy(client)) {
2722 res.channel.dispose();
2723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002725 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002727 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002728 case MSG_SET_ACTIVE:
2729 try {
2730 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2731 } catch (RemoteException e) {
2732 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2733 + ((ClientState)msg.obj).pid + " uid "
2734 + ((ClientState)msg.obj).uid);
2735 }
2736 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002737 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2738 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002739 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002740 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002741 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002742 } catch (RemoteException e) {
2743 Slog.w(TAG, "Got RemoteException sending "
2744 + "setUserActionNotificationSequenceNumber("
2745 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002746 + clientState.pid + " uid "
2747 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002748 }
2749 return true;
2750 }
satok01038492012-04-09 21:08:27 +09002751
2752 // --------------------------------------------------------------
2753 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002754 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002755 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757 return false;
2758 }
2759
satokdc9ddae2011-10-06 12:22:36 +09002760 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002761 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2762 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002763 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002764 if (DEBUG) {
2765 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2766 }
satok723a27e2010-11-11 14:58:11 +09002767 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002768 return true;
2769 }
2770
2771 return false;
2772 }
2773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002775 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002776 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002777 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002778 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 list.clear();
2781 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002782
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002783 // Use for queryIntentServicesAsUser
2784 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002785 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002786 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002788 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002790 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2791 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002792
satoke7c6998e2011-06-03 17:57:59 +09002793 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2794 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 for (int i = 0; i < services.size(); ++i) {
2796 ResolveInfo ri = services.get(i);
2797 ServiceInfo si = ri.serviceInfo;
2798 ComponentName compName = new ComponentName(si.packageName, si.name);
2799 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2800 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002801 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 + ": it does not require the permission "
2803 + android.Manifest.permission.BIND_INPUT_METHOD);
2804 continue;
2805 }
2806
Joe Onorato8a9b2202010-02-26 18:56:32 -08002807 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808
2809 try {
satoke7c6998e2011-06-03 17:57:59 +09002810 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002812 final String id = p.getId();
2813 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814
2815 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002816 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002820 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002822 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002825
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002826 if (resetDefaultEnabledIme) {
2827 final ArrayList<InputMethodInfo> defaultEnabledIme =
2828 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2829 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2830 final InputMethodInfo imi = defaultEnabledIme.get(i);
2831 if (DEBUG) {
2832 Slog.d(TAG, "--- enable ime = " + imi);
2833 }
2834 setInputMethodEnabledLocked(imi.getId(), true);
2835 }
2836 }
2837
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002838 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002839 if (!TextUtils.isEmpty(defaultImiId)) {
2840 if (!map.containsKey(defaultImiId)) {
2841 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2842 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002843 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002844 }
2845 } else {
2846 // Double check that the default IME is certainly enabled.
2847 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002848 }
2849 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002850 // Here is not the perfect place to reset the switching controller. Ideally
2851 // mSwitchingController and mSettings should be able to share the same state.
2852 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2853 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002854 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002858
satok217f5482010-12-15 05:19:19 +09002859 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002860 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002861 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002862 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2863 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002864 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002865 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002866 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002867 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002868 }
2869
2870 private void showConfigureInputMethods() {
2871 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2872 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2873 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2874 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002875 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002876 }
2877
satok2c93efc2012-04-02 19:33:47 +09002878 private boolean isScreenLocked() {
2879 return mKeyguardManager != null
2880 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2881 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002882
Seigo Nonakad4474cb2015-05-04 16:53:24 -07002883 private void showInputMethodMenu() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002884 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002887 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002888
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002889 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002890 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002892
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002893 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002894 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002895 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2896 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002897 if (immis == null || immis.size() == 0) {
2898 return;
2899 }
2900
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002901 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902
satok688bd472012-02-09 20:09:17 +09002903 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002904 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonakad4474cb2015-05-04 16:53:24 -07002905 true /* showSubtypes */, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002906
satokc3690562012-01-10 20:14:43 +09002907 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002908 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002909 if (currentSubtype != null) {
2910 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002911 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2912 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002913 }
2914 }
2915
Ken Wakasa761eb372011-03-04 19:06:18 +09002916 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002917 mIms = new InputMethodInfo[N];
2918 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002919 int checkedItem = 0;
2920 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002921 final ImeSubtypeListItem item = imList.get(i);
2922 mIms[i] = item.mImi;
2923 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002924 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002925 int subtypeId = mSubtypeIds[i];
2926 if ((subtypeId == NOT_A_SUBTYPE_ID)
2927 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2928 || (subtypeId == lastInputMethodSubtypeId)) {
2929 checkedItem = i;
2930 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002933
2934 final Context settingsContext = new ContextThemeWrapper(context,
2935 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2936
2937 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002938 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2939 @Override
2940 public void onCancel(DialogInterface dialog) {
2941 hideInputMethodMenu();
2942 }
2943 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002944
2945 final Context dialogContext = mDialogBuilder.getContext();
2946 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2947 com.android.internal.R.styleable.DialogPreference,
2948 com.android.internal.R.attr.alertDialogStyle, 0);
2949 final Drawable dialogIcon = a.getDrawable(
2950 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2951 a.recycle();
2952
2953 mDialogBuilder.setIcon(dialogIcon);
2954
2955 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2956 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002957 final View tv = inflater.inflate(
2958 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2959 mDialogBuilder.setCustomTitle(tv);
2960
2961 // Setup layout for a toggle switch of the hardware keyboard
2962 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002963 mSwitchingDialogTitleView
2964 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2965 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2966 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002967 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002968 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002969 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002970 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2971 @Override
2972 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002973 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002974 // Ensure that the input method dialog is dismissed when changing
2975 // the hardware keyboard state.
2976 hideInputMethodMenu();
2977 }
2978 });
2979
Alan Viverette505e3ab2014-11-24 15:22:11 -08002980 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002981 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2982 final OnClickListener choiceListener = new OnClickListener() {
2983 @Override
2984 public void onClick(final DialogInterface dialog, final int which) {
2985 synchronized (mMethodMap) {
2986 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2987 || mSubtypeIds.length <= which) {
2988 return;
satok01038492012-04-09 21:08:27 +09002989 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002990 final InputMethodInfo im = mIms[which];
2991 int subtypeId = mSubtypeIds[which];
2992 adapter.mCheckedItem = which;
2993 adapter.notifyDataSetChanged();
2994 hideInputMethodMenu();
2995 if (im != null) {
2996 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2997 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002998 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002999 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003001 }
3002 }
3003 };
3004 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003006 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003007 final OnClickListener positiveListener = new OnClickListener() {
3008 @Override
3009 public void onClick(DialogInterface dialog, int whichButton) {
3010 showConfigureInputMethods();
3011 }
3012 };
satok82beadf2010-12-27 19:03:06 +09003013 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003014 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003017 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 mSwitchingDialog.getWindow().setType(
3019 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003020 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3021 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003022 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04003023 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 mSwitchingDialog.show();
3025 }
3026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003027
Ken Wakasa05dbb652011-08-22 15:22:43 +09003028 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3029 private final LayoutInflater mInflater;
3030 private final int mTextViewResourceId;
3031 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003032 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003033 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3034 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3035 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003036
Ken Wakasa05dbb652011-08-22 15:22:43 +09003037 mTextViewResourceId = textViewResourceId;
3038 mItemsList = itemsList;
3039 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003040 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003041 }
3042
3043 @Override
3044 public View getView(int position, View convertView, ViewGroup parent) {
3045 final View view = convertView != null ? convertView
3046 : mInflater.inflate(mTextViewResourceId, null);
3047 if (position < 0 || position >= mItemsList.size()) return view;
3048 final ImeSubtypeListItem item = mItemsList.get(position);
3049 final CharSequence imeName = item.mImeName;
3050 final CharSequence subtypeName = item.mSubtypeName;
3051 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3052 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3053 if (TextUtils.isEmpty(subtypeName)) {
3054 firstTextView.setText(imeName);
3055 secondTextView.setVisibility(View.GONE);
3056 } else {
3057 firstTextView.setText(subtypeName);
3058 secondTextView.setText(imeName);
3059 secondTextView.setVisibility(View.VISIBLE);
3060 }
3061 final RadioButton radioButton =
3062 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3063 radioButton.setChecked(position == mCheckedItem);
3064 return view;
3065 }
3066 }
3067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003069 synchronized (mMethodMap) {
3070 hideInputMethodMenuLocked();
3071 }
3072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003073
The Android Open Source Project10592532009-03-18 17:39:46 -07003074 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003075 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
The Android Open Source Project10592532009-03-18 17:39:46 -07003077 if (mSwitchingDialog != null) {
3078 mSwitchingDialog.dismiss();
3079 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003081
Jason Monk807ef762014-05-08 15:47:46 -04003082 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07003083 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003084 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003088
satok42c5a162011-05-26 16:46:14 +09003089 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003091 // TODO: Make this work even for non-current users?
3092 if (!calledFromValidUser()) {
3093 return false;
3094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 synchronized (mMethodMap) {
3096 if (mContext.checkCallingOrSelfPermission(
3097 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3098 != PackageManager.PERMISSION_GRANTED) {
3099 throw new SecurityException(
3100 "Requires permission "
3101 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3102 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 long ident = Binder.clearCallingIdentity();
3105 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003106 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 } finally {
3108 Binder.restoreCallingIdentity(ident);
3109 }
3110 }
3111 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003112
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003113 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3114 // Make sure this is a valid input method.
3115 InputMethodInfo imm = mMethodMap.get(id);
3116 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003117 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003118 }
3119
satokd87c2592010-09-29 11:52:06 +09003120 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3121 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003122
satokd87c2592010-09-29 11:52:06 +09003123 if (enabled) {
3124 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3125 if (pair.first.equals(id)) {
3126 // We are enabling this input method, but it is already enabled.
3127 // Nothing to do. The previous state was enabled.
3128 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003129 }
3130 }
satokd87c2592010-09-29 11:52:06 +09003131 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3132 // Previous state was disabled.
3133 return false;
3134 } else {
3135 StringBuilder builder = new StringBuilder();
3136 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3137 builder, enabledInputMethodsList, id)) {
3138 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003139 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003140 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3141 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3142 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003143 }
3144 // Previous state was enabled.
3145 return true;
3146 } else {
3147 // We are disabling the input method but it is already disabled.
3148 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003149 return false;
3150 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003151 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003152 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003153
satok723a27e2010-11-11 14:58:11 +09003154 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3155 boolean setSubtypeOnly) {
3156 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003157 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003158
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003159 mCurUserActionNotificationSequenceNumber =
3160 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3161 if (DEBUG) {
3162 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3163 + mCurUserActionNotificationSequenceNumber);
3164 }
3165
3166 if (mCurClient != null && mCurClient.client != null) {
3167 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3168 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003169 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003170 }
3171
satok723a27e2010-11-11 14:58:11 +09003172 // Set Subtype here
3173 if (imi == null || subtypeId < 0) {
3174 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003175 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003176 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003177 if (subtypeId < imi.getSubtypeCount()) {
3178 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3179 mSettings.putSelectedSubtype(subtype.hashCode());
3180 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003181 } else {
3182 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003183 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003184 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003185 }
satokab751aa2010-09-14 19:17:36 +09003186 }
satok723a27e2010-11-11 14:58:11 +09003187
satok4c0e7152012-06-20 20:08:44 +09003188 // Workaround.
3189 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3190 // IMEs are not recognized and considered uninstalled.
3191 // Actually, we can't move everything after SystemReady because
3192 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3193 // the default IME here and try cheking the default IME again in systemReady().
3194 // TODO: Do nothing before system ready and implement a separated logic for
3195 // the encryption lock screen.
3196 // TODO: ASEC should be ready before IMMS is instantiated.
3197 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003198 // Set InputMethod here
3199 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3200 }
3201 }
3202
3203 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3204 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3205 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3206 // newDefaultIme is empty when there is no candidate for the selected IME.
3207 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3208 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3209 if (subtypeHashCode != null) {
3210 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003211 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003212 imi, Integer.valueOf(subtypeHashCode));
3213 } catch (NumberFormatException e) {
3214 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3215 }
3216 }
3217 }
3218 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003219 }
3220
satok4e4569d2010-11-19 18:45:53 +09003221 // If there are no selected shortcuts, tries finding the most applicable ones.
3222 private Pair<InputMethodInfo, InputMethodSubtype>
3223 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3224 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3225 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003226 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003227 boolean foundInSystemIME = false;
3228
3229 // Search applicable subtype for each InputMethodInfo
3230 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003231 final String imiId = imi.getId();
3232 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3233 continue;
3234 }
satokcd7cd292010-11-20 15:46:23 +09003235 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003236 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003237 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003238 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003239 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003240 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003241 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003242 }
satokdf31ae62011-01-15 06:19:44 +09003243 // 2. Search by the system locale from enabledSubtypes.
3244 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003245 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003246 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003247 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003248 }
satoka86f5e42011-09-02 17:12:42 +09003249 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003250 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003251 final ArrayList<InputMethodSubtype> subtypesForSearch =
3252 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003253 ? InputMethodUtils.getSubtypes(imi)
3254 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003255 // 4. Search by the current subtype's locale from all subtypes.
3256 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003257 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003258 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003259 }
3260 // 5. Search by the system locale from all subtypes.
3261 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003262 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003263 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003264 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003265 }
satokcd7cd292010-11-20 15:46:23 +09003266 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003267 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003268 // The current input method is the most applicable IME.
3269 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003270 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003271 break;
satok7599a7f2010-12-22 13:45:23 +09003272 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003273 // The system input method is 2nd applicable IME.
3274 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003275 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003276 if ((imi.getServiceInfo().applicationInfo.flags
3277 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3278 foundInSystemIME = true;
3279 }
satok4e4569d2010-11-19 18:45:53 +09003280 }
3281 }
3282 }
3283 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003284 if (mostApplicableIMI != null) {
3285 Slog.w(TAG, "Most applicable shortcut input method was:"
3286 + mostApplicableIMI.getId());
3287 if (mostApplicableSubtype != null) {
3288 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3289 + "," + mostApplicableSubtype.getMode() + ","
3290 + mostApplicableSubtype.getLocale());
3291 }
3292 }
satok4e4569d2010-11-19 18:45:53 +09003293 }
satokcd7cd292010-11-20 15:46:23 +09003294 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003295 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003296 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003297 } else {
3298 return null;
3299 }
3300 }
3301
satokab751aa2010-09-14 19:17:36 +09003302 /**
3303 * @return Return the current subtype of this input method.
3304 */
satok42c5a162011-05-26 16:46:14 +09003305 @Override
satokab751aa2010-09-14 19:17:36 +09003306 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003307 // TODO: Make this work even for non-current users?
3308 if (!calledFromValidUser()) {
3309 return null;
3310 }
3311 synchronized (mMethodMap) {
3312 return getCurrentInputMethodSubtypeLocked();
3313 }
3314 }
3315
3316 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003317 if (mCurMethodId == null) {
3318 return null;
3319 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003320 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003321 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3322 if (imi == null || imi.getSubtypeCount() == 0) {
3323 return null;
satok4e4569d2010-11-19 18:45:53 +09003324 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003325 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003326 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3327 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003328 if (subtypeId == NOT_A_SUBTYPE_ID) {
3329 // If there are no selected subtypes, the framework will try to find
3330 // the most applicable subtype from explicitly or implicitly enabled
3331 // subtypes.
3332 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003333 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003334 // If there is only one explicitly or implicitly enabled subtype,
3335 // just returns it.
3336 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3337 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3338 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003339 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003340 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003341 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003342 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003343 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003344 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3345 true);
satok4e4569d2010-11-19 18:45:53 +09003346 }
satok3ef8b292010-11-23 06:06:29 +09003347 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003348 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003349 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003350 }
3351 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003352 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003353 }
3354
satokf3db1af2010-11-23 13:34:33 +09003355 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3356 InputMethodSubtype subtype) {
3357 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3358 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3359 } else {
3360 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3361 subtypes.add(subtype);
3362 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3363 }
3364 }
3365
satok4e4569d2010-11-19 18:45:53 +09003366 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003367 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003368 @Override
satok4e4569d2010-11-19 18:45:53 +09003369 public List getShortcutInputMethodsAndSubtypes() {
3370 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003371 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003372 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003373 // If there are no selected shortcut subtypes, the framework will try to find
3374 // the most applicable subtype from all subtypes whose mode is
3375 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003376 Pair<InputMethodInfo, InputMethodSubtype> info =
3377 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003378 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003379 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003380 ret.add(info.first);
3381 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003382 }
satok3da92232011-01-11 22:46:30 +09003383 return ret;
satokf3db1af2010-11-23 13:34:33 +09003384 }
satokf3db1af2010-11-23 13:34:33 +09003385 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3386 ret.add(imi);
3387 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3388 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003389 }
3390 }
satokf3db1af2010-11-23 13:34:33 +09003391 return ret;
satok4e4569d2010-11-19 18:45:53 +09003392 }
3393 }
3394
satok42c5a162011-05-26 16:46:14 +09003395 @Override
satokb66d2872010-11-10 01:04:04 +09003396 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003397 // TODO: Make this work even for non-current users?
3398 if (!calledFromValidUser()) {
3399 return false;
3400 }
satokb66d2872010-11-10 01:04:04 +09003401 synchronized (mMethodMap) {
3402 if (subtype != null && mCurMethodId != null) {
3403 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003404 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003405 if (subtypeId != NOT_A_SUBTYPE_ID) {
3406 setInputMethodLocked(mCurMethodId, subtypeId);
3407 return true;
3408 }
3409 }
3410 return false;
3411 }
3412 }
3413
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003414 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003415 private static class InputMethodFileManager {
3416 private static final String SYSTEM_PATH = "system";
3417 private static final String INPUT_METHOD_PATH = "inputmethod";
3418 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3419 private static final String NODE_SUBTYPES = "subtypes";
3420 private static final String NODE_SUBTYPE = "subtype";
3421 private static final String NODE_IMI = "imi";
3422 private static final String ATTR_ID = "id";
3423 private static final String ATTR_LABEL = "label";
3424 private static final String ATTR_ICON = "icon";
3425 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3426 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3427 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3428 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3429 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3430 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003431 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003432 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003433 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003434 if (methodMap == null) {
3435 throw new NullPointerException("methodMap is null");
3436 }
3437 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003438 final File systemDir = userId == UserHandle.USER_OWNER
3439 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3440 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003441 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3442 if (!inputMethodDir.mkdirs()) {
3443 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3444 }
3445 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3446 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3447 if (!subtypeFile.exists()) {
3448 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003449 writeAdditionalInputMethodSubtypes(
3450 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003451 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003452 readAdditionalInputMethodSubtypes(
3453 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003454 }
3455 }
3456
3457 private void deleteAllInputMethodSubtypes(String imiId) {
3458 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003459 mAdditionalSubtypesMap.remove(imiId);
3460 writeAdditionalInputMethodSubtypes(
3461 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003462 }
3463 }
3464
3465 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003466 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003467 synchronized (mMethodMap) {
3468 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3469 final int N = additionalSubtypes.length;
3470 for (int i = 0; i < N; ++i) {
3471 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003472 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003473 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003474 } else {
3475 Slog.w(TAG, "Duplicated subtype definition found: "
3476 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003477 }
3478 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003479 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3480 writeAdditionalInputMethodSubtypes(
3481 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003482 }
3483 }
3484
3485 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3486 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003487 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003488 }
3489 }
3490
3491 private static void writeAdditionalInputMethodSubtypes(
3492 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3493 HashMap<String, InputMethodInfo> methodMap) {
3494 // Safety net for the case that this function is called before methodMap is set.
3495 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3496 FileOutputStream fos = null;
3497 try {
3498 fos = subtypesFile.startWrite();
3499 final XmlSerializer out = new FastXmlSerializer();
3500 out.setOutput(fos, "utf-8");
3501 out.startDocument(null, true);
3502 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3503 out.startTag(null, NODE_SUBTYPES);
3504 for (String imiId : allSubtypes.keySet()) {
3505 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3506 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3507 continue;
3508 }
3509 out.startTag(null, NODE_IMI);
3510 out.attribute(null, ATTR_ID, imiId);
3511 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3512 final int N = subtypesList.size();
3513 for (int i = 0; i < N; ++i) {
3514 final InputMethodSubtype subtype = subtypesList.get(i);
3515 out.startTag(null, NODE_SUBTYPE);
3516 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3517 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3518 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3519 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3520 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3521 out.attribute(null, ATTR_IS_AUXILIARY,
3522 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3523 out.endTag(null, NODE_SUBTYPE);
3524 }
3525 out.endTag(null, NODE_IMI);
3526 }
3527 out.endTag(null, NODE_SUBTYPES);
3528 out.endDocument();
3529 subtypesFile.finishWrite(fos);
3530 } catch (java.io.IOException e) {
3531 Slog.w(TAG, "Error writing subtypes", e);
3532 if (fos != null) {
3533 subtypesFile.failWrite(fos);
3534 }
3535 }
3536 }
3537
3538 private static void readAdditionalInputMethodSubtypes(
3539 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3540 if (allSubtypes == null || subtypesFile == null) return;
3541 allSubtypes.clear();
3542 FileInputStream fis = null;
3543 try {
3544 fis = subtypesFile.openRead();
3545 final XmlPullParser parser = Xml.newPullParser();
3546 parser.setInput(fis, null);
3547 int type = parser.getEventType();
3548 // Skip parsing until START_TAG
3549 while ((type = parser.next()) != XmlPullParser.START_TAG
3550 && type != XmlPullParser.END_DOCUMENT) {}
3551 String firstNodeName = parser.getName();
3552 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3553 throw new XmlPullParserException("Xml doesn't start with subtypes");
3554 }
3555 final int depth =parser.getDepth();
3556 String currentImiId = null;
3557 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3558 while (((type = parser.next()) != XmlPullParser.END_TAG
3559 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3560 if (type != XmlPullParser.START_TAG)
3561 continue;
3562 final String nodeName = parser.getName();
3563 if (NODE_IMI.equals(nodeName)) {
3564 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3565 if (TextUtils.isEmpty(currentImiId)) {
3566 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3567 continue;
3568 }
3569 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3570 allSubtypes.put(currentImiId, tempSubtypesArray);
3571 } else if (NODE_SUBTYPE.equals(nodeName)) {
3572 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3573 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3574 continue;
3575 }
3576 final int icon = Integer.valueOf(
3577 parser.getAttributeValue(null, ATTR_ICON));
3578 final int label = Integer.valueOf(
3579 parser.getAttributeValue(null, ATTR_LABEL));
3580 final String imeSubtypeLocale =
3581 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3582 final String imeSubtypeMode =
3583 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3584 final String imeSubtypeExtraValue =
3585 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003586 final boolean isAuxiliary = "1".equals(String.valueOf(
3587 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003588 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3589 .setSubtypeNameResId(label)
3590 .setSubtypeIconResId(icon)
3591 .setSubtypeLocale(imeSubtypeLocale)
3592 .setSubtypeMode(imeSubtypeMode)
3593 .setSubtypeExtraValue(imeSubtypeExtraValue)
3594 .setIsAuxiliary(isAuxiliary)
3595 .build();
satoke7c6998e2011-06-03 17:57:59 +09003596 tempSubtypesArray.add(subtype);
3597 }
3598 }
3599 } catch (XmlPullParserException e) {
3600 Slog.w(TAG, "Error reading subtypes: " + e);
3601 return;
3602 } catch (java.io.IOException e) {
3603 Slog.w(TAG, "Error reading subtypes: " + e);
3604 return;
3605 } catch (NumberFormatException e) {
3606 Slog.w(TAG, "Error reading subtypes: " + e);
3607 return;
3608 } finally {
3609 if (fis != null) {
3610 try {
3611 fis.close();
3612 } catch (java.io.IOException e1) {
3613 Slog.w(TAG, "Failed to close.");
3614 }
3615 }
3616 }
3617 }
3618 }
3619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 @Override
3621 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3622 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3623 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3626 + Binder.getCallingPid()
3627 + ", uid=" + Binder.getCallingUid());
3628 return;
3629 }
3630
3631 IInputMethod method;
3632 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 synchronized (mMethodMap) {
3637 p.println("Current Input Method Manager state:");
3638 int N = mMethodList.size();
3639 p.println(" Input Methods:");
3640 for (int i=0; i<N; i++) {
3641 InputMethodInfo info = mMethodList.get(i);
3642 p.println(" InputMethod #" + i + ":");
3643 info.dump(p, " ");
3644 }
3645 p.println(" Clients:");
3646 for (ClientState ci : mClients.values()) {
3647 p.println(" Client " + ci + ":");
3648 p.println(" client=" + ci.client);
3649 p.println(" inputContext=" + ci.inputContext);
3650 p.println(" sessionRequested=" + ci.sessionRequested);
3651 p.println(" curSession=" + ci.curSession);
3652 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003653 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003655 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3656 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3658 + " mBoundToMethod=" + mBoundToMethod);
3659 p.println(" mCurToken=" + mCurToken);
3660 p.println(" mCurIntent=" + mCurIntent);
3661 method = mCurMethod;
3662 p.println(" mCurMethod=" + mCurMethod);
3663 p.println(" mEnabledSession=" + mEnabledSession);
3664 p.println(" mShowRequested=" + mShowRequested
3665 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3666 + " mShowForced=" + mShowForced
3667 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003668 p.println(" mCurUserActionNotificationSequenceNumber="
3669 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003670 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003672
Jeff Brownb88102f2010-09-08 11:49:43 -07003673 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 pw.flush();
3676 try {
3677 client.client.asBinder().dump(fd, args);
3678 } catch (RemoteException e) {
3679 p.println("Input method client dead: " + e);
3680 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003681 } else {
3682 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003684
Jeff Brownb88102f2010-09-08 11:49:43 -07003685 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 pw.flush();
3688 try {
3689 method.asBinder().dump(fd, args);
3690 } catch (RemoteException e) {
3691 p.println("Input method service dead: " + e);
3692 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003693 } else {
3694 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 }
3696 }
3697}