blob: 060c8e30212f701b580eb1e0e5b9236bf3f9271d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090054import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090056import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070058import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090059import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.pm.PackageManager;
61import android.content.pm.ResolveInfo;
62import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080063import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.res.Resources;
66import android.content.res.TypedArray;
67import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080068import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070069import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090071import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Handler;
73import android.os.IBinder;
74import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Parcel;
79import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080080import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.ServiceManager;
82import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090083import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080084import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.provider.Settings;
86import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090087import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070088import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090090import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090091import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.util.PrintWriterPrinter;
93import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090094import android.util.Slog;
95import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090096import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070098import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090099import android.view.LayoutInflater;
100import android.view.View;
101import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900103import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.inputmethod.InputBinding;
105import android.view.inputmethod.InputMethod;
106import android.view.inputmethod.InputMethodInfo;
107import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900108import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900109import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900110import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900111import android.widget.CompoundButton;
112import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900113import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900114import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900115import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
satoke7c6998e2011-06-03 17:57:59 +0900117import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900119import java.io.FileInputStream;
120import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.io.IOException;
122import java.io.PrintWriter;
123import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900124import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import java.util.HashMap;
126import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900127import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129/**
130 * This class provides a system service that manages input methods.
131 */
132public class InputMethodManagerService extends IInputMethodManager.Stub
133 implements ServiceConnection, Handler.Callback {
134 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700135 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900138 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900139 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900140 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final int MSG_UNBIND_INPUT = 1000;
143 static final int MSG_BIND_INPUT = 1010;
144 static final int MSG_SHOW_SOFT_INPUT = 1020;
145 static final int MSG_HIDE_SOFT_INPUT = 1030;
146 static final int MSG_ATTACH_TOKEN = 1040;
147 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 static final int MSG_START_INPUT = 2000;
150 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int MSG_UNBIND_METHOD = 3000;
153 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700154 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900155 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
satok01038492012-04-09 21:08:27 +0900157 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
158
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700159 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
satokf9f01002011-05-19 21:31:50 +0900161 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
162
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900163 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900164 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900165
satok4e4569d2010-11-19 18:45:53 +0900166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800168 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900170 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final IWindowManager mIWindowManager;
173 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700174 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900175 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900176 private final HardKeyboardListener mHardKeyboardListener;
177 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900179 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // All known input methods. mMethodMap also serves as the global
182 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900183 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
184 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900185 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
186 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900187 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700189 // Used to bring IME service up to visible adjustment while it is being shown.
190 final ServiceConnection mVisibleConnection = new ServiceConnection() {
191 @Override public void onServiceConnected(ComponentName name, IBinder service) {
192 }
193
194 @Override public void onServiceDisconnected(ComponentName name) {
195 }
196 };
197 boolean mVisibleBound = false;
198
satok7cfc0ed2011-06-20 21:29:36 +0900199 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700200 private NotificationManager mNotificationManager;
201 private KeyguardManager mKeyguardManager;
202 private StatusBarManagerService mStatusBar;
203 private Notification mImeSwitcherNotification;
204 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900205 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900206 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900207 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900208
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900209 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final ClientState client;
211 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700212
213 IInputMethodSession session;
214 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 @Override
217 public String toString() {
218 return "SessionState{uid " + client.uid + " pid " + client.pid
219 + " method " + Integer.toHexString(
220 System.identityHashCode(method))
221 + " session " + Integer.toHexString(
222 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700223 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 + "}";
225 }
226
227 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700228 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 client = _client;
230 method = _method;
231 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700232 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
Jeff Brownc28867a2013-03-26 15:42:39 -0700236 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 final IInputMethodClient client;
238 final IInputContext inputContext;
239 final int uid;
240 final int pid;
241 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 boolean sessionRequested;
244 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 @Override
247 public String toString() {
248 return "ClientState{" + Integer.toHexString(
249 System.identityHashCode(this)) + " uid " + uid
250 + " pid " + pid + "}";
251 }
252
253 ClientState(IInputMethodClient _client, IInputContext _inputContext,
254 int _uid, int _pid) {
255 client = _client;
256 inputContext = _inputContext;
257 uid = _uid;
258 pid = _pid;
259 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
260 }
261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 final HashMap<IBinder, ClientState> mClients
264 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700267 * Set once the system is ready to run third party code.
268 */
269 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * Id of the currently selected input method.
273 */
274 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * The current binding sequence number, incremented every time there is
278 * a new bind performed.
279 */
280 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * The client that is currently bound to an input method.
284 */
285 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700288 * The last window token that gained focus.
289 */
290 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 * The input context last provided by the current client.
294 */
295 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The attributes last provided by the current client.
299 */
300 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * The input method ID of the input method service that we are currently
304 * connected to or in the process of connecting to.
305 */
306 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
satokab751aa2010-09-14 19:17:36 +0900309 * The current subtype of the current input method.
310 */
311 private InputMethodSubtype mCurrentSubtype;
312
satok4e4569d2010-11-19 18:45:53 +0900313 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900314 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
315 mShortcutInputMethodsAndSubtypes =
316 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900317
John Spurlocke0980502013-10-25 11:59:29 -0400318 // Was the keyguard locked when this client became current?
319 private boolean mCurClientInKeyguard;
320
satokab751aa2010-09-14 19:17:36 +0900321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 * Set to true if our ServiceConnection is currently actively bound to
323 * a service (whether or not we have gotten its IBinder back yet).
324 */
325 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if the client has asked for the input method to be shown.
329 */
330 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Set if we were explicitly told to show the input method.
334 */
335 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * Set if we were forced to be shown.
339 */
340 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Set if we last told the input method to show itself.
344 */
345 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * The Intent used to connect to the current input method.
349 */
350 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * The token we have made for the currently active input method, to
354 * identify it in the future.
355 */
356 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * If non-null, this is the input method service we are currently connected
360 * to.
361 */
362 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * Time that we last initiated a bind to the input method, to determine
366 * if we should try to disconnect and reconnect to it.
367 */
368 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * Have we called mCurMethod.bindInput()?
372 */
373 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * Currently enabled session. Only touched by service thread, not
377 * protected by a lock.
378 */
379 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * True if the screen is on. The value is true initially.
383 */
384 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900386 int mCurUserActionNotificationSequenceNumber = 0;
387
Joe Onorato857fd9b2011-01-27 15:08:35 -0800388 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
389 int mImeWindowVis;
390
Ken Wakasa05dbb652011-08-22 15:22:43 +0900391 private AlertDialog.Builder mDialogBuilder;
392 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900393 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900394 private InputMethodInfo[] mIms;
395 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900396 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700397 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900398 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
399 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800402 String mLastEnabled = "";
403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 SettingsObserver(Handler handler) {
405 super(handler);
406 ContentResolver resolver = mContext.getContentResolver();
407 resolver.registerContentObserver(Settings.Secure.getUriFor(
408 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900409 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900410 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
411 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900412 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700413 resolver.registerContentObserver(Settings.Secure.getUriFor(
414 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
Michael Wright7b5a96b2014-08-09 19:28:42 -0700417 @Override public void onChange(boolean selfChange, Uri uri) {
418 final Uri showImeUri =
419 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700421 if (showImeUri.equals(uri)) {
422 updateKeyboardFromSettingsLocked();
423 } else {
424 boolean enabledChanged = false;
425 String newEnabled = mSettings.getEnabledInputMethodsStr();
426 if (!mLastEnabled.equals(newEnabled)) {
427 mLastEnabled = newEnabled;
428 enabledChanged = true;
429 }
430 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433 }
434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900436 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
437 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700439 if (mCurClient != null && mCurClient.client != null) {
440 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
441 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900444
445 @Override
446 public void onReceive(Context context, Intent intent) {
447 final String action = intent.getAction();
448 if (Intent.ACTION_SCREEN_ON.equals(action)) {
449 mScreenOn = true;
450 refreshImeWindowVisibilityLocked();
451 updateActive();
452 return;
453 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
454 mScreenOn = false;
455 setImeWindowVisibilityStatusHiddenLocked();
456 updateActive();
457 return;
458 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
459 hideInputMethodMenu();
460 // No need to updateActive
461 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800462 } else if (Intent.ACTION_USER_ADDED.equals(action)
463 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100464 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800465 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900466 } else {
467 Slog.w(TAG, "Unexpected intent " + intent);
468 }
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900473 private boolean isChangingPackagesOfCurrentUser() {
474 final int userId = getChangingUserId();
475 final boolean retval = userId == mSettings.getCurrentUserId();
476 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900477 if (!retval) {
478 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
479 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900480 }
481 return retval;
482 }
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800485 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900486 if (!isChangingPackagesOfCurrentUser()) {
487 return false;
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 final int N = mMethodList.size();
492 if (curInputMethodId != null) {
493 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 InputMethodInfo imi = mMethodList.get(i);
495 if (imi.getId().equals(curInputMethodId)) {
496 for (String pkg : packages) {
497 if (imi.getPackageName().equals(pkg)) {
498 if (!doit) {
499 return true;
500 }
satok723a27e2010-11-11 14:58:11 +0900501 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 chooseNewDefaultIMELocked();
503 return true;
504 }
505 }
506 }
507 }
508 }
509 }
510 return false;
511 }
512
513 @Override
514 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900515 if (!isChangingPackagesOfCurrentUser()) {
516 return;
517 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 synchronized (mMethodMap) {
519 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900520 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 final int N = mMethodList.size();
522 if (curInputMethodId != null) {
523 for (int i=0; i<N; i++) {
524 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900525 final String imiId = imi.getId();
526 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800527 curIm = imi;
528 }
satoke7c6998e2011-06-03 17:57:59 +0900529
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900531 if (isPackageModified(imi.getPackageName())) {
532 mFileManager.deleteAllInputMethodSubtypes(imiId);
533 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 if (change == PACKAGE_TEMPORARY_CHANGE
535 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800537 + imi.getComponent());
538 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 }
541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800542
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900543 buildInputMethodListLocked(
544 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800549 int change = isPackageDisappearing(curIm.getPackageName());
550 if (change == PACKAGE_TEMPORARY_CHANGE
551 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800552 ServiceInfo si = null;
553 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900554 si = mIPackageManager.getServiceInfo(
555 curIm.getComponent(), 0, mSettings.getCurrentUserId());
556 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800557 }
558 if (si == null) {
559 // Uh oh, current input method is no longer around!
560 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900562 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800563 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800564 changed = true;
565 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900567 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800568 }
569 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800570 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800571 }
satokab751aa2010-09-14 19:17:36 +0900572
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800573 if (curIm == null) {
574 // We currently don't have a default input method... is
575 // one now available?
576 changed = chooseNewDefaultIMELocked();
577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800578
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800579 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800580 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 }
583 }
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
Jeff Brownc28867a2013-03-26 15:42:39 -0700586 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900587 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700588 private final IInputMethod mMethod;
589 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800590
Jeff Brownc28867a2013-03-26 15:42:39 -0700591 MethodCallback(InputMethodManagerService imms, IInputMethod method,
592 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900593 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700594 mMethod = method;
595 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
satoke7c6998e2011-06-03 17:57:59 +0900598 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700599 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700600 long ident = Binder.clearCallingIdentity();
601 try {
602 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
603 } finally {
604 Binder.restoreCallingIdentity(ident);
605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800608
satok01038492012-04-09 21:08:27 +0900609 private class HardKeyboardListener
610 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
611 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700612 public void onHardKeyboardStatusChange(boolean available) {
613 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
614 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900615 }
616
Michael Wright7b5a96b2014-08-09 19:28:42 -0700617 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900618 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700619 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900620 }
621 synchronized(mMethodMap) {
622 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
623 && mSwitchingDialog.isShowing()) {
624 mSwitchingDialogTitleView.findViewById(
625 com.android.internal.R.id.hard_keyboard_section).setVisibility(
626 available ? View.VISIBLE : View.GONE);
627 }
628 }
629 }
630 }
631
632 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800635 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mHandler = new Handler(this);
637 mIWindowManager = IWindowManager.Stub.asInterface(
638 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800639 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900640 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public void executeMessage(Message msg) {
642 handleMessage(msg);
643 }
Mita Yuned218c72012-12-06 17:18:25 -0800644 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900645 mWindowManagerService = windowManager;
646 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700647 mHasFeature = context.getPackageManager().hasSystemFeature(
648 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900649
satok7cfc0ed2011-06-20 21:29:36 +0900650 mImeSwitcherNotification = new Notification();
651 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
652 mImeSwitcherNotification.when = 0;
653 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
654 mImeSwitcherNotification.tickerText = null;
655 mImeSwitcherNotification.defaults = 0; // please be quiet
656 mImeSwitcherNotification.sound = null;
657 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400658
659 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
661 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400662
satok7cfc0ed2011-06-20 21:29:36 +0900663 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900664 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900665
666 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900667
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900668 final IntentFilter broadcastFilter = new IntentFilter();
669 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
670 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
671 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800672 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
673 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
satok7cfc0ed2011-06-20 21:29:36 +0900676 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900677 int userId = 0;
678 try {
679 ActivityManagerNative.getDefault().registerUserSwitchObserver(
680 new IUserSwitchObserver.Stub() {
681 @Override
682 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900683 synchronized(mMethodMap) {
684 switchUserLocked(newUserId);
685 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900686 if (reply != null) {
687 try {
688 reply.sendResult(null);
689 } catch (RemoteException e) {
690 }
691 }
692 }
693
694 @Override
695 public void onUserSwitchComplete(int newUserId) throws RemoteException {
696 }
697 });
698 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
699 } catch (RemoteException e) {
700 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
701 }
satok81f8b7c2012-12-04 20:42:56 +0900702 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900703
satokd87c2592010-09-29 11:52:06 +0900704 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900705 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900706 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100707 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900708 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900709 synchronized (mMethodMap) {
710 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
711 mSettings, context);
712 }
satok0a1bcf42012-05-16 19:26:31 +0900713
714 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900715 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900716 if (DEBUG) {
717 Slog.d(TAG, "Initial default ime = " + defaultImiId);
718 }
satok0a1bcf42012-05-16 19:26:31 +0900719 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
720
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900721 synchronized (mMethodMap) {
722 buildInputMethodListLocked(mMethodList, mMethodMap,
723 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
724 }
satokd87c2592010-09-29 11:52:06 +0900725 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
satok0a1bcf42012-05-16 19:26:31 +0900727 if (!mImeSelectedOnBoot) {
728 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900729 synchronized (mMethodMap) {
730 resetDefaultImeLocked(context);
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900735 synchronized (mMethodMap) {
736 updateFromSettingsLocked(true);
737 }
satok5b927c432012-05-01 20:09:34 +0900738
739 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
740 // according to the new system locale.
741 final IntentFilter filter = new IntentFilter();
742 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
743 mContext.registerReceiver(
744 new BroadcastReceiver() {
745 @Override
746 public void onReceive(Context context, Intent intent) {
747 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900748 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900749 }
750 }
751 }, filter);
752 }
753
satok5b927c432012-05-01 20:09:34 +0900754 private void resetDefaultImeLocked(Context context) {
755 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900756 if (mCurMethodId != null
757 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900758 return;
759 }
760
761 InputMethodInfo defIm = null;
762 for (InputMethodInfo imi : mMethodList) {
763 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900764 if (InputMethodUtils.isValidSystemDefaultIme(
765 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900766 defIm = imi;
767 Slog.i(TAG, "Selected default: " + imi.getId());
768 }
769 }
770 }
771 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900772 defIm = InputMethodUtils.getMostApplicableDefaultIME(
773 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900774 Slog.i(TAG, "No default found, using " + defIm.getId());
775 }
776 if (defIm != null) {
777 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
778 }
779 }
780
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900781 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
782 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900783 if (!mSystemReady) {
784 // not system ready
785 return;
786 }
787 final Locale newLocale = mRes.getConfiguration().locale;
788 if (!updateOnlyWhenLocaleChanged
789 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
790 if (!updateOnlyWhenLocaleChanged) {
791 hideCurrentInputLocked(0, null);
792 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700793 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900794 }
795 if (DEBUG) {
796 Slog.i(TAG, "Locale has been changed to " + newLocale);
797 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900798 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 if (!updateOnlyWhenLocaleChanged) {
800 final String selectedImiId = mSettings.getSelectedInputMethod();
801 if (TextUtils.isEmpty(selectedImiId)) {
802 // This is the first time of the user switch and
803 // set the current ime to the proper one.
804 resetDefaultImeLocked(mContext);
805 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900806 } else {
807 // If the locale is changed, needs to reset the default ime
808 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800810 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 mLastSystemLocale = newLocale;
812 if (!updateOnlyWhenLocaleChanged) {
813 try {
814 startInputInnerLocked();
815 } catch (RuntimeException e) {
816 Slog.w(TAG, "Unexpected exception", e);
817 }
818 }
819 }
820 }
821
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900822 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900823 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
824 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900825 }
826
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900827 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900828 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100829 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900830 // InputMethodFileManager should be reset when the user is changed
831 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900832 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900833 // For secondary users, the list of enabled IMEs may not have been updated since the
834 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
835 // not be empty even if the IME has been uninstalled by the primary user.
836 // Even in such cases, IMMS works fine because it will find the most applicable
837 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900838 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900839 if (DEBUG) {
840 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
841 }
842 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900843 initialUserSwitch /* needsToResetDefaultIme */);
844 if (initialUserSwitch) {
845 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
846 mSettings.getEnabledInputMethodListLocked());
847 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900848 }
849
Kenny Guy2a764942014-04-02 13:29:20 +0100850 void updateCurrentProfileIds() {
851 List<UserInfo> profiles =
852 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
853 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
854 for (int i = 0; i < currentProfileIds.length; i++) {
855 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800856 }
Kenny Guy2a764942014-04-02 13:29:20 +0100857 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800858 }
859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 @Override
861 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
862 throws RemoteException {
863 try {
864 return super.onTransact(code, data, reply, flags);
865 } catch (RuntimeException e) {
866 // The input method manager only throws security exceptions, so let's
867 // log all others.
868 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700869 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871 throw e;
872 }
873 }
874
Svetoslav Ganova0027152013-06-25 14:59:53 -0700875 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700876 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900877 if (DEBUG) {
878 Slog.d(TAG, "--- systemReady");
879 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700880 if (!mSystemReady) {
881 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900882 mKeyguardManager =
883 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700884 mNotificationManager = (NotificationManager)
885 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
886 mStatusBar = statusBar;
887 statusBar.setIconVisibility("ime", false);
888 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900889 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
890 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900891 if (mShowOngoingImeSwitcherForPhones) {
892 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
893 mHardKeyboardListener);
894 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900895 buildInputMethodListLocked(mMethodList, mMethodMap,
896 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900897 if (!mImeSelectedOnBoot) {
898 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900899 resetStateIfCurrentLocaleChangedLocked();
900 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
901 mContext.getPackageManager(),
902 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900903 }
904 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700905 try {
906 startInputInnerLocked();
907 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800908 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700909 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700910 }
911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
satok15452a42011-10-28 17:58:28 +0900914 private void setImeWindowVisibilityStatusHiddenLocked() {
915 mImeWindowVis = 0;
916 updateImeWindowStatusLocked();
917 }
918
satok3afd6c02011-11-18 08:38:19 +0900919 private void refreshImeWindowVisibilityLocked() {
920 final Configuration conf = mRes.getConfiguration();
921 final boolean haveHardKeyboard = conf.keyboard
922 != Configuration.KEYBOARD_NOKEYS;
923 final boolean hardKeyShown = haveHardKeyboard
924 && conf.hardKeyboardHidden
925 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400926
927 final boolean isScreenLocked = isKeyguardLocked();
928 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400929 // We assume the softkeyboard is shown when the input is active as long as the
930 // hard keyboard is not shown.
931 final boolean inputVisible = inputActive && !hardKeyShown;
932 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
933 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900934 updateImeWindowStatusLocked();
935 }
936
satok15452a42011-10-28 17:58:28 +0900937 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900938 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700939 }
940
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900941 // ---------------------------------------------------------------------------------------
942 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
943 // 1) it comes from the system process
944 // 2) the calling process' user id is identical to the current user id IMMS thinks.
945 private boolean calledFromValidUser() {
946 final int uid = Binder.getCallingUid();
947 final int userId = UserHandle.getUserId(uid);
948 if (DEBUG) {
949 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
950 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
951 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900952 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
953 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
Kenny Guy2a764942014-04-02 13:29:20 +0100955 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900958
959 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
960 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
961 // must not manage background users' states in any functions.
962 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
963 // by a token.
964 if (mContext.checkCallingOrSelfPermission(
965 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
966 == PackageManager.PERMISSION_GRANTED) {
967 if (DEBUG) {
968 Slog.d(TAG, "--- Access granted because the calling process has "
969 + "the INTERACT_ACROSS_USERS_FULL permission");
970 }
971 return true;
972 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900973 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
974 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900975 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900976 }
977
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900978
979 /**
980 * Returns true iff the caller is identified to be the current input method with the token.
981 * @param token The window token given to the input method when it was started.
982 * @return true if and only if non-null valid token is specified.
983 */
984 private boolean calledWithValidToken(IBinder token) {
985 if (token == null || mCurToken != token) {
986 return false;
987 }
988 return true;
989 }
990
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 private boolean bindCurrentInputMethodService(
992 Intent service, ServiceConnection conn, int flags) {
993 if (service == null || conn == null) {
994 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
995 return false;
996 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800997 return mContext.bindServiceAsUser(service, conn, flags,
998 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 }
1000
satoke7c6998e2011-06-03 17:57:59 +09001001 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 // TODO: Make this work even for non-current users?
1004 if (!calledFromValidUser()) {
1005 return Collections.emptyList();
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 synchronized (mMethodMap) {
1008 return new ArrayList<InputMethodInfo>(mMethodList);
1009 }
1010 }
1011
satoke7c6998e2011-06-03 17:57:59 +09001012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001014 // TODO: Make this work even for non-current users?
1015 if (!calledFromValidUser()) {
1016 return Collections.emptyList();
1017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001019 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 }
1022
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001023 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001024 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001025 * @return enabled subtypes of the specified imi
1026 */
satoke7c6998e2011-06-03 17:57:59 +09001027 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001028 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001029 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001030 // TODO: Make this work even for non-current users?
1031 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001032 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001033 }
satok67ddf9c2010-11-17 09:45:54 +09001034 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001035 final InputMethodInfo imi;
1036 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001037 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001038 } else {
1039 imi = mMethodMap.get(imiId);
1040 }
1041 if (imi == null) {
1042 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001043 }
1044 return mSettings.getEnabledInputMethodSubtypeListLocked(
1045 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001046 }
1047 }
1048
satoke7c6998e2011-06-03 17:57:59 +09001049 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 public void addClient(IInputMethodClient client,
1051 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 if (!calledFromValidUser()) {
1053 return;
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 synchronized (mMethodMap) {
1056 mClients.put(client.asBinder(), new ClientState(client,
1057 inputContext, uid, pid));
1058 }
1059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001060
satoke7c6998e2011-06-03 17:57:59 +09001061 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001063 if (!calledFromValidUser()) {
1064 return;
1065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001067 ClientState cs = mClients.remove(client.asBinder());
1068 if (cs != null) {
1069 clearClientSessionLocked(cs);
1070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 void executeOrSendMessage(IInterface target, Message msg) {
1075 if (target.asBinder() instanceof Binder) {
1076 mCaller.sendMessage(msg);
1077 } else {
1078 handleMessage(msg);
1079 msg.recycle();
1080 }
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001083 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001085 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 + mCurClient.client.asBinder());
1087 if (mBoundToMethod) {
1088 mBoundToMethod = false;
1089 if (mCurMethod != null) {
1090 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1091 MSG_UNBIND_INPUT, mCurMethod));
1092 }
1093 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001094
1095 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1096 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1098 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1099 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project10592532009-03-18 17:39:46 -07001102 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 private int getImeShowFlags() {
1107 int flags = 0;
1108 if (mShowForced) {
1109 flags |= InputMethod.SHOW_FORCED
1110 | InputMethod.SHOW_EXPLICIT;
1111 } else if (mShowExplicitlyRequested) {
1112 flags |= InputMethod.SHOW_EXPLICIT;
1113 }
1114 return flags;
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 private int getAppShowFlags() {
1118 int flags = 0;
1119 if (mShowForced) {
1120 flags |= InputMethodManager.SHOW_FORCED;
1121 } else if (!mShowExplicitlyRequested) {
1122 flags |= InputMethodManager.SHOW_IMPLICIT;
1123 }
1124 return flags;
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
Dianne Hackborn7663d802012-02-24 13:08:49 -08001127 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 if (!mBoundToMethod) {
1129 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1130 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1131 mBoundToMethod = true;
1132 }
1133 final SessionState session = mCurClient.curSession;
1134 if (initial) {
1135 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1136 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1137 } else {
1138 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1139 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1140 }
1141 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001142 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001143 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001145 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001146 (session.channel != null ? session.channel.dup() : null),
1147 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001151 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 // If no method is currently selected, do nothing.
1153 if (mCurMethodId == null) {
1154 return mNoBinding;
1155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 ClientState cs = mClients.get(client.asBinder());
1158 if (cs == null) {
1159 throw new IllegalArgumentException("unknown client "
1160 + client.asBinder());
1161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 try {
1164 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1165 // Check with the window manager to make sure this client actually
1166 // has a window with focus. If not, reject. This is thread safe
1167 // because if the focus changes some time before or after, the
1168 // next client receiving focus that has any interest in input will
1169 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001170 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1172 return null;
1173 }
1174 } catch (RemoteException e) {
1175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001176
Dianne Hackborn7663d802012-02-24 13:08:49 -08001177 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1178 }
1179
1180 InputBindResult startInputUncheckedLocked(ClientState cs,
1181 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1182 // If no method is currently selected, do nothing.
1183 if (mCurMethodId == null) {
1184 return mNoBinding;
1185 }
1186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001188 // Was the keyguard locked when switching over to the new client?
1189 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 // If the client is changing, we need to switch over to the new
1191 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001192 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001193 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001194 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195
1196 // If the screen is on, inform the new client it is active
1197 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001198 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1199 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 // Bump up the sequence for this client and attach it.
1204 mCurSeq++;
1205 if (mCurSeq <= 0) mCurSeq = 1;
1206 mCurClient = cs;
1207 mCurInputContext = inputContext;
1208 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 // Check if the input method is changing.
1211 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1212 if (cs.curSession != null) {
1213 // Fast case: if we are already connected to the input method,
1214 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001215 return attachNewInputLocked(
1216 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 if (mHaveConnection) {
1219 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 // Return to client, and we will get back with it when
1221 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001222 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001223 return new InputBindResult(null, null, mCurId, mCurSeq,
1224 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 } else if (SystemClock.uptimeMillis()
1226 < (mLastBindTime+TIME_TO_RECONNECT)) {
1227 // In this case we have connected to the service, but
1228 // don't yet have its interface. If it hasn't been too
1229 // long since we did the connection, we'll return to
1230 // the client and wait to get the service interface so
1231 // we can report back. If it has been too long, we want
1232 // to fall through so we can try a disconnect/reconnect
1233 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001234 return new InputBindResult(null, null, mCurId, mCurSeq,
1235 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1238 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240 }
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001243 return startInputInnerLocked();
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001246 InputBindResult startInputInnerLocked() {
1247 if (mCurMethodId == null) {
1248 return mNoBinding;
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001251 if (!mSystemReady) {
1252 // If the system is not yet ready, we shouldn't be running third
1253 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001254 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1255 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1259 if (info == null) {
1260 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001263 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1266 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001267 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1268 com.android.internal.R.string.input_method_binding_label);
1269 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1270 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001271 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001272 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1273 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 mLastBindTime = SystemClock.uptimeMillis();
1275 mHaveConnection = true;
1276 mCurId = info.getId();
1277 mCurToken = new Binder();
1278 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001279 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 mIWindowManager.addWindowToken(mCurToken,
1281 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1282 } catch (RemoteException e) {
1283 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001284 return new InputBindResult(null, null, mCurId, mCurSeq,
1285 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 } else {
1287 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 + mCurIntent);
1290 }
1291 return null;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
satoke7c6998e2011-06-03 17:57:59 +09001294 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001296 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001297 if (!calledFromValidUser()) {
1298 return null;
1299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 synchronized (mMethodMap) {
1301 final long ident = Binder.clearCallingIdentity();
1302 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001303 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 } finally {
1305 Binder.restoreCallingIdentity(ident);
1306 }
1307 }
1308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
satoke7c6998e2011-06-03 17:57:59 +09001310 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 public void finishInput(IInputMethodClient client) {
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
satoke7c6998e2011-06-03 17:57:59 +09001314 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 public void onServiceConnected(ComponentName name, IBinder service) {
1316 synchronized (mMethodMap) {
1317 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1318 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001319 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001320 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001321 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001322 return;
1323 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001324 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001325 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1326 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001328 clearClientSessionLocked(mCurClient);
1329 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
1331 }
1332 }
1333 }
1334
Jeff Brownc28867a2013-03-26 15:42:39 -07001335 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1336 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 synchronized (mMethodMap) {
1338 if (mCurMethod != null && method != null
1339 && mCurMethod.asBinder() == method.asBinder()) {
1340 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001341 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001343 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001344 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 if (res.method != null) {
1346 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1347 MSG_BIND_METHOD, mCurClient.client, res));
1348 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001349 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001353
1354 // Session abandoned. Close its associated input channel.
1355 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001358 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001359 if (mVisibleBound) {
1360 mContext.unbindService(mVisibleConnection);
1361 mVisibleBound = false;
1362 }
1363
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001364 if (mHaveConnection) {
1365 mContext.unbindService(this);
1366 mHaveConnection = false;
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001369 if (mCurToken != null) {
1370 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001371 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001372 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001373 // The current IME is shown. Hence an IME switch (transition) is happening.
1374 mWindowManagerService.saveLastInputMethodWindowForTransition();
1375 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001376 mIWindowManager.removeWindowToken(mCurToken);
1377 } catch (RemoteException e) {
1378 }
1379 mCurToken = null;
1380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
The Android Open Source Project10592532009-03-18 17:39:46 -07001382 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001383 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001384
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001385 if (reportToClient && mCurClient != null) {
1386 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1387 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1388 }
1389 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001390
1391 void requestClientSessionLocked(ClientState cs) {
1392 if (!cs.sessionRequested) {
1393 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1394 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1395 cs.sessionRequested = true;
1396 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1397 MSG_CREATE_SESSION, mCurMethod, channels[1],
1398 new MethodCallback(this, mCurMethod, channels[0])));
1399 }
1400 }
1401
1402 void clearClientSessionLocked(ClientState cs) {
1403 finishSessionLocked(cs.curSession);
1404 cs.curSession = null;
1405 cs.sessionRequested = false;
1406 }
1407
1408 private void finishSessionLocked(SessionState sessionState) {
1409 if (sessionState != null) {
1410 if (sessionState.session != null) {
1411 try {
1412 sessionState.session.finishSession();
1413 } catch (RemoteException e) {
1414 Slog.w(TAG, "Session failed to close due to remote exception", e);
1415 setImeWindowVisibilityStatusHiddenLocked();
1416 }
1417 sessionState.session = null;
1418 }
1419 if (sessionState.channel != null) {
1420 sessionState.channel.dispose();
1421 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001422 }
1423 }
1424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001426 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 if (mCurMethod != null) {
1428 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001429 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001431
Jeff Brownc28867a2013-03-26 15:42:39 -07001432 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001433 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 mCurMethod = null;
1435 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001436 if (mStatusBar != null) {
1437 mStatusBar.setIconVisibility("ime", false);
1438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
satoke7c6998e2011-06-03 17:57:59 +09001441 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 public void onServiceDisconnected(ComponentName name) {
1443 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001444 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 + " mCurIntent=" + mCurIntent);
1446 if (mCurMethod != null && mCurIntent != null
1447 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001448 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 // We consider this to be a new bind attempt, since the system
1450 // should now try to restart the service for us.
1451 mLastBindTime = SystemClock.uptimeMillis();
1452 mShowRequested = mInputShown;
1453 mInputShown = false;
1454 if (mCurClient != null) {
1455 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1456 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1457 }
1458 }
1459 }
1460 }
1461
satokf9f01002011-05-19 21:31:50 +09001462 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1464 long ident = Binder.clearCallingIdentity();
1465 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001467 if (!calledWithValidToken(token)) {
1468 final int uid = Binder.getCallingUid();
1469 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1470 + " token:" + token);
1471 return;
1472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001474 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001475 if (mStatusBar != null) {
1476 mStatusBar.setIconVisibility("ime", false);
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001479 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001480 CharSequence contentDescription = null;
1481 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001482 // Use PackageManager to load label
1483 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001484 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001485 mIPackageManager.getApplicationInfo(packageName, 0,
1486 mSettings.getCurrentUserId()));
1487 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001488 /* ignore */
1489 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001490 if (mStatusBar != null) {
1491 mStatusBar.setIcon("ime", packageName, iconId, 0,
1492 contentDescription != null
1493 ? contentDescription.toString() : null);
1494 mStatusBar.setIconVisibility("ime", true);
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
1497 }
1498 } finally {
1499 Binder.restoreCallingIdentity(ident);
1500 }
1501 }
1502
satok7cfc0ed2011-06-20 21:29:36 +09001503 private boolean needsToShowImeSwitchOngoingNotification() {
1504 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001505 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001506 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001507 synchronized (mMethodMap) {
1508 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1509 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001510 if (N > 2) return true;
1511 if (N < 1) return false;
1512 int nonAuxCount = 0;
1513 int auxCount = 0;
1514 InputMethodSubtype nonAuxSubtype = null;
1515 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001516 for(int i = 0; i < N; ++i) {
1517 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001518 final List<InputMethodSubtype> subtypes =
1519 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001520 final int subtypeCount = subtypes.size();
1521 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001522 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001523 } else {
1524 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001525 final InputMethodSubtype subtype = subtypes.get(j);
1526 if (!subtype.isAuxiliary()) {
1527 ++nonAuxCount;
1528 nonAuxSubtype = subtype;
1529 } else {
1530 ++auxCount;
1531 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001532 }
1533 }
1534 }
satok7cfc0ed2011-06-20 21:29:36 +09001535 }
satokb6359412011-06-28 17:47:41 +09001536 if (nonAuxCount > 1 || auxCount > 1) {
1537 return true;
1538 } else if (nonAuxCount == 1 && auxCount == 1) {
1539 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001540 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1541 || auxSubtype.overridesImplicitlyEnabledSubtype()
1542 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001543 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1544 return false;
1545 }
1546 return true;
1547 }
1548 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001549 }
satok7cfc0ed2011-06-20 21:29:36 +09001550 }
1551
John Spurlocke0980502013-10-25 11:59:29 -04001552 private boolean isKeyguardLocked() {
1553 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1554 }
1555
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001556 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001557 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001558 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001559 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001560 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001561 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001562 if (!calledWithValidToken(token)) {
1563 final int uid = Binder.getCallingUid();
1564 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1565 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001566 return;
1567 }
satok06487a52010-10-29 11:37:18 +09001568 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001569 // apply policy for binder calls
1570 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1571 vis = 0;
1572 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001573 mImeWindowVis = vis;
1574 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001575 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1576 && (mWindowManagerService.isHardKeyboardAvailable()
1577 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001578 final boolean needsToShowImeSwitcher = iconVisibility
1579 && needsToShowImeSwitchOngoingNotification();
1580 if (mStatusBar != null) {
1581 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1582 needsToShowImeSwitcher);
1583 }
satok5bc8e732011-07-22 21:07:23 +09001584 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001585 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001586 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001587 final CharSequence title = mRes.getText(
1588 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001589 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1590 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001591
Selim Cinek255dd042014-08-19 22:29:02 +02001592 mImeSwitcherNotification.color = mContext.getResources().getColor(
1593 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001594 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001595 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001596 if ((mNotificationManager != null)
1597 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001598 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001599 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001600 }
1601 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001602 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001603 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001604 mNotificationShown = true;
1605 }
satok7cfc0ed2011-06-20 21:29:36 +09001606 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001607 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001608 if (DEBUG) {
1609 Slog.d(TAG, "--- hide notification");
1610 }
1611 mNotificationManager.cancelAsUser(null,
1612 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001613 mNotificationShown = false;
1614 }
1615 }
satok06487a52010-10-29 11:37:18 +09001616 }
1617 } finally {
1618 Binder.restoreCallingIdentity(ident);
1619 }
1620 }
1621
satoke7c6998e2011-06-03 17:57:59 +09001622 @Override
satokf9f01002011-05-19 21:31:50 +09001623 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001624 if (!calledFromValidUser()) {
1625 return;
1626 }
satokf9f01002011-05-19 21:31:50 +09001627 synchronized (mMethodMap) {
1628 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1629 for (int i = 0; i < spans.length; ++i) {
1630 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001631 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001632 mSecureSuggestionSpans.put(ss, currentImi);
1633 }
1634 }
1635 }
1636 }
1637
satoke7c6998e2011-06-03 17:57:59 +09001638 @Override
satokf9f01002011-05-19 21:31:50 +09001639 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001640 if (!calledFromValidUser()) {
1641 return false;
1642 }
satokf9f01002011-05-19 21:31:50 +09001643 synchronized (mMethodMap) {
1644 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1645 // TODO: Do not send the intent if the process of the targetImi is already dead.
1646 if (targetImi != null) {
1647 final String[] suggestions = span.getSuggestions();
1648 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001649 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001650 final Intent intent = new Intent();
1651 // Ensures that only a class in the original IME package will receive the
1652 // notification.
satok42c5a162011-05-26 16:46:14 +09001653 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001654 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1655 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1656 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1657 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001658 final long ident = Binder.clearCallingIdentity();
1659 try {
1660 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1661 } finally {
1662 Binder.restoreCallingIdentity(ident);
1663 }
satokf9f01002011-05-19 21:31:50 +09001664 return true;
1665 }
1666 }
1667 return false;
1668 }
1669
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001670 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001671 updateInputMethodsFromSettingsLocked(enabledMayChange);
1672 updateKeyboardFromSettingsLocked();
1673 }
1674
1675 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001676 if (enabledMayChange) {
1677 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1678 for (int i=0; i<enabled.size(); i++) {
1679 // We allow the user to select "disabled until used" apps, so if they
1680 // are enabling one of those here we now need to make it enabled.
1681 InputMethodInfo imm = enabled.get(i);
1682 try {
1683 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1684 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1685 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001686 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001687 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001688 if (DEBUG) {
1689 Slog.d(TAG, "Update state(" + imm.getId()
1690 + "): DISABLED_UNTIL_USED -> DEFAULT");
1691 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001692 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1693 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001694 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1695 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001696 }
1697 } catch (RemoteException e) {
1698 }
1699 }
1700 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001701 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1702 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1703 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1704 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001705 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001706 // There is no input method selected, try to choose new applicable input method.
1707 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001708 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001709 }
1710 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001712 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001714 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001715 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001716 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
satokf3db1af2010-11-23 13:34:33 +09001718 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001719 } else {
1720 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001721 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001722 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001724 // Here is not the perfect place to reset the switching controller. Ideally
1725 // mSwitchingController and mSettings should be able to share the same state.
1726 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1727 // the same enabled IMEs list.
1728 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001729
1730 }
1731
1732 public void updateKeyboardFromSettingsLocked() {
1733 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1734 if (mSwitchingDialog != null
1735 && mSwitchingDialogTitleView != null
1736 && mSwitchingDialog.isShowing()) {
1737 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1738 com.android.internal.R.id.hard_keyboard_switch);
1739 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
satokab751aa2010-09-14 19:17:36 +09001743 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 InputMethodInfo info = mMethodMap.get(id);
1745 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001746 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001748
satokd81e9502012-05-21 12:58:45 +09001749 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001751 final int subtypeCount = info.getSubtypeCount();
1752 if (subtypeCount <= 0) {
1753 return;
satokcd7cd292010-11-20 15:46:23 +09001754 }
satokd81e9502012-05-21 12:58:45 +09001755 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1756 final InputMethodSubtype newSubtype;
1757 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1758 newSubtype = info.getSubtypeAt(subtypeId);
1759 } else {
1760 // If subtype is null, try to find the most applicable one from
1761 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001762 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001763 }
1764 if (newSubtype == null || oldSubtype == null) {
1765 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1766 + ", new subtype = " + newSubtype);
1767 return;
1768 }
1769 if (newSubtype != oldSubtype) {
1770 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1771 if (mCurMethod != null) {
1772 try {
1773 refreshImeWindowVisibilityLocked();
1774 mCurMethod.changeInputMethodSubtype(newSubtype);
1775 } catch (RemoteException e) {
1776 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001777 }
1778 }
1779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 return;
1781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001782
satokd81e9502012-05-21 12:58:45 +09001783 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 final long ident = Binder.clearCallingIdentity();
1785 try {
satokab751aa2010-09-14 19:17:36 +09001786 // Set a subtype to this input method.
1787 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001788 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1789 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1790 // because mCurMethodId is stored as a history in
1791 // setSelectedInputMethodAndSubtypeLocked().
1792 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793
1794 if (ActivityManagerNative.isSystemReady()) {
1795 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001796 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001798 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001800 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 } finally {
1802 Binder.restoreCallingIdentity(ident);
1803 }
1804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001805
satok42c5a162011-05-26 16:46:14 +09001806 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001807 public boolean showSoftInput(IInputMethodClient client, int flags,
1808 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001809 if (!calledFromValidUser()) {
1810 return false;
1811 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001812 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 long ident = Binder.clearCallingIdentity();
1814 try {
1815 synchronized (mMethodMap) {
1816 if (mCurClient == null || client == null
1817 || mCurClient.client.asBinder() != client.asBinder()) {
1818 try {
1819 // We need to check if this is the current client with
1820 // focus in the window manager, to allow this call to
1821 // be made before input is started in it.
1822 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001823 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001824 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001827 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 }
1829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001830
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001831 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001832 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834 } finally {
1835 Binder.restoreCallingIdentity(ident);
1836 }
1837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
The Android Open Source Project4df24232009-03-05 14:34:35 -08001839 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 mShowRequested = true;
1841 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1842 mShowExplicitlyRequested = true;
1843 }
1844 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1845 mShowExplicitlyRequested = true;
1846 mShowForced = true;
1847 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001848
Dianne Hackborncc278702009-09-02 23:07:23 -07001849 if (!mSystemReady) {
1850 return false;
1851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001852
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001855 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1857 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1858 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001860 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001861 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001862 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1863 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001864 mVisibleBound = true;
1865 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001866 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001868 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 // The client has asked to have the input method shown, but
1870 // we have been sitting here too long with a connection to the
1871 // service and no interface received, so let's disconnect/connect
1872 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001875 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001877 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001878 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001879 } else {
1880 if (DEBUG) {
1881 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1882 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
The Android Open Source Project4df24232009-03-05 14:34:35 -08001886 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
satok42c5a162011-05-26 16:46:14 +09001889 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001890 public boolean hideSoftInput(IInputMethodClient client, int flags,
1891 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001892 if (!calledFromValidUser()) {
1893 return false;
1894 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001895 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 long ident = Binder.clearCallingIdentity();
1897 try {
1898 synchronized (mMethodMap) {
1899 if (mCurClient == null || client == null
1900 || mCurClient.client.asBinder() != client.asBinder()) {
1901 try {
1902 // We need to check if this is the current client with
1903 // focus in the window manager, to allow this call to
1904 // be made before input is started in it.
1905 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001906 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1907 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001908 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001909 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
1911 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001912 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001913 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
1915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001916
Joe Onorato8a9b2202010-02-26 18:56:32 -08001917 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001918 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
1920 } finally {
1921 Binder.restoreCallingIdentity(ident);
1922 }
1923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001924
The Android Open Source Project4df24232009-03-05 14:34:35 -08001925 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1927 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001928 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 -08001929 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001932 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 -08001933 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001937 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1938 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1939 res = true;
1940 } else {
1941 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001943 if (mHaveConnection && mVisibleBound) {
1944 mContext.unbindService(mVisibleConnection);
1945 mVisibleBound = false;
1946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 mInputShown = false;
1948 mShowRequested = false;
1949 mShowExplicitlyRequested = false;
1950 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001951 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001953
satok42c5a162011-05-26 16:46:14 +09001954 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001955 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1956 int controlFlags, int softInputMode, int windowFlags,
1957 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001958 // Needs to check the validity before clearing calling identity
1959 final boolean calledFromValidUser = calledFromValidUser();
1960
Dianne Hackborn7663d802012-02-24 13:08:49 -08001961 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 long ident = Binder.clearCallingIdentity();
1963 try {
1964 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001965 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001966 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001968 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001969
Dianne Hackborn7663d802012-02-24 13:08:49 -08001970 ClientState cs = mClients.get(client.asBinder());
1971 if (cs == null) {
1972 throw new IllegalArgumentException("unknown client "
1973 + client.asBinder());
1974 }
1975
1976 try {
1977 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1978 // Check with the window manager to make sure this client actually
1979 // has a window with focus. If not, reject. This is thread safe
1980 // because if the focus changes some time before or after, the
1981 // next client receiving focus that has any interest in input will
1982 // be calling through here after that change happens.
1983 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1984 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1985 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001987 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001990 if (!calledFromValidUser) {
1991 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1992 Slog.w(TAG, "If you want to interect with IME, you need "
1993 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1994 hideCurrentInputLocked(0, null);
1995 return null;
1996 }
1997
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001998 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001999 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002000 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002001 if (attribute != null) {
2002 return startInputUncheckedLocked(cs, inputContext, attribute,
2003 controlFlags);
2004 }
2005 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002006 }
2007 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002009 // Should we auto-show the IME even if the caller has not
2010 // specified what should be done with it?
2011 // We only do this automatically if the window can resize
2012 // to accommodate the IME (so what the user sees will give
2013 // them good context without input information being obscured
2014 // by the IME) or if running on a large screen where there
2015 // is more room for the target window + IME.
2016 final boolean doAutoShow =
2017 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2018 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2019 || mRes.getConfiguration().isLayoutSizeAtLeast(
2020 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002021 final boolean isTextEditor =
2022 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2023
2024 // We want to start input before showing the IME, but after closing
2025 // it. We want to do this after closing it to help the IME disappear
2026 // more quickly (not get stuck behind it initializing itself for the
2027 // new focused input, even if its window wants to hide the IME).
2028 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2031 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002032 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2034 // There is no focus view, and this window will
2035 // be behind any soft input window, so hide the
2036 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002037 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002038 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002040 } else if (isTextEditor && doAutoShow && (softInputMode &
2041 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 // There is a focus view, and we are navigating forward
2043 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002044 // We only do this automatically if the window can resize
2045 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002046 // them good context without input information being obscured
2047 // by the IME) or if running on a large screen where there
2048 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002049 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002050 if (attribute != null) {
2051 res = startInputUncheckedLocked(cs, inputContext, attribute,
2052 controlFlags);
2053 didStart = true;
2054 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002055 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057 break;
2058 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2059 // Do nothing.
2060 break;
2061 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2062 if ((softInputMode &
2063 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002064 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067 break;
2068 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002069 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 break;
2072 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2073 if ((softInputMode &
2074 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002075 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002076 if (attribute != null) {
2077 res = startInputUncheckedLocked(cs, inputContext, attribute,
2078 controlFlags);
2079 didStart = true;
2080 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002081 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 break;
2084 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002085 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002086 if (attribute != null) {
2087 res = startInputUncheckedLocked(cs, inputContext, attribute,
2088 controlFlags);
2089 didStart = true;
2090 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002091 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 break;
2093 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002094
2095 if (!didStart && attribute != null) {
2096 res = startInputUncheckedLocked(cs, inputContext, attribute,
2097 controlFlags);
2098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100 } finally {
2101 Binder.restoreCallingIdentity(ident);
2102 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002103
2104 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002106
satok42c5a162011-05-26 16:46:14 +09002107 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002109 if (!calledFromValidUser()) {
2110 return;
2111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 synchronized (mMethodMap) {
2113 if (mCurClient == null || client == null
2114 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002115 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002116 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
2118
satok440aab52010-11-25 09:43:11 +09002119 // Always call subtype picker, because subtype picker is a superset of input method
2120 // picker.
satokab751aa2010-09-14 19:17:36 +09002121 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2122 }
2123 }
2124
satok42c5a162011-05-26 16:46:14 +09002125 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 if (!calledFromValidUser()) {
2128 return;
2129 }
satok28203512010-11-24 11:06:49 +09002130 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2131 }
2132
satok42c5a162011-05-26 16:46:14 +09002133 @Override
satok28203512010-11-24 11:06:49 +09002134 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002135 if (!calledFromValidUser()) {
2136 return;
2137 }
satok28203512010-11-24 11:06:49 +09002138 synchronized (mMethodMap) {
2139 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002140 setInputMethodWithSubtypeIdLocked(token, id,
2141 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2142 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002143 } else {
2144 setInputMethod(token, id);
2145 }
2146 }
satokab751aa2010-09-14 19:17:36 +09002147 }
2148
satok42c5a162011-05-26 16:46:14 +09002149 @Override
satokb416a712010-11-25 20:42:14 +09002150 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002151 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002152 if (!calledFromValidUser()) {
2153 return;
2154 }
satokb416a712010-11-25 20:42:14 +09002155 synchronized (mMethodMap) {
2156 if (mCurClient == null || client == null
2157 || mCurClient.client.asBinder() != client.asBinder()) {
2158 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2159 }
satok7fee71f2010-12-17 18:54:26 +09002160 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2161 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002162 }
2163 }
2164
satok4fc87d62011-05-20 16:13:43 +09002165 @Override
satok735cf382010-11-11 20:40:09 +09002166 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002167 if (!calledFromValidUser()) {
2168 return false;
2169 }
satok735cf382010-11-11 20:40:09 +09002170 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002171 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002172 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002173 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002174 lastImi = mMethodMap.get(lastIme.first);
2175 } else {
2176 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002177 }
satok4fc87d62011-05-20 16:13:43 +09002178 String targetLastImiId = null;
2179 int subtypeId = NOT_A_SUBTYPE_ID;
2180 if (lastIme != null && lastImi != null) {
2181 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2182 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2183 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2184 : mCurrentSubtype.hashCode();
2185 // If the last IME is the same as the current IME and the last subtype is not
2186 // defined, there is no need to switch to the last IME.
2187 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2188 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002189 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002190 }
2191 }
2192
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002193 if (TextUtils.isEmpty(targetLastImiId)
2194 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002195 // This is a safety net. If the currentSubtype can't be added to the history
2196 // and the framework couldn't find the last ime, we will make the last ime be
2197 // the most applicable enabled keyboard subtype of the system imes.
2198 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2199 if (enabled != null) {
2200 final int N = enabled.size();
2201 final String locale = mCurrentSubtype == null
2202 ? mRes.getConfiguration().locale.toString()
2203 : mCurrentSubtype.getLocale();
2204 for (int i = 0; i < N; ++i) {
2205 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002206 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002207 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002208 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2209 InputMethodUtils.getSubtypes(imi),
2210 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002211 if (keyboardSubtype != null) {
2212 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002213 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002214 imi, keyboardSubtype.hashCode());
2215 if(keyboardSubtype.getLocale().equals(locale)) {
2216 break;
2217 }
2218 }
2219 }
2220 }
2221 }
2222 }
2223
2224 if (!TextUtils.isEmpty(targetLastImiId)) {
2225 if (DEBUG) {
2226 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2227 + ", from: " + mCurMethodId + ", " + subtypeId);
2228 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002229 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002230 return true;
2231 } else {
2232 return false;
2233 }
satok735cf382010-11-11 20:40:09 +09002234 }
2235 }
2236
satoke7c6998e2011-06-03 17:57:59 +09002237 @Override
satok688bd472012-02-09 20:09:17 +09002238 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002239 if (!calledFromValidUser()) {
2240 return false;
2241 }
satok688bd472012-02-09 20:09:17 +09002242 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002243 if (!calledWithValidToken(token)) {
2244 final int uid = Binder.getCallingUid();
2245 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2246 + " token:" + token);
2247 return false;
2248 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002249 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002250 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2251 if (nextSubtype == null) {
2252 return false;
2253 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002254 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2255 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002256 return true;
2257 }
2258 }
2259
2260 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002261 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2262 if (!calledFromValidUser()) {
2263 return false;
2264 }
2265 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002266 if (!calledWithValidToken(token)) {
2267 final int uid = Binder.getCallingUid();
2268 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2269 + "token. uid:" + uid + " token:" + token);
2270 return false;
2271 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002272 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002273 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2274 if (nextSubtype == null) {
2275 return false;
2276 }
2277 return true;
2278 }
2279 }
2280
2281 @Override
satok68f1b782011-04-11 14:26:04 +09002282 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002283 if (!calledFromValidUser()) {
2284 return null;
2285 }
satok68f1b782011-04-11 14:26:04 +09002286 synchronized (mMethodMap) {
2287 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2288 // TODO: Handle the case of the last IME with no subtypes
2289 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2290 || TextUtils.isEmpty(lastIme.second)) return null;
2291 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2292 if (lastImi == null) return null;
2293 try {
2294 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002295 final int lastSubtypeId =
2296 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002297 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2298 return null;
2299 }
2300 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002301 } catch (NumberFormatException e) {
2302 return null;
2303 }
2304 }
2305 }
2306
satoke7c6998e2011-06-03 17:57:59 +09002307 @Override
satokee5e77c2011-09-02 18:50:15 +09002308 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002309 if (!calledFromValidUser()) {
2310 return;
2311 }
satok91e88122011-07-18 11:11:42 +09002312 // By this IPC call, only a process which shares the same uid with the IME can add
2313 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002314 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002315 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002316 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002317 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002318 final String[] packageInfos;
2319 try {
2320 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2321 } catch (RemoteException e) {
2322 Slog.e(TAG, "Failed to get package infos");
2323 return;
2324 }
satok91e88122011-07-18 11:11:42 +09002325 if (packageInfos != null) {
2326 final int packageNum = packageInfos.length;
2327 for (int i = 0; i < packageNum; ++i) {
2328 if (packageInfos[i].equals(imi.getPackageName())) {
2329 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002330 final long ident = Binder.clearCallingIdentity();
2331 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002332 buildInputMethodListLocked(mMethodList, mMethodMap,
2333 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002334 } finally {
2335 Binder.restoreCallingIdentity(ident);
2336 }
satokee5e77c2011-09-02 18:50:15 +09002337 return;
satok91e88122011-07-18 11:11:42 +09002338 }
2339 }
2340 }
satoke7c6998e2011-06-03 17:57:59 +09002341 }
satokee5e77c2011-09-02 18:50:15 +09002342 return;
satoke7c6998e2011-06-03 17:57:59 +09002343 }
2344
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002345 @Override
2346 public int getInputMethodWindowVisibleHeight() {
2347 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2348 }
2349
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002350 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002351 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002352 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002353 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002354 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002355 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002356 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2357 if (DEBUG) {
2358 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2359 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2360 + " actual: " + sequenceNumber);
2361 }
2362 return;
2363 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002364 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2365 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002366 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002367 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002368 }
2369 }
2370
satok28203512010-11-24 11:06:49 +09002371 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002373 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2374 }
2375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002377 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2378 if (token == null) {
2379 if (mContext.checkCallingOrSelfPermission(
2380 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2381 != PackageManager.PERMISSION_GRANTED) {
2382 throw new SecurityException(
2383 "Using null token requires permission "
2384 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002386 } else if (mCurToken != token) {
2387 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2388 + " token: " + token);
2389 return;
2390 }
2391
2392 final long ident = Binder.clearCallingIdentity();
2393 try {
2394 setInputMethodLocked(id, subtypeId);
2395 } finally {
2396 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 }
2398 }
2399
satok42c5a162011-05-26 16:46:14 +09002400 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002402 if (!calledFromValidUser()) {
2403 return;
2404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002406 if (!calledWithValidToken(token)) {
2407 final int uid = Binder.getCallingUid();
2408 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2409 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 return;
2411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 long ident = Binder.clearCallingIdentity();
2413 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002414 hideCurrentInputLocked(flags, null);
2415 } finally {
2416 Binder.restoreCallingIdentity(ident);
2417 }
2418 }
2419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002420
satok42c5a162011-05-26 16:46:14 +09002421 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002422 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002423 if (!calledFromValidUser()) {
2424 return;
2425 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002426 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002427 if (!calledWithValidToken(token)) {
2428 final int uid = Binder.getCallingUid();
2429 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2430 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002431 return;
2432 }
2433 long ident = Binder.clearCallingIdentity();
2434 try {
2435 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 } finally {
2437 Binder.restoreCallingIdentity(ident);
2438 }
2439 }
2440 }
2441
2442 void setEnabledSessionInMainThread(SessionState session) {
2443 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002444 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002446 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002447 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 } catch (RemoteException e) {
2449 }
2450 }
2451 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002452 if (mEnabledSession != null && mEnabledSession.session != null) {
2453 try {
2454 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2455 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2456 } catch (RemoteException e) {
2457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 }
2459 }
2460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002461
satok42c5a162011-05-26 16:46:14 +09002462 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002464 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 switch (msg.what) {
2466 case MSG_SHOW_IM_PICKER:
2467 showInputMethodMenu();
2468 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002469
satokab751aa2010-09-14 19:17:36 +09002470 case MSG_SHOW_IM_SUBTYPE_PICKER:
2471 showInputMethodSubtypeMenu();
2472 return true;
2473
satok47a44912010-10-06 16:03:58 +09002474 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002475 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002476 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002477 args.recycle();
satok217f5482010-12-15 05:19:19 +09002478 return true;
2479
2480 case MSG_SHOW_IM_CONFIG:
2481 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002482 return true;
2483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 case MSG_UNBIND_INPUT:
2487 try {
2488 ((IInputMethod)msg.obj).unbindInput();
2489 } catch (RemoteException e) {
2490 // There is nothing interesting about the method dying.
2491 }
2492 return true;
2493 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002494 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 try {
2496 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2497 } catch (RemoteException e) {
2498 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 return true;
2501 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002502 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002504 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002505 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002506 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 } catch (RemoteException e) {
2508 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002509 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 return true;
2511 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002512 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002514 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002515 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002516 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 } catch (RemoteException e) {
2518 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002519 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 return true;
2521 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002522 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002524 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2526 } catch (RemoteException e) {
2527 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002528 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002530 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002531 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002532 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002533 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002535 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002537 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002538 // Dispose the channel if the input method is not local to this process
2539 // because the remote proxy will get its own copy when unparceled.
2540 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002541 channel.dispose();
2542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002544 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002550 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 try {
2552 SessionState session = (SessionState)args.arg1;
2553 setEnabledSessionInMainThread(session);
2554 session.method.startInput((IInputContext)args.arg2,
2555 (EditorInfo)args.arg3);
2556 } catch (RemoteException e) {
2557 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002558 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 return true;
2560 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002561 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 try {
2563 SessionState session = (SessionState)args.arg1;
2564 setEnabledSessionInMainThread(session);
2565 session.method.restartInput((IInputContext)args.arg2,
2566 (EditorInfo)args.arg3);
2567 } catch (RemoteException e) {
2568 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002569 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 case MSG_UNBIND_METHOD:
2575 try {
2576 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2577 } catch (RemoteException e) {
2578 // There is nothing interesting about the last client dying.
2579 }
2580 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002581 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002582 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002583 IInputMethodClient client = (IInputMethodClient)args.arg1;
2584 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002586 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002588 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002589 } finally {
2590 // Dispose the channel if the input method is not local to this process
2591 // because the remote proxy will get its own copy when unparceled.
2592 if (res.channel != null && Binder.isProxy(client)) {
2593 res.channel.dispose();
2594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002596 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002598 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002599 case MSG_SET_ACTIVE:
2600 try {
2601 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2602 } catch (RemoteException e) {
2603 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2604 + ((ClientState)msg.obj).pid + " uid "
2605 + ((ClientState)msg.obj).uid);
2606 }
2607 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002608 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2609 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002610 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002611 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002612 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002613 } catch (RemoteException e) {
2614 Slog.w(TAG, "Got RemoteException sending "
2615 + "setUserActionNotificationSequenceNumber("
2616 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002617 + clientState.pid + " uid "
2618 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002619 }
2620 return true;
2621 }
satok01038492012-04-09 21:08:27 +09002622
2623 // --------------------------------------------------------------
2624 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002625 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002626 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 }
2628 return false;
2629 }
2630
satokdc9ddae2011-10-06 12:22:36 +09002631 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002632 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2633 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002634 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002635 if (DEBUG) {
2636 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2637 }
satok723a27e2010-11-11 14:58:11 +09002638 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002639 return true;
2640 }
2641
2642 return false;
2643 }
2644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002646 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002647 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002648 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002649 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 list.clear();
2652 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002653
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002654 // Use for queryIntentServicesAsUser
2655 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002656 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002657 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002659 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002661 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2662 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002663
satoke7c6998e2011-06-03 17:57:59 +09002664 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2665 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 for (int i = 0; i < services.size(); ++i) {
2667 ResolveInfo ri = services.get(i);
2668 ServiceInfo si = ri.serviceInfo;
2669 ComponentName compName = new ComponentName(si.packageName, si.name);
2670 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2671 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002672 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 + ": it does not require the permission "
2674 + android.Manifest.permission.BIND_INPUT_METHOD);
2675 continue;
2676 }
2677
Joe Onorato8a9b2202010-02-26 18:56:32 -08002678 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679
2680 try {
satoke7c6998e2011-06-03 17:57:59 +09002681 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002683 final String id = p.getId();
2684 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685
2686 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002687 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002691 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002693 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
2695 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002696
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002697 if (resetDefaultEnabledIme) {
2698 final ArrayList<InputMethodInfo> defaultEnabledIme =
2699 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2700 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2701 final InputMethodInfo imi = defaultEnabledIme.get(i);
2702 if (DEBUG) {
2703 Slog.d(TAG, "--- enable ime = " + imi);
2704 }
2705 setInputMethodEnabledLocked(imi.getId(), true);
2706 }
2707 }
2708
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002709 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002710 if (!TextUtils.isEmpty(defaultImiId)) {
2711 if (!map.containsKey(defaultImiId)) {
2712 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2713 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002714 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002715 }
2716 } else {
2717 // Double check that the default IME is certainly enabled.
2718 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002719 }
2720 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002721 // Here is not the perfect place to reset the switching controller. Ideally
2722 // mSwitchingController and mSettings should be able to share the same state.
2723 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2724 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002725 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002729
satokab751aa2010-09-14 19:17:36 +09002730 private void showInputMethodMenu() {
2731 showInputMethodMenuInternal(false);
2732 }
2733
2734 private void showInputMethodSubtypeMenu() {
2735 showInputMethodMenuInternal(true);
2736 }
2737
satok217f5482010-12-15 05:19:19 +09002738 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002739 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002740 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002741 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2742 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002743 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002744 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002745 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002746 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002747 }
2748
2749 private void showConfigureInputMethods() {
2750 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2751 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2752 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2753 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002754 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002755 }
2756
satok2c93efc2012-04-02 19:33:47 +09002757 private boolean isScreenLocked() {
2758 return mKeyguardManager != null
2759 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2760 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002761
satokab751aa2010-09-14 19:17:36 +09002762 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002766 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002767
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002768 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002769 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002770 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002771
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002772 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002773 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002774 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2775 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002776 if (immis == null || immis.size() == 0) {
2777 return;
2778 }
2779
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002780 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781
satok688bd472012-02-09 20:09:17 +09002782 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002783 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002784 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002785
satokc3690562012-01-10 20:14:43 +09002786 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002787 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002788 if (currentSubtype != null) {
2789 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002790 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2791 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002792 }
2793 }
2794
Ken Wakasa761eb372011-03-04 19:06:18 +09002795 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002796 mIms = new InputMethodInfo[N];
2797 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002798 int checkedItem = 0;
2799 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002800 final ImeSubtypeListItem item = imList.get(i);
2801 mIms[i] = item.mImi;
2802 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002803 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002804 int subtypeId = mSubtypeIds[i];
2805 if ((subtypeId == NOT_A_SUBTYPE_ID)
2806 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2807 || (subtypeId == lastInputMethodSubtypeId)) {
2808 checkedItem = i;
2809 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002812 final Context themedContext = new ContextThemeWrapper(context,
2813 android.R.style.Theme_DeviceDefault_Settings);
2814 mDialogBuilder = new AlertDialog.Builder(themedContext);
2815 final TypedArray a = themedContext.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002816 com.android.internal.R.styleable.DialogPreference,
2817 com.android.internal.R.attr.alertDialogStyle, 0);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002818 mDialogBuilder.setIcon(a.getDrawable(
2819 com.android.internal.R.styleable.DialogPreference_dialogIcon));
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002820 a.recycle();
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002821 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2822 @Override
2823 public void onCancel(DialogInterface dialog) {
2824 hideInputMethodMenu();
2825 }
2826 });
satok01038492012-04-09 21:08:27 +09002827 final LayoutInflater inflater =
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002828 (LayoutInflater)themedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002829 final View tv = inflater.inflate(
2830 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2831 mDialogBuilder.setCustomTitle(tv);
2832
2833 // Setup layout for a toggle switch of the hardware keyboard
2834 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002835 mSwitchingDialogTitleView
2836 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2837 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2838 ? View.VISIBLE : View.GONE);
2839 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2840 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002841 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002842 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2843 @Override
2844 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002845 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002846 // Ensure that the input method dialog is dismissed when changing
2847 // the hardware keyboard state.
2848 hideInputMethodMenu();
2849 }
2850 });
2851
2852 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(themedContext,
2853 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2854 final OnClickListener choiceListener = new OnClickListener() {
2855 @Override
2856 public void onClick(final DialogInterface dialog, final int which) {
2857 synchronized (mMethodMap) {
2858 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2859 || mSubtypeIds.length <= which) {
2860 return;
satok01038492012-04-09 21:08:27 +09002861 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002862 final InputMethodInfo im = mIms[which];
2863 int subtypeId = mSubtypeIds[which];
2864 adapter.mCheckedItem = which;
2865 adapter.notifyDataSetChanged();
2866 hideInputMethodMenu();
2867 if (im != null) {
2868 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2869 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002870 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002871 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002873 }
2874 }
2875 };
2876 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877
satokbc81b692011-08-26 16:22:22 +09002878 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002879 final OnClickListener positiveListener = new OnClickListener() {
2880 @Override
2881 public void onClick(DialogInterface dialog, int whichButton) {
2882 showConfigureInputMethods();
2883 }
2884 };
satok82beadf2010-12-27 19:03:06 +09002885 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002886 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09002887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002889 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 mSwitchingDialog.getWindow().setType(
2891 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002892 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2893 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002894 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002895 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 mSwitchingDialog.show();
2897 }
2898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002899
Ken Wakasa05dbb652011-08-22 15:22:43 +09002900 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2901 private final LayoutInflater mInflater;
2902 private final int mTextViewResourceId;
2903 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002904 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002905 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2906 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2907 super(context, textViewResourceId, itemsList);
2908 mTextViewResourceId = textViewResourceId;
2909 mItemsList = itemsList;
2910 mCheckedItem = checkedItem;
2911 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2912 }
2913
2914 @Override
2915 public View getView(int position, View convertView, ViewGroup parent) {
2916 final View view = convertView != null ? convertView
2917 : mInflater.inflate(mTextViewResourceId, null);
2918 if (position < 0 || position >= mItemsList.size()) return view;
2919 final ImeSubtypeListItem item = mItemsList.get(position);
2920 final CharSequence imeName = item.mImeName;
2921 final CharSequence subtypeName = item.mSubtypeName;
2922 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2923 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2924 if (TextUtils.isEmpty(subtypeName)) {
2925 firstTextView.setText(imeName);
2926 secondTextView.setVisibility(View.GONE);
2927 } else {
2928 firstTextView.setText(subtypeName);
2929 secondTextView.setText(imeName);
2930 secondTextView.setVisibility(View.VISIBLE);
2931 }
2932 final RadioButton radioButton =
2933 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2934 radioButton.setChecked(position == mCheckedItem);
2935 return view;
2936 }
2937 }
2938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002940 synchronized (mMethodMap) {
2941 hideInputMethodMenuLocked();
2942 }
2943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002944
The Android Open Source Project10592532009-03-18 17:39:46 -07002945 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002946 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947
The Android Open Source Project10592532009-03-18 17:39:46 -07002948 if (mSwitchingDialog != null) {
2949 mSwitchingDialog.dismiss();
2950 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002952
Jason Monk807ef762014-05-08 15:47:46 -04002953 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002954 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002955 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
satok42c5a162011-05-26 16:46:14 +09002960 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002962 // TODO: Make this work even for non-current users?
2963 if (!calledFromValidUser()) {
2964 return false;
2965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 synchronized (mMethodMap) {
2967 if (mContext.checkCallingOrSelfPermission(
2968 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2969 != PackageManager.PERMISSION_GRANTED) {
2970 throw new SecurityException(
2971 "Requires permission "
2972 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2973 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 long ident = Binder.clearCallingIdentity();
2976 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002977 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 } finally {
2979 Binder.restoreCallingIdentity(ident);
2980 }
2981 }
2982 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002983
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002984 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2985 // Make sure this is a valid input method.
2986 InputMethodInfo imm = mMethodMap.get(id);
2987 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002988 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002989 }
2990
satokd87c2592010-09-29 11:52:06 +09002991 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2992 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002993
satokd87c2592010-09-29 11:52:06 +09002994 if (enabled) {
2995 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2996 if (pair.first.equals(id)) {
2997 // We are enabling this input method, but it is already enabled.
2998 // Nothing to do. The previous state was enabled.
2999 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003000 }
3001 }
satokd87c2592010-09-29 11:52:06 +09003002 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3003 // Previous state was disabled.
3004 return false;
3005 } else {
3006 StringBuilder builder = new StringBuilder();
3007 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3008 builder, enabledInputMethodsList, id)) {
3009 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003010 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003011 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3012 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3013 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003014 }
3015 // Previous state was enabled.
3016 return true;
3017 } else {
3018 // We are disabling the input method but it is already disabled.
3019 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003020 return false;
3021 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003022 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003023 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003024
satok723a27e2010-11-11 14:58:11 +09003025 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3026 boolean setSubtypeOnly) {
3027 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003028 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003029
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003030 mCurUserActionNotificationSequenceNumber =
3031 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3032 if (DEBUG) {
3033 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3034 + mCurUserActionNotificationSequenceNumber);
3035 }
3036
3037 if (mCurClient != null && mCurClient.client != null) {
3038 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3039 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003040 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003041 }
3042
satok723a27e2010-11-11 14:58:11 +09003043 // Set Subtype here
3044 if (imi == null || subtypeId < 0) {
3045 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003046 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003047 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003048 if (subtypeId < imi.getSubtypeCount()) {
3049 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3050 mSettings.putSelectedSubtype(subtype.hashCode());
3051 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003052 } else {
3053 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003054 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003055 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003056 }
satokab751aa2010-09-14 19:17:36 +09003057 }
satok723a27e2010-11-11 14:58:11 +09003058
satok4c0e7152012-06-20 20:08:44 +09003059 // Workaround.
3060 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3061 // IMEs are not recognized and considered uninstalled.
3062 // Actually, we can't move everything after SystemReady because
3063 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3064 // the default IME here and try cheking the default IME again in systemReady().
3065 // TODO: Do nothing before system ready and implement a separated logic for
3066 // the encryption lock screen.
3067 // TODO: ASEC should be ready before IMMS is instantiated.
3068 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003069 // Set InputMethod here
3070 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3071 }
3072 }
3073
3074 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3075 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3076 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3077 // newDefaultIme is empty when there is no candidate for the selected IME.
3078 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3079 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3080 if (subtypeHashCode != null) {
3081 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003082 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003083 imi, Integer.valueOf(subtypeHashCode));
3084 } catch (NumberFormatException e) {
3085 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3086 }
3087 }
3088 }
3089 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003090 }
3091
satok4e4569d2010-11-19 18:45:53 +09003092 // If there are no selected shortcuts, tries finding the most applicable ones.
3093 private Pair<InputMethodInfo, InputMethodSubtype>
3094 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3095 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3096 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003097 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003098 boolean foundInSystemIME = false;
3099
3100 // Search applicable subtype for each InputMethodInfo
3101 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003102 final String imiId = imi.getId();
3103 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3104 continue;
3105 }
satokcd7cd292010-11-20 15:46:23 +09003106 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003107 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003108 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003109 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003110 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003111 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003112 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003113 }
satokdf31ae62011-01-15 06:19:44 +09003114 // 2. Search by the system locale from enabledSubtypes.
3115 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003116 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003117 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003118 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003119 }
satoka86f5e42011-09-02 17:12:42 +09003120 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003121 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003122 final ArrayList<InputMethodSubtype> subtypesForSearch =
3123 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003124 ? InputMethodUtils.getSubtypes(imi)
3125 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003126 // 4. Search by the current subtype's locale from all subtypes.
3127 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003128 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003129 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003130 }
3131 // 5. Search by the system locale from all subtypes.
3132 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003133 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003134 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003135 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003136 }
satokcd7cd292010-11-20 15:46:23 +09003137 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003138 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003139 // The current input method is the most applicable IME.
3140 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003141 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003142 break;
satok7599a7f2010-12-22 13:45:23 +09003143 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003144 // The system input method is 2nd applicable IME.
3145 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003146 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003147 if ((imi.getServiceInfo().applicationInfo.flags
3148 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3149 foundInSystemIME = true;
3150 }
satok4e4569d2010-11-19 18:45:53 +09003151 }
3152 }
3153 }
3154 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003155 if (mostApplicableIMI != null) {
3156 Slog.w(TAG, "Most applicable shortcut input method was:"
3157 + mostApplicableIMI.getId());
3158 if (mostApplicableSubtype != null) {
3159 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3160 + "," + mostApplicableSubtype.getMode() + ","
3161 + mostApplicableSubtype.getLocale());
3162 }
3163 }
satok4e4569d2010-11-19 18:45:53 +09003164 }
satokcd7cd292010-11-20 15:46:23 +09003165 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003166 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003167 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003168 } else {
3169 return null;
3170 }
3171 }
3172
satokab751aa2010-09-14 19:17:36 +09003173 /**
3174 * @return Return the current subtype of this input method.
3175 */
satok42c5a162011-05-26 16:46:14 +09003176 @Override
satokab751aa2010-09-14 19:17:36 +09003177 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003178 // TODO: Make this work even for non-current users?
3179 if (!calledFromValidUser()) {
3180 return null;
3181 }
3182 synchronized (mMethodMap) {
3183 return getCurrentInputMethodSubtypeLocked();
3184 }
3185 }
3186
3187 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003188 if (mCurMethodId == null) {
3189 return null;
3190 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003191 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003192 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3193 if (imi == null || imi.getSubtypeCount() == 0) {
3194 return null;
satok4e4569d2010-11-19 18:45:53 +09003195 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003196 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003197 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3198 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003199 if (subtypeId == NOT_A_SUBTYPE_ID) {
3200 // If there are no selected subtypes, the framework will try to find
3201 // the most applicable subtype from explicitly or implicitly enabled
3202 // subtypes.
3203 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003204 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003205 // If there is only one explicitly or implicitly enabled subtype,
3206 // just returns it.
3207 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3208 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3209 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003210 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003211 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003212 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003213 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003214 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003215 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3216 true);
satok4e4569d2010-11-19 18:45:53 +09003217 }
satok3ef8b292010-11-23 06:06:29 +09003218 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003219 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003220 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003221 }
3222 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003223 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003224 }
3225
satokf3db1af2010-11-23 13:34:33 +09003226 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3227 InputMethodSubtype subtype) {
3228 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3229 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3230 } else {
3231 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3232 subtypes.add(subtype);
3233 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3234 }
3235 }
3236
satok4e4569d2010-11-19 18:45:53 +09003237 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003238 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003239 @Override
satok4e4569d2010-11-19 18:45:53 +09003240 public List getShortcutInputMethodsAndSubtypes() {
3241 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003242 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003243 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003244 // If there are no selected shortcut subtypes, the framework will try to find
3245 // the most applicable subtype from all subtypes whose mode is
3246 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003247 Pair<InputMethodInfo, InputMethodSubtype> info =
3248 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003249 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003250 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003251 ret.add(info.first);
3252 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003253 }
satok3da92232011-01-11 22:46:30 +09003254 return ret;
satokf3db1af2010-11-23 13:34:33 +09003255 }
satokf3db1af2010-11-23 13:34:33 +09003256 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3257 ret.add(imi);
3258 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3259 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003260 }
3261 }
satokf3db1af2010-11-23 13:34:33 +09003262 return ret;
satok4e4569d2010-11-19 18:45:53 +09003263 }
3264 }
3265
satok42c5a162011-05-26 16:46:14 +09003266 @Override
satokb66d2872010-11-10 01:04:04 +09003267 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003268 // TODO: Make this work even for non-current users?
3269 if (!calledFromValidUser()) {
3270 return false;
3271 }
satokb66d2872010-11-10 01:04:04 +09003272 synchronized (mMethodMap) {
3273 if (subtype != null && mCurMethodId != null) {
3274 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003275 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003276 if (subtypeId != NOT_A_SUBTYPE_ID) {
3277 setInputMethodLocked(mCurMethodId, subtypeId);
3278 return true;
3279 }
3280 }
3281 return false;
3282 }
3283 }
3284
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003285 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003286 private static class InputMethodFileManager {
3287 private static final String SYSTEM_PATH = "system";
3288 private static final String INPUT_METHOD_PATH = "inputmethod";
3289 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3290 private static final String NODE_SUBTYPES = "subtypes";
3291 private static final String NODE_SUBTYPE = "subtype";
3292 private static final String NODE_IMI = "imi";
3293 private static final String ATTR_ID = "id";
3294 private static final String ATTR_LABEL = "label";
3295 private static final String ATTR_ICON = "icon";
3296 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3297 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3298 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3299 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3300 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3301 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003302 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003303 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003304 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003305 if (methodMap == null) {
3306 throw new NullPointerException("methodMap is null");
3307 }
3308 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003309 final File systemDir = userId == UserHandle.USER_OWNER
3310 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3311 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003312 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3313 if (!inputMethodDir.mkdirs()) {
3314 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3315 }
3316 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3317 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3318 if (!subtypeFile.exists()) {
3319 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003320 writeAdditionalInputMethodSubtypes(
3321 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003322 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003323 readAdditionalInputMethodSubtypes(
3324 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003325 }
3326 }
3327
3328 private void deleteAllInputMethodSubtypes(String imiId) {
3329 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003330 mAdditionalSubtypesMap.remove(imiId);
3331 writeAdditionalInputMethodSubtypes(
3332 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003333 }
3334 }
3335
3336 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003337 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003338 synchronized (mMethodMap) {
3339 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3340 final int N = additionalSubtypes.length;
3341 for (int i = 0; i < N; ++i) {
3342 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003343 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003344 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003345 } else {
3346 Slog.w(TAG, "Duplicated subtype definition found: "
3347 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003348 }
3349 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003350 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3351 writeAdditionalInputMethodSubtypes(
3352 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003353 }
3354 }
3355
3356 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3357 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003358 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003359 }
3360 }
3361
3362 private static void writeAdditionalInputMethodSubtypes(
3363 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3364 HashMap<String, InputMethodInfo> methodMap) {
3365 // Safety net for the case that this function is called before methodMap is set.
3366 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3367 FileOutputStream fos = null;
3368 try {
3369 fos = subtypesFile.startWrite();
3370 final XmlSerializer out = new FastXmlSerializer();
3371 out.setOutput(fos, "utf-8");
3372 out.startDocument(null, true);
3373 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3374 out.startTag(null, NODE_SUBTYPES);
3375 for (String imiId : allSubtypes.keySet()) {
3376 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3377 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3378 continue;
3379 }
3380 out.startTag(null, NODE_IMI);
3381 out.attribute(null, ATTR_ID, imiId);
3382 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3383 final int N = subtypesList.size();
3384 for (int i = 0; i < N; ++i) {
3385 final InputMethodSubtype subtype = subtypesList.get(i);
3386 out.startTag(null, NODE_SUBTYPE);
3387 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3388 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3389 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3390 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3391 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3392 out.attribute(null, ATTR_IS_AUXILIARY,
3393 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3394 out.endTag(null, NODE_SUBTYPE);
3395 }
3396 out.endTag(null, NODE_IMI);
3397 }
3398 out.endTag(null, NODE_SUBTYPES);
3399 out.endDocument();
3400 subtypesFile.finishWrite(fos);
3401 } catch (java.io.IOException e) {
3402 Slog.w(TAG, "Error writing subtypes", e);
3403 if (fos != null) {
3404 subtypesFile.failWrite(fos);
3405 }
3406 }
3407 }
3408
3409 private static void readAdditionalInputMethodSubtypes(
3410 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3411 if (allSubtypes == null || subtypesFile == null) return;
3412 allSubtypes.clear();
3413 FileInputStream fis = null;
3414 try {
3415 fis = subtypesFile.openRead();
3416 final XmlPullParser parser = Xml.newPullParser();
3417 parser.setInput(fis, null);
3418 int type = parser.getEventType();
3419 // Skip parsing until START_TAG
3420 while ((type = parser.next()) != XmlPullParser.START_TAG
3421 && type != XmlPullParser.END_DOCUMENT) {}
3422 String firstNodeName = parser.getName();
3423 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3424 throw new XmlPullParserException("Xml doesn't start with subtypes");
3425 }
3426 final int depth =parser.getDepth();
3427 String currentImiId = null;
3428 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3429 while (((type = parser.next()) != XmlPullParser.END_TAG
3430 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3431 if (type != XmlPullParser.START_TAG)
3432 continue;
3433 final String nodeName = parser.getName();
3434 if (NODE_IMI.equals(nodeName)) {
3435 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3436 if (TextUtils.isEmpty(currentImiId)) {
3437 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3438 continue;
3439 }
3440 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3441 allSubtypes.put(currentImiId, tempSubtypesArray);
3442 } else if (NODE_SUBTYPE.equals(nodeName)) {
3443 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3444 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3445 continue;
3446 }
3447 final int icon = Integer.valueOf(
3448 parser.getAttributeValue(null, ATTR_ICON));
3449 final int label = Integer.valueOf(
3450 parser.getAttributeValue(null, ATTR_LABEL));
3451 final String imeSubtypeLocale =
3452 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3453 final String imeSubtypeMode =
3454 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3455 final String imeSubtypeExtraValue =
3456 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003457 final boolean isAuxiliary = "1".equals(String.valueOf(
3458 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003459 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3460 .setSubtypeNameResId(label)
3461 .setSubtypeIconResId(icon)
3462 .setSubtypeLocale(imeSubtypeLocale)
3463 .setSubtypeMode(imeSubtypeMode)
3464 .setSubtypeExtraValue(imeSubtypeExtraValue)
3465 .setIsAuxiliary(isAuxiliary)
3466 .build();
satoke7c6998e2011-06-03 17:57:59 +09003467 tempSubtypesArray.add(subtype);
3468 }
3469 }
3470 } catch (XmlPullParserException e) {
3471 Slog.w(TAG, "Error reading subtypes: " + e);
3472 return;
3473 } catch (java.io.IOException e) {
3474 Slog.w(TAG, "Error reading subtypes: " + e);
3475 return;
3476 } catch (NumberFormatException e) {
3477 Slog.w(TAG, "Error reading subtypes: " + e);
3478 return;
3479 } finally {
3480 if (fis != null) {
3481 try {
3482 fis.close();
3483 } catch (java.io.IOException e1) {
3484 Slog.w(TAG, "Failed to close.");
3485 }
3486 }
3487 }
3488 }
3489 }
3490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 @Override
3492 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3493 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3494 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3497 + Binder.getCallingPid()
3498 + ", uid=" + Binder.getCallingUid());
3499 return;
3500 }
3501
3502 IInputMethod method;
3503 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 synchronized (mMethodMap) {
3508 p.println("Current Input Method Manager state:");
3509 int N = mMethodList.size();
3510 p.println(" Input Methods:");
3511 for (int i=0; i<N; i++) {
3512 InputMethodInfo info = mMethodList.get(i);
3513 p.println(" InputMethod #" + i + ":");
3514 info.dump(p, " ");
3515 }
3516 p.println(" Clients:");
3517 for (ClientState ci : mClients.values()) {
3518 p.println(" Client " + ci + ":");
3519 p.println(" client=" + ci.client);
3520 p.println(" inputContext=" + ci.inputContext);
3521 p.println(" sessionRequested=" + ci.sessionRequested);
3522 p.println(" curSession=" + ci.curSession);
3523 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003524 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003526 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3527 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3529 + " mBoundToMethod=" + mBoundToMethod);
3530 p.println(" mCurToken=" + mCurToken);
3531 p.println(" mCurIntent=" + mCurIntent);
3532 method = mCurMethod;
3533 p.println(" mCurMethod=" + mCurMethod);
3534 p.println(" mEnabledSession=" + mEnabledSession);
3535 p.println(" mShowRequested=" + mShowRequested
3536 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3537 + " mShowForced=" + mShowForced
3538 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003539 p.println(" mCurUserActionNotificationSequenceNumber="
3540 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003541 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003543
Jeff Brownb88102f2010-09-08 11:49:43 -07003544 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 pw.flush();
3547 try {
3548 client.client.asBinder().dump(fd, args);
3549 } catch (RemoteException e) {
3550 p.println("Input method client dead: " + e);
3551 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003552 } else {
3553 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003555
Jeff Brownb88102f2010-09-08 11:49:43 -07003556 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 pw.flush();
3559 try {
3560 method.asBinder().dump(fd, args);
3561 } catch (RemoteException e) {
3562 p.println("Input method service dead: " + e);
3563 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003564 } else {
3565 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 }
3567 }
3568}