blob: 4fda3707cb22c68be0d4289f3528a929dd5cb64a [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900151 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900166 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
satok01038492012-04-09 21:08:27 +0900168 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
169
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700170 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
satokf9f01002011-05-19 21:31:50 +0900172 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
173
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900174 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900175 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176
satok4e4569d2010-11-19 18:45:53 +0900177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800179 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900181 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final IWindowManager mIWindowManager;
184 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700185 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900186 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900187 private final HardKeyboardListener mHardKeyboardListener;
188 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900189 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900191 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // All known input methods. mMethodMap also serves as the global
194 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900195 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
196 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900197 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
198 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900199 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700201 // Used to bring IME service up to visible adjustment while it is being shown.
202 final ServiceConnection mVisibleConnection = new ServiceConnection() {
203 @Override public void onServiceConnected(ComponentName name, IBinder service) {
204 }
205
206 @Override public void onServiceDisconnected(ComponentName name) {
207 }
208 };
209 boolean mVisibleBound = false;
210
satok7cfc0ed2011-06-20 21:29:36 +0900211 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700212 private NotificationManager mNotificationManager;
213 private KeyguardManager mKeyguardManager;
214 private StatusBarManagerService mStatusBar;
215 private Notification mImeSwitcherNotification;
216 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900217 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900218 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900219 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900220
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900221 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 final ClientState client;
223 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700224
225 IInputMethodSession session;
226 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 @Override
229 public String toString() {
230 return "SessionState{uid " + client.uid + " pid " + client.pid
231 + " method " + Integer.toHexString(
232 System.identityHashCode(method))
233 + " session " + Integer.toHexString(
234 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700235 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 + "}";
237 }
238
239 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 client = _client;
242 method = _method;
243 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700244 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
Jeff Brownc28867a2013-03-26 15:42:39 -0700248 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IInputMethodClient client;
250 final IInputContext inputContext;
251 final int uid;
252 final int pid;
253 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 boolean sessionRequested;
256 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 @Override
259 public String toString() {
260 return "ClientState{" + Integer.toHexString(
261 System.identityHashCode(this)) + " uid " + uid
262 + " pid " + pid + "}";
263 }
264
265 ClientState(IInputMethodClient _client, IInputContext _inputContext,
266 int _uid, int _pid) {
267 client = _client;
268 inputContext = _inputContext;
269 uid = _uid;
270 pid = _pid;
271 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
272 }
273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final HashMap<IBinder, ClientState> mClients
276 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Id of the currently selected input method.
285 */
286 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The current binding sequence number, incremented every time there is
290 * a new bind performed.
291 */
292 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The client that is currently bound to an input method.
296 */
297 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700300 * The last window token that gained focus.
301 */
302 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * The input context last provided by the current client.
306 */
307 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The attributes last provided by the current client.
311 */
312 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The input method ID of the input method service that we are currently
316 * connected to or in the process of connecting to.
317 */
318 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
satokab751aa2010-09-14 19:17:36 +0900321 * The current subtype of the current input method.
322 */
323 private InputMethodSubtype mCurrentSubtype;
324
satok4e4569d2010-11-19 18:45:53 +0900325 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900326 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
327 mShortcutInputMethodsAndSubtypes =
328 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900329
John Spurlocke0980502013-10-25 11:59:29 -0400330 // Was the keyguard locked when this client became current?
331 private boolean mCurClientInKeyguard;
332
satokab751aa2010-09-14 19:17:36 +0900333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 * Set to true if our ServiceConnection is currently actively bound to
335 * a service (whether or not we have gotten its IBinder back yet).
336 */
337 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * Set if the client has asked for the input method to be shown.
341 */
342 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * Set if we were explicitly told to show the input method.
346 */
347 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Set if we were forced to be shown.
351 */
352 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Set if we last told the input method to show itself.
356 */
357 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * The Intent used to connect to the current input method.
361 */
362 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * The token we have made for the currently active input method, to
366 * identify it in the future.
367 */
368 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * If non-null, this is the input method service we are currently connected
372 * to.
373 */
374 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * Time that we last initiated a bind to the input method, to determine
378 * if we should try to disconnect and reconnect to it.
379 */
380 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
383 * Have we called mCurMethod.bindInput()?
384 */
385 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Currently enabled session. Only touched by service thread, not
389 * protected by a lock.
390 */
391 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * True if the screen is on. The value is true initially.
395 */
396 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900398 int mCurUserActionNotificationSequenceNumber = 0;
399
Joe Onorato857fd9b2011-01-27 15:08:35 -0800400 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
401 int mImeWindowVis;
402
Ken Wakasa05dbb652011-08-22 15:22:43 +0900403 private AlertDialog.Builder mDialogBuilder;
404 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900405 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900406 private InputMethodInfo[] mIms;
407 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900408 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700409 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
411 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800414 String mLastEnabled = "";
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 SettingsObserver(Handler handler) {
417 super(handler);
418 ContentResolver resolver = mContext.getContentResolver();
419 resolver.registerContentObserver(Settings.Secure.getUriFor(
420 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900421 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900422 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
423 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900424 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700425 resolver.registerContentObserver(Settings.Secure.getUriFor(
426 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
Michael Wright7b5a96b2014-08-09 19:28:42 -0700429 @Override public void onChange(boolean selfChange, Uri uri) {
430 final Uri showImeUri =
431 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700433 if (showImeUri.equals(uri)) {
434 updateKeyboardFromSettingsLocked();
435 } else {
436 boolean enabledChanged = false;
437 String newEnabled = mSettings.getEnabledInputMethodsStr();
438 if (!mLastEnabled.equals(newEnabled)) {
439 mLastEnabled = newEnabled;
440 enabledChanged = true;
441 }
442 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900448 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
449 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700451 if (mCurClient != null && mCurClient.client != null) {
452 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
453 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456
457 @Override
458 public void onReceive(Context context, Intent intent) {
459 final String action = intent.getAction();
460 if (Intent.ACTION_SCREEN_ON.equals(action)) {
461 mScreenOn = true;
462 refreshImeWindowVisibilityLocked();
463 updateActive();
464 return;
465 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
466 mScreenOn = false;
467 setImeWindowVisibilityStatusHiddenLocked();
468 updateActive();
469 return;
470 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
471 hideInputMethodMenu();
472 // No need to updateActive
473 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800474 } else if (Intent.ACTION_USER_ADDED.equals(action)
475 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100476 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800477 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700478 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
479 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
480 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
481 final String prevValue = intent.getStringExtra(
482 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
483 final String newValue = intent.getStringExtra(
484 Intent.EXTRA_SETTING_NEW_VALUE);
485 restoreEnabledInputMethods(mContext, prevValue, newValue);
486 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 } else {
488 Slog.w(TAG, "Unexpected intent " + intent);
489 }
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700493 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
494 // does not attempt to validate on the fly with any installed device policy, so must only
495 // be run in the context of initial device setup.
496 //
497 // TODO: Move this method to InputMethodUtils with adding unit tests.
498 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
499 if (DEBUG_RESTORE) {
500 Slog.i(TAG, "Restoring enabled input methods:");
501 Slog.i(TAG, "prev=" + prevValue);
502 Slog.i(TAG, " new=" + newValue);
503 }
504 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
505 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
506 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
507
508 // Merge the restored ime+subtype enabled states into the live state
509 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
510 final String imeId = entry.getKey();
511 ArraySet<String> prevSubtypes = prevMap.get(imeId);
512 if (prevSubtypes == null) {
513 prevSubtypes = new ArraySet<String>(2);
514 prevMap.put(imeId, prevSubtypes);
515 }
516 prevSubtypes.addAll(entry.getValue());
517 }
518
519 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
520 if (DEBUG_RESTORE) {
521 Slog.i(TAG, "Merged IME string:");
522 Slog.i(TAG, " " + mergedImesAndSubtypesString);
523 }
524 Settings.Secure.putString(context.getContentResolver(),
525 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
526 }
527
528 // TODO: Move this method to InputMethodUtils with adding unit tests.
529 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
530 // we want to use the canonical InputMethodSettings implementation,
531 // so we convert data structures first.
532 List<Pair<String, ArrayList<String>>> imeMap =
533 new ArrayList<Pair<String, ArrayList<String>>>(4);
534 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
535 final String imeName = entry.getKey();
536 final ArraySet<String> subtypeSet = entry.getValue();
537 final ArrayList<String> subtypes = new ArrayList<String>(2);
538 if (subtypeSet != null) {
539 subtypes.addAll(subtypeSet);
540 }
541 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
542 }
543 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
544 }
545
546 // TODO: Move this method to InputMethodUtils with adding unit tests.
547 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
548 final String inputMethodsAndSubtypesString) {
549 final ArrayMap<String, ArraySet<String>> imeMap =
550 new ArrayMap<String, ArraySet<String>>();
551 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
552 return imeMap;
553 }
554
555 final SimpleStringSplitter typeSplitter =
556 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
557 final SimpleStringSplitter subtypeSplitter =
558 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
559 List<Pair<String, ArrayList<String>>> allImeSettings =
560 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
561 typeSplitter,
562 subtypeSplitter);
563 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
564 ArraySet<String> subtypes = new ArraySet<String>();
565 if (ime.second != null) {
566 subtypes.addAll(ime.second);
567 }
568 imeMap.put(ime.first, subtypes);
569 }
570 return imeMap;
571 }
572
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800573 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 private boolean isChangingPackagesOfCurrentUser() {
575 final int userId = getChangingUserId();
576 final boolean retval = userId == mSettings.getCurrentUserId();
577 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900578 if (!retval) {
579 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
580 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 }
582 return retval;
583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800586 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900587 if (!isChangingPackagesOfCurrentUser()) {
588 return false;
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900591 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 final int N = mMethodList.size();
593 if (curInputMethodId != null) {
594 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800595 InputMethodInfo imi = mMethodList.get(i);
596 if (imi.getId().equals(curInputMethodId)) {
597 for (String pkg : packages) {
598 if (imi.getPackageName().equals(pkg)) {
599 if (!doit) {
600 return true;
601 }
satok723a27e2010-11-11 14:58:11 +0900602 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800603 chooseNewDefaultIMELocked();
604 return true;
605 }
606 }
607 }
608 }
609 }
610 }
611 return false;
612 }
613
614 @Override
615 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900616 if (!isChangingPackagesOfCurrentUser()) {
617 return;
618 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 synchronized (mMethodMap) {
620 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800622 final int N = mMethodList.size();
623 if (curInputMethodId != null) {
624 for (int i=0; i<N; i++) {
625 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900626 final String imiId = imi.getId();
627 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 curIm = imi;
629 }
satoke7c6998e2011-06-03 17:57:59 +0900630
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800631 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900632 if (isPackageModified(imi.getPackageName())) {
633 mFileManager.deleteAllInputMethodSubtypes(imiId);
634 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800635 if (change == PACKAGE_TEMPORARY_CHANGE
636 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800638 + imi.getComponent());
639 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 }
642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900644 buildInputMethodListLocked(
645 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800649 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 int change = isPackageDisappearing(curIm.getPackageName());
651 if (change == PACKAGE_TEMPORARY_CHANGE
652 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800653 ServiceInfo si = null;
654 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 si = mIPackageManager.getServiceInfo(
656 curIm.getComponent(), 0, mSettings.getCurrentUserId());
657 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800658 }
659 if (si == null) {
660 // Uh oh, current input method is no longer around!
661 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900663 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800665 changed = true;
666 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800667 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900668 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
670 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800671 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 }
satokab751aa2010-09-14 19:17:36 +0900673
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 if (curIm == null) {
675 // We currently don't have a default input method... is
676 // one now available?
677 changed = chooseNewDefaultIMELocked();
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800681 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683 }
684 }
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
Jeff Brownc28867a2013-03-26 15:42:39 -0700687 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900688 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700689 private final IInputMethod mMethod;
690 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
Jeff Brownc28867a2013-03-26 15:42:39 -0700692 MethodCallback(InputMethodManagerService imms, IInputMethod method,
693 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900694 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700695 mMethod = method;
696 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
satoke7c6998e2011-06-03 17:57:59 +0900699 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700700 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700701 long ident = Binder.clearCallingIdentity();
702 try {
703 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
704 } finally {
705 Binder.restoreCallingIdentity(ident);
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
satok01038492012-04-09 21:08:27 +0900710 private class HardKeyboardListener
711 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
712 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700713 public void onHardKeyboardStatusChange(boolean available) {
714 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
715 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900716 }
717
Michael Wright7b5a96b2014-08-09 19:28:42 -0700718 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900719 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700720 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900721 }
722 synchronized(mMethodMap) {
723 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
724 && mSwitchingDialog.isShowing()) {
725 mSwitchingDialogTitleView.findViewById(
726 com.android.internal.R.id.hard_keyboard_section).setVisibility(
727 available ? View.VISIBLE : View.GONE);
728 }
729 }
730 }
731 }
732
733 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900734 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800736 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mHandler = new Handler(this);
738 mIWindowManager = IWindowManager.Stub.asInterface(
739 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800740 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900741 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 public void executeMessage(Message msg) {
743 handleMessage(msg);
744 }
Mita Yuned218c72012-12-06 17:18:25 -0800745 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900746 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900747 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900748 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700749 mHasFeature = context.getPackageManager().hasSystemFeature(
750 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900751
satok7cfc0ed2011-06-20 21:29:36 +0900752 mImeSwitcherNotification = new Notification();
753 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
754 mImeSwitcherNotification.when = 0;
755 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
756 mImeSwitcherNotification.tickerText = null;
757 mImeSwitcherNotification.defaults = 0; // please be quiet
758 mImeSwitcherNotification.sound = null;
759 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400760
761 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400762 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
763 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400764
satok7cfc0ed2011-06-20 21:29:36 +0900765 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900766 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900767
768 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900769
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900770 final IntentFilter broadcastFilter = new IntentFilter();
771 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
772 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
773 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800774 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
775 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700776 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900777 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
satok7cfc0ed2011-06-20 21:29:36 +0900779 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 int userId = 0;
781 try {
782 ActivityManagerNative.getDefault().registerUserSwitchObserver(
783 new IUserSwitchObserver.Stub() {
784 @Override
785 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900786 synchronized(mMethodMap) {
787 switchUserLocked(newUserId);
788 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 if (reply != null) {
790 try {
791 reply.sendResult(null);
792 } catch (RemoteException e) {
793 }
794 }
795 }
796
797 @Override
798 public void onUserSwitchComplete(int newUserId) throws RemoteException {
799 }
Kenny Guy42979622015-04-13 18:03:05 +0000800
801 @Override
802 public void onForegroundProfileSwitch(int newProfileId) {
803 // Ignore.
804 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900805 });
806 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
807 } catch (RemoteException e) {
808 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
809 }
satok81f8b7c2012-12-04 20:42:56 +0900810 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900811
satokd87c2592010-09-29 11:52:06 +0900812 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900813 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100815 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900816 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900817 synchronized (mMethodMap) {
818 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
819 mSettings, context);
820 }
satok0a1bcf42012-05-16 19:26:31 +0900821
822 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900823 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900824 if (DEBUG) {
825 Slog.d(TAG, "Initial default ime = " + defaultImiId);
826 }
satok0a1bcf42012-05-16 19:26:31 +0900827 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
828
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900829 synchronized (mMethodMap) {
830 buildInputMethodListLocked(mMethodList, mMethodMap,
831 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
832 }
satokd87c2592010-09-29 11:52:06 +0900833 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834
satok0a1bcf42012-05-16 19:26:31 +0900835 if (!mImeSelectedOnBoot) {
836 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900837 synchronized (mMethodMap) {
838 resetDefaultImeLocked(context);
839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900843 synchronized (mMethodMap) {
844 updateFromSettingsLocked(true);
845 }
satok5b927c432012-05-01 20:09:34 +0900846
847 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
848 // according to the new system locale.
849 final IntentFilter filter = new IntentFilter();
850 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
851 mContext.registerReceiver(
852 new BroadcastReceiver() {
853 @Override
854 public void onReceive(Context context, Intent intent) {
855 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900856 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900857 }
858 }
859 }, filter);
860 }
861
satok5b927c432012-05-01 20:09:34 +0900862 private void resetDefaultImeLocked(Context context) {
863 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900864 if (mCurMethodId != null
865 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900866 return;
867 }
868
869 InputMethodInfo defIm = null;
870 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900871 if (defIm == null && mSystemReady) {
872 final Locale systemLocale = context.getResources().getConfiguration().locale;
873 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
874 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
875 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900876 defIm = imi;
877 Slog.i(TAG, "Selected default: " + imi.getId());
878 }
879 }
880 }
881 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900882 defIm = InputMethodUtils.getMostApplicableDefaultIME(
883 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900884 if (defIm != null) {
885 Slog.i(TAG, "Default found, using " + defIm.getId());
886 } else {
887 Slog.i(TAG, "No default found");
888 }
satok5b927c432012-05-01 20:09:34 +0900889 }
890 if (defIm != null) {
891 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
892 }
893 }
894
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900895 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
896 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 if (!mSystemReady) {
898 // not system ready
899 return;
900 }
901 final Locale newLocale = mRes.getConfiguration().locale;
902 if (!updateOnlyWhenLocaleChanged
903 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
904 if (!updateOnlyWhenLocaleChanged) {
905 hideCurrentInputLocked(0, null);
906 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700907 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 }
909 if (DEBUG) {
910 Slog.i(TAG, "Locale has been changed to " + newLocale);
911 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900912 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900913 if (!updateOnlyWhenLocaleChanged) {
914 final String selectedImiId = mSettings.getSelectedInputMethod();
915 if (TextUtils.isEmpty(selectedImiId)) {
916 // This is the first time of the user switch and
917 // set the current ime to the proper one.
918 resetDefaultImeLocked(mContext);
919 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900920 } else {
921 // If the locale is changed, needs to reset the default ime
922 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900923 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800924 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900925 mLastSystemLocale = newLocale;
926 if (!updateOnlyWhenLocaleChanged) {
927 try {
928 startInputInnerLocked();
929 } catch (RuntimeException e) {
930 Slog.w(TAG, "Unexpected exception", e);
931 }
932 }
933 }
934 }
935
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900936 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900937 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
938 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 }
940
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900941 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100943 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900944 // InputMethodFileManager should be reset when the user is changed
945 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900946 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900947 // For secondary users, the list of enabled IMEs may not have been updated since the
948 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
949 // not be empty even if the IME has been uninstalled by the primary user.
950 // Even in such cases, IMMS works fine because it will find the most applicable
951 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900952 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900953 if (DEBUG) {
954 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
955 }
956 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900957 initialUserSwitch /* needsToResetDefaultIme */);
958 if (initialUserSwitch) {
959 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
960 mSettings.getEnabledInputMethodListLocked());
961 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 }
963
Kenny Guy2a764942014-04-02 13:29:20 +0100964 void updateCurrentProfileIds() {
965 List<UserInfo> profiles =
966 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
967 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
968 for (int i = 0; i < currentProfileIds.length; i++) {
969 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800970 }
Kenny Guy2a764942014-04-02 13:29:20 +0100971 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800972 }
973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 @Override
975 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
976 throws RemoteException {
977 try {
978 return super.onTransact(code, data, reply, flags);
979 } catch (RuntimeException e) {
980 // The input method manager only throws security exceptions, so let's
981 // log all others.
982 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700983 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 throw e;
986 }
987 }
988
Svetoslav Ganova0027152013-06-25 14:59:53 -0700989 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700990 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (DEBUG) {
992 Slog.d(TAG, "--- systemReady");
993 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700994 if (!mSystemReady) {
995 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 mKeyguardManager =
997 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700998 mNotificationManager = (NotificationManager)
999 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1000 mStatusBar = statusBar;
1001 statusBar.setIconVisibility("ime", false);
1002 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +09001003 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1004 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001005 if (mShowOngoingImeSwitcherForPhones) {
1006 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1007 mHardKeyboardListener);
1008 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001009 buildInputMethodListLocked(mMethodList, mMethodMap,
1010 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001011 if (!mImeSelectedOnBoot) {
1012 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001013 resetStateIfCurrentLocaleChangedLocked();
1014 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1015 mContext.getPackageManager(),
1016 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001017 }
1018 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001019 try {
1020 startInputInnerLocked();
1021 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001022 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001023 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001024 }
1025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
satok15452a42011-10-28 17:58:28 +09001028 private void setImeWindowVisibilityStatusHiddenLocked() {
1029 mImeWindowVis = 0;
1030 updateImeWindowStatusLocked();
1031 }
1032
satok3afd6c02011-11-18 08:38:19 +09001033 private void refreshImeWindowVisibilityLocked() {
1034 final Configuration conf = mRes.getConfiguration();
1035 final boolean haveHardKeyboard = conf.keyboard
1036 != Configuration.KEYBOARD_NOKEYS;
1037 final boolean hardKeyShown = haveHardKeyboard
1038 && conf.hardKeyboardHidden
1039 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -04001040
1041 final boolean isScreenLocked = isKeyguardLocked();
1042 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -04001043 // We assume the softkeyboard is shown when the input is active as long as the
1044 // hard keyboard is not shown.
1045 final boolean inputVisible = inputActive && !hardKeyShown;
1046 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
1047 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +09001048 updateImeWindowStatusLocked();
1049 }
1050
satok15452a42011-10-28 17:58:28 +09001051 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +09001052 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001053 }
1054
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001055 // ---------------------------------------------------------------------------------------
1056 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1057 // 1) it comes from the system process
1058 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1059 private boolean calledFromValidUser() {
1060 final int uid = Binder.getCallingUid();
1061 final int userId = UserHandle.getUserId(uid);
1062 if (DEBUG) {
1063 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1064 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1065 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001066 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1067 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001068 }
Kenny Guy2a764942014-04-02 13:29:20 +01001069 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001071 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001072
1073 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1074 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1075 // must not manage background users' states in any functions.
1076 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1077 // by a token.
1078 if (mContext.checkCallingOrSelfPermission(
1079 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1080 == PackageManager.PERMISSION_GRANTED) {
1081 if (DEBUG) {
1082 Slog.d(TAG, "--- Access granted because the calling process has "
1083 + "the INTERACT_ACROSS_USERS_FULL permission");
1084 }
1085 return true;
1086 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001087 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1088 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001089 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001090 }
1091
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001092
1093 /**
1094 * Returns true iff the caller is identified to be the current input method with the token.
1095 * @param token The window token given to the input method when it was started.
1096 * @return true if and only if non-null valid token is specified.
1097 */
1098 private boolean calledWithValidToken(IBinder token) {
1099 if (token == null || mCurToken != token) {
1100 return false;
1101 }
1102 return true;
1103 }
1104
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 private boolean bindCurrentInputMethodService(
1106 Intent service, ServiceConnection conn, int flags) {
1107 if (service == null || conn == null) {
1108 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1109 return false;
1110 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001111 return mContext.bindServiceAsUser(service, conn, flags,
1112 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001113 }
1114
satoke7c6998e2011-06-03 17:57:59 +09001115 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 // TODO: Make this work even for non-current users?
1118 if (!calledFromValidUser()) {
1119 return Collections.emptyList();
1120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 synchronized (mMethodMap) {
1122 return new ArrayList<InputMethodInfo>(mMethodList);
1123 }
1124 }
1125
satoke7c6998e2011-06-03 17:57:59 +09001126 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 // TODO: Make this work even for non-current users?
1129 if (!calledFromValidUser()) {
1130 return Collections.emptyList();
1131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001133 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 }
1136
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001137 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001138 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001139 * @return enabled subtypes of the specified imi
1140 */
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001142 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001143 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001144 // TODO: Make this work even for non-current users?
1145 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001146 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001147 }
satok67ddf9c2010-11-17 09:45:54 +09001148 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001149 final InputMethodInfo imi;
1150 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001151 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001152 } else {
1153 imi = mMethodMap.get(imiId);
1154 }
1155 if (imi == null) {
1156 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001157 }
1158 return mSettings.getEnabledInputMethodSubtypeListLocked(
1159 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001160 }
1161 }
1162
satoke7c6998e2011-06-03 17:57:59 +09001163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 public void addClient(IInputMethodClient client,
1165 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001166 if (!calledFromValidUser()) {
1167 return;
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 synchronized (mMethodMap) {
1170 mClients.put(client.asBinder(), new ClientState(client,
1171 inputContext, uid, pid));
1172 }
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
satoke7c6998e2011-06-03 17:57:59 +09001175 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001177 if (!calledFromValidUser()) {
1178 return;
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001181 ClientState cs = mClients.remove(client.asBinder());
1182 if (cs != null) {
1183 clearClientSessionLocked(cs);
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 void executeOrSendMessage(IInterface target, Message msg) {
1189 if (target.asBinder() instanceof Binder) {
1190 mCaller.sendMessage(msg);
1191 } else {
1192 handleMessage(msg);
1193 msg.recycle();
1194 }
1195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001197 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 + mCurClient.client.asBinder());
1201 if (mBoundToMethod) {
1202 mBoundToMethod = false;
1203 if (mCurMethod != null) {
1204 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1205 MSG_UNBIND_INPUT, mCurMethod));
1206 }
1207 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001208
1209 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1210 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1212 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1213 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
The Android Open Source Project10592532009-03-18 17:39:46 -07001216 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 private int getImeShowFlags() {
1221 int flags = 0;
1222 if (mShowForced) {
1223 flags |= InputMethod.SHOW_FORCED
1224 | InputMethod.SHOW_EXPLICIT;
1225 } else if (mShowExplicitlyRequested) {
1226 flags |= InputMethod.SHOW_EXPLICIT;
1227 }
1228 return flags;
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 private int getAppShowFlags() {
1232 int flags = 0;
1233 if (mShowForced) {
1234 flags |= InputMethodManager.SHOW_FORCED;
1235 } else if (!mShowExplicitlyRequested) {
1236 flags |= InputMethodManager.SHOW_IMPLICIT;
1237 }
1238 return flags;
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
Dianne Hackborn7663d802012-02-24 13:08:49 -08001241 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 if (!mBoundToMethod) {
1243 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1244 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1245 mBoundToMethod = true;
1246 }
1247 final SessionState session = mCurClient.curSession;
1248 if (initial) {
1249 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1250 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1251 } else {
1252 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1253 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1254 }
1255 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001257 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001259 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001260 (session.channel != null ? session.channel.dup() : null),
1261 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001265 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 // If no method is currently selected, do nothing.
1267 if (mCurMethodId == null) {
1268 return mNoBinding;
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 ClientState cs = mClients.get(client.asBinder());
1272 if (cs == null) {
1273 throw new IllegalArgumentException("unknown client "
1274 + client.asBinder());
1275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 try {
1278 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1279 // Check with the window manager to make sure this client actually
1280 // has a window with focus. If not, reject. This is thread safe
1281 // because if the focus changes some time before or after, the
1282 // next client receiving focus that has any interest in input will
1283 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001284 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1286 return null;
1287 }
1288 } catch (RemoteException e) {
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
Dianne Hackborn7663d802012-02-24 13:08:49 -08001291 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1292 }
1293
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001294 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001295 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1296 // If no method is currently selected, do nothing.
1297 if (mCurMethodId == null) {
1298 return mNoBinding;
1299 }
1300
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001301 if (attribute != null) {
1302 // We accept an empty package name as a valid data.
1303 if (!TextUtils.isEmpty(attribute.packageName) &&
1304 !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1305 attribute.packageName)) {
1306 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1307 + " uid=" + cs.uid + " package=" + attribute.packageName);
1308 return mNoBinding;
1309 }
1310 }
1311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001313 // Was the keyguard locked when switching over to the new client?
1314 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 // If the client is changing, we need to switch over to the new
1316 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001317 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001319 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320
1321 // If the screen is on, inform the new client it is active
1322 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001323 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1324 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 // Bump up the sequence for this client and attach it.
1329 mCurSeq++;
1330 if (mCurSeq <= 0) mCurSeq = 1;
1331 mCurClient = cs;
1332 mCurInputContext = inputContext;
1333 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 // Check if the input method is changing.
1336 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1337 if (cs.curSession != null) {
1338 // Fast case: if we are already connected to the input method,
1339 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001340 return attachNewInputLocked(
1341 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343 if (mHaveConnection) {
1344 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 // Return to client, and we will get back with it when
1346 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001347 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001348 return new InputBindResult(null, null, mCurId, mCurSeq,
1349 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 } else if (SystemClock.uptimeMillis()
1351 < (mLastBindTime+TIME_TO_RECONNECT)) {
1352 // In this case we have connected to the service, but
1353 // don't yet have its interface. If it hasn't been too
1354 // long since we did the connection, we'll return to
1355 // the client and wait to get the service interface so
1356 // we can report back. If it has been too long, we want
1357 // to fall through so we can try a disconnect/reconnect
1358 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001359 return new InputBindResult(null, null, mCurId, mCurSeq,
1360 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1363 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
1366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001368 return startInputInnerLocked();
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001371 InputBindResult startInputInnerLocked() {
1372 if (mCurMethodId == null) {
1373 return mNoBinding;
1374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001376 if (!mSystemReady) {
1377 // If the system is not yet ready, we shouldn't be running third
1378 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001379 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1380 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1384 if (info == null) {
1385 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001388 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1391 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001392 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1393 com.android.internal.R.string.input_method_binding_label);
1394 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1395 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001396 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001397 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1398 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 mLastBindTime = SystemClock.uptimeMillis();
1400 mHaveConnection = true;
1401 mCurId = info.getId();
1402 mCurToken = new Binder();
1403 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001404 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 mIWindowManager.addWindowToken(mCurToken,
1406 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1407 } catch (RemoteException e) {
1408 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001409 return new InputBindResult(null, null, mCurId, mCurSeq,
1410 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 } else {
1412 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 + mCurIntent);
1415 }
1416 return null;
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
satoke7c6998e2011-06-03 17:57:59 +09001419 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001421 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001422 if (!calledFromValidUser()) {
1423 return null;
1424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 synchronized (mMethodMap) {
1426 final long ident = Binder.clearCallingIdentity();
1427 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001428 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 } finally {
1430 Binder.restoreCallingIdentity(ident);
1431 }
1432 }
1433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
satoke7c6998e2011-06-03 17:57:59 +09001435 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 public void finishInput(IInputMethodClient client) {
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
satoke7c6998e2011-06-03 17:57:59 +09001439 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 public void onServiceConnected(ComponentName name, IBinder service) {
1441 synchronized (mMethodMap) {
1442 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1443 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001444 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001445 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001446 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001447 return;
1448 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001449 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001450 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1451 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001453 clearClientSessionLocked(mCurClient);
1454 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 }
1456 }
1457 }
1458 }
1459
Jeff Brownc28867a2013-03-26 15:42:39 -07001460 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1461 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 synchronized (mMethodMap) {
1463 if (mCurMethod != null && method != null
1464 && mCurMethod.asBinder() == method.asBinder()) {
1465 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001466 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001468 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001469 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 if (res.method != null) {
1471 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1472 MSG_BIND_METHOD, mCurClient.client, res));
1473 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001474 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 }
1476 }
1477 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001478
1479 // Session abandoned. Close its associated input channel.
1480 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001482
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001483 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001484 if (mVisibleBound) {
1485 mContext.unbindService(mVisibleConnection);
1486 mVisibleBound = false;
1487 }
1488
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001489 if (mHaveConnection) {
1490 mContext.unbindService(this);
1491 mHaveConnection = false;
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001494 if (mCurToken != null) {
1495 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001496 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001497 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001498 // The current IME is shown. Hence an IME switch (transition) is happening.
1499 mWindowManagerService.saveLastInputMethodWindowForTransition();
1500 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001501 mIWindowManager.removeWindowToken(mCurToken);
1502 } catch (RemoteException e) {
1503 }
1504 mCurToken = null;
1505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
The Android Open Source Project10592532009-03-18 17:39:46 -07001507 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001508 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001509
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001510 if (reportToClient && mCurClient != null) {
1511 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1512 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1513 }
1514 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001515
1516 void requestClientSessionLocked(ClientState cs) {
1517 if (!cs.sessionRequested) {
1518 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1519 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1520 cs.sessionRequested = true;
1521 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1522 MSG_CREATE_SESSION, mCurMethod, channels[1],
1523 new MethodCallback(this, mCurMethod, channels[0])));
1524 }
1525 }
1526
1527 void clearClientSessionLocked(ClientState cs) {
1528 finishSessionLocked(cs.curSession);
1529 cs.curSession = null;
1530 cs.sessionRequested = false;
1531 }
1532
1533 private void finishSessionLocked(SessionState sessionState) {
1534 if (sessionState != null) {
1535 if (sessionState.session != null) {
1536 try {
1537 sessionState.session.finishSession();
1538 } catch (RemoteException e) {
1539 Slog.w(TAG, "Session failed to close due to remote exception", e);
1540 setImeWindowVisibilityStatusHiddenLocked();
1541 }
1542 sessionState.session = null;
1543 }
1544 if (sessionState.channel != null) {
1545 sessionState.channel.dispose();
1546 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001547 }
1548 }
1549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001550
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001551 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (mCurMethod != null) {
1553 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001554 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001556
Jeff Brownc28867a2013-03-26 15:42:39 -07001557 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001558 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 mCurMethod = null;
1560 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001561 if (mStatusBar != null) {
1562 mStatusBar.setIconVisibility("ime", false);
1563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
satoke7c6998e2011-06-03 17:57:59 +09001566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 public void onServiceDisconnected(ComponentName name) {
1568 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001569 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 + " mCurIntent=" + mCurIntent);
1571 if (mCurMethod != null && mCurIntent != null
1572 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001573 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 // We consider this to be a new bind attempt, since the system
1575 // should now try to restart the service for us.
1576 mLastBindTime = SystemClock.uptimeMillis();
1577 mShowRequested = mInputShown;
1578 mInputShown = false;
1579 if (mCurClient != null) {
1580 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1581 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1582 }
1583 }
1584 }
1585 }
1586
satokf9f01002011-05-19 21:31:50 +09001587 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1589 long ident = Binder.clearCallingIdentity();
1590 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001592 if (!calledWithValidToken(token)) {
1593 final int uid = Binder.getCallingUid();
1594 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1595 + " token:" + token);
1596 return;
1597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001599 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001600 if (mStatusBar != null) {
1601 mStatusBar.setIconVisibility("ime", false);
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001604 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001605 CharSequence contentDescription = null;
1606 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001607 // Use PackageManager to load label
1608 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001609 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001610 mIPackageManager.getApplicationInfo(packageName, 0,
1611 mSettings.getCurrentUserId()));
1612 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001613 /* ignore */
1614 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001615 if (mStatusBar != null) {
1616 mStatusBar.setIcon("ime", packageName, iconId, 0,
1617 contentDescription != null
1618 ? contentDescription.toString() : null);
1619 mStatusBar.setIconVisibility("ime", true);
1620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
1622 }
1623 } finally {
1624 Binder.restoreCallingIdentity(ident);
1625 }
1626 }
1627
satok7cfc0ed2011-06-20 21:29:36 +09001628 private boolean needsToShowImeSwitchOngoingNotification() {
1629 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001630 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001631 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001632 synchronized (mMethodMap) {
1633 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1634 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001635 if (N > 2) return true;
1636 if (N < 1) return false;
1637 int nonAuxCount = 0;
1638 int auxCount = 0;
1639 InputMethodSubtype nonAuxSubtype = null;
1640 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001641 for(int i = 0; i < N; ++i) {
1642 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001643 final List<InputMethodSubtype> subtypes =
1644 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001645 final int subtypeCount = subtypes.size();
1646 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001647 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001648 } else {
1649 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001650 final InputMethodSubtype subtype = subtypes.get(j);
1651 if (!subtype.isAuxiliary()) {
1652 ++nonAuxCount;
1653 nonAuxSubtype = subtype;
1654 } else {
1655 ++auxCount;
1656 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001657 }
1658 }
1659 }
satok7cfc0ed2011-06-20 21:29:36 +09001660 }
satokb6359412011-06-28 17:47:41 +09001661 if (nonAuxCount > 1 || auxCount > 1) {
1662 return true;
1663 } else if (nonAuxCount == 1 && auxCount == 1) {
1664 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001665 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1666 || auxSubtype.overridesImplicitlyEnabledSubtype()
1667 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001668 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1669 return false;
1670 }
1671 return true;
1672 }
1673 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001674 }
satok7cfc0ed2011-06-20 21:29:36 +09001675 }
1676
John Spurlocke0980502013-10-25 11:59:29 -04001677 private boolean isKeyguardLocked() {
1678 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1679 }
1680
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001681 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001682 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001683 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001684 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001685 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001686 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001687 if (!calledWithValidToken(token)) {
1688 final int uid = Binder.getCallingUid();
1689 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1690 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001691 return;
1692 }
satok06487a52010-10-29 11:37:18 +09001693 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001694 // apply policy for binder calls
1695 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1696 vis = 0;
1697 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001698 mImeWindowVis = vis;
1699 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001700 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1701 && (mWindowManagerService.isHardKeyboardAvailable()
1702 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001703 final boolean needsToShowImeSwitcher = iconVisibility
1704 && needsToShowImeSwitchOngoingNotification();
1705 if (mStatusBar != null) {
1706 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1707 needsToShowImeSwitcher);
1708 }
satok5bc8e732011-07-22 21:07:23 +09001709 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001710 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001711 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001712 final CharSequence title = mRes.getText(
1713 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001714 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1715 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001716
Alan Viverette4a357cd2015-03-18 18:37:18 -07001717 mImeSwitcherNotification.color = mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +02001718 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001719 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001720 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001721 if ((mNotificationManager != null)
1722 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001723 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001724 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001725 }
1726 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001727 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001728 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001729 mNotificationShown = true;
1730 }
satok7cfc0ed2011-06-20 21:29:36 +09001731 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001732 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001733 if (DEBUG) {
1734 Slog.d(TAG, "--- hide notification");
1735 }
1736 mNotificationManager.cancelAsUser(null,
1737 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001738 mNotificationShown = false;
1739 }
1740 }
satok06487a52010-10-29 11:37:18 +09001741 }
1742 } finally {
1743 Binder.restoreCallingIdentity(ident);
1744 }
1745 }
1746
satoke7c6998e2011-06-03 17:57:59 +09001747 @Override
satokf9f01002011-05-19 21:31:50 +09001748 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001749 if (!calledFromValidUser()) {
1750 return;
1751 }
satokf9f01002011-05-19 21:31:50 +09001752 synchronized (mMethodMap) {
1753 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1754 for (int i = 0; i < spans.length; ++i) {
1755 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001756 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001757 mSecureSuggestionSpans.put(ss, currentImi);
1758 }
1759 }
1760 }
1761 }
1762
satoke7c6998e2011-06-03 17:57:59 +09001763 @Override
satokf9f01002011-05-19 21:31:50 +09001764 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001765 if (!calledFromValidUser()) {
1766 return false;
1767 }
satokf9f01002011-05-19 21:31:50 +09001768 synchronized (mMethodMap) {
1769 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1770 // TODO: Do not send the intent if the process of the targetImi is already dead.
1771 if (targetImi != null) {
1772 final String[] suggestions = span.getSuggestions();
1773 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001774 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001775 final Intent intent = new Intent();
1776 // Ensures that only a class in the original IME package will receive the
1777 // notification.
satok42c5a162011-05-26 16:46:14 +09001778 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001779 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1780 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1781 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1782 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001783 final long ident = Binder.clearCallingIdentity();
1784 try {
1785 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1786 } finally {
1787 Binder.restoreCallingIdentity(ident);
1788 }
satokf9f01002011-05-19 21:31:50 +09001789 return true;
1790 }
1791 }
1792 return false;
1793 }
1794
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001795 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001796 updateInputMethodsFromSettingsLocked(enabledMayChange);
1797 updateKeyboardFromSettingsLocked();
1798 }
1799
1800 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001801 if (enabledMayChange) {
1802 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1803 for (int i=0; i<enabled.size(); i++) {
1804 // We allow the user to select "disabled until used" apps, so if they
1805 // are enabling one of those here we now need to make it enabled.
1806 InputMethodInfo imm = enabled.get(i);
1807 try {
1808 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1809 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1810 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001811 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001812 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001813 if (DEBUG) {
1814 Slog.d(TAG, "Update state(" + imm.getId()
1815 + "): DISABLED_UNTIL_USED -> DEFAULT");
1816 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001817 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1818 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001819 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1820 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001821 }
1822 } catch (RemoteException e) {
1823 }
1824 }
1825 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001826 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1827 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1828 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1829 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001830 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001831 // There is no input method selected, try to choose new applicable input method.
1832 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001833 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001834 }
1835 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001837 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001839 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001840 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001841 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
satokf3db1af2010-11-23 13:34:33 +09001843 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001844 } else {
1845 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001846 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001847 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001849 // Here is not the perfect place to reset the switching controller. Ideally
1850 // mSwitchingController and mSettings should be able to share the same state.
1851 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1852 // the same enabled IMEs list.
1853 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001854
1855 }
1856
1857 public void updateKeyboardFromSettingsLocked() {
1858 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1859 if (mSwitchingDialog != null
1860 && mSwitchingDialogTitleView != null
1861 && mSwitchingDialog.isShowing()) {
1862 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1863 com.android.internal.R.id.hard_keyboard_switch);
1864 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001867
satokab751aa2010-09-14 19:17:36 +09001868 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 InputMethodInfo info = mMethodMap.get(id);
1870 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001871 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001874 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001875 // We have already made sure that the package name belongs to the application's UID.
1876 // No further UID check is required.
1877 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1878 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001879 }
1880 }
1881
satokd81e9502012-05-21 12:58:45 +09001882 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001884 final int subtypeCount = info.getSubtypeCount();
1885 if (subtypeCount <= 0) {
1886 return;
satokcd7cd292010-11-20 15:46:23 +09001887 }
satokd81e9502012-05-21 12:58:45 +09001888 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1889 final InputMethodSubtype newSubtype;
1890 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1891 newSubtype = info.getSubtypeAt(subtypeId);
1892 } else {
1893 // If subtype is null, try to find the most applicable one from
1894 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001895 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001896 }
1897 if (newSubtype == null || oldSubtype == null) {
1898 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1899 + ", new subtype = " + newSubtype);
1900 return;
1901 }
1902 if (newSubtype != oldSubtype) {
1903 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1904 if (mCurMethod != null) {
1905 try {
1906 refreshImeWindowVisibilityLocked();
1907 mCurMethod.changeInputMethodSubtype(newSubtype);
1908 } catch (RemoteException e) {
1909 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001910 }
1911 }
1912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 return;
1914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
satokd81e9502012-05-21 12:58:45 +09001916 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 final long ident = Binder.clearCallingIdentity();
1918 try {
satokab751aa2010-09-14 19:17:36 +09001919 // Set a subtype to this input method.
1920 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001921 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1922 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1923 // because mCurMethodId is stored as a history in
1924 // setSelectedInputMethodAndSubtypeLocked().
1925 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926
1927 if (ActivityManagerNative.isSystemReady()) {
1928 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001929 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001931 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001933 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 } finally {
1935 Binder.restoreCallingIdentity(ident);
1936 }
1937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001938
satok42c5a162011-05-26 16:46:14 +09001939 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001940 public boolean showSoftInput(IInputMethodClient client, int flags,
1941 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001942 if (!calledFromValidUser()) {
1943 return false;
1944 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001945 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 long ident = Binder.clearCallingIdentity();
1947 try {
1948 synchronized (mMethodMap) {
1949 if (mCurClient == null || client == null
1950 || mCurClient.client.asBinder() != client.asBinder()) {
1951 try {
1952 // We need to check if this is the current client with
1953 // focus in the window manager, to allow this call to
1954 // be made before input is started in it.
1955 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001956 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001957 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001960 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001963
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001964 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001965 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967 } finally {
1968 Binder.restoreCallingIdentity(ident);
1969 }
1970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001971
The Android Open Source Project4df24232009-03-05 14:34:35 -08001972 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 mShowRequested = true;
1974 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1975 mShowExplicitlyRequested = true;
1976 }
1977 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1978 mShowExplicitlyRequested = true;
1979 mShowForced = true;
1980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001981
Dianne Hackborncc278702009-09-02 23:07:23 -07001982 if (!mSystemReady) {
1983 return false;
1984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001985
The Android Open Source Project4df24232009-03-05 14:34:35 -08001986 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001988 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001989 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1990 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1991 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001993 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001994 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001995 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07001996 | Context.BIND_TREAT_LIKE_ACTIVITY
1997 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001998 mVisibleBound = true;
1999 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002000 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002002 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 // The client has asked to have the input method shown, but
2004 // we have been sitting here too long with a connection to the
2005 // service and no interface received, so let's disconnect/connect
2006 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002009 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002011 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002012 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002013 } else {
2014 if (DEBUG) {
2015 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2016 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
The Android Open Source Project4df24232009-03-05 14:34:35 -08002020 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
satok42c5a162011-05-26 16:46:14 +09002023 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002024 public boolean hideSoftInput(IInputMethodClient client, int flags,
2025 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002026 if (!calledFromValidUser()) {
2027 return false;
2028 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002029 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 long ident = Binder.clearCallingIdentity();
2031 try {
2032 synchronized (mMethodMap) {
2033 if (mCurClient == null || client == null
2034 || mCurClient.client.asBinder() != client.asBinder()) {
2035 try {
2036 // We need to check if this is the current client with
2037 // focus in the window manager, to allow this call to
2038 // be made before input is started in it.
2039 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002040 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2041 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09002042 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002043 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09002046 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002047 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002050
Joe Onorato8a9b2202010-02-26 18:56:32 -08002051 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002052 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
2054 } finally {
2055 Binder.restoreCallingIdentity(ident);
2056 }
2057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002058
The Android Open Source Project4df24232009-03-05 14:34:35 -08002059 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2061 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002062 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 -08002063 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
2065 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002066 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 -08002067 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002069 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2072 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2073 res = true;
2074 } else {
2075 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002077 if (mHaveConnection && mVisibleBound) {
2078 mContext.unbindService(mVisibleConnection);
2079 mVisibleBound = false;
2080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 mInputShown = false;
2082 mShowRequested = false;
2083 mShowExplicitlyRequested = false;
2084 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002085 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002087
satok42c5a162011-05-26 16:46:14 +09002088 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002089 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2090 int controlFlags, int softInputMode, int windowFlags,
2091 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002092 // Needs to check the validity before clearing calling identity
2093 final boolean calledFromValidUser = calledFromValidUser();
2094
Dianne Hackborn7663d802012-02-24 13:08:49 -08002095 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 long ident = Binder.clearCallingIdentity();
2097 try {
2098 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002099 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002100 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002102 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002103
Dianne Hackborn7663d802012-02-24 13:08:49 -08002104 ClientState cs = mClients.get(client.asBinder());
2105 if (cs == null) {
2106 throw new IllegalArgumentException("unknown client "
2107 + client.asBinder());
2108 }
2109
2110 try {
2111 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2112 // Check with the window manager to make sure this client actually
2113 // has a window with focus. If not, reject. This is thread safe
2114 // because if the focus changes some time before or after, the
2115 // next client receiving focus that has any interest in input will
2116 // be calling through here after that change happens.
2117 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2118 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2119 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002121 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002124 if (!calledFromValidUser) {
2125 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2126 Slog.w(TAG, "If you want to interect with IME, you need "
2127 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2128 hideCurrentInputLocked(0, null);
2129 return null;
2130 }
2131
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002132 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002133 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002134 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002135 if (attribute != null) {
2136 return startInputUncheckedLocked(cs, inputContext, attribute,
2137 controlFlags);
2138 }
2139 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002140 }
2141 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002143 // Should we auto-show the IME even if the caller has not
2144 // specified what should be done with it?
2145 // We only do this automatically if the window can resize
2146 // to accommodate the IME (so what the user sees will give
2147 // them good context without input information being obscured
2148 // by the IME) or if running on a large screen where there
2149 // is more room for the target window + IME.
2150 final boolean doAutoShow =
2151 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2152 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2153 || mRes.getConfiguration().isLayoutSizeAtLeast(
2154 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002155 final boolean isTextEditor =
2156 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2157
2158 // We want to start input before showing the IME, but after closing
2159 // it. We want to do this after closing it to help the IME disappear
2160 // more quickly (not get stuck behind it initializing itself for the
2161 // new focused input, even if its window wants to hide the IME).
2162 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2165 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002166 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2168 // There is no focus view, and this window will
2169 // be behind any soft input window, so hide the
2170 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002171 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002172 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002174 } else if (isTextEditor && doAutoShow && (softInputMode &
2175 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 // There is a focus view, and we are navigating forward
2177 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002178 // We only do this automatically if the window can resize
2179 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002180 // them good context without input information being obscured
2181 // by the IME) or if running on a large screen where there
2182 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002183 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002184 if (attribute != null) {
2185 res = startInputUncheckedLocked(cs, inputContext, attribute,
2186 controlFlags);
2187 didStart = true;
2188 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
2191 break;
2192 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2193 // Do nothing.
2194 break;
2195 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2196 if ((softInputMode &
2197 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002198 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002199 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
2201 break;
2202 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002203 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002204 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 break;
2206 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2207 if ((softInputMode &
2208 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002209 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002210 if (attribute != null) {
2211 res = startInputUncheckedLocked(cs, inputContext, attribute,
2212 controlFlags);
2213 didStart = true;
2214 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002215 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217 break;
2218 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002219 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002220 if (attribute != null) {
2221 res = startInputUncheckedLocked(cs, inputContext, attribute,
2222 controlFlags);
2223 didStart = true;
2224 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002225 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 break;
2227 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002228
2229 if (!didStart && attribute != null) {
2230 res = startInputUncheckedLocked(cs, inputContext, attribute,
2231 controlFlags);
2232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234 } finally {
2235 Binder.restoreCallingIdentity(ident);
2236 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002237
2238 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002240
satok42c5a162011-05-26 16:46:14 +09002241 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002243 if (!calledFromValidUser()) {
2244 return;
2245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 synchronized (mMethodMap) {
2247 if (mCurClient == null || client == null
2248 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002249 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002250 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 }
2252
satok440aab52010-11-25 09:43:11 +09002253 // Always call subtype picker, because subtype picker is a superset of input method
2254 // picker.
satokab751aa2010-09-14 19:17:36 +09002255 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2256 }
2257 }
2258
satok42c5a162011-05-26 16:46:14 +09002259 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002261 if (!calledFromValidUser()) {
2262 return;
2263 }
satok28203512010-11-24 11:06:49 +09002264 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2265 }
2266
satok42c5a162011-05-26 16:46:14 +09002267 @Override
satok28203512010-11-24 11:06:49 +09002268 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002269 if (!calledFromValidUser()) {
2270 return;
2271 }
satok28203512010-11-24 11:06:49 +09002272 synchronized (mMethodMap) {
2273 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002274 setInputMethodWithSubtypeIdLocked(token, id,
2275 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2276 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002277 } else {
2278 setInputMethod(token, id);
2279 }
2280 }
satokab751aa2010-09-14 19:17:36 +09002281 }
2282
satok42c5a162011-05-26 16:46:14 +09002283 @Override
satokb416a712010-11-25 20:42:14 +09002284 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002285 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002286 if (!calledFromValidUser()) {
2287 return;
2288 }
satokb416a712010-11-25 20:42:14 +09002289 synchronized (mMethodMap) {
2290 if (mCurClient == null || client == null
2291 || mCurClient.client.asBinder() != client.asBinder()) {
2292 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2293 }
satok7fee71f2010-12-17 18:54:26 +09002294 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2295 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002296 }
2297 }
2298
satok4fc87d62011-05-20 16:13:43 +09002299 @Override
satok735cf382010-11-11 20:40:09 +09002300 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002301 if (!calledFromValidUser()) {
2302 return false;
2303 }
satok735cf382010-11-11 20:40:09 +09002304 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002305 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002306 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002307 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002308 lastImi = mMethodMap.get(lastIme.first);
2309 } else {
2310 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002311 }
satok4fc87d62011-05-20 16:13:43 +09002312 String targetLastImiId = null;
2313 int subtypeId = NOT_A_SUBTYPE_ID;
2314 if (lastIme != null && lastImi != null) {
2315 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2316 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2317 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2318 : mCurrentSubtype.hashCode();
2319 // If the last IME is the same as the current IME and the last subtype is not
2320 // defined, there is no need to switch to the last IME.
2321 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2322 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002323 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002324 }
2325 }
2326
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002327 if (TextUtils.isEmpty(targetLastImiId)
2328 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002329 // This is a safety net. If the currentSubtype can't be added to the history
2330 // and the framework couldn't find the last ime, we will make the last ime be
2331 // the most applicable enabled keyboard subtype of the system imes.
2332 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2333 if (enabled != null) {
2334 final int N = enabled.size();
2335 final String locale = mCurrentSubtype == null
2336 ? mRes.getConfiguration().locale.toString()
2337 : mCurrentSubtype.getLocale();
2338 for (int i = 0; i < N; ++i) {
2339 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002340 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002341 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002342 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2343 InputMethodUtils.getSubtypes(imi),
2344 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002345 if (keyboardSubtype != null) {
2346 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002347 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002348 imi, keyboardSubtype.hashCode());
2349 if(keyboardSubtype.getLocale().equals(locale)) {
2350 break;
2351 }
2352 }
2353 }
2354 }
2355 }
2356 }
2357
2358 if (!TextUtils.isEmpty(targetLastImiId)) {
2359 if (DEBUG) {
2360 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2361 + ", from: " + mCurMethodId + ", " + subtypeId);
2362 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002363 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002364 return true;
2365 } else {
2366 return false;
2367 }
satok735cf382010-11-11 20:40:09 +09002368 }
2369 }
2370
satoke7c6998e2011-06-03 17:57:59 +09002371 @Override
satok688bd472012-02-09 20:09:17 +09002372 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002373 if (!calledFromValidUser()) {
2374 return false;
2375 }
satok688bd472012-02-09 20:09:17 +09002376 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002377 if (!calledWithValidToken(token)) {
2378 final int uid = Binder.getCallingUid();
2379 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2380 + " token:" + token);
2381 return false;
2382 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002383 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002384 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2385 if (nextSubtype == null) {
2386 return false;
2387 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002388 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2389 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002390 return true;
2391 }
2392 }
2393
2394 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002395 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2396 if (!calledFromValidUser()) {
2397 return false;
2398 }
2399 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002400 if (!calledWithValidToken(token)) {
2401 final int uid = Binder.getCallingUid();
2402 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2403 + "token. uid:" + uid + " token:" + token);
2404 return false;
2405 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002406 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002407 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2408 if (nextSubtype == null) {
2409 return false;
2410 }
2411 return true;
2412 }
2413 }
2414
2415 @Override
satok68f1b782011-04-11 14:26:04 +09002416 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002417 if (!calledFromValidUser()) {
2418 return null;
2419 }
satok68f1b782011-04-11 14:26:04 +09002420 synchronized (mMethodMap) {
2421 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2422 // TODO: Handle the case of the last IME with no subtypes
2423 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2424 || TextUtils.isEmpty(lastIme.second)) return null;
2425 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2426 if (lastImi == null) return null;
2427 try {
2428 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002429 final int lastSubtypeId =
2430 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002431 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2432 return null;
2433 }
2434 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002435 } catch (NumberFormatException e) {
2436 return null;
2437 }
2438 }
2439 }
2440
satoke7c6998e2011-06-03 17:57:59 +09002441 @Override
satokee5e77c2011-09-02 18:50:15 +09002442 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002443 if (!calledFromValidUser()) {
2444 return;
2445 }
satok91e88122011-07-18 11:11:42 +09002446 // By this IPC call, only a process which shares the same uid with the IME can add
2447 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002448 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002449 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002450 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002451 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002452 final String[] packageInfos;
2453 try {
2454 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2455 } catch (RemoteException e) {
2456 Slog.e(TAG, "Failed to get package infos");
2457 return;
2458 }
satok91e88122011-07-18 11:11:42 +09002459 if (packageInfos != null) {
2460 final int packageNum = packageInfos.length;
2461 for (int i = 0; i < packageNum; ++i) {
2462 if (packageInfos[i].equals(imi.getPackageName())) {
2463 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002464 final long ident = Binder.clearCallingIdentity();
2465 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002466 buildInputMethodListLocked(mMethodList, mMethodMap,
2467 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002468 } finally {
2469 Binder.restoreCallingIdentity(ident);
2470 }
satokee5e77c2011-09-02 18:50:15 +09002471 return;
satok91e88122011-07-18 11:11:42 +09002472 }
2473 }
2474 }
satoke7c6998e2011-06-03 17:57:59 +09002475 }
satokee5e77c2011-09-02 18:50:15 +09002476 return;
satoke7c6998e2011-06-03 17:57:59 +09002477 }
2478
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002479 @Override
2480 public int getInputMethodWindowVisibleHeight() {
2481 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2482 }
2483
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002484 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002485 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002486 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002487 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002488 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002489 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002490 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2491 if (DEBUG) {
2492 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2493 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2494 + " actual: " + sequenceNumber);
2495 }
2496 return;
2497 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002498 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2499 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002500 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002501 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002502 }
2503 }
2504
satok28203512010-11-24 11:06:49 +09002505 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002507 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2508 }
2509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002511 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2512 if (token == null) {
2513 if (mContext.checkCallingOrSelfPermission(
2514 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2515 != PackageManager.PERMISSION_GRANTED) {
2516 throw new SecurityException(
2517 "Using null token requires permission "
2518 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002520 } else if (mCurToken != token) {
2521 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2522 + " token: " + token);
2523 return;
2524 }
2525
2526 final long ident = Binder.clearCallingIdentity();
2527 try {
2528 setInputMethodLocked(id, subtypeId);
2529 } finally {
2530 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
2532 }
2533
satok42c5a162011-05-26 16:46:14 +09002534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 if (!calledFromValidUser()) {
2537 return;
2538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002540 if (!calledWithValidToken(token)) {
2541 final int uid = Binder.getCallingUid();
2542 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2543 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 return;
2545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 long ident = Binder.clearCallingIdentity();
2547 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002548 hideCurrentInputLocked(flags, null);
2549 } finally {
2550 Binder.restoreCallingIdentity(ident);
2551 }
2552 }
2553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002554
satok42c5a162011-05-26 16:46:14 +09002555 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002556 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002557 if (!calledFromValidUser()) {
2558 return;
2559 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002560 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002561 if (!calledWithValidToken(token)) {
2562 final int uid = Binder.getCallingUid();
2563 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2564 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002565 return;
2566 }
2567 long ident = Binder.clearCallingIdentity();
2568 try {
2569 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 } finally {
2571 Binder.restoreCallingIdentity(ident);
2572 }
2573 }
2574 }
2575
2576 void setEnabledSessionInMainThread(SessionState session) {
2577 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002578 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002580 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002581 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 } catch (RemoteException e) {
2583 }
2584 }
2585 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002586 if (mEnabledSession != null && mEnabledSession.session != null) {
2587 try {
2588 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2589 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2590 } catch (RemoteException e) {
2591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593 }
2594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002595
satok42c5a162011-05-26 16:46:14 +09002596 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002598 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 switch (msg.what) {
2600 case MSG_SHOW_IM_PICKER:
2601 showInputMethodMenu();
2602 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002603
satokab751aa2010-09-14 19:17:36 +09002604 case MSG_SHOW_IM_SUBTYPE_PICKER:
2605 showInputMethodSubtypeMenu();
2606 return true;
2607
satok47a44912010-10-06 16:03:58 +09002608 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002609 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002610 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002611 args.recycle();
satok217f5482010-12-15 05:19:19 +09002612 return true;
2613
2614 case MSG_SHOW_IM_CONFIG:
2615 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002616 return true;
2617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 case MSG_UNBIND_INPUT:
2621 try {
2622 ((IInputMethod)msg.obj).unbindInput();
2623 } catch (RemoteException e) {
2624 // There is nothing interesting about the method dying.
2625 }
2626 return true;
2627 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002628 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 try {
2630 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2631 } catch (RemoteException e) {
2632 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002633 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 return true;
2635 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002636 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002638 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002639 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002640 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 } catch (RemoteException e) {
2642 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002643 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 return true;
2645 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002646 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002648 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002649 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002650 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 } catch (RemoteException e) {
2652 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002653 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 return true;
2655 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002656 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002658 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2660 } catch (RemoteException e) {
2661 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002662 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002664 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002665 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002666 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002667 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002669 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002671 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002672 // Dispose the channel if the input method is not local to this process
2673 // because the remote proxy will get its own copy when unparceled.
2674 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002675 channel.dispose();
2676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002678 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002684 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 try {
2686 SessionState session = (SessionState)args.arg1;
2687 setEnabledSessionInMainThread(session);
2688 session.method.startInput((IInputContext)args.arg2,
2689 (EditorInfo)args.arg3);
2690 } catch (RemoteException e) {
2691 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002692 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 return true;
2694 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002695 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 try {
2697 SessionState session = (SessionState)args.arg1;
2698 setEnabledSessionInMainThread(session);
2699 session.method.restartInput((IInputContext)args.arg2,
2700 (EditorInfo)args.arg3);
2701 } catch (RemoteException e) {
2702 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002703 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 case MSG_UNBIND_METHOD:
2709 try {
2710 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2711 } catch (RemoteException e) {
2712 // There is nothing interesting about the last client dying.
2713 }
2714 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002715 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002716 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002717 IInputMethodClient client = (IInputMethodClient)args.arg1;
2718 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002720 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002722 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002723 } finally {
2724 // Dispose the channel if the input method is not local to this process
2725 // because the remote proxy will get its own copy when unparceled.
2726 if (res.channel != null && Binder.isProxy(client)) {
2727 res.channel.dispose();
2728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002730 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002732 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002733 case MSG_SET_ACTIVE:
2734 try {
2735 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2736 } catch (RemoteException e) {
2737 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2738 + ((ClientState)msg.obj).pid + " uid "
2739 + ((ClientState)msg.obj).uid);
2740 }
2741 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002742 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2743 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002744 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002745 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002746 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002747 } catch (RemoteException e) {
2748 Slog.w(TAG, "Got RemoteException sending "
2749 + "setUserActionNotificationSequenceNumber("
2750 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002751 + clientState.pid + " uid "
2752 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002753 }
2754 return true;
2755 }
satok01038492012-04-09 21:08:27 +09002756
2757 // --------------------------------------------------------------
2758 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002759 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002760 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 }
2762 return false;
2763 }
2764
satokdc9ddae2011-10-06 12:22:36 +09002765 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002766 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2767 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002768 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002769 if (DEBUG) {
2770 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2771 }
satok723a27e2010-11-11 14:58:11 +09002772 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002773 return true;
2774 }
2775
2776 return false;
2777 }
2778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002780 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002781 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002782 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002783 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 list.clear();
2786 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002787
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002788 // Use for queryIntentServicesAsUser
2789 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002790 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002791 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002793 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002795 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2796 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
satoke7c6998e2011-06-03 17:57:59 +09002798 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2799 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 for (int i = 0; i < services.size(); ++i) {
2801 ResolveInfo ri = services.get(i);
2802 ServiceInfo si = ri.serviceInfo;
2803 ComponentName compName = new ComponentName(si.packageName, si.name);
2804 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2805 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002806 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 + ": it does not require the permission "
2808 + android.Manifest.permission.BIND_INPUT_METHOD);
2809 continue;
2810 }
2811
Joe Onorato8a9b2202010-02-26 18:56:32 -08002812 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813
2814 try {
satoke7c6998e2011-06-03 17:57:59 +09002815 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002817 final String id = p.getId();
2818 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819
2820 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002821 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002825 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002827 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
2829 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002830
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002831 if (resetDefaultEnabledIme) {
2832 final ArrayList<InputMethodInfo> defaultEnabledIme =
2833 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2834 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2835 final InputMethodInfo imi = defaultEnabledIme.get(i);
2836 if (DEBUG) {
2837 Slog.d(TAG, "--- enable ime = " + imi);
2838 }
2839 setInputMethodEnabledLocked(imi.getId(), true);
2840 }
2841 }
2842
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002843 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002844 if (!TextUtils.isEmpty(defaultImiId)) {
2845 if (!map.containsKey(defaultImiId)) {
2846 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2847 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002848 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002849 }
2850 } else {
2851 // Double check that the default IME is certainly enabled.
2852 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002853 }
2854 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002855 // Here is not the perfect place to reset the switching controller. Ideally
2856 // mSwitchingController and mSettings should be able to share the same state.
2857 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2858 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002859 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002863
satokab751aa2010-09-14 19:17:36 +09002864 private void showInputMethodMenu() {
2865 showInputMethodMenuInternal(false);
2866 }
2867
2868 private void showInputMethodSubtypeMenu() {
2869 showInputMethodMenuInternal(true);
2870 }
2871
satok217f5482010-12-15 05:19:19 +09002872 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002873 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002874 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002875 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2876 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002877 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002878 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002879 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002880 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002881 }
2882
2883 private void showConfigureInputMethods() {
2884 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2886 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2887 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002888 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002889 }
2890
satok2c93efc2012-04-02 19:33:47 +09002891 private boolean isScreenLocked() {
2892 return mKeyguardManager != null
2893 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2894 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002895
satokab751aa2010-09-14 19:17:36 +09002896 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002897 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002900 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002901
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002902 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002903 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002904 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002905
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002906 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002907 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002908 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2909 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002910 if (immis == null || immis.size() == 0) {
2911 return;
2912 }
2913
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002914 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915
satok688bd472012-02-09 20:09:17 +09002916 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002917 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002918 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002919
satokc3690562012-01-10 20:14:43 +09002920 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002921 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002922 if (currentSubtype != null) {
2923 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002924 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2925 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002926 }
2927 }
2928
Ken Wakasa761eb372011-03-04 19:06:18 +09002929 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002930 mIms = new InputMethodInfo[N];
2931 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002932 int checkedItem = 0;
2933 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002934 final ImeSubtypeListItem item = imList.get(i);
2935 mIms[i] = item.mImi;
2936 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002937 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002938 int subtypeId = mSubtypeIds[i];
2939 if ((subtypeId == NOT_A_SUBTYPE_ID)
2940 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2941 || (subtypeId == lastInputMethodSubtypeId)) {
2942 checkedItem = i;
2943 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002946
2947 final Context settingsContext = new ContextThemeWrapper(context,
2948 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2949
2950 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002951 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2952 @Override
2953 public void onCancel(DialogInterface dialog) {
2954 hideInputMethodMenu();
2955 }
2956 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002957
2958 final Context dialogContext = mDialogBuilder.getContext();
2959 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2960 com.android.internal.R.styleable.DialogPreference,
2961 com.android.internal.R.attr.alertDialogStyle, 0);
2962 final Drawable dialogIcon = a.getDrawable(
2963 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2964 a.recycle();
2965
2966 mDialogBuilder.setIcon(dialogIcon);
2967
2968 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2969 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002970 final View tv = inflater.inflate(
2971 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2972 mDialogBuilder.setCustomTitle(tv);
2973
2974 // Setup layout for a toggle switch of the hardware keyboard
2975 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002976 mSwitchingDialogTitleView
2977 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2978 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2979 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002980 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002981 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002982 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002983 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2984 @Override
2985 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002986 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002987 // Ensure that the input method dialog is dismissed when changing
2988 // the hardware keyboard state.
2989 hideInputMethodMenu();
2990 }
2991 });
2992
Alan Viverette505e3ab2014-11-24 15:22:11 -08002993 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002994 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2995 final OnClickListener choiceListener = new OnClickListener() {
2996 @Override
2997 public void onClick(final DialogInterface dialog, final int which) {
2998 synchronized (mMethodMap) {
2999 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3000 || mSubtypeIds.length <= which) {
3001 return;
satok01038492012-04-09 21:08:27 +09003002 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003003 final InputMethodInfo im = mIms[which];
3004 int subtypeId = mSubtypeIds[which];
3005 adapter.mCheckedItem = which;
3006 adapter.notifyDataSetChanged();
3007 hideInputMethodMenu();
3008 if (im != null) {
3009 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3010 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003011 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003012 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003014 }
3015 }
3016 };
3017 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018
satokbc81b692011-08-26 16:22:22 +09003019 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003020 final OnClickListener positiveListener = new OnClickListener() {
3021 @Override
3022 public void onClick(DialogInterface dialog, int whichButton) {
3023 showConfigureInputMethods();
3024 }
3025 };
satok82beadf2010-12-27 19:03:06 +09003026 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003027 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003030 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 mSwitchingDialog.getWindow().setType(
3032 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003033 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3034 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003035 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04003036 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 mSwitchingDialog.show();
3038 }
3039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003040
Ken Wakasa05dbb652011-08-22 15:22:43 +09003041 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3042 private final LayoutInflater mInflater;
3043 private final int mTextViewResourceId;
3044 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003045 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003046 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3047 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3048 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003049
Ken Wakasa05dbb652011-08-22 15:22:43 +09003050 mTextViewResourceId = textViewResourceId;
3051 mItemsList = itemsList;
3052 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003053 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003054 }
3055
3056 @Override
3057 public View getView(int position, View convertView, ViewGroup parent) {
3058 final View view = convertView != null ? convertView
3059 : mInflater.inflate(mTextViewResourceId, null);
3060 if (position < 0 || position >= mItemsList.size()) return view;
3061 final ImeSubtypeListItem item = mItemsList.get(position);
3062 final CharSequence imeName = item.mImeName;
3063 final CharSequence subtypeName = item.mSubtypeName;
3064 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3065 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3066 if (TextUtils.isEmpty(subtypeName)) {
3067 firstTextView.setText(imeName);
3068 secondTextView.setVisibility(View.GONE);
3069 } else {
3070 firstTextView.setText(subtypeName);
3071 secondTextView.setText(imeName);
3072 secondTextView.setVisibility(View.VISIBLE);
3073 }
3074 final RadioButton radioButton =
3075 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3076 radioButton.setChecked(position == mCheckedItem);
3077 return view;
3078 }
3079 }
3080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003082 synchronized (mMethodMap) {
3083 hideInputMethodMenuLocked();
3084 }
3085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003086
The Android Open Source Project10592532009-03-18 17:39:46 -07003087 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003088 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089
The Android Open Source Project10592532009-03-18 17:39:46 -07003090 if (mSwitchingDialog != null) {
3091 mSwitchingDialog.dismiss();
3092 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003094
Jason Monk807ef762014-05-08 15:47:46 -04003095 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07003096 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003097 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003101
satok42c5a162011-05-26 16:46:14 +09003102 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003104 // TODO: Make this work even for non-current users?
3105 if (!calledFromValidUser()) {
3106 return false;
3107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 synchronized (mMethodMap) {
3109 if (mContext.checkCallingOrSelfPermission(
3110 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3111 != PackageManager.PERMISSION_GRANTED) {
3112 throw new SecurityException(
3113 "Requires permission "
3114 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3115 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 long ident = Binder.clearCallingIdentity();
3118 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003119 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 } finally {
3121 Binder.restoreCallingIdentity(ident);
3122 }
3123 }
3124 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003125
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003126 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3127 // Make sure this is a valid input method.
3128 InputMethodInfo imm = mMethodMap.get(id);
3129 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003130 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003131 }
3132
satokd87c2592010-09-29 11:52:06 +09003133 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3134 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003135
satokd87c2592010-09-29 11:52:06 +09003136 if (enabled) {
3137 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3138 if (pair.first.equals(id)) {
3139 // We are enabling this input method, but it is already enabled.
3140 // Nothing to do. The previous state was enabled.
3141 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003142 }
3143 }
satokd87c2592010-09-29 11:52:06 +09003144 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3145 // Previous state was disabled.
3146 return false;
3147 } else {
3148 StringBuilder builder = new StringBuilder();
3149 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3150 builder, enabledInputMethodsList, id)) {
3151 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003152 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003153 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3154 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3155 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003156 }
3157 // Previous state was enabled.
3158 return true;
3159 } else {
3160 // We are disabling the input method but it is already disabled.
3161 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003162 return false;
3163 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003164 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003165 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003166
satok723a27e2010-11-11 14:58:11 +09003167 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3168 boolean setSubtypeOnly) {
3169 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003170 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003171
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003172 mCurUserActionNotificationSequenceNumber =
3173 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3174 if (DEBUG) {
3175 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3176 + mCurUserActionNotificationSequenceNumber);
3177 }
3178
3179 if (mCurClient != null && mCurClient.client != null) {
3180 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3181 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003182 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003183 }
3184
satok723a27e2010-11-11 14:58:11 +09003185 // Set Subtype here
3186 if (imi == null || subtypeId < 0) {
3187 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003188 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003189 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003190 if (subtypeId < imi.getSubtypeCount()) {
3191 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3192 mSettings.putSelectedSubtype(subtype.hashCode());
3193 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003194 } else {
3195 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003196 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003197 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003198 }
satokab751aa2010-09-14 19:17:36 +09003199 }
satok723a27e2010-11-11 14:58:11 +09003200
satok4c0e7152012-06-20 20:08:44 +09003201 // Workaround.
3202 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3203 // IMEs are not recognized and considered uninstalled.
3204 // Actually, we can't move everything after SystemReady because
3205 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3206 // the default IME here and try cheking the default IME again in systemReady().
3207 // TODO: Do nothing before system ready and implement a separated logic for
3208 // the encryption lock screen.
3209 // TODO: ASEC should be ready before IMMS is instantiated.
3210 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003211 // Set InputMethod here
3212 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3213 }
3214 }
3215
3216 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3217 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3218 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3219 // newDefaultIme is empty when there is no candidate for the selected IME.
3220 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3221 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3222 if (subtypeHashCode != null) {
3223 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003224 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003225 imi, Integer.valueOf(subtypeHashCode));
3226 } catch (NumberFormatException e) {
3227 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3228 }
3229 }
3230 }
3231 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003232 }
3233
satok4e4569d2010-11-19 18:45:53 +09003234 // If there are no selected shortcuts, tries finding the most applicable ones.
3235 private Pair<InputMethodInfo, InputMethodSubtype>
3236 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3237 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3238 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003239 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003240 boolean foundInSystemIME = false;
3241
3242 // Search applicable subtype for each InputMethodInfo
3243 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003244 final String imiId = imi.getId();
3245 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3246 continue;
3247 }
satokcd7cd292010-11-20 15:46:23 +09003248 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003249 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003250 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003251 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003252 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003253 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003254 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003255 }
satokdf31ae62011-01-15 06:19:44 +09003256 // 2. Search by the system locale from enabledSubtypes.
3257 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003258 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003259 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003260 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003261 }
satoka86f5e42011-09-02 17:12:42 +09003262 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003263 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003264 final ArrayList<InputMethodSubtype> subtypesForSearch =
3265 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003266 ? InputMethodUtils.getSubtypes(imi)
3267 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003268 // 4. Search by the current subtype's locale from all subtypes.
3269 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003270 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003271 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003272 }
3273 // 5. Search by the system locale from all subtypes.
3274 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003275 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003276 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003277 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003278 }
satokcd7cd292010-11-20 15:46:23 +09003279 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003280 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003281 // The current input method is the most applicable IME.
3282 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003283 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003284 break;
satok7599a7f2010-12-22 13:45:23 +09003285 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003286 // The system input method is 2nd applicable IME.
3287 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003288 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003289 if ((imi.getServiceInfo().applicationInfo.flags
3290 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3291 foundInSystemIME = true;
3292 }
satok4e4569d2010-11-19 18:45:53 +09003293 }
3294 }
3295 }
3296 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003297 if (mostApplicableIMI != null) {
3298 Slog.w(TAG, "Most applicable shortcut input method was:"
3299 + mostApplicableIMI.getId());
3300 if (mostApplicableSubtype != null) {
3301 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3302 + "," + mostApplicableSubtype.getMode() + ","
3303 + mostApplicableSubtype.getLocale());
3304 }
3305 }
satok4e4569d2010-11-19 18:45:53 +09003306 }
satokcd7cd292010-11-20 15:46:23 +09003307 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003308 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003309 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003310 } else {
3311 return null;
3312 }
3313 }
3314
satokab751aa2010-09-14 19:17:36 +09003315 /**
3316 * @return Return the current subtype of this input method.
3317 */
satok42c5a162011-05-26 16:46:14 +09003318 @Override
satokab751aa2010-09-14 19:17:36 +09003319 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003320 // TODO: Make this work even for non-current users?
3321 if (!calledFromValidUser()) {
3322 return null;
3323 }
3324 synchronized (mMethodMap) {
3325 return getCurrentInputMethodSubtypeLocked();
3326 }
3327 }
3328
3329 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003330 if (mCurMethodId == null) {
3331 return null;
3332 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003333 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003334 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3335 if (imi == null || imi.getSubtypeCount() == 0) {
3336 return null;
satok4e4569d2010-11-19 18:45:53 +09003337 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003338 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003339 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3340 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003341 if (subtypeId == NOT_A_SUBTYPE_ID) {
3342 // If there are no selected subtypes, the framework will try to find
3343 // the most applicable subtype from explicitly or implicitly enabled
3344 // subtypes.
3345 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003346 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003347 // If there is only one explicitly or implicitly enabled subtype,
3348 // just returns it.
3349 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3350 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3351 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003352 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003353 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003354 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003355 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003356 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003357 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3358 true);
satok4e4569d2010-11-19 18:45:53 +09003359 }
satok3ef8b292010-11-23 06:06:29 +09003360 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003361 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003362 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003363 }
3364 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003365 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003366 }
3367
satokf3db1af2010-11-23 13:34:33 +09003368 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3369 InputMethodSubtype subtype) {
3370 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3371 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3372 } else {
3373 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3374 subtypes.add(subtype);
3375 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3376 }
3377 }
3378
satok4e4569d2010-11-19 18:45:53 +09003379 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003380 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003381 @Override
satok4e4569d2010-11-19 18:45:53 +09003382 public List getShortcutInputMethodsAndSubtypes() {
3383 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003384 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003385 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003386 // If there are no selected shortcut subtypes, the framework will try to find
3387 // the most applicable subtype from all subtypes whose mode is
3388 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003389 Pair<InputMethodInfo, InputMethodSubtype> info =
3390 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003391 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003392 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003393 ret.add(info.first);
3394 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003395 }
satok3da92232011-01-11 22:46:30 +09003396 return ret;
satokf3db1af2010-11-23 13:34:33 +09003397 }
satokf3db1af2010-11-23 13:34:33 +09003398 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3399 ret.add(imi);
3400 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3401 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003402 }
3403 }
satokf3db1af2010-11-23 13:34:33 +09003404 return ret;
satok4e4569d2010-11-19 18:45:53 +09003405 }
3406 }
3407
satok42c5a162011-05-26 16:46:14 +09003408 @Override
satokb66d2872010-11-10 01:04:04 +09003409 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003410 // TODO: Make this work even for non-current users?
3411 if (!calledFromValidUser()) {
3412 return false;
3413 }
satokb66d2872010-11-10 01:04:04 +09003414 synchronized (mMethodMap) {
3415 if (subtype != null && mCurMethodId != null) {
3416 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003417 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003418 if (subtypeId != NOT_A_SUBTYPE_ID) {
3419 setInputMethodLocked(mCurMethodId, subtypeId);
3420 return true;
3421 }
3422 }
3423 return false;
3424 }
3425 }
3426
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003427 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003428 private static class InputMethodFileManager {
3429 private static final String SYSTEM_PATH = "system";
3430 private static final String INPUT_METHOD_PATH = "inputmethod";
3431 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3432 private static final String NODE_SUBTYPES = "subtypes";
3433 private static final String NODE_SUBTYPE = "subtype";
3434 private static final String NODE_IMI = "imi";
3435 private static final String ATTR_ID = "id";
3436 private static final String ATTR_LABEL = "label";
3437 private static final String ATTR_ICON = "icon";
3438 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3439 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3440 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3441 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3442 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3443 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003444 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003445 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003446 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003447 if (methodMap == null) {
3448 throw new NullPointerException("methodMap is null");
3449 }
3450 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003451 final File systemDir = userId == UserHandle.USER_OWNER
3452 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3453 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003454 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3455 if (!inputMethodDir.mkdirs()) {
3456 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3457 }
3458 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3459 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3460 if (!subtypeFile.exists()) {
3461 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003462 writeAdditionalInputMethodSubtypes(
3463 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003464 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003465 readAdditionalInputMethodSubtypes(
3466 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003467 }
3468 }
3469
3470 private void deleteAllInputMethodSubtypes(String imiId) {
3471 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003472 mAdditionalSubtypesMap.remove(imiId);
3473 writeAdditionalInputMethodSubtypes(
3474 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003475 }
3476 }
3477
3478 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003479 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003480 synchronized (mMethodMap) {
3481 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3482 final int N = additionalSubtypes.length;
3483 for (int i = 0; i < N; ++i) {
3484 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003485 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003486 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003487 } else {
3488 Slog.w(TAG, "Duplicated subtype definition found: "
3489 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003490 }
3491 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003492 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3493 writeAdditionalInputMethodSubtypes(
3494 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003495 }
3496 }
3497
3498 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3499 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003500 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003501 }
3502 }
3503
3504 private static void writeAdditionalInputMethodSubtypes(
3505 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3506 HashMap<String, InputMethodInfo> methodMap) {
3507 // Safety net for the case that this function is called before methodMap is set.
3508 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3509 FileOutputStream fos = null;
3510 try {
3511 fos = subtypesFile.startWrite();
3512 final XmlSerializer out = new FastXmlSerializer();
3513 out.setOutput(fos, "utf-8");
3514 out.startDocument(null, true);
3515 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3516 out.startTag(null, NODE_SUBTYPES);
3517 for (String imiId : allSubtypes.keySet()) {
3518 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3519 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3520 continue;
3521 }
3522 out.startTag(null, NODE_IMI);
3523 out.attribute(null, ATTR_ID, imiId);
3524 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3525 final int N = subtypesList.size();
3526 for (int i = 0; i < N; ++i) {
3527 final InputMethodSubtype subtype = subtypesList.get(i);
3528 out.startTag(null, NODE_SUBTYPE);
3529 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3530 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3531 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3532 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3533 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3534 out.attribute(null, ATTR_IS_AUXILIARY,
3535 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3536 out.endTag(null, NODE_SUBTYPE);
3537 }
3538 out.endTag(null, NODE_IMI);
3539 }
3540 out.endTag(null, NODE_SUBTYPES);
3541 out.endDocument();
3542 subtypesFile.finishWrite(fos);
3543 } catch (java.io.IOException e) {
3544 Slog.w(TAG, "Error writing subtypes", e);
3545 if (fos != null) {
3546 subtypesFile.failWrite(fos);
3547 }
3548 }
3549 }
3550
3551 private static void readAdditionalInputMethodSubtypes(
3552 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3553 if (allSubtypes == null || subtypesFile == null) return;
3554 allSubtypes.clear();
3555 FileInputStream fis = null;
3556 try {
3557 fis = subtypesFile.openRead();
3558 final XmlPullParser parser = Xml.newPullParser();
3559 parser.setInput(fis, null);
3560 int type = parser.getEventType();
3561 // Skip parsing until START_TAG
3562 while ((type = parser.next()) != XmlPullParser.START_TAG
3563 && type != XmlPullParser.END_DOCUMENT) {}
3564 String firstNodeName = parser.getName();
3565 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3566 throw new XmlPullParserException("Xml doesn't start with subtypes");
3567 }
3568 final int depth =parser.getDepth();
3569 String currentImiId = null;
3570 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3571 while (((type = parser.next()) != XmlPullParser.END_TAG
3572 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3573 if (type != XmlPullParser.START_TAG)
3574 continue;
3575 final String nodeName = parser.getName();
3576 if (NODE_IMI.equals(nodeName)) {
3577 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3578 if (TextUtils.isEmpty(currentImiId)) {
3579 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3580 continue;
3581 }
3582 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3583 allSubtypes.put(currentImiId, tempSubtypesArray);
3584 } else if (NODE_SUBTYPE.equals(nodeName)) {
3585 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3586 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3587 continue;
3588 }
3589 final int icon = Integer.valueOf(
3590 parser.getAttributeValue(null, ATTR_ICON));
3591 final int label = Integer.valueOf(
3592 parser.getAttributeValue(null, ATTR_LABEL));
3593 final String imeSubtypeLocale =
3594 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3595 final String imeSubtypeMode =
3596 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3597 final String imeSubtypeExtraValue =
3598 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003599 final boolean isAuxiliary = "1".equals(String.valueOf(
3600 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003601 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3602 .setSubtypeNameResId(label)
3603 .setSubtypeIconResId(icon)
3604 .setSubtypeLocale(imeSubtypeLocale)
3605 .setSubtypeMode(imeSubtypeMode)
3606 .setSubtypeExtraValue(imeSubtypeExtraValue)
3607 .setIsAuxiliary(isAuxiliary)
3608 .build();
satoke7c6998e2011-06-03 17:57:59 +09003609 tempSubtypesArray.add(subtype);
3610 }
3611 }
3612 } catch (XmlPullParserException e) {
3613 Slog.w(TAG, "Error reading subtypes: " + e);
3614 return;
3615 } catch (java.io.IOException e) {
3616 Slog.w(TAG, "Error reading subtypes: " + e);
3617 return;
3618 } catch (NumberFormatException e) {
3619 Slog.w(TAG, "Error reading subtypes: " + e);
3620 return;
3621 } finally {
3622 if (fis != null) {
3623 try {
3624 fis.close();
3625 } catch (java.io.IOException e1) {
3626 Slog.w(TAG, "Failed to close.");
3627 }
3628 }
3629 }
3630 }
3631 }
3632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 @Override
3634 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3635 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3636 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3639 + Binder.getCallingPid()
3640 + ", uid=" + Binder.getCallingUid());
3641 return;
3642 }
3643
3644 IInputMethod method;
3645 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 synchronized (mMethodMap) {
3650 p.println("Current Input Method Manager state:");
3651 int N = mMethodList.size();
3652 p.println(" Input Methods:");
3653 for (int i=0; i<N; i++) {
3654 InputMethodInfo info = mMethodList.get(i);
3655 p.println(" InputMethod #" + i + ":");
3656 info.dump(p, " ");
3657 }
3658 p.println(" Clients:");
3659 for (ClientState ci : mClients.values()) {
3660 p.println(" Client " + ci + ":");
3661 p.println(" client=" + ci.client);
3662 p.println(" inputContext=" + ci.inputContext);
3663 p.println(" sessionRequested=" + ci.sessionRequested);
3664 p.println(" curSession=" + ci.curSession);
3665 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003666 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003668 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3669 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3671 + " mBoundToMethod=" + mBoundToMethod);
3672 p.println(" mCurToken=" + mCurToken);
3673 p.println(" mCurIntent=" + mCurIntent);
3674 method = mCurMethod;
3675 p.println(" mCurMethod=" + mCurMethod);
3676 p.println(" mEnabledSession=" + mEnabledSession);
3677 p.println(" mShowRequested=" + mShowRequested
3678 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3679 + " mShowForced=" + mShowForced
3680 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003681 p.println(" mCurUserActionNotificationSequenceNumber="
3682 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003683 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003685
Jeff Brownb88102f2010-09-08 11:49:43 -07003686 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 pw.flush();
3689 try {
3690 client.client.asBinder().dump(fd, args);
3691 } catch (RemoteException e) {
3692 p.println("Input method client dead: " + e);
3693 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003694 } else {
3695 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003697
Jeff Brownb88102f2010-09-08 11:49:43 -07003698 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 pw.flush();
3701 try {
3702 method.asBinder().dump(fd, args);
3703 } catch (RemoteException e) {
3704 p.println("Input method service dead: " + e);
3705 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003706 } else {
3707 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 }
3709 }
3710}