blob: 2e7b745782f8ce4ec84ec2072ffd9aab15dcc770 [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;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900110import android.widget.CompoundButton;
111import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900112import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900113import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900114import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
satoke7c6998e2011-06-03 17:57:59 +0900116import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900118import java.io.FileInputStream;
119import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.io.IOException;
121import java.io.PrintWriter;
122import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900123import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.util.HashMap;
125import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900126import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128/**
129 * This class provides a system service that manages input methods.
130 */
131public class InputMethodManagerService extends IInputMethodManager.Stub
132 implements ServiceConnection, Handler.Callback {
133 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700134 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900137 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900138 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900139 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final int MSG_UNBIND_INPUT = 1000;
142 static final int MSG_BIND_INPUT = 1010;
143 static final int MSG_SHOW_SOFT_INPUT = 1020;
144 static final int MSG_HIDE_SOFT_INPUT = 1030;
145 static final int MSG_ATTACH_TOKEN = 1040;
146 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_START_INPUT = 2000;
149 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final int MSG_UNBIND_METHOD = 3000;
152 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700153 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900154 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
satok01038492012-04-09 21:08:27 +0900156 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
157
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700158 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
satokf9f01002011-05-19 21:31:50 +0900160 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
161
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900162 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900163 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900164
satok4e4569d2010-11-19 18:45:53 +0900165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800167 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900169 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final IWindowManager mIWindowManager;
172 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700173 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900174 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900175 private final HardKeyboardListener mHardKeyboardListener;
176 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900178 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 // All known input methods. mMethodMap also serves as the global
181 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900182 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
183 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900184 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
185 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900186 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700188 // Used to bring IME service up to visible adjustment while it is being shown.
189 final ServiceConnection mVisibleConnection = new ServiceConnection() {
190 @Override public void onServiceConnected(ComponentName name, IBinder service) {
191 }
192
193 @Override public void onServiceDisconnected(ComponentName name) {
194 }
195 };
196 boolean mVisibleBound = false;
197
satok7cfc0ed2011-06-20 21:29:36 +0900198 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700199 private NotificationManager mNotificationManager;
200 private KeyguardManager mKeyguardManager;
201 private StatusBarManagerService mStatusBar;
202 private Notification mImeSwitcherNotification;
203 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900204 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900205 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900206 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900207
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900208 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final ClientState client;
210 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700211
212 IInputMethodSession session;
213 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 @Override
216 public String toString() {
217 return "SessionState{uid " + client.uid + " pid " + client.pid
218 + " method " + Integer.toHexString(
219 System.identityHashCode(method))
220 + " session " + Integer.toHexString(
221 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700222 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 + "}";
224 }
225
226 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700227 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 client = _client;
229 method = _method;
230 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700231 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
Jeff Brownc28867a2013-03-26 15:42:39 -0700235 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final IInputMethodClient client;
237 final IInputContext inputContext;
238 final int uid;
239 final int pid;
240 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 boolean sessionRequested;
243 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 @Override
246 public String toString() {
247 return "ClientState{" + Integer.toHexString(
248 System.identityHashCode(this)) + " uid " + uid
249 + " pid " + pid + "}";
250 }
251
252 ClientState(IInputMethodClient _client, IInputContext _inputContext,
253 int _uid, int _pid) {
254 client = _client;
255 inputContext = _inputContext;
256 uid = _uid;
257 pid = _pid;
258 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
259 }
260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 final HashMap<IBinder, ClientState> mClients
263 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 * Set once the system is ready to run third party code.
267 */
268 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 * Id of the currently selected input method.
272 */
273 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * The current binding sequence number, incremented every time there is
277 * a new bind performed.
278 */
279 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * The client that is currently bound to an input method.
283 */
284 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 * The last window token that gained focus.
288 */
289 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 * The input context last provided by the current client.
293 */
294 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
297 * The attributes last provided by the current client.
298 */
299 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * The input method ID of the input method service that we are currently
303 * connected to or in the process of connecting to.
304 */
305 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
satokab751aa2010-09-14 19:17:36 +0900308 * The current subtype of the current input method.
309 */
310 private InputMethodSubtype mCurrentSubtype;
311
satok4e4569d2010-11-19 18:45:53 +0900312 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900313 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
314 mShortcutInputMethodsAndSubtypes =
315 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900316
John Spurlocke0980502013-10-25 11:59:29 -0400317 // Was the keyguard locked when this client became current?
318 private boolean mCurClientInKeyguard;
319
satokab751aa2010-09-14 19:17:36 +0900320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * Set to true if our ServiceConnection is currently actively bound to
322 * a service (whether or not we have gotten its IBinder back yet).
323 */
324 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Set if the client has asked for the input method to be shown.
328 */
329 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * Set if we were explicitly told to show the input method.
333 */
334 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
337 * Set if we were forced to be shown.
338 */
339 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
342 * Set if we last told the input method to show itself.
343 */
344 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 /**
347 * The Intent used to connect to the current input method.
348 */
349 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 /**
352 * The token we have made for the currently active input method, to
353 * identify it in the future.
354 */
355 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * If non-null, this is the input method service we are currently connected
359 * to.
360 */
361 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * Time that we last initiated a bind to the input method, to determine
365 * if we should try to disconnect and reconnect to it.
366 */
367 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * Have we called mCurMethod.bindInput()?
371 */
372 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * Currently enabled session. Only touched by service thread, not
376 * protected by a lock.
377 */
378 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
381 * True if the screen is on. The value is true initially.
382 */
383 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900385 int mCurUserActionNotificationSequenceNumber = 0;
386
Joe Onorato857fd9b2011-01-27 15:08:35 -0800387 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
388 int mImeWindowVis;
389
Ken Wakasa05dbb652011-08-22 15:22:43 +0900390 private AlertDialog.Builder mDialogBuilder;
391 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900392 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900393 private InputMethodInfo[] mIms;
394 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900395 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700396 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900397 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
398 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800401 String mLastEnabled = "";
402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 SettingsObserver(Handler handler) {
404 super(handler);
405 ContentResolver resolver = mContext.getContentResolver();
406 resolver.registerContentObserver(Settings.Secure.getUriFor(
407 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900408 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900409 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
410 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900411 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700412 resolver.registerContentObserver(Settings.Secure.getUriFor(
413 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
Michael Wright7b5a96b2014-08-09 19:28:42 -0700416 @Override public void onChange(boolean selfChange, Uri uri) {
417 final Uri showImeUri =
418 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700420 if (showImeUri.equals(uri)) {
421 updateKeyboardFromSettingsLocked();
422 } else {
423 boolean enabledChanged = false;
424 String newEnabled = mSettings.getEnabledInputMethodsStr();
425 if (!mLastEnabled.equals(newEnabled)) {
426 mLastEnabled = newEnabled;
427 enabledChanged = true;
428 }
429 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432 }
433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900435 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
436 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700438 if (mCurClient != null && mCurClient.client != null) {
439 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
440 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443
444 @Override
445 public void onReceive(Context context, Intent intent) {
446 final String action = intent.getAction();
447 if (Intent.ACTION_SCREEN_ON.equals(action)) {
448 mScreenOn = true;
449 refreshImeWindowVisibilityLocked();
450 updateActive();
451 return;
452 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
453 mScreenOn = false;
454 setImeWindowVisibilityStatusHiddenLocked();
455 updateActive();
456 return;
457 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
458 hideInputMethodMenu();
459 // No need to updateActive
460 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800461 } else if (Intent.ACTION_USER_ADDED.equals(action)
462 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100463 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800464 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900465 } else {
466 Slog.w(TAG, "Unexpected intent " + intent);
467 }
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800471 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900472 private boolean isChangingPackagesOfCurrentUser() {
473 final int userId = getChangingUserId();
474 final boolean retval = userId == mSettings.getCurrentUserId();
475 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900476 if (!retval) {
477 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
478 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900479 }
480 return retval;
481 }
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 if (!isChangingPackagesOfCurrentUser()) {
486 return false;
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900489 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 final int N = mMethodList.size();
491 if (curInputMethodId != null) {
492 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800493 InputMethodInfo imi = mMethodList.get(i);
494 if (imi.getId().equals(curInputMethodId)) {
495 for (String pkg : packages) {
496 if (imi.getPackageName().equals(pkg)) {
497 if (!doit) {
498 return true;
499 }
satok723a27e2010-11-11 14:58:11 +0900500 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 chooseNewDefaultIMELocked();
502 return true;
503 }
504 }
505 }
506 }
507 }
508 }
509 return false;
510 }
511
512 @Override
513 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900514 if (!isChangingPackagesOfCurrentUser()) {
515 return;
516 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517 synchronized (mMethodMap) {
518 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900519 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800520 final int N = mMethodList.size();
521 if (curInputMethodId != null) {
522 for (int i=0; i<N; i++) {
523 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900524 final String imiId = imi.getId();
525 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800526 curIm = imi;
527 }
satoke7c6998e2011-06-03 17:57:59 +0900528
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900530 if (isPackageModified(imi.getPackageName())) {
531 mFileManager.deleteAllInputMethodSubtypes(imiId);
532 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 if (change == PACKAGE_TEMPORARY_CHANGE
534 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800536 + imi.getComponent());
537 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 }
540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900542 buildInputMethodListLocked(
543 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800547 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 int change = isPackageDisappearing(curIm.getPackageName());
549 if (change == PACKAGE_TEMPORARY_CHANGE
550 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800551 ServiceInfo si = null;
552 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900553 si = mIPackageManager.getServiceInfo(
554 curIm.getComponent(), 0, mSettings.getCurrentUserId());
555 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800556 }
557 if (si == null) {
558 // Uh oh, current input method is no longer around!
559 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800560 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900561 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800562 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800563 changed = true;
564 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900566 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800567 }
568 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800569 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800570 }
satokab751aa2010-09-14 19:17:36 +0900571
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800572 if (curIm == null) {
573 // We currently don't have a default input method... is
574 // one now available?
575 changed = chooseNewDefaultIMELocked();
576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800578 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800579 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 }
582 }
583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
Jeff Brownc28867a2013-03-26 15:42:39 -0700585 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900586 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700587 private final IInputMethod mMethod;
588 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
Jeff Brownc28867a2013-03-26 15:42:39 -0700590 MethodCallback(InputMethodManagerService imms, IInputMethod method,
591 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900592 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700593 mMethod = method;
594 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
satoke7c6998e2011-06-03 17:57:59 +0900597 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700598 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700599 long ident = Binder.clearCallingIdentity();
600 try {
601 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
602 } finally {
603 Binder.restoreCallingIdentity(ident);
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800607
satok01038492012-04-09 21:08:27 +0900608 private class HardKeyboardListener
609 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
610 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700611 public void onHardKeyboardStatusChange(boolean available) {
612 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
613 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900614 }
615
Michael Wright7b5a96b2014-08-09 19:28:42 -0700616 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900617 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700618 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900619 }
620 synchronized(mMethodMap) {
621 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
622 && mSwitchingDialog.isShowing()) {
623 mSwitchingDialogTitleView.findViewById(
624 com.android.internal.R.id.hard_keyboard_section).setVisibility(
625 available ? View.VISIBLE : View.GONE);
626 }
627 }
628 }
629 }
630
631 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900632 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800634 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 mHandler = new Handler(this);
636 mIWindowManager = IWindowManager.Stub.asInterface(
637 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800638 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900639 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public void executeMessage(Message msg) {
641 handleMessage(msg);
642 }
Mita Yuned218c72012-12-06 17:18:25 -0800643 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900644 mWindowManagerService = windowManager;
645 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700646 mHasFeature = context.getPackageManager().hasSystemFeature(
647 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900648
satok7cfc0ed2011-06-20 21:29:36 +0900649 mImeSwitcherNotification = new Notification();
650 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
651 mImeSwitcherNotification.when = 0;
652 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
653 mImeSwitcherNotification.tickerText = null;
654 mImeSwitcherNotification.defaults = 0; // please be quiet
655 mImeSwitcherNotification.sound = null;
656 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400657
658 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
660 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400661
satok7cfc0ed2011-06-20 21:29:36 +0900662 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900663 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900664
665 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900666
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 final IntentFilter broadcastFilter = new IntentFilter();
668 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
669 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
670 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800671 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
672 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900673 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
satok7cfc0ed2011-06-20 21:29:36 +0900675 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900676 int userId = 0;
677 try {
678 ActivityManagerNative.getDefault().registerUserSwitchObserver(
679 new IUserSwitchObserver.Stub() {
680 @Override
681 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900682 synchronized(mMethodMap) {
683 switchUserLocked(newUserId);
684 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 if (reply != null) {
686 try {
687 reply.sendResult(null);
688 } catch (RemoteException e) {
689 }
690 }
691 }
692
693 @Override
694 public void onUserSwitchComplete(int newUserId) throws RemoteException {
695 }
696 });
697 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
698 } catch (RemoteException e) {
699 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
700 }
satok81f8b7c2012-12-04 20:42:56 +0900701 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900702
satokd87c2592010-09-29 11:52:06 +0900703 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900704 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900705 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100706 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900707 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900708 synchronized (mMethodMap) {
709 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
710 mSettings, context);
711 }
satok0a1bcf42012-05-16 19:26:31 +0900712
713 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900714 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900715 if (DEBUG) {
716 Slog.d(TAG, "Initial default ime = " + defaultImiId);
717 }
satok0a1bcf42012-05-16 19:26:31 +0900718 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
719
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900720 synchronized (mMethodMap) {
721 buildInputMethodListLocked(mMethodList, mMethodMap,
722 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
723 }
satokd87c2592010-09-29 11:52:06 +0900724 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725
satok0a1bcf42012-05-16 19:26:31 +0900726 if (!mImeSelectedOnBoot) {
727 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900728 synchronized (mMethodMap) {
729 resetDefaultImeLocked(context);
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900734 synchronized (mMethodMap) {
735 updateFromSettingsLocked(true);
736 }
satok5b927c432012-05-01 20:09:34 +0900737
738 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
739 // according to the new system locale.
740 final IntentFilter filter = new IntentFilter();
741 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
742 mContext.registerReceiver(
743 new BroadcastReceiver() {
744 @Override
745 public void onReceive(Context context, Intent intent) {
746 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900747 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900748 }
749 }
750 }, filter);
751 }
752
satok5b927c432012-05-01 20:09:34 +0900753 private void resetDefaultImeLocked(Context context) {
754 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900755 if (mCurMethodId != null
756 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900757 return;
758 }
759
760 InputMethodInfo defIm = null;
761 for (InputMethodInfo imi : mMethodList) {
762 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900763 if (InputMethodUtils.isValidSystemDefaultIme(
764 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900765 defIm = imi;
766 Slog.i(TAG, "Selected default: " + imi.getId());
767 }
768 }
769 }
770 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900771 defIm = InputMethodUtils.getMostApplicableDefaultIME(
772 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900773 Slog.i(TAG, "No default found, using " + defIm.getId());
774 }
775 if (defIm != null) {
776 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
777 }
778 }
779
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900780 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
781 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 if (!mSystemReady) {
783 // not system ready
784 return;
785 }
786 final Locale newLocale = mRes.getConfiguration().locale;
787 if (!updateOnlyWhenLocaleChanged
788 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
789 if (!updateOnlyWhenLocaleChanged) {
790 hideCurrentInputLocked(0, null);
791 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700792 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900793 }
794 if (DEBUG) {
795 Slog.i(TAG, "Locale has been changed to " + newLocale);
796 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900797 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 if (!updateOnlyWhenLocaleChanged) {
799 final String selectedImiId = mSettings.getSelectedInputMethod();
800 if (TextUtils.isEmpty(selectedImiId)) {
801 // This is the first time of the user switch and
802 // set the current ime to the proper one.
803 resetDefaultImeLocked(mContext);
804 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900805 } else {
806 // If the locale is changed, needs to reset the default ime
807 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900808 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800809 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900810 mLastSystemLocale = newLocale;
811 if (!updateOnlyWhenLocaleChanged) {
812 try {
813 startInputInnerLocked();
814 } catch (RuntimeException e) {
815 Slog.w(TAG, "Unexpected exception", e);
816 }
817 }
818 }
819 }
820
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900821 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900822 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
823 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900824 }
825
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900826 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900827 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100828 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900829 // InputMethodFileManager should be reset when the user is changed
830 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900831 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900832 // For secondary users, the list of enabled IMEs may not have been updated since the
833 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
834 // not be empty even if the IME has been uninstalled by the primary user.
835 // Even in such cases, IMMS works fine because it will find the most applicable
836 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900837 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900838 if (DEBUG) {
839 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
840 }
841 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900842 initialUserSwitch /* needsToResetDefaultIme */);
843 if (initialUserSwitch) {
844 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
845 mSettings.getEnabledInputMethodListLocked());
846 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900847 }
848
Kenny Guy2a764942014-04-02 13:29:20 +0100849 void updateCurrentProfileIds() {
850 List<UserInfo> profiles =
851 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
852 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
853 for (int i = 0; i < currentProfileIds.length; i++) {
854 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800855 }
Kenny Guy2a764942014-04-02 13:29:20 +0100856 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800857 }
858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 @Override
860 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
861 throws RemoteException {
862 try {
863 return super.onTransact(code, data, reply, flags);
864 } catch (RuntimeException e) {
865 // The input method manager only throws security exceptions, so let's
866 // log all others.
867 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700868 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870 throw e;
871 }
872 }
873
Svetoslav Ganova0027152013-06-25 14:59:53 -0700874 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700875 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900876 if (DEBUG) {
877 Slog.d(TAG, "--- systemReady");
878 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700879 if (!mSystemReady) {
880 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900881 mKeyguardManager =
882 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700883 mNotificationManager = (NotificationManager)
884 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
885 mStatusBar = statusBar;
886 statusBar.setIconVisibility("ime", false);
887 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900888 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
889 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900890 if (mShowOngoingImeSwitcherForPhones) {
891 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
892 mHardKeyboardListener);
893 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900894 buildInputMethodListLocked(mMethodList, mMethodMap,
895 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900896 if (!mImeSelectedOnBoot) {
897 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900898 resetStateIfCurrentLocaleChangedLocked();
899 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
900 mContext.getPackageManager(),
901 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900902 }
903 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700904 try {
905 startInputInnerLocked();
906 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800907 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700908 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700909 }
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
satok15452a42011-10-28 17:58:28 +0900913 private void setImeWindowVisibilityStatusHiddenLocked() {
914 mImeWindowVis = 0;
915 updateImeWindowStatusLocked();
916 }
917
satok3afd6c02011-11-18 08:38:19 +0900918 private void refreshImeWindowVisibilityLocked() {
919 final Configuration conf = mRes.getConfiguration();
920 final boolean haveHardKeyboard = conf.keyboard
921 != Configuration.KEYBOARD_NOKEYS;
922 final boolean hardKeyShown = haveHardKeyboard
923 && conf.hardKeyboardHidden
924 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400925
926 final boolean isScreenLocked = isKeyguardLocked();
927 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400928 // We assume the softkeyboard is shown when the input is active as long as the
929 // hard keyboard is not shown.
930 final boolean inputVisible = inputActive && !hardKeyShown;
931 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
932 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900933 updateImeWindowStatusLocked();
934 }
935
satok15452a42011-10-28 17:58:28 +0900936 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900937 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700938 }
939
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 // ---------------------------------------------------------------------------------------
941 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
942 // 1) it comes from the system process
943 // 2) the calling process' user id is identical to the current user id IMMS thinks.
944 private boolean calledFromValidUser() {
945 final int uid = Binder.getCallingUid();
946 final int userId = UserHandle.getUserId(uid);
947 if (DEBUG) {
948 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
949 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
950 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900951 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
952 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 }
Kenny Guy2a764942014-04-02 13:29:20 +0100954 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900957
958 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
959 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
960 // must not manage background users' states in any functions.
961 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
962 // by a token.
963 if (mContext.checkCallingOrSelfPermission(
964 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
965 == PackageManager.PERMISSION_GRANTED) {
966 if (DEBUG) {
967 Slog.d(TAG, "--- Access granted because the calling process has "
968 + "the INTERACT_ACROSS_USERS_FULL permission");
969 }
970 return true;
971 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900972 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
973 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900974 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 }
976
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900977
978 /**
979 * Returns true iff the caller is identified to be the current input method with the token.
980 * @param token The window token given to the input method when it was started.
981 * @return true if and only if non-null valid token is specified.
982 */
983 private boolean calledWithValidToken(IBinder token) {
984 if (token == null || mCurToken != token) {
985 return false;
986 }
987 return true;
988 }
989
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 private boolean bindCurrentInputMethodService(
991 Intent service, ServiceConnection conn, int flags) {
992 if (service == null || conn == null) {
993 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
994 return false;
995 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800996 return mContext.bindServiceAsUser(service, conn, flags,
997 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 }
999
satoke7c6998e2011-06-03 17:57:59 +09001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001002 // TODO: Make this work even for non-current users?
1003 if (!calledFromValidUser()) {
1004 return Collections.emptyList();
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 synchronized (mMethodMap) {
1007 return new ArrayList<InputMethodInfo>(mMethodList);
1008 }
1009 }
1010
satoke7c6998e2011-06-03 17:57:59 +09001011 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001013 // TODO: Make this work even for non-current users?
1014 if (!calledFromValidUser()) {
1015 return Collections.emptyList();
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001018 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
1020 }
1021
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001022 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001023 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001024 * @return enabled subtypes of the specified imi
1025 */
satoke7c6998e2011-06-03 17:57:59 +09001026 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001027 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001028 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001029 // TODO: Make this work even for non-current users?
1030 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001031 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001032 }
satok67ddf9c2010-11-17 09:45:54 +09001033 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001034 final InputMethodInfo imi;
1035 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001036 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001037 } else {
1038 imi = mMethodMap.get(imiId);
1039 }
1040 if (imi == null) {
1041 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001042 }
1043 return mSettings.getEnabledInputMethodSubtypeListLocked(
1044 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001045 }
1046 }
1047
satoke7c6998e2011-06-03 17:57:59 +09001048 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 public void addClient(IInputMethodClient client,
1050 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001051 if (!calledFromValidUser()) {
1052 return;
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 synchronized (mMethodMap) {
1055 mClients.put(client.asBinder(), new ClientState(client,
1056 inputContext, uid, pid));
1057 }
1058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001059
satoke7c6998e2011-06-03 17:57:59 +09001060 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001062 if (!calledFromValidUser()) {
1063 return;
1064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001066 ClientState cs = mClients.remove(client.asBinder());
1067 if (cs != null) {
1068 clearClientSessionLocked(cs);
1069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 void executeOrSendMessage(IInterface target, Message msg) {
1074 if (target.asBinder() instanceof Binder) {
1075 mCaller.sendMessage(msg);
1076 } else {
1077 handleMessage(msg);
1078 msg.recycle();
1079 }
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001082 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 + mCurClient.client.asBinder());
1086 if (mBoundToMethod) {
1087 mBoundToMethod = false;
1088 if (mCurMethod != null) {
1089 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1090 MSG_UNBIND_INPUT, mCurMethod));
1091 }
1092 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001093
1094 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1095 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1097 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1098 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
The Android Open Source Project10592532009-03-18 17:39:46 -07001101 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 private int getImeShowFlags() {
1106 int flags = 0;
1107 if (mShowForced) {
1108 flags |= InputMethod.SHOW_FORCED
1109 | InputMethod.SHOW_EXPLICIT;
1110 } else if (mShowExplicitlyRequested) {
1111 flags |= InputMethod.SHOW_EXPLICIT;
1112 }
1113 return flags;
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 private int getAppShowFlags() {
1117 int flags = 0;
1118 if (mShowForced) {
1119 flags |= InputMethodManager.SHOW_FORCED;
1120 } else if (!mShowExplicitlyRequested) {
1121 flags |= InputMethodManager.SHOW_IMPLICIT;
1122 }
1123 return flags;
1124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Dianne Hackborn7663d802012-02-24 13:08:49 -08001126 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 if (!mBoundToMethod) {
1128 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1129 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1130 mBoundToMethod = true;
1131 }
1132 final SessionState session = mCurClient.curSession;
1133 if (initial) {
1134 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1135 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1136 } else {
1137 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1138 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1139 }
1140 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001142 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001144 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001145 (session.channel != null ? session.channel.dup() : null),
1146 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001150 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // If no method is currently selected, do nothing.
1152 if (mCurMethodId == null) {
1153 return mNoBinding;
1154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 ClientState cs = mClients.get(client.asBinder());
1157 if (cs == null) {
1158 throw new IllegalArgumentException("unknown client "
1159 + client.asBinder());
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 try {
1163 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1164 // Check with the window manager to make sure this client actually
1165 // has a window with focus. If not, reject. This is thread safe
1166 // because if the focus changes some time before or after, the
1167 // next client receiving focus that has any interest in input will
1168 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1171 return null;
1172 }
1173 } catch (RemoteException e) {
1174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
Dianne Hackborn7663d802012-02-24 13:08:49 -08001176 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1177 }
1178
1179 InputBindResult startInputUncheckedLocked(ClientState cs,
1180 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1181 // If no method is currently selected, do nothing.
1182 if (mCurMethodId == null) {
1183 return mNoBinding;
1184 }
1185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001187 // Was the keyguard locked when switching over to the new client?
1188 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 // If the client is changing, we need to switch over to the new
1190 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001191 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001192 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001193 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194
1195 // If the screen is on, inform the new client it is active
1196 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001197 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1198 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 // Bump up the sequence for this client and attach it.
1203 mCurSeq++;
1204 if (mCurSeq <= 0) mCurSeq = 1;
1205 mCurClient = cs;
1206 mCurInputContext = inputContext;
1207 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 // Check if the input method is changing.
1210 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1211 if (cs.curSession != null) {
1212 // Fast case: if we are already connected to the input method,
1213 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001214 return attachNewInputLocked(
1215 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
1217 if (mHaveConnection) {
1218 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 // Return to client, and we will get back with it when
1220 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001221 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001222 return new InputBindResult(null, null, mCurId, mCurSeq,
1223 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 } else if (SystemClock.uptimeMillis()
1225 < (mLastBindTime+TIME_TO_RECONNECT)) {
1226 // In this case we have connected to the service, but
1227 // don't yet have its interface. If it hasn't been too
1228 // long since we did the connection, we'll return to
1229 // the client and wait to get the service interface so
1230 // we can report back. If it has been too long, we want
1231 // to fall through so we can try a disconnect/reconnect
1232 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001233 return new InputBindResult(null, null, mCurId, mCurSeq,
1234 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1237 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 }
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001242 return startInputInnerLocked();
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001245 InputBindResult startInputInnerLocked() {
1246 if (mCurMethodId == null) {
1247 return mNoBinding;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001250 if (!mSystemReady) {
1251 // If the system is not yet ready, we shouldn't be running third
1252 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001253 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1254 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1258 if (info == null) {
1259 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001262 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1265 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001266 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1267 com.android.internal.R.string.input_method_binding_label);
1268 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1269 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001270 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001271 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1272 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mLastBindTime = SystemClock.uptimeMillis();
1274 mHaveConnection = true;
1275 mCurId = info.getId();
1276 mCurToken = new Binder();
1277 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001278 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 mIWindowManager.addWindowToken(mCurToken,
1280 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1281 } catch (RemoteException e) {
1282 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001283 return new InputBindResult(null, null, mCurId, mCurSeq,
1284 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 } else {
1286 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001287 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 + mCurIntent);
1289 }
1290 return null;
1291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
satoke7c6998e2011-06-03 17:57:59 +09001293 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001295 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001296 if (!calledFromValidUser()) {
1297 return null;
1298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 synchronized (mMethodMap) {
1300 final long ident = Binder.clearCallingIdentity();
1301 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001302 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 } finally {
1304 Binder.restoreCallingIdentity(ident);
1305 }
1306 }
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
satoke7c6998e2011-06-03 17:57:59 +09001309 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 public void finishInput(IInputMethodClient client) {
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
satoke7c6998e2011-06-03 17:57:59 +09001313 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 public void onServiceConnected(ComponentName name, IBinder service) {
1315 synchronized (mMethodMap) {
1316 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1317 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001318 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001319 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001320 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001321 return;
1322 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001323 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001324 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1325 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001327 clearClientSessionLocked(mCurClient);
1328 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 }
1331 }
1332 }
1333
Jeff Brownc28867a2013-03-26 15:42:39 -07001334 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1335 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 synchronized (mMethodMap) {
1337 if (mCurMethod != null && method != null
1338 && mCurMethod.asBinder() == method.asBinder()) {
1339 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001340 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001342 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001343 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (res.method != null) {
1345 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1346 MSG_BIND_METHOD, mCurClient.client, res));
1347 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001348 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350 }
1351 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001352
1353 // Session abandoned. Close its associated input channel.
1354 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001357 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001358 if (mVisibleBound) {
1359 mContext.unbindService(mVisibleConnection);
1360 mVisibleBound = false;
1361 }
1362
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001363 if (mHaveConnection) {
1364 mContext.unbindService(this);
1365 mHaveConnection = false;
1366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001368 if (mCurToken != null) {
1369 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001371 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001372 // The current IME is shown. Hence an IME switch (transition) is happening.
1373 mWindowManagerService.saveLastInputMethodWindowForTransition();
1374 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001375 mIWindowManager.removeWindowToken(mCurToken);
1376 } catch (RemoteException e) {
1377 }
1378 mCurToken = null;
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
The Android Open Source Project10592532009-03-18 17:39:46 -07001381 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001382 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001383
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001384 if (reportToClient && mCurClient != null) {
1385 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1386 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1387 }
1388 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001389
1390 void requestClientSessionLocked(ClientState cs) {
1391 if (!cs.sessionRequested) {
1392 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1393 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1394 cs.sessionRequested = true;
1395 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1396 MSG_CREATE_SESSION, mCurMethod, channels[1],
1397 new MethodCallback(this, mCurMethod, channels[0])));
1398 }
1399 }
1400
1401 void clearClientSessionLocked(ClientState cs) {
1402 finishSessionLocked(cs.curSession);
1403 cs.curSession = null;
1404 cs.sessionRequested = false;
1405 }
1406
1407 private void finishSessionLocked(SessionState sessionState) {
1408 if (sessionState != null) {
1409 if (sessionState.session != null) {
1410 try {
1411 sessionState.session.finishSession();
1412 } catch (RemoteException e) {
1413 Slog.w(TAG, "Session failed to close due to remote exception", e);
1414 setImeWindowVisibilityStatusHiddenLocked();
1415 }
1416 sessionState.session = null;
1417 }
1418 if (sessionState.channel != null) {
1419 sessionState.channel.dispose();
1420 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001421 }
1422 }
1423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001425 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 if (mCurMethod != null) {
1427 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001428 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001430
Jeff Brownc28867a2013-03-26 15:42:39 -07001431 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001432 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mCurMethod = null;
1434 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001435 if (mStatusBar != null) {
1436 mStatusBar.setIconVisibility("ime", false);
1437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
satoke7c6998e2011-06-03 17:57:59 +09001440 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 public void onServiceDisconnected(ComponentName name) {
1442 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001443 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 + " mCurIntent=" + mCurIntent);
1445 if (mCurMethod != null && mCurIntent != null
1446 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001447 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 // We consider this to be a new bind attempt, since the system
1449 // should now try to restart the service for us.
1450 mLastBindTime = SystemClock.uptimeMillis();
1451 mShowRequested = mInputShown;
1452 mInputShown = false;
1453 if (mCurClient != null) {
1454 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1455 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1456 }
1457 }
1458 }
1459 }
1460
satokf9f01002011-05-19 21:31:50 +09001461 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1463 long ident = Binder.clearCallingIdentity();
1464 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001466 if (!calledWithValidToken(token)) {
1467 final int uid = Binder.getCallingUid();
1468 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1469 + " token:" + token);
1470 return;
1471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001473 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001474 if (mStatusBar != null) {
1475 mStatusBar.setIconVisibility("ime", false);
1476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001478 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001479 CharSequence contentDescription = null;
1480 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 // Use PackageManager to load label
1482 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001483 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001484 mIPackageManager.getApplicationInfo(packageName, 0,
1485 mSettings.getCurrentUserId()));
1486 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001487 /* ignore */
1488 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001489 if (mStatusBar != null) {
1490 mStatusBar.setIcon("ime", packageName, iconId, 0,
1491 contentDescription != null
1492 ? contentDescription.toString() : null);
1493 mStatusBar.setIconVisibility("ime", true);
1494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
1496 }
1497 } finally {
1498 Binder.restoreCallingIdentity(ident);
1499 }
1500 }
1501
satok7cfc0ed2011-06-20 21:29:36 +09001502 private boolean needsToShowImeSwitchOngoingNotification() {
1503 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001504 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001505 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001506 synchronized (mMethodMap) {
1507 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1508 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001509 if (N > 2) return true;
1510 if (N < 1) return false;
1511 int nonAuxCount = 0;
1512 int auxCount = 0;
1513 InputMethodSubtype nonAuxSubtype = null;
1514 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001515 for(int i = 0; i < N; ++i) {
1516 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001517 final List<InputMethodSubtype> subtypes =
1518 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001519 final int subtypeCount = subtypes.size();
1520 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001521 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001522 } else {
1523 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001524 final InputMethodSubtype subtype = subtypes.get(j);
1525 if (!subtype.isAuxiliary()) {
1526 ++nonAuxCount;
1527 nonAuxSubtype = subtype;
1528 } else {
1529 ++auxCount;
1530 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001531 }
1532 }
1533 }
satok7cfc0ed2011-06-20 21:29:36 +09001534 }
satokb6359412011-06-28 17:47:41 +09001535 if (nonAuxCount > 1 || auxCount > 1) {
1536 return true;
1537 } else if (nonAuxCount == 1 && auxCount == 1) {
1538 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001539 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1540 || auxSubtype.overridesImplicitlyEnabledSubtype()
1541 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001542 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1543 return false;
1544 }
1545 return true;
1546 }
1547 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001548 }
satok7cfc0ed2011-06-20 21:29:36 +09001549 }
1550
John Spurlocke0980502013-10-25 11:59:29 -04001551 private boolean isKeyguardLocked() {
1552 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1553 }
1554
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001555 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001556 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001557 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001558 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001559 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001560 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001561 if (!calledWithValidToken(token)) {
1562 final int uid = Binder.getCallingUid();
1563 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1564 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001565 return;
1566 }
satok06487a52010-10-29 11:37:18 +09001567 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001568 // apply policy for binder calls
1569 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1570 vis = 0;
1571 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001572 mImeWindowVis = vis;
1573 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001574 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1575 && (mWindowManagerService.isHardKeyboardAvailable()
1576 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001577 final boolean needsToShowImeSwitcher = iconVisibility
1578 && needsToShowImeSwitchOngoingNotification();
1579 if (mStatusBar != null) {
1580 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1581 needsToShowImeSwitcher);
1582 }
satok5bc8e732011-07-22 21:07:23 +09001583 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001584 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001585 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001586 final CharSequence title = mRes.getText(
1587 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001588 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1589 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001590
satok7cfc0ed2011-06-20 21:29:36 +09001591 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001592 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001593 if ((mNotificationManager != null)
1594 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001595 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001596 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001597 }
1598 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001599 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001600 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001601 mNotificationShown = true;
1602 }
satok7cfc0ed2011-06-20 21:29:36 +09001603 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001604 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001605 if (DEBUG) {
1606 Slog.d(TAG, "--- hide notification");
1607 }
1608 mNotificationManager.cancelAsUser(null,
1609 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001610 mNotificationShown = false;
1611 }
1612 }
satok06487a52010-10-29 11:37:18 +09001613 }
1614 } finally {
1615 Binder.restoreCallingIdentity(ident);
1616 }
1617 }
1618
satoke7c6998e2011-06-03 17:57:59 +09001619 @Override
satokf9f01002011-05-19 21:31:50 +09001620 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 if (!calledFromValidUser()) {
1622 return;
1623 }
satokf9f01002011-05-19 21:31:50 +09001624 synchronized (mMethodMap) {
1625 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1626 for (int i = 0; i < spans.length; ++i) {
1627 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001628 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001629 mSecureSuggestionSpans.put(ss, currentImi);
1630 }
1631 }
1632 }
1633 }
1634
satoke7c6998e2011-06-03 17:57:59 +09001635 @Override
satokf9f01002011-05-19 21:31:50 +09001636 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001637 if (!calledFromValidUser()) {
1638 return false;
1639 }
satokf9f01002011-05-19 21:31:50 +09001640 synchronized (mMethodMap) {
1641 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1642 // TODO: Do not send the intent if the process of the targetImi is already dead.
1643 if (targetImi != null) {
1644 final String[] suggestions = span.getSuggestions();
1645 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001646 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001647 final Intent intent = new Intent();
1648 // Ensures that only a class in the original IME package will receive the
1649 // notification.
satok42c5a162011-05-26 16:46:14 +09001650 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001651 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1652 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1653 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1654 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001655 final long ident = Binder.clearCallingIdentity();
1656 try {
1657 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1658 } finally {
1659 Binder.restoreCallingIdentity(ident);
1660 }
satokf9f01002011-05-19 21:31:50 +09001661 return true;
1662 }
1663 }
1664 return false;
1665 }
1666
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001667 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001668 updateInputMethodsFromSettingsLocked(enabledMayChange);
1669 updateKeyboardFromSettingsLocked();
1670 }
1671
1672 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001673 if (enabledMayChange) {
1674 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1675 for (int i=0; i<enabled.size(); i++) {
1676 // We allow the user to select "disabled until used" apps, so if they
1677 // are enabling one of those here we now need to make it enabled.
1678 InputMethodInfo imm = enabled.get(i);
1679 try {
1680 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1681 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1682 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001683 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001684 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001685 if (DEBUG) {
1686 Slog.d(TAG, "Update state(" + imm.getId()
1687 + "): DISABLED_UNTIL_USED -> DEFAULT");
1688 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001689 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1690 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001691 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1692 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001693 }
1694 } catch (RemoteException e) {
1695 }
1696 }
1697 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001698 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1699 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1700 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1701 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001702 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001703 // There is no input method selected, try to choose new applicable input method.
1704 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001705 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001706 }
1707 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001709 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001711 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001712 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001713 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
satokf3db1af2010-11-23 13:34:33 +09001715 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001716 } else {
1717 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001718 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001719 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001721 // Here is not the perfect place to reset the switching controller. Ideally
1722 // mSwitchingController and mSettings should be able to share the same state.
1723 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1724 // the same enabled IMEs list.
1725 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001726
1727 }
1728
1729 public void updateKeyboardFromSettingsLocked() {
1730 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1731 if (mSwitchingDialog != null
1732 && mSwitchingDialogTitleView != null
1733 && mSwitchingDialog.isShowing()) {
1734 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1735 com.android.internal.R.id.hard_keyboard_switch);
1736 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
satokab751aa2010-09-14 19:17:36 +09001740 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 InputMethodInfo info = mMethodMap.get(id);
1742 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001743 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001745
satokd81e9502012-05-21 12:58:45 +09001746 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001748 final int subtypeCount = info.getSubtypeCount();
1749 if (subtypeCount <= 0) {
1750 return;
satokcd7cd292010-11-20 15:46:23 +09001751 }
satokd81e9502012-05-21 12:58:45 +09001752 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1753 final InputMethodSubtype newSubtype;
1754 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1755 newSubtype = info.getSubtypeAt(subtypeId);
1756 } else {
1757 // If subtype is null, try to find the most applicable one from
1758 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001759 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001760 }
1761 if (newSubtype == null || oldSubtype == null) {
1762 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1763 + ", new subtype = " + newSubtype);
1764 return;
1765 }
1766 if (newSubtype != oldSubtype) {
1767 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1768 if (mCurMethod != null) {
1769 try {
1770 refreshImeWindowVisibilityLocked();
1771 mCurMethod.changeInputMethodSubtype(newSubtype);
1772 } catch (RemoteException e) {
1773 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001774 }
1775 }
1776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 return;
1778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001779
satokd81e9502012-05-21 12:58:45 +09001780 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 final long ident = Binder.clearCallingIdentity();
1782 try {
satokab751aa2010-09-14 19:17:36 +09001783 // Set a subtype to this input method.
1784 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001785 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1786 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1787 // because mCurMethodId is stored as a history in
1788 // setSelectedInputMethodAndSubtypeLocked().
1789 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
1791 if (ActivityManagerNative.isSystemReady()) {
1792 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001793 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001795 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001797 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 } finally {
1799 Binder.restoreCallingIdentity(ident);
1800 }
1801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
satok42c5a162011-05-26 16:46:14 +09001803 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001804 public boolean showSoftInput(IInputMethodClient client, int flags,
1805 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001806 if (!calledFromValidUser()) {
1807 return false;
1808 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001809 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 long ident = Binder.clearCallingIdentity();
1811 try {
1812 synchronized (mMethodMap) {
1813 if (mCurClient == null || client == null
1814 || mCurClient.client.asBinder() != client.asBinder()) {
1815 try {
1816 // We need to check if this is the current client with
1817 // focus in the window manager, to allow this call to
1818 // be made before input is started in it.
1819 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001820 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001828 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001829 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 }
1831 } finally {
1832 Binder.restoreCallingIdentity(ident);
1833 }
1834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 mShowRequested = true;
1838 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1839 mShowExplicitlyRequested = true;
1840 }
1841 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1842 mShowExplicitlyRequested = true;
1843 mShowForced = true;
1844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
Dianne Hackborncc278702009-09-02 23:07:23 -07001846 if (!mSystemReady) {
1847 return false;
1848 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001849
The Android Open Source Project4df24232009-03-05 14:34:35 -08001850 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001852 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1854 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1855 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001857 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001858 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001859 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1860 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001861 mVisibleBound = true;
1862 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001865 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 // The client has asked to have the input method shown, but
1867 // we have been sitting here too long with a connection to the
1868 // service and no interface received, so let's disconnect/connect
1869 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001870 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001872 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001874 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001875 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001876 } else {
1877 if (DEBUG) {
1878 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1879 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
The Android Open Source Project4df24232009-03-05 14:34:35 -08001883 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
satok42c5a162011-05-26 16:46:14 +09001886 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001887 public boolean hideSoftInput(IInputMethodClient client, int flags,
1888 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001889 if (!calledFromValidUser()) {
1890 return false;
1891 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001892 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 long ident = Binder.clearCallingIdentity();
1894 try {
1895 synchronized (mMethodMap) {
1896 if (mCurClient == null || client == null
1897 || mCurClient.client.asBinder() != client.asBinder()) {
1898 try {
1899 // We need to check if this is the current client with
1900 // focus in the window manager, to allow this call to
1901 // be made before input is started in it.
1902 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001903 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1904 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001905 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001906 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001909 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001910 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
1912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001913
Joe Onorato8a9b2202010-02-26 18:56:32 -08001914 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001915 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917 } finally {
1918 Binder.restoreCallingIdentity(ident);
1919 }
1920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001921
The Android Open Source Project4df24232009-03-05 14:34:35 -08001922 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1924 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001925 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 -08001926 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001929 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 -08001930 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001932 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001934 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1935 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1936 res = true;
1937 } else {
1938 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001940 if (mHaveConnection && mVisibleBound) {
1941 mContext.unbindService(mVisibleConnection);
1942 mVisibleBound = false;
1943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 mInputShown = false;
1945 mShowRequested = false;
1946 mShowExplicitlyRequested = false;
1947 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001948 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001950
satok42c5a162011-05-26 16:46:14 +09001951 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001952 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1953 int controlFlags, int softInputMode, int windowFlags,
1954 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001955 // Needs to check the validity before clearing calling identity
1956 final boolean calledFromValidUser = calledFromValidUser();
1957
Dianne Hackborn7663d802012-02-24 13:08:49 -08001958 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 long ident = Binder.clearCallingIdentity();
1960 try {
1961 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001962 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001963 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001965 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001966
Dianne Hackborn7663d802012-02-24 13:08:49 -08001967 ClientState cs = mClients.get(client.asBinder());
1968 if (cs == null) {
1969 throw new IllegalArgumentException("unknown client "
1970 + client.asBinder());
1971 }
1972
1973 try {
1974 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1975 // Check with the window manager to make sure this client actually
1976 // has a window with focus. If not, reject. This is thread safe
1977 // because if the focus changes some time before or after, the
1978 // next client receiving focus that has any interest in input will
1979 // be calling through here after that change happens.
1980 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1981 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1982 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001984 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001986
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001987 if (!calledFromValidUser) {
1988 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1989 Slog.w(TAG, "If you want to interect with IME, you need "
1990 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1991 hideCurrentInputLocked(0, null);
1992 return null;
1993 }
1994
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001995 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001996 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001997 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001998 if (attribute != null) {
1999 return startInputUncheckedLocked(cs, inputContext, attribute,
2000 controlFlags);
2001 }
2002 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002003 }
2004 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002006 // Should we auto-show the IME even if the caller has not
2007 // specified what should be done with it?
2008 // We only do this automatically if the window can resize
2009 // to accommodate the IME (so what the user sees will give
2010 // them good context without input information being obscured
2011 // by the IME) or if running on a large screen where there
2012 // is more room for the target window + IME.
2013 final boolean doAutoShow =
2014 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2015 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2016 || mRes.getConfiguration().isLayoutSizeAtLeast(
2017 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002018 final boolean isTextEditor =
2019 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2020
2021 // We want to start input before showing the IME, but after closing
2022 // it. We want to do this after closing it to help the IME disappear
2023 // more quickly (not get stuck behind it initializing itself for the
2024 // new focused input, even if its window wants to hide the IME).
2025 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2028 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002029 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2031 // There is no focus view, and this window will
2032 // be behind any soft input window, so hide the
2033 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002034 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002035 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002037 } else if (isTextEditor && doAutoShow && (softInputMode &
2038 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 // There is a focus view, and we are navigating forward
2040 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002041 // We only do this automatically if the window can resize
2042 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002043 // them good context without input information being obscured
2044 // by the IME) or if running on a large screen where there
2045 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002046 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002047 if (attribute != null) {
2048 res = startInputUncheckedLocked(cs, inputContext, attribute,
2049 controlFlags);
2050 didStart = true;
2051 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002052 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
2054 break;
2055 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2056 // Do nothing.
2057 break;
2058 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2059 if ((softInputMode &
2060 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002061 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064 break;
2065 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002066 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002067 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 break;
2069 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2070 if ((softInputMode &
2071 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002072 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002073 if (attribute != null) {
2074 res = startInputUncheckedLocked(cs, inputContext, attribute,
2075 controlFlags);
2076 didStart = true;
2077 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080 break;
2081 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002082 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002083 if (attribute != null) {
2084 res = startInputUncheckedLocked(cs, inputContext, attribute,
2085 controlFlags);
2086 didStart = true;
2087 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002088 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 break;
2090 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002091
2092 if (!didStart && attribute != null) {
2093 res = startInputUncheckedLocked(cs, inputContext, attribute,
2094 controlFlags);
2095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(ident);
2099 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002100
2101 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002103
satok42c5a162011-05-26 16:46:14 +09002104 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002106 if (!calledFromValidUser()) {
2107 return;
2108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 synchronized (mMethodMap) {
2110 if (mCurClient == null || client == null
2111 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002112 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002113 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115
satok440aab52010-11-25 09:43:11 +09002116 // Always call subtype picker, because subtype picker is a superset of input method
2117 // picker.
satokab751aa2010-09-14 19:17:36 +09002118 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2119 }
2120 }
2121
satok42c5a162011-05-26 16:46:14 +09002122 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 if (!calledFromValidUser()) {
2125 return;
2126 }
satok28203512010-11-24 11:06:49 +09002127 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2128 }
2129
satok42c5a162011-05-26 16:46:14 +09002130 @Override
satok28203512010-11-24 11:06:49 +09002131 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002132 if (!calledFromValidUser()) {
2133 return;
2134 }
satok28203512010-11-24 11:06:49 +09002135 synchronized (mMethodMap) {
2136 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002137 setInputMethodWithSubtypeIdLocked(token, id,
2138 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2139 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002140 } else {
2141 setInputMethod(token, id);
2142 }
2143 }
satokab751aa2010-09-14 19:17:36 +09002144 }
2145
satok42c5a162011-05-26 16:46:14 +09002146 @Override
satokb416a712010-11-25 20:42:14 +09002147 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002148 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002149 if (!calledFromValidUser()) {
2150 return;
2151 }
satokb416a712010-11-25 20:42:14 +09002152 synchronized (mMethodMap) {
2153 if (mCurClient == null || client == null
2154 || mCurClient.client.asBinder() != client.asBinder()) {
2155 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2156 }
satok7fee71f2010-12-17 18:54:26 +09002157 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2158 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002159 }
2160 }
2161
satok4fc87d62011-05-20 16:13:43 +09002162 @Override
satok735cf382010-11-11 20:40:09 +09002163 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002164 if (!calledFromValidUser()) {
2165 return false;
2166 }
satok735cf382010-11-11 20:40:09 +09002167 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002168 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002169 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002170 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002171 lastImi = mMethodMap.get(lastIme.first);
2172 } else {
2173 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002174 }
satok4fc87d62011-05-20 16:13:43 +09002175 String targetLastImiId = null;
2176 int subtypeId = NOT_A_SUBTYPE_ID;
2177 if (lastIme != null && lastImi != null) {
2178 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2179 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2180 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2181 : mCurrentSubtype.hashCode();
2182 // If the last IME is the same as the current IME and the last subtype is not
2183 // defined, there is no need to switch to the last IME.
2184 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2185 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002186 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002187 }
2188 }
2189
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002190 if (TextUtils.isEmpty(targetLastImiId)
2191 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002192 // This is a safety net. If the currentSubtype can't be added to the history
2193 // and the framework couldn't find the last ime, we will make the last ime be
2194 // the most applicable enabled keyboard subtype of the system imes.
2195 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2196 if (enabled != null) {
2197 final int N = enabled.size();
2198 final String locale = mCurrentSubtype == null
2199 ? mRes.getConfiguration().locale.toString()
2200 : mCurrentSubtype.getLocale();
2201 for (int i = 0; i < N; ++i) {
2202 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002203 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002204 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002205 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2206 InputMethodUtils.getSubtypes(imi),
2207 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002208 if (keyboardSubtype != null) {
2209 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002210 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002211 imi, keyboardSubtype.hashCode());
2212 if(keyboardSubtype.getLocale().equals(locale)) {
2213 break;
2214 }
2215 }
2216 }
2217 }
2218 }
2219 }
2220
2221 if (!TextUtils.isEmpty(targetLastImiId)) {
2222 if (DEBUG) {
2223 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2224 + ", from: " + mCurMethodId + ", " + subtypeId);
2225 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002226 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002227 return true;
2228 } else {
2229 return false;
2230 }
satok735cf382010-11-11 20:40:09 +09002231 }
2232 }
2233
satoke7c6998e2011-06-03 17:57:59 +09002234 @Override
satok688bd472012-02-09 20:09:17 +09002235 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002236 if (!calledFromValidUser()) {
2237 return false;
2238 }
satok688bd472012-02-09 20:09:17 +09002239 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002240 if (!calledWithValidToken(token)) {
2241 final int uid = Binder.getCallingUid();
2242 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2243 + " token:" + token);
2244 return false;
2245 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002246 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002247 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2248 if (nextSubtype == null) {
2249 return false;
2250 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002251 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2252 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002253 return true;
2254 }
2255 }
2256
2257 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002258 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2259 if (!calledFromValidUser()) {
2260 return false;
2261 }
2262 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002263 if (!calledWithValidToken(token)) {
2264 final int uid = Binder.getCallingUid();
2265 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2266 + "token. uid:" + uid + " token:" + token);
2267 return false;
2268 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002269 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002270 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2271 if (nextSubtype == null) {
2272 return false;
2273 }
2274 return true;
2275 }
2276 }
2277
2278 @Override
satok68f1b782011-04-11 14:26:04 +09002279 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002280 if (!calledFromValidUser()) {
2281 return null;
2282 }
satok68f1b782011-04-11 14:26:04 +09002283 synchronized (mMethodMap) {
2284 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2285 // TODO: Handle the case of the last IME with no subtypes
2286 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2287 || TextUtils.isEmpty(lastIme.second)) return null;
2288 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2289 if (lastImi == null) return null;
2290 try {
2291 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002292 final int lastSubtypeId =
2293 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002294 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2295 return null;
2296 }
2297 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002298 } catch (NumberFormatException e) {
2299 return null;
2300 }
2301 }
2302 }
2303
satoke7c6998e2011-06-03 17:57:59 +09002304 @Override
satokee5e77c2011-09-02 18:50:15 +09002305 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002306 if (!calledFromValidUser()) {
2307 return;
2308 }
satok91e88122011-07-18 11:11:42 +09002309 // By this IPC call, only a process which shares the same uid with the IME can add
2310 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002311 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002312 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002313 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002314 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002315 final String[] packageInfos;
2316 try {
2317 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2318 } catch (RemoteException e) {
2319 Slog.e(TAG, "Failed to get package infos");
2320 return;
2321 }
satok91e88122011-07-18 11:11:42 +09002322 if (packageInfos != null) {
2323 final int packageNum = packageInfos.length;
2324 for (int i = 0; i < packageNum; ++i) {
2325 if (packageInfos[i].equals(imi.getPackageName())) {
2326 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002327 final long ident = Binder.clearCallingIdentity();
2328 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002329 buildInputMethodListLocked(mMethodList, mMethodMap,
2330 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002331 } finally {
2332 Binder.restoreCallingIdentity(ident);
2333 }
satokee5e77c2011-09-02 18:50:15 +09002334 return;
satok91e88122011-07-18 11:11:42 +09002335 }
2336 }
2337 }
satoke7c6998e2011-06-03 17:57:59 +09002338 }
satokee5e77c2011-09-02 18:50:15 +09002339 return;
satoke7c6998e2011-06-03 17:57:59 +09002340 }
2341
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002342 @Override
2343 public int getInputMethodWindowVisibleHeight() {
2344 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2345 }
2346
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002347 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002348 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002349 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002350 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002351 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002352 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002353 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2354 if (DEBUG) {
2355 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2356 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2357 + " actual: " + sequenceNumber);
2358 }
2359 return;
2360 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002361 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2362 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002363 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002364 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002365 }
2366 }
2367
satok28203512010-11-24 11:06:49 +09002368 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002370 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2371 }
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002374 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2375 if (token == null) {
2376 if (mContext.checkCallingOrSelfPermission(
2377 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2378 != PackageManager.PERMISSION_GRANTED) {
2379 throw new SecurityException(
2380 "Using null token requires permission "
2381 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002383 } else if (mCurToken != token) {
2384 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2385 + " token: " + token);
2386 return;
2387 }
2388
2389 final long ident = Binder.clearCallingIdentity();
2390 try {
2391 setInputMethodLocked(id, subtypeId);
2392 } finally {
2393 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 }
2395 }
2396
satok42c5a162011-05-26 16:46:14 +09002397 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002399 if (!calledFromValidUser()) {
2400 return;
2401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002403 if (!calledWithValidToken(token)) {
2404 final int uid = Binder.getCallingUid();
2405 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2406 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 return;
2408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 long ident = Binder.clearCallingIdentity();
2410 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002411 hideCurrentInputLocked(flags, null);
2412 } finally {
2413 Binder.restoreCallingIdentity(ident);
2414 }
2415 }
2416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002417
satok42c5a162011-05-26 16:46:14 +09002418 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002419 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002420 if (!calledFromValidUser()) {
2421 return;
2422 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002423 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002424 if (!calledWithValidToken(token)) {
2425 final int uid = Binder.getCallingUid();
2426 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2427 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002428 return;
2429 }
2430 long ident = Binder.clearCallingIdentity();
2431 try {
2432 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 } finally {
2434 Binder.restoreCallingIdentity(ident);
2435 }
2436 }
2437 }
2438
2439 void setEnabledSessionInMainThread(SessionState session) {
2440 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002441 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002443 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002444 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 } catch (RemoteException e) {
2446 }
2447 }
2448 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002449 if (mEnabledSession != null && mEnabledSession.session != null) {
2450 try {
2451 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2452 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2453 } catch (RemoteException e) {
2454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 }
2456 }
2457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002458
satok42c5a162011-05-26 16:46:14 +09002459 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002461 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 switch (msg.what) {
2463 case MSG_SHOW_IM_PICKER:
2464 showInputMethodMenu();
2465 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002466
satokab751aa2010-09-14 19:17:36 +09002467 case MSG_SHOW_IM_SUBTYPE_PICKER:
2468 showInputMethodSubtypeMenu();
2469 return true;
2470
satok47a44912010-10-06 16:03:58 +09002471 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002472 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002473 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002474 args.recycle();
satok217f5482010-12-15 05:19:19 +09002475 return true;
2476
2477 case MSG_SHOW_IM_CONFIG:
2478 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002479 return true;
2480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 case MSG_UNBIND_INPUT:
2484 try {
2485 ((IInputMethod)msg.obj).unbindInput();
2486 } catch (RemoteException e) {
2487 // There is nothing interesting about the method dying.
2488 }
2489 return true;
2490 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002491 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 try {
2493 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2494 } catch (RemoteException e) {
2495 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002496 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 return true;
2498 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002501 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002502 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002503 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 } catch (RemoteException e) {
2505 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002506 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 return true;
2508 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002509 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002511 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002512 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002513 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 } catch (RemoteException e) {
2515 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002516 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 return true;
2518 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002519 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002521 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2523 } catch (RemoteException e) {
2524 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002525 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002527 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002528 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002529 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002530 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002532 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002534 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002535 // Dispose the channel if the input method is not local to this process
2536 // because the remote proxy will get its own copy when unparceled.
2537 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002538 channel.dispose();
2539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002541 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002547 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 try {
2549 SessionState session = (SessionState)args.arg1;
2550 setEnabledSessionInMainThread(session);
2551 session.method.startInput((IInputContext)args.arg2,
2552 (EditorInfo)args.arg3);
2553 } catch (RemoteException e) {
2554 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002555 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 return true;
2557 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002558 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 try {
2560 SessionState session = (SessionState)args.arg1;
2561 setEnabledSessionInMainThread(session);
2562 session.method.restartInput((IInputContext)args.arg2,
2563 (EditorInfo)args.arg3);
2564 } catch (RemoteException e) {
2565 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002566 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 case MSG_UNBIND_METHOD:
2572 try {
2573 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2574 } catch (RemoteException e) {
2575 // There is nothing interesting about the last client dying.
2576 }
2577 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002578 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002579 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002580 IInputMethodClient client = (IInputMethodClient)args.arg1;
2581 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002583 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002585 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002586 } finally {
2587 // Dispose the channel if the input method is not local to this process
2588 // because the remote proxy will get its own copy when unparceled.
2589 if (res.channel != null && Binder.isProxy(client)) {
2590 res.channel.dispose();
2591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002593 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002595 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002596 case MSG_SET_ACTIVE:
2597 try {
2598 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2599 } catch (RemoteException e) {
2600 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2601 + ((ClientState)msg.obj).pid + " uid "
2602 + ((ClientState)msg.obj).uid);
2603 }
2604 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002605 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2606 final int sequenceNumber = msg.arg1;
2607 final IInputMethodClient client = (IInputMethodClient)msg.obj;
2608 try {
2609 client.setUserActionNotificationSequenceNumber(sequenceNumber);
2610 } catch (RemoteException e) {
2611 Slog.w(TAG, "Got RemoteException sending "
2612 + "setUserActionNotificationSequenceNumber("
2613 + sequenceNumber + ") notification to pid "
2614 + ((ClientState)msg.obj).pid + " uid "
2615 + ((ClientState)msg.obj).uid);
2616 }
2617 return true;
2618 }
satok01038492012-04-09 21:08:27 +09002619
2620 // --------------------------------------------------------------
2621 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002622 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002623 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 }
2625 return false;
2626 }
2627
satokdc9ddae2011-10-06 12:22:36 +09002628 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002629 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2630 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002631 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002632 if (DEBUG) {
2633 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2634 }
satok723a27e2010-11-11 14:58:11 +09002635 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002636 return true;
2637 }
2638
2639 return false;
2640 }
2641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002643 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002644 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002645 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002646 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 list.clear();
2649 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002650
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002651 // Use for queryIntentServicesAsUser
2652 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002653 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002654 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002656 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002658 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2659 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002660
satoke7c6998e2011-06-03 17:57:59 +09002661 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2662 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 for (int i = 0; i < services.size(); ++i) {
2664 ResolveInfo ri = services.get(i);
2665 ServiceInfo si = ri.serviceInfo;
2666 ComponentName compName = new ComponentName(si.packageName, si.name);
2667 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2668 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002669 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 + ": it does not require the permission "
2671 + android.Manifest.permission.BIND_INPUT_METHOD);
2672 continue;
2673 }
2674
Joe Onorato8a9b2202010-02-26 18:56:32 -08002675 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676
2677 try {
satoke7c6998e2011-06-03 17:57:59 +09002678 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002680 final String id = p.getId();
2681 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
2683 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002684 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002688 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002690 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
2692 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002693
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002694 if (resetDefaultEnabledIme) {
2695 final ArrayList<InputMethodInfo> defaultEnabledIme =
2696 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2697 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2698 final InputMethodInfo imi = defaultEnabledIme.get(i);
2699 if (DEBUG) {
2700 Slog.d(TAG, "--- enable ime = " + imi);
2701 }
2702 setInputMethodEnabledLocked(imi.getId(), true);
2703 }
2704 }
2705
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002706 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002707 if (!TextUtils.isEmpty(defaultImiId)) {
2708 if (!map.containsKey(defaultImiId)) {
2709 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2710 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002711 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002712 }
2713 } else {
2714 // Double check that the default IME is certainly enabled.
2715 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002716 }
2717 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002718 // Here is not the perfect place to reset the switching controller. Ideally
2719 // mSwitchingController and mSettings should be able to share the same state.
2720 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2721 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002722 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002726
satokab751aa2010-09-14 19:17:36 +09002727 private void showInputMethodMenu() {
2728 showInputMethodMenuInternal(false);
2729 }
2730
2731 private void showInputMethodSubtypeMenu() {
2732 showInputMethodMenuInternal(true);
2733 }
2734
satok217f5482010-12-15 05:19:19 +09002735 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002736 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002737 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002738 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2739 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002740 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002741 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002742 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002743 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002744 }
2745
2746 private void showConfigureInputMethods() {
2747 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2748 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2749 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2750 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002751 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002752 }
2753
satok2c93efc2012-04-02 19:33:47 +09002754 private boolean isScreenLocked() {
2755 return mKeyguardManager != null
2756 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2757 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002758
satokab751aa2010-09-14 19:17:36 +09002759 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002760 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002763 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002764
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002765 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002766 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002767 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002768
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002769 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002770 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002771 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2772 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002773 if (immis == null || immis.size() == 0) {
2774 return;
2775 }
2776
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002777 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778
satok688bd472012-02-09 20:09:17 +09002779 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002780 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002781 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002782
satokc3690562012-01-10 20:14:43 +09002783 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002784 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002785 if (currentSubtype != null) {
2786 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002787 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2788 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002789 }
2790 }
2791
Ken Wakasa761eb372011-03-04 19:06:18 +09002792 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002793 mIms = new InputMethodInfo[N];
2794 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002795 int checkedItem = 0;
2796 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002797 final ImeSubtypeListItem item = imList.get(i);
2798 mIms[i] = item.mImi;
2799 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002800 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002801 int subtypeId = mSubtypeIds[i];
2802 if ((subtypeId == NOT_A_SUBTYPE_ID)
2803 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2804 || (subtypeId == lastInputMethodSubtypeId)) {
2805 checkedItem = i;
2806 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002809 final Context themedContext = new ContextThemeWrapper(context,
2810 android.R.style.Theme_DeviceDefault_Settings);
2811 mDialogBuilder = new AlertDialog.Builder(themedContext);
2812 final TypedArray a = themedContext.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002813 com.android.internal.R.styleable.DialogPreference,
2814 com.android.internal.R.attr.alertDialogStyle, 0);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002815 mDialogBuilder.setIcon(a.getDrawable(
2816 com.android.internal.R.styleable.DialogPreference_dialogIcon));
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002817 a.recycle();
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002818 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2819 @Override
2820 public void onCancel(DialogInterface dialog) {
2821 hideInputMethodMenu();
2822 }
2823 });
satok01038492012-04-09 21:08:27 +09002824 final LayoutInflater inflater =
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002825 (LayoutInflater)themedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002826 final View tv = inflater.inflate(
2827 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2828 mDialogBuilder.setCustomTitle(tv);
2829
2830 // Setup layout for a toggle switch of the hardware keyboard
2831 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002832 mSwitchingDialogTitleView
2833 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2834 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2835 ? View.VISIBLE : View.GONE);
2836 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2837 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002838 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002839 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2840 @Override
2841 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002842 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002843 // Ensure that the input method dialog is dismissed when changing
2844 // the hardware keyboard state.
2845 hideInputMethodMenu();
2846 }
2847 });
2848
2849 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(themedContext,
2850 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2851 final OnClickListener choiceListener = new OnClickListener() {
2852 @Override
2853 public void onClick(final DialogInterface dialog, final int which) {
2854 synchronized (mMethodMap) {
2855 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2856 || mSubtypeIds.length <= which) {
2857 return;
satok01038492012-04-09 21:08:27 +09002858 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002859 final InputMethodInfo im = mIms[which];
2860 int subtypeId = mSubtypeIds[which];
2861 adapter.mCheckedItem = which;
2862 adapter.notifyDataSetChanged();
2863 hideInputMethodMenu();
2864 if (im != null) {
2865 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2866 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002867 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002868 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002870 }
2871 }
2872 };
2873 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
satokbc81b692011-08-26 16:22:22 +09002875 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002876 final OnClickListener positiveListener = new OnClickListener() {
2877 @Override
2878 public void onClick(DialogInterface dialog, int whichButton) {
2879 showConfigureInputMethods();
2880 }
2881 };
satok82beadf2010-12-27 19:03:06 +09002882 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002883 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09002884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002886 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 mSwitchingDialog.getWindow().setType(
2888 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002889 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2890 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002891 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002892 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 mSwitchingDialog.show();
2894 }
2895 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002896
Ken Wakasa05dbb652011-08-22 15:22:43 +09002897 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2898 private final LayoutInflater mInflater;
2899 private final int mTextViewResourceId;
2900 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002901 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002902 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2903 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2904 super(context, textViewResourceId, itemsList);
2905 mTextViewResourceId = textViewResourceId;
2906 mItemsList = itemsList;
2907 mCheckedItem = checkedItem;
2908 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2909 }
2910
2911 @Override
2912 public View getView(int position, View convertView, ViewGroup parent) {
2913 final View view = convertView != null ? convertView
2914 : mInflater.inflate(mTextViewResourceId, null);
2915 if (position < 0 || position >= mItemsList.size()) return view;
2916 final ImeSubtypeListItem item = mItemsList.get(position);
2917 final CharSequence imeName = item.mImeName;
2918 final CharSequence subtypeName = item.mSubtypeName;
2919 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2920 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2921 if (TextUtils.isEmpty(subtypeName)) {
2922 firstTextView.setText(imeName);
2923 secondTextView.setVisibility(View.GONE);
2924 } else {
2925 firstTextView.setText(subtypeName);
2926 secondTextView.setText(imeName);
2927 secondTextView.setVisibility(View.VISIBLE);
2928 }
2929 final RadioButton radioButton =
2930 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2931 radioButton.setChecked(position == mCheckedItem);
2932 return view;
2933 }
2934 }
2935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002937 synchronized (mMethodMap) {
2938 hideInputMethodMenuLocked();
2939 }
2940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002941
The Android Open Source Project10592532009-03-18 17:39:46 -07002942 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002943 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944
The Android Open Source Project10592532009-03-18 17:39:46 -07002945 if (mSwitchingDialog != null) {
2946 mSwitchingDialog.dismiss();
2947 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002949
Jason Monk807ef762014-05-08 15:47:46 -04002950 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002951 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002952 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002956
satok42c5a162011-05-26 16:46:14 +09002957 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002959 // TODO: Make this work even for non-current users?
2960 if (!calledFromValidUser()) {
2961 return false;
2962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 synchronized (mMethodMap) {
2964 if (mContext.checkCallingOrSelfPermission(
2965 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2966 != PackageManager.PERMISSION_GRANTED) {
2967 throw new SecurityException(
2968 "Requires permission "
2969 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2970 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 long ident = Binder.clearCallingIdentity();
2973 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002974 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 } finally {
2976 Binder.restoreCallingIdentity(ident);
2977 }
2978 }
2979 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002980
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002981 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2982 // Make sure this is a valid input method.
2983 InputMethodInfo imm = mMethodMap.get(id);
2984 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002985 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002986 }
2987
satokd87c2592010-09-29 11:52:06 +09002988 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2989 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002990
satokd87c2592010-09-29 11:52:06 +09002991 if (enabled) {
2992 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2993 if (pair.first.equals(id)) {
2994 // We are enabling this input method, but it is already enabled.
2995 // Nothing to do. The previous state was enabled.
2996 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002997 }
2998 }
satokd87c2592010-09-29 11:52:06 +09002999 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3000 // Previous state was disabled.
3001 return false;
3002 } else {
3003 StringBuilder builder = new StringBuilder();
3004 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3005 builder, enabledInputMethodsList, id)) {
3006 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003007 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003008 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3009 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3010 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003011 }
3012 // Previous state was enabled.
3013 return true;
3014 } else {
3015 // We are disabling the input method but it is already disabled.
3016 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003017 return false;
3018 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003019 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003020 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003021
satok723a27e2010-11-11 14:58:11 +09003022 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3023 boolean setSubtypeOnly) {
3024 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003025 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003026
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003027 mCurUserActionNotificationSequenceNumber =
3028 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3029 if (DEBUG) {
3030 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3031 + mCurUserActionNotificationSequenceNumber);
3032 }
3033
3034 if (mCurClient != null && mCurClient.client != null) {
3035 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3036 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
3037 mCurUserActionNotificationSequenceNumber, mCurClient.client));
3038 }
3039
satok723a27e2010-11-11 14:58:11 +09003040 // Set Subtype here
3041 if (imi == null || subtypeId < 0) {
3042 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003043 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003044 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003045 if (subtypeId < imi.getSubtypeCount()) {
3046 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3047 mSettings.putSelectedSubtype(subtype.hashCode());
3048 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003049 } else {
3050 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003051 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003052 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003053 }
satokab751aa2010-09-14 19:17:36 +09003054 }
satok723a27e2010-11-11 14:58:11 +09003055
satok4c0e7152012-06-20 20:08:44 +09003056 // Workaround.
3057 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3058 // IMEs are not recognized and considered uninstalled.
3059 // Actually, we can't move everything after SystemReady because
3060 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3061 // the default IME here and try cheking the default IME again in systemReady().
3062 // TODO: Do nothing before system ready and implement a separated logic for
3063 // the encryption lock screen.
3064 // TODO: ASEC should be ready before IMMS is instantiated.
3065 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003066 // Set InputMethod here
3067 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3068 }
3069 }
3070
3071 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3072 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3073 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3074 // newDefaultIme is empty when there is no candidate for the selected IME.
3075 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3076 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3077 if (subtypeHashCode != null) {
3078 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003079 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003080 imi, Integer.valueOf(subtypeHashCode));
3081 } catch (NumberFormatException e) {
3082 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3083 }
3084 }
3085 }
3086 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003087 }
3088
satok4e4569d2010-11-19 18:45:53 +09003089 // If there are no selected shortcuts, tries finding the most applicable ones.
3090 private Pair<InputMethodInfo, InputMethodSubtype>
3091 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3092 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3093 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003094 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003095 boolean foundInSystemIME = false;
3096
3097 // Search applicable subtype for each InputMethodInfo
3098 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003099 final String imiId = imi.getId();
3100 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3101 continue;
3102 }
satokcd7cd292010-11-20 15:46:23 +09003103 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003104 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003105 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003106 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003107 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003108 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003109 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003110 }
satokdf31ae62011-01-15 06:19:44 +09003111 // 2. Search by the system locale from enabledSubtypes.
3112 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003113 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003114 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003115 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003116 }
satoka86f5e42011-09-02 17:12:42 +09003117 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003118 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003119 final ArrayList<InputMethodSubtype> subtypesForSearch =
3120 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003121 ? InputMethodUtils.getSubtypes(imi)
3122 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003123 // 4. Search by the current subtype's locale from all subtypes.
3124 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003125 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003126 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003127 }
3128 // 5. Search by the system locale from all subtypes.
3129 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003130 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003131 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003132 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003133 }
satokcd7cd292010-11-20 15:46:23 +09003134 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003135 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003136 // The current input method is the most applicable IME.
3137 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003138 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003139 break;
satok7599a7f2010-12-22 13:45:23 +09003140 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003141 // The system input method is 2nd applicable IME.
3142 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003143 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003144 if ((imi.getServiceInfo().applicationInfo.flags
3145 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3146 foundInSystemIME = true;
3147 }
satok4e4569d2010-11-19 18:45:53 +09003148 }
3149 }
3150 }
3151 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003152 if (mostApplicableIMI != null) {
3153 Slog.w(TAG, "Most applicable shortcut input method was:"
3154 + mostApplicableIMI.getId());
3155 if (mostApplicableSubtype != null) {
3156 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3157 + "," + mostApplicableSubtype.getMode() + ","
3158 + mostApplicableSubtype.getLocale());
3159 }
3160 }
satok4e4569d2010-11-19 18:45:53 +09003161 }
satokcd7cd292010-11-20 15:46:23 +09003162 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003163 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003164 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003165 } else {
3166 return null;
3167 }
3168 }
3169
satokab751aa2010-09-14 19:17:36 +09003170 /**
3171 * @return Return the current subtype of this input method.
3172 */
satok42c5a162011-05-26 16:46:14 +09003173 @Override
satokab751aa2010-09-14 19:17:36 +09003174 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003175 // TODO: Make this work even for non-current users?
3176 if (!calledFromValidUser()) {
3177 return null;
3178 }
3179 synchronized (mMethodMap) {
3180 return getCurrentInputMethodSubtypeLocked();
3181 }
3182 }
3183
3184 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003185 if (mCurMethodId == null) {
3186 return null;
3187 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003188 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003189 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3190 if (imi == null || imi.getSubtypeCount() == 0) {
3191 return null;
satok4e4569d2010-11-19 18:45:53 +09003192 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003193 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003194 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3195 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003196 if (subtypeId == NOT_A_SUBTYPE_ID) {
3197 // If there are no selected subtypes, the framework will try to find
3198 // the most applicable subtype from explicitly or implicitly enabled
3199 // subtypes.
3200 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003201 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003202 // If there is only one explicitly or implicitly enabled subtype,
3203 // just returns it.
3204 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3205 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3206 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003207 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003208 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003209 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003210 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003211 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003212 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3213 true);
satok4e4569d2010-11-19 18:45:53 +09003214 }
satok3ef8b292010-11-23 06:06:29 +09003215 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003216 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003217 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003218 }
3219 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003220 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003221 }
3222
satokf3db1af2010-11-23 13:34:33 +09003223 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3224 InputMethodSubtype subtype) {
3225 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3226 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3227 } else {
3228 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3229 subtypes.add(subtype);
3230 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3231 }
3232 }
3233
satok4e4569d2010-11-19 18:45:53 +09003234 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003235 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003236 @Override
satok4e4569d2010-11-19 18:45:53 +09003237 public List getShortcutInputMethodsAndSubtypes() {
3238 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003239 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003240 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003241 // If there are no selected shortcut subtypes, the framework will try to find
3242 // the most applicable subtype from all subtypes whose mode is
3243 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003244 Pair<InputMethodInfo, InputMethodSubtype> info =
3245 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003246 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003247 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003248 ret.add(info.first);
3249 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003250 }
satok3da92232011-01-11 22:46:30 +09003251 return ret;
satokf3db1af2010-11-23 13:34:33 +09003252 }
satokf3db1af2010-11-23 13:34:33 +09003253 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3254 ret.add(imi);
3255 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3256 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003257 }
3258 }
satokf3db1af2010-11-23 13:34:33 +09003259 return ret;
satok4e4569d2010-11-19 18:45:53 +09003260 }
3261 }
3262
satok42c5a162011-05-26 16:46:14 +09003263 @Override
satokb66d2872010-11-10 01:04:04 +09003264 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003265 // TODO: Make this work even for non-current users?
3266 if (!calledFromValidUser()) {
3267 return false;
3268 }
satokb66d2872010-11-10 01:04:04 +09003269 synchronized (mMethodMap) {
3270 if (subtype != null && mCurMethodId != null) {
3271 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003272 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003273 if (subtypeId != NOT_A_SUBTYPE_ID) {
3274 setInputMethodLocked(mCurMethodId, subtypeId);
3275 return true;
3276 }
3277 }
3278 return false;
3279 }
3280 }
3281
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003282 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003283 private static class InputMethodFileManager {
3284 private static final String SYSTEM_PATH = "system";
3285 private static final String INPUT_METHOD_PATH = "inputmethod";
3286 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3287 private static final String NODE_SUBTYPES = "subtypes";
3288 private static final String NODE_SUBTYPE = "subtype";
3289 private static final String NODE_IMI = "imi";
3290 private static final String ATTR_ID = "id";
3291 private static final String ATTR_LABEL = "label";
3292 private static final String ATTR_ICON = "icon";
3293 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3294 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3295 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3296 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3297 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3298 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003299 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003300 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003301 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003302 if (methodMap == null) {
3303 throw new NullPointerException("methodMap is null");
3304 }
3305 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003306 final File systemDir = userId == UserHandle.USER_OWNER
3307 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3308 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003309 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3310 if (!inputMethodDir.mkdirs()) {
3311 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3312 }
3313 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3314 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3315 if (!subtypeFile.exists()) {
3316 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003317 writeAdditionalInputMethodSubtypes(
3318 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003319 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003320 readAdditionalInputMethodSubtypes(
3321 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003322 }
3323 }
3324
3325 private void deleteAllInputMethodSubtypes(String imiId) {
3326 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003327 mAdditionalSubtypesMap.remove(imiId);
3328 writeAdditionalInputMethodSubtypes(
3329 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003330 }
3331 }
3332
3333 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003334 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003335 synchronized (mMethodMap) {
3336 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3337 final int N = additionalSubtypes.length;
3338 for (int i = 0; i < N; ++i) {
3339 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003340 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003341 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003342 } else {
3343 Slog.w(TAG, "Duplicated subtype definition found: "
3344 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003345 }
3346 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003347 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3348 writeAdditionalInputMethodSubtypes(
3349 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003350 }
3351 }
3352
3353 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3354 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003355 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003356 }
3357 }
3358
3359 private static void writeAdditionalInputMethodSubtypes(
3360 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3361 HashMap<String, InputMethodInfo> methodMap) {
3362 // Safety net for the case that this function is called before methodMap is set.
3363 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3364 FileOutputStream fos = null;
3365 try {
3366 fos = subtypesFile.startWrite();
3367 final XmlSerializer out = new FastXmlSerializer();
3368 out.setOutput(fos, "utf-8");
3369 out.startDocument(null, true);
3370 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3371 out.startTag(null, NODE_SUBTYPES);
3372 for (String imiId : allSubtypes.keySet()) {
3373 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3374 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3375 continue;
3376 }
3377 out.startTag(null, NODE_IMI);
3378 out.attribute(null, ATTR_ID, imiId);
3379 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3380 final int N = subtypesList.size();
3381 for (int i = 0; i < N; ++i) {
3382 final InputMethodSubtype subtype = subtypesList.get(i);
3383 out.startTag(null, NODE_SUBTYPE);
3384 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3385 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3386 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3387 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3388 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3389 out.attribute(null, ATTR_IS_AUXILIARY,
3390 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3391 out.endTag(null, NODE_SUBTYPE);
3392 }
3393 out.endTag(null, NODE_IMI);
3394 }
3395 out.endTag(null, NODE_SUBTYPES);
3396 out.endDocument();
3397 subtypesFile.finishWrite(fos);
3398 } catch (java.io.IOException e) {
3399 Slog.w(TAG, "Error writing subtypes", e);
3400 if (fos != null) {
3401 subtypesFile.failWrite(fos);
3402 }
3403 }
3404 }
3405
3406 private static void readAdditionalInputMethodSubtypes(
3407 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3408 if (allSubtypes == null || subtypesFile == null) return;
3409 allSubtypes.clear();
3410 FileInputStream fis = null;
3411 try {
3412 fis = subtypesFile.openRead();
3413 final XmlPullParser parser = Xml.newPullParser();
3414 parser.setInput(fis, null);
3415 int type = parser.getEventType();
3416 // Skip parsing until START_TAG
3417 while ((type = parser.next()) != XmlPullParser.START_TAG
3418 && type != XmlPullParser.END_DOCUMENT) {}
3419 String firstNodeName = parser.getName();
3420 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3421 throw new XmlPullParserException("Xml doesn't start with subtypes");
3422 }
3423 final int depth =parser.getDepth();
3424 String currentImiId = null;
3425 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3426 while (((type = parser.next()) != XmlPullParser.END_TAG
3427 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3428 if (type != XmlPullParser.START_TAG)
3429 continue;
3430 final String nodeName = parser.getName();
3431 if (NODE_IMI.equals(nodeName)) {
3432 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3433 if (TextUtils.isEmpty(currentImiId)) {
3434 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3435 continue;
3436 }
3437 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3438 allSubtypes.put(currentImiId, tempSubtypesArray);
3439 } else if (NODE_SUBTYPE.equals(nodeName)) {
3440 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3441 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3442 continue;
3443 }
3444 final int icon = Integer.valueOf(
3445 parser.getAttributeValue(null, ATTR_ICON));
3446 final int label = Integer.valueOf(
3447 parser.getAttributeValue(null, ATTR_LABEL));
3448 final String imeSubtypeLocale =
3449 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3450 final String imeSubtypeMode =
3451 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3452 final String imeSubtypeExtraValue =
3453 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003454 final boolean isAuxiliary = "1".equals(String.valueOf(
3455 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003456 final InputMethodSubtype subtype =
3457 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3458 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3459 tempSubtypesArray.add(subtype);
3460 }
3461 }
3462 } catch (XmlPullParserException e) {
3463 Slog.w(TAG, "Error reading subtypes: " + e);
3464 return;
3465 } catch (java.io.IOException e) {
3466 Slog.w(TAG, "Error reading subtypes: " + e);
3467 return;
3468 } catch (NumberFormatException e) {
3469 Slog.w(TAG, "Error reading subtypes: " + e);
3470 return;
3471 } finally {
3472 if (fis != null) {
3473 try {
3474 fis.close();
3475 } catch (java.io.IOException e1) {
3476 Slog.w(TAG, "Failed to close.");
3477 }
3478 }
3479 }
3480 }
3481 }
3482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 @Override
3484 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3485 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3486 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3489 + Binder.getCallingPid()
3490 + ", uid=" + Binder.getCallingUid());
3491 return;
3492 }
3493
3494 IInputMethod method;
3495 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 synchronized (mMethodMap) {
3500 p.println("Current Input Method Manager state:");
3501 int N = mMethodList.size();
3502 p.println(" Input Methods:");
3503 for (int i=0; i<N; i++) {
3504 InputMethodInfo info = mMethodList.get(i);
3505 p.println(" InputMethod #" + i + ":");
3506 info.dump(p, " ");
3507 }
3508 p.println(" Clients:");
3509 for (ClientState ci : mClients.values()) {
3510 p.println(" Client " + ci + ":");
3511 p.println(" client=" + ci.client);
3512 p.println(" inputContext=" + ci.inputContext);
3513 p.println(" sessionRequested=" + ci.sessionRequested);
3514 p.println(" curSession=" + ci.curSession);
3515 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003516 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003518 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3519 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3521 + " mBoundToMethod=" + mBoundToMethod);
3522 p.println(" mCurToken=" + mCurToken);
3523 p.println(" mCurIntent=" + mCurIntent);
3524 method = mCurMethod;
3525 p.println(" mCurMethod=" + mCurMethod);
3526 p.println(" mEnabledSession=" + mEnabledSession);
3527 p.println(" mShowRequested=" + mShowRequested
3528 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3529 + " mShowForced=" + mShowForced
3530 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003531 p.println(" mCurUserActionNotificationSequenceNumber="
3532 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003533 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003535
Jeff Brownb88102f2010-09-08 11:49:43 -07003536 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 pw.flush();
3539 try {
3540 client.client.asBinder().dump(fd, args);
3541 } catch (RemoteException e) {
3542 p.println("Input method client dead: " + e);
3543 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003544 } else {
3545 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003547
Jeff Brownb88102f2010-09-08 11:49:43 -07003548 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 pw.flush();
3551 try {
3552 method.asBinder().dump(fd, args);
3553 } catch (RemoteException e) {
3554 p.println("Input method service dead: " + e);
3555 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003556 } else {
3557 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 }
3559 }
3560}