blob: 6827b3f5086268baca0d5c65ae0b7b925d998b6f [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;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080062import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.res.Resources;
65import android.content.res.TypedArray;
66import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080067import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090069import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
72import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Parcel;
77import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080078import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
80import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080082import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.provider.Settings;
84import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090085import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070086import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090088import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090089import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.PrintWriterPrinter;
91import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090092import android.util.Slog;
93import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070095import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090096import android.view.LayoutInflater;
97import android.view.View;
98import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.inputmethod.InputBinding;
102import android.view.inputmethod.InputMethod;
103import android.view.inputmethod.InputMethodInfo;
104import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900107import android.widget.CompoundButton;
108import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900110import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.FileInputStream;
116import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.IOException;
118import java.io.PrintWriter;
119import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900120import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.HashMap;
122import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900123import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125/**
126 * This class provides a system service that manages input methods.
127 */
128public class InputMethodManagerService extends IInputMethodManager.Stub
129 implements ServiceConnection, Handler.Callback {
130 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900136 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_INPUT = 1000;
139 static final int MSG_BIND_INPUT = 1010;
140 static final int MSG_SHOW_SOFT_INPUT = 1020;
141 static final int MSG_HIDE_SOFT_INPUT = 1030;
142 static final int MSG_ATTACH_TOKEN = 1040;
143 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final int MSG_START_INPUT = 2000;
146 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_UNBIND_METHOD = 3000;
149 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700150 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
satok01038492012-04-09 21:08:27 +0900152 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
153
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700154 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
satokf9f01002011-05-19 21:31:50 +0900156 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
157
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900159 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900160
satok4e4569d2010-11-19 18:45:53 +0900161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800163 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900165 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final IWindowManager mIWindowManager;
168 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700169 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900170 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900171 private final HardKeyboardListener mHardKeyboardListener;
172 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
Jeff Brownc28867a2013-03-26 15:42:39 -0700174 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 // All known input methods. mMethodMap also serves as the global
177 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900178 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
179 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900180 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
181 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900182 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700184 // Used to bring IME service up to visible adjustment while it is being shown.
185 final ServiceConnection mVisibleConnection = new ServiceConnection() {
186 @Override public void onServiceConnected(ComponentName name, IBinder service) {
187 }
188
189 @Override public void onServiceDisconnected(ComponentName name) {
190 }
191 };
192 boolean mVisibleBound = false;
193
satok7cfc0ed2011-06-20 21:29:36 +0900194 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700195 private NotificationManager mNotificationManager;
196 private KeyguardManager mKeyguardManager;
197 private StatusBarManagerService mStatusBar;
198 private Notification mImeSwitcherNotification;
199 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900200 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900201 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900202 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 class SessionState {
205 final ClientState client;
206 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700207
208 IInputMethodSession session;
209 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 @Override
212 public String toString() {
213 return "SessionState{uid " + client.uid + " pid " + client.pid
214 + " method " + Integer.toHexString(
215 System.identityHashCode(method))
216 + " session " + Integer.toHexString(
217 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700218 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 + "}";
220 }
221
222 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700223 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 client = _client;
225 method = _method;
226 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Jeff Brownc28867a2013-03-26 15:42:39 -0700231 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
John Spurlocke0980502013-10-25 11:59:29 -0400313 // Was the keyguard locked when this client became current?
314 private boolean mCurClientInKeyguard;
315
satokab751aa2010-09-14 19:17:36 +0900316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * Set to true if our ServiceConnection is currently actively bound to
318 * a service (whether or not we have gotten its IBinder back yet).
319 */
320 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if the client has asked for the input method to be shown.
324 */
325 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we were explicitly told to show the input method.
329 */
330 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Set if we were forced to be shown.
334 */
335 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * Set if we last told the input method to show itself.
339 */
340 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * The Intent used to connect to the current input method.
344 */
345 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * The token we have made for the currently active input method, to
349 * identify it in the future.
350 */
351 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * If non-null, this is the input method service we are currently connected
355 * to.
356 */
357 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * Time that we last initiated a bind to the input method, to determine
361 * if we should try to disconnect and reconnect to it.
362 */
363 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Have we called mCurMethod.bindInput()?
367 */
368 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * Currently enabled session. Only touched by service thread, not
372 * protected by a lock.
373 */
374 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * True if the screen is on. The value is true initially.
378 */
379 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
Joe Onorato857fd9b2011-01-27 15:08:35 -0800381 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
382 int mImeWindowVis;
383
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private AlertDialog.Builder mDialogBuilder;
385 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900386 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900387 private InputMethodInfo[] mIms;
388 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900389 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900390 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
391 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800394 String mLastEnabled = "";
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 SettingsObserver(Handler handler) {
397 super(handler);
398 ContentResolver resolver = mContext.getContentResolver();
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
400 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900401 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900402 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
403 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900404 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 @Override public void onChange(boolean selfChange) {
408 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800409 boolean enabledChanged = false;
410 String newEnabled = mSettings.getEnabledInputMethodsStr();
411 if (!mLastEnabled.equals(newEnabled)) {
412 mLastEnabled = newEnabled;
413 enabledChanged = true;
414 }
415 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900420 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
421 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700423 if (mCurClient != null && mCurClient.client != null) {
424 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
425 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
427 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900428
429 @Override
430 public void onReceive(Context context, Intent intent) {
431 final String action = intent.getAction();
432 if (Intent.ACTION_SCREEN_ON.equals(action)) {
433 mScreenOn = true;
434 refreshImeWindowVisibilityLocked();
435 updateActive();
436 return;
437 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
438 mScreenOn = false;
439 setImeWindowVisibilityStatusHiddenLocked();
440 updateActive();
441 return;
442 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
443 hideInputMethodMenu();
444 // No need to updateActive
445 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800446 } else if (Intent.ACTION_USER_ADDED.equals(action)
447 || Intent.ACTION_USER_REMOVED.equals(action)) {
448 updateRelatedUserIds();
449 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900450 } else {
451 Slog.w(TAG, "Unexpected intent " + intent);
452 }
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 private boolean isChangingPackagesOfCurrentUser() {
458 final int userId = getChangingUserId();
459 final boolean retval = userId == mSettings.getCurrentUserId();
460 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900461 if (!retval) {
462 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
463 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900464 }
465 return retval;
466 }
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900470 if (!isChangingPackagesOfCurrentUser()) {
471 return false;
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900474 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 final int N = mMethodList.size();
476 if (curInputMethodId != null) {
477 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 InputMethodInfo imi = mMethodList.get(i);
479 if (imi.getId().equals(curInputMethodId)) {
480 for (String pkg : packages) {
481 if (imi.getPackageName().equals(pkg)) {
482 if (!doit) {
483 return true;
484 }
satok723a27e2010-11-11 14:58:11 +0900485 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 chooseNewDefaultIMELocked();
487 return true;
488 }
489 }
490 }
491 }
492 }
493 }
494 return false;
495 }
496
497 @Override
498 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900499 if (!isChangingPackagesOfCurrentUser()) {
500 return;
501 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 synchronized (mMethodMap) {
503 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900504 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 final int N = mMethodList.size();
506 if (curInputMethodId != null) {
507 for (int i=0; i<N; i++) {
508 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900509 final String imiId = imi.getId();
510 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 curIm = imi;
512 }
satoke7c6998e2011-06-03 17:57:59 +0900513
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900515 if (isPackageModified(imi.getPackageName())) {
516 mFileManager.deleteAllInputMethodSubtypes(imiId);
517 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 if (change == PACKAGE_TEMPORARY_CHANGE
519 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800520 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 + imi.getComponent());
522 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800526
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900527 buildInputMethodListLocked(
528 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800531
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800532 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 int change = isPackageDisappearing(curIm.getPackageName());
534 if (change == PACKAGE_TEMPORARY_CHANGE
535 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 ServiceInfo si = null;
537 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900538 si = mIPackageManager.getServiceInfo(
539 curIm.getComponent(), 0, mSettings.getCurrentUserId());
540 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 }
542 if (si == null) {
543 // Uh oh, current input method is no longer around!
544 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900546 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800547 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 changed = true;
549 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900551 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800552 }
553 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800554 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800555 }
satokab751aa2010-09-14 19:17:36 +0900556
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800557 if (curIm == null) {
558 // We currently don't have a default input method... is
559 // one now available?
560 changed = chooseNewDefaultIMELocked();
561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800562
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800563 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800564 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
567 }
568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800569
Jeff Brownc28867a2013-03-26 15:42:39 -0700570 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900571 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700572 private final IInputMethod mMethod;
573 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
Jeff Brownc28867a2013-03-26 15:42:39 -0700575 MethodCallback(InputMethodManagerService imms, IInputMethod method,
576 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900577 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700578 mMethod = method;
579 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800581
satoke7c6998e2011-06-03 17:57:59 +0900582 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700583 public void sessionCreated(IInputMethodSession session) {
584 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
satok01038492012-04-09 21:08:27 +0900588 private class HardKeyboardListener
589 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
590 @Override
591 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
592 mHandler.sendMessage(mHandler.obtainMessage(
593 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
594 }
595
596 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
597 if (DEBUG) {
598 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
599 + enabled);
600 }
601 synchronized(mMethodMap) {
602 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
603 && mSwitchingDialog.isShowing()) {
604 mSwitchingDialogTitleView.findViewById(
605 com.android.internal.R.id.hard_keyboard_section).setVisibility(
606 available ? View.VISIBLE : View.GONE);
607 }
608 }
609 }
610 }
611
612 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900613 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800615 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 mHandler = new Handler(this);
617 mIWindowManager = IWindowManager.Stub.asInterface(
618 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800619 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900620 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public void executeMessage(Message msg) {
622 handleMessage(msg);
623 }
Mita Yuned218c72012-12-06 17:18:25 -0800624 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900625 mWindowManagerService = windowManager;
626 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700627 mHasFeature = context.getPackageManager().hasSystemFeature(
628 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900629
satok7cfc0ed2011-06-20 21:29:36 +0900630 mImeSwitcherNotification = new Notification();
631 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
632 mImeSwitcherNotification.when = 0;
633 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
634 mImeSwitcherNotification.tickerText = null;
635 mImeSwitcherNotification.defaults = 0; // please be quiet
636 mImeSwitcherNotification.sound = null;
637 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400638
639 // Tag this notification specially so SystemUI knows it's important
640 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
641
satok7cfc0ed2011-06-20 21:29:36 +0900642 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900643 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900644
645 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900646
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 final IntentFilter broadcastFilter = new IntentFilter();
648 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
649 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
650 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800651 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
652 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900653 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800654
satok7cfc0ed2011-06-20 21:29:36 +0900655 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900656 int userId = 0;
657 try {
658 ActivityManagerNative.getDefault().registerUserSwitchObserver(
659 new IUserSwitchObserver.Stub() {
660 @Override
661 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900662 synchronized(mMethodMap) {
663 switchUserLocked(newUserId);
664 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 if (reply != null) {
666 try {
667 reply.sendResult(null);
668 } catch (RemoteException e) {
669 }
670 }
671 }
672
673 @Override
674 public void onUserSwitchComplete(int newUserId) throws RemoteException {
675 }
676 });
677 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
678 } catch (RemoteException e) {
679 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
680 }
satok81f8b7c2012-12-04 20:42:56 +0900681 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900682
satokd87c2592010-09-29 11:52:06 +0900683 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900684 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Amith Yamasani734983f2014-03-04 16:48:05 -0800686 updateRelatedUserIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900687 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900688 mSwitchingController = new InputMethodSubtypeSwitchingController(mSettings);
689 mSwitchingController.resetCircularListLocked(context);
satok0a1bcf42012-05-16 19:26:31 +0900690
691 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900692 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900693 if (DEBUG) {
694 Slog.d(TAG, "Initial default ime = " + defaultImiId);
695 }
satok0a1bcf42012-05-16 19:26:31 +0900696 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
697
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900698 buildInputMethodListLocked(mMethodList, mMethodMap,
699 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900700 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701
satok0a1bcf42012-05-16 19:26:31 +0900702 if (!mImeSelectedOnBoot) {
703 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900704 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800708 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900709
710 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
711 // according to the new system locale.
712 final IntentFilter filter = new IntentFilter();
713 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
714 mContext.registerReceiver(
715 new BroadcastReceiver() {
716 @Override
717 public void onReceive(Context context, Intent intent) {
718 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900719 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900720 }
721 }
722 }, filter);
723 }
724
satok5b927c432012-05-01 20:09:34 +0900725 private void resetDefaultImeLocked(Context context) {
726 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900727 if (mCurMethodId != null
728 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900729 return;
730 }
731
732 InputMethodInfo defIm = null;
733 for (InputMethodInfo imi : mMethodList) {
734 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900735 if (InputMethodUtils.isValidSystemDefaultIme(
736 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900737 defIm = imi;
738 Slog.i(TAG, "Selected default: " + imi.getId());
739 }
740 }
741 }
742 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900743 defIm = InputMethodUtils.getMostApplicableDefaultIME(
744 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900745 Slog.i(TAG, "No default found, using " + defIm.getId());
746 }
747 if (defIm != null) {
748 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
749 }
750 }
751
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900752 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
753 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 if (!mSystemReady) {
755 // not system ready
756 return;
757 }
758 final Locale newLocale = mRes.getConfiguration().locale;
759 if (!updateOnlyWhenLocaleChanged
760 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
761 if (!updateOnlyWhenLocaleChanged) {
762 hideCurrentInputLocked(0, null);
763 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700764 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900765 }
766 if (DEBUG) {
767 Slog.i(TAG, "Locale has been changed to " + newLocale);
768 }
Satoshi Kataokad787f692013-10-26 04:44:21 +0900769 // CircularList should be reset when the locale is changed.
770 mSwitchingController.resetCircularListLocked(mContext);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900771 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 if (!updateOnlyWhenLocaleChanged) {
773 final String selectedImiId = mSettings.getSelectedInputMethod();
774 if (TextUtils.isEmpty(selectedImiId)) {
775 // This is the first time of the user switch and
776 // set the current ime to the proper one.
777 resetDefaultImeLocked(mContext);
778 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900779 } else {
780 // If the locale is changed, needs to reset the default ime
781 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800783 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900784 mLastSystemLocale = newLocale;
785 if (!updateOnlyWhenLocaleChanged) {
786 try {
787 startInputInnerLocked();
788 } catch (RuntimeException e) {
789 Slog.w(TAG, "Unexpected exception", e);
790 }
791 }
792 }
793 }
794
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900795 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900796 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
797 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 }
799
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900800 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900801 mSettings.setCurrentUserId(newUserId);
Amith Yamasani734983f2014-03-04 16:48:05 -0800802 updateRelatedUserIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900803 // InputMethodFileManager should be reset when the user is changed
804 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900805 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900806 // For secondary users, the list of enabled IMEs may not have been updated since the
807 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
808 // not be empty even if the IME has been uninstalled by the primary user.
809 // Even in such cases, IMMS works fine because it will find the most applicable
810 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900811 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900812 if (DEBUG) {
813 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
814 }
815 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900816 initialUserSwitch /* needsToResetDefaultIme */);
817 if (initialUserSwitch) {
818 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
819 mSettings.getEnabledInputMethodListLocked());
820 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900821 }
822
Amith Yamasani734983f2014-03-04 16:48:05 -0800823 void updateRelatedUserIds() {
824 List<UserInfo> relatedUsers =
825 UserManager.get(mContext).getRelatedUsers(mSettings.getCurrentUserId());
826 int[] relatedUserIds = new int[relatedUsers.size()]; // relatedUsers will not be null
827 for (int i = 0; i < relatedUserIds.length; i++) {
828 relatedUserIds[i] = relatedUsers.get(i).id;
829 }
830 mSettings.setRelatedUserIds(relatedUserIds);
831 }
832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 @Override
834 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
835 throws RemoteException {
836 try {
837 return super.onTransact(code, data, reply, flags);
838 } catch (RuntimeException e) {
839 // The input method manager only throws security exceptions, so let's
840 // log all others.
841 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700842 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844 throw e;
845 }
846 }
847
Svetoslav Ganova0027152013-06-25 14:59:53 -0700848 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700849 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900850 if (DEBUG) {
851 Slog.d(TAG, "--- systemReady");
852 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700853 if (!mSystemReady) {
854 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900855 mKeyguardManager =
856 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700857 mNotificationManager = (NotificationManager)
858 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
859 mStatusBar = statusBar;
860 statusBar.setIconVisibility("ime", false);
861 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900862 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
863 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900864 if (mShowOngoingImeSwitcherForPhones) {
865 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
866 mHardKeyboardListener);
867 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900868 buildInputMethodListLocked(mMethodList, mMethodMap,
869 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900870 if (!mImeSelectedOnBoot) {
871 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900872 resetStateIfCurrentLocaleChangedLocked();
873 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
874 mContext.getPackageManager(),
875 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900876 }
877 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700878 try {
879 startInputInnerLocked();
880 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800881 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700882 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700883 }
884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
satok15452a42011-10-28 17:58:28 +0900887 private void setImeWindowVisibilityStatusHiddenLocked() {
888 mImeWindowVis = 0;
889 updateImeWindowStatusLocked();
890 }
891
satok3afd6c02011-11-18 08:38:19 +0900892 private void refreshImeWindowVisibilityLocked() {
893 final Configuration conf = mRes.getConfiguration();
894 final boolean haveHardKeyboard = conf.keyboard
895 != Configuration.KEYBOARD_NOKEYS;
896 final boolean hardKeyShown = haveHardKeyboard
897 && conf.hardKeyboardHidden
898 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400899
900 final boolean isScreenLocked = isKeyguardLocked();
901 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400902 // We assume the softkeyboard is shown when the input is active as long as the
903 // hard keyboard is not shown.
904 final boolean inputVisible = inputActive && !hardKeyShown;
905 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
906 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900907 updateImeWindowStatusLocked();
908 }
909
satok15452a42011-10-28 17:58:28 +0900910 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900911 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700912 }
913
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900914 // ---------------------------------------------------------------------------------------
915 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
916 // 1) it comes from the system process
917 // 2) the calling process' user id is identical to the current user id IMMS thinks.
918 private boolean calledFromValidUser() {
919 final int uid = Binder.getCallingUid();
920 final int userId = UserHandle.getUserId(uid);
921 if (DEBUG) {
922 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
923 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
924 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900925 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
926 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 }
Amith Yamasani734983f2014-03-04 16:48:05 -0800928 if (uid == Process.SYSTEM_UID || mSettings.isRelatedToOrCurrentUser(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900931
932 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
933 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
934 // must not manage background users' states in any functions.
935 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
936 // by a token.
937 if (mContext.checkCallingOrSelfPermission(
938 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
939 == PackageManager.PERMISSION_GRANTED) {
940 if (DEBUG) {
941 Slog.d(TAG, "--- Access granted because the calling process has "
942 + "the INTERACT_ACROSS_USERS_FULL permission");
943 }
944 return true;
945 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900946 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
947 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900948 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 }
950
951 private boolean bindCurrentInputMethodService(
952 Intent service, ServiceConnection conn, int flags) {
953 if (service == null || conn == null) {
954 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
955 return false;
956 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800957 return mContext.bindServiceAsUser(service, conn, flags,
958 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 }
960
satoke7c6998e2011-06-03 17:57:59 +0900961 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900963 // TODO: Make this work even for non-current users?
964 if (!calledFromValidUser()) {
965 return Collections.emptyList();
966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 synchronized (mMethodMap) {
968 return new ArrayList<InputMethodInfo>(mMethodList);
969 }
970 }
971
satoke7c6998e2011-06-03 17:57:59 +0900972 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 // TODO: Make this work even for non-current users?
975 if (!calledFromValidUser()) {
976 return Collections.emptyList();
977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900979 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 }
982
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900983 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900984 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900985 * @return enabled subtypes of the specified imi
986 */
satoke7c6998e2011-06-03 17:57:59 +0900987 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900988 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900989 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 // TODO: Make this work even for non-current users?
991 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900992 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900993 }
satok67ddf9c2010-11-17 09:45:54 +0900994 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900995 final InputMethodInfo imi;
996 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900997 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900998 } else {
999 imi = mMethodMap.get(imiId);
1000 }
1001 if (imi == null) {
1002 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001003 }
1004 return mSettings.getEnabledInputMethodSubtypeListLocked(
1005 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001006 }
1007 }
1008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public void addClient(IInputMethodClient client,
1011 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001012 if (!calledFromValidUser()) {
1013 return;
1014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 synchronized (mMethodMap) {
1016 mClients.put(client.asBinder(), new ClientState(client,
1017 inputContext, uid, pid));
1018 }
1019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
satoke7c6998e2011-06-03 17:57:59 +09001021 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001023 if (!calledFromValidUser()) {
1024 return;
1025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001027 ClientState cs = mClients.remove(client.asBinder());
1028 if (cs != null) {
1029 clearClientSessionLocked(cs);
1030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 void executeOrSendMessage(IInterface target, Message msg) {
1035 if (target.asBinder() instanceof Binder) {
1036 mCaller.sendMessage(msg);
1037 } else {
1038 handleMessage(msg);
1039 msg.recycle();
1040 }
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001043 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 + mCurClient.client.asBinder());
1047 if (mBoundToMethod) {
1048 mBoundToMethod = false;
1049 if (mCurMethod != null) {
1050 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1051 MSG_UNBIND_INPUT, mCurMethod));
1052 }
1053 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001054
1055 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1056 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1058 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1059 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Project10592532009-03-18 17:39:46 -07001062 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 private int getImeShowFlags() {
1067 int flags = 0;
1068 if (mShowForced) {
1069 flags |= InputMethod.SHOW_FORCED
1070 | InputMethod.SHOW_EXPLICIT;
1071 } else if (mShowExplicitlyRequested) {
1072 flags |= InputMethod.SHOW_EXPLICIT;
1073 }
1074 return flags;
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 private int getAppShowFlags() {
1078 int flags = 0;
1079 if (mShowForced) {
1080 flags |= InputMethodManager.SHOW_FORCED;
1081 } else if (!mShowExplicitlyRequested) {
1082 flags |= InputMethodManager.SHOW_IMPLICIT;
1083 }
1084 return flags;
1085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001086
Dianne Hackborn7663d802012-02-24 13:08:49 -08001087 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 if (!mBoundToMethod) {
1089 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1090 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1091 mBoundToMethod = true;
1092 }
1093 final SessionState session = mCurClient.curSession;
1094 if (initial) {
1095 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1096 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1097 } else {
1098 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1099 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1100 }
1101 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001102 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001103 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001105 return new InputBindResult(session.session,
1106 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001110 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 // If no method is currently selected, do nothing.
1112 if (mCurMethodId == null) {
1113 return mNoBinding;
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 ClientState cs = mClients.get(client.asBinder());
1117 if (cs == null) {
1118 throw new IllegalArgumentException("unknown client "
1119 + client.asBinder());
1120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 try {
1123 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1124 // Check with the window manager to make sure this client actually
1125 // has a window with focus. If not, reject. This is thread safe
1126 // because if the focus changes some time before or after, the
1127 // next client receiving focus that has any interest in input will
1128 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001129 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1131 return null;
1132 }
1133 } catch (RemoteException e) {
1134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001135
Dianne Hackborn7663d802012-02-24 13:08:49 -08001136 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1137 }
1138
1139 InputBindResult startInputUncheckedLocked(ClientState cs,
1140 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1141 // If no method is currently selected, do nothing.
1142 if (mCurMethodId == null) {
1143 return mNoBinding;
1144 }
1145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001147 // Was the keyguard locked when switching over to the new client?
1148 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 // If the client is changing, we need to switch over to the new
1150 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001151 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001152 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001153 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154
1155 // If the screen is on, inform the new client it is active
1156 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001157 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1158 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 // Bump up the sequence for this client and attach it.
1163 mCurSeq++;
1164 if (mCurSeq <= 0) mCurSeq = 1;
1165 mCurClient = cs;
1166 mCurInputContext = inputContext;
1167 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Check if the input method is changing.
1170 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1171 if (cs.curSession != null) {
1172 // Fast case: if we are already connected to the input method,
1173 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001174 return attachNewInputLocked(
1175 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 if (mHaveConnection) {
1178 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // Return to client, and we will get back with it when
1180 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001181 requestClientSessionLocked(cs);
1182 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 } else if (SystemClock.uptimeMillis()
1184 < (mLastBindTime+TIME_TO_RECONNECT)) {
1185 // In this case we have connected to the service, but
1186 // don't yet have its interface. If it hasn't been too
1187 // long since we did the connection, we'll return to
1188 // the client and wait to get the service interface so
1189 // we can report back. If it has been too long, we want
1190 // to fall through so we can try a disconnect/reconnect
1191 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001192 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1195 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
1198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001200 return startInputInnerLocked();
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001203 InputBindResult startInputInnerLocked() {
1204 if (mCurMethodId == null) {
1205 return mNoBinding;
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001208 if (!mSystemReady) {
1209 // If the system is not yet ready, we shouldn't be running third
1210 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001211 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1215 if (info == null) {
1216 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001219 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1222 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001223 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1224 com.android.internal.R.string.input_method_binding_label);
1225 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1226 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001227 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornc8230512013-07-13 21:32:12 -07001228 | Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 mLastBindTime = SystemClock.uptimeMillis();
1230 mHaveConnection = true;
1231 mCurId = info.getId();
1232 mCurToken = new Binder();
1233 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001234 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 mIWindowManager.addWindowToken(mCurToken,
1236 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1237 } catch (RemoteException e) {
1238 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001239 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 } else {
1241 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 + mCurIntent);
1244 }
1245 return null;
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
satoke7c6998e2011-06-03 17:57:59 +09001248 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001250 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001251 if (!calledFromValidUser()) {
1252 return null;
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 synchronized (mMethodMap) {
1255 final long ident = Binder.clearCallingIdentity();
1256 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001257 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 } finally {
1259 Binder.restoreCallingIdentity(ident);
1260 }
1261 }
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
satoke7c6998e2011-06-03 17:57:59 +09001264 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 public void finishInput(IInputMethodClient client) {
1266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001267
satoke7c6998e2011-06-03 17:57:59 +09001268 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 public void onServiceConnected(ComponentName name, IBinder service) {
1270 synchronized (mMethodMap) {
1271 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1272 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001273 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001274 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001275 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001276 return;
1277 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001278 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001279 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1280 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001282 clearClientSessionLocked(mCurClient);
1283 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
1286 }
1287 }
1288
Jeff Brownc28867a2013-03-26 15:42:39 -07001289 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1290 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 synchronized (mMethodMap) {
1292 if (mCurMethod != null && method != null
1293 && mCurMethod.asBinder() == method.asBinder()) {
1294 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001295 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001297 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001298 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (res.method != null) {
1300 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1301 MSG_BIND_METHOD, mCurClient.client, res));
1302 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001303 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
1305 }
1306 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001307
1308 // Session abandoned. Close its associated input channel.
1309 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001312 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001313 if (mVisibleBound) {
1314 mContext.unbindService(mVisibleConnection);
1315 mVisibleBound = false;
1316 }
1317
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001318 if (mHaveConnection) {
1319 mContext.unbindService(this);
1320 mHaveConnection = false;
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 if (mCurToken != null) {
1324 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001325 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001326 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001327 // The current IME is shown. Hence an IME switch (transition) is happening.
1328 mWindowManagerService.saveLastInputMethodWindowForTransition();
1329 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001330 mIWindowManager.removeWindowToken(mCurToken);
1331 } catch (RemoteException e) {
1332 }
1333 mCurToken = null;
1334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
The Android Open Source Project10592532009-03-18 17:39:46 -07001336 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001337 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001339 if (reportToClient && mCurClient != null) {
1340 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1341 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1342 }
1343 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001344
1345 void requestClientSessionLocked(ClientState cs) {
1346 if (!cs.sessionRequested) {
1347 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1348 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1349 cs.sessionRequested = true;
1350 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1351 MSG_CREATE_SESSION, mCurMethod, channels[1],
1352 new MethodCallback(this, mCurMethod, channels[0])));
1353 }
1354 }
1355
1356 void clearClientSessionLocked(ClientState cs) {
1357 finishSessionLocked(cs.curSession);
1358 cs.curSession = null;
1359 cs.sessionRequested = false;
1360 }
1361
1362 private void finishSessionLocked(SessionState sessionState) {
1363 if (sessionState != null) {
1364 if (sessionState.session != null) {
1365 try {
1366 sessionState.session.finishSession();
1367 } catch (RemoteException e) {
1368 Slog.w(TAG, "Session failed to close due to remote exception", e);
1369 setImeWindowVisibilityStatusHiddenLocked();
1370 }
1371 sessionState.session = null;
1372 }
1373 if (sessionState.channel != null) {
1374 sessionState.channel.dispose();
1375 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001376 }
1377 }
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001380 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 if (mCurMethod != null) {
1382 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001383 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001385
Jeff Brownc28867a2013-03-26 15:42:39 -07001386 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001387 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 mCurMethod = null;
1389 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001390 if (mStatusBar != null) {
1391 mStatusBar.setIconVisibility("ime", false);
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001394
satoke7c6998e2011-06-03 17:57:59 +09001395 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 public void onServiceDisconnected(ComponentName name) {
1397 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001398 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 + " mCurIntent=" + mCurIntent);
1400 if (mCurMethod != null && mCurIntent != null
1401 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001402 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 // We consider this to be a new bind attempt, since the system
1404 // should now try to restart the service for us.
1405 mLastBindTime = SystemClock.uptimeMillis();
1406 mShowRequested = mInputShown;
1407 mInputShown = false;
1408 if (mCurClient != null) {
1409 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1410 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1411 }
1412 }
1413 }
1414 }
1415
satokf9f01002011-05-19 21:31:50 +09001416 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001418 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 long ident = Binder.clearCallingIdentity();
1420 try {
1421 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001422 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 return;
1424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 synchronized (mMethodMap) {
1427 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001428 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001429 if (mStatusBar != null) {
1430 mStatusBar.setIconVisibility("ime", false);
1431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001433 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001434 CharSequence contentDescription = null;
1435 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001436 // Use PackageManager to load label
1437 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001438 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001439 mIPackageManager.getApplicationInfo(packageName, 0,
1440 mSettings.getCurrentUserId()));
1441 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001442 /* ignore */
1443 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001444 if (mStatusBar != null) {
1445 mStatusBar.setIcon("ime", packageName, iconId, 0,
1446 contentDescription != null
1447 ? contentDescription.toString() : null);
1448 mStatusBar.setIconVisibility("ime", true);
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452 } finally {
1453 Binder.restoreCallingIdentity(ident);
1454 }
1455 }
1456
satok7cfc0ed2011-06-20 21:29:36 +09001457 private boolean needsToShowImeSwitchOngoingNotification() {
1458 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001459 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001460 synchronized (mMethodMap) {
1461 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1462 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001463 if (N > 2) return true;
1464 if (N < 1) return false;
1465 int nonAuxCount = 0;
1466 int auxCount = 0;
1467 InputMethodSubtype nonAuxSubtype = null;
1468 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001469 for(int i = 0; i < N; ++i) {
1470 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001471 final List<InputMethodSubtype> subtypes =
1472 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001473 final int subtypeCount = subtypes.size();
1474 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001475 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001476 } else {
1477 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001478 final InputMethodSubtype subtype = subtypes.get(j);
1479 if (!subtype.isAuxiliary()) {
1480 ++nonAuxCount;
1481 nonAuxSubtype = subtype;
1482 } else {
1483 ++auxCount;
1484 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001485 }
1486 }
1487 }
satok7cfc0ed2011-06-20 21:29:36 +09001488 }
satokb6359412011-06-28 17:47:41 +09001489 if (nonAuxCount > 1 || auxCount > 1) {
1490 return true;
1491 } else if (nonAuxCount == 1 && auxCount == 1) {
1492 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001493 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1494 || auxSubtype.overridesImplicitlyEnabledSubtype()
1495 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001496 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1497 return false;
1498 }
1499 return true;
1500 }
1501 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001502 }
satok7cfc0ed2011-06-20 21:29:36 +09001503 }
1504
John Spurlocke0980502013-10-25 11:59:29 -04001505 private boolean isKeyguardLocked() {
1506 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1507 }
1508
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001509 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001510 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001511 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001512 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001513 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001514 try {
1515 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001516 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001517 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001518 return;
1519 }
satok06487a52010-10-29 11:37:18 +09001520 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001521 // apply policy for binder calls
1522 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1523 vis = 0;
1524 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001525 mImeWindowVis = vis;
1526 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001527 if (mStatusBar != null) {
1528 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1529 }
jungheang.lee217fd292013-02-26 16:53:22 +09001530 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1531 && (mWindowManagerService.isHardKeyboardAvailable()
1532 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001533 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1534 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001535 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001536 final CharSequence title = mRes.getText(
1537 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001538 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1539 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001540
satok7cfc0ed2011-06-20 21:29:36 +09001541 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001542 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001543 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001544 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001545 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001546 }
1547 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001548 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001549 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001550 mNotificationShown = true;
1551 }
satok7cfc0ed2011-06-20 21:29:36 +09001552 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001553 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001554 if (DEBUG) {
1555 Slog.d(TAG, "--- hide notification");
1556 }
1557 mNotificationManager.cancelAsUser(null,
1558 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001559 mNotificationShown = false;
1560 }
1561 }
satok06487a52010-10-29 11:37:18 +09001562 }
1563 } finally {
1564 Binder.restoreCallingIdentity(ident);
1565 }
1566 }
1567
satoke7c6998e2011-06-03 17:57:59 +09001568 @Override
satokf9f01002011-05-19 21:31:50 +09001569 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001570 if (!calledFromValidUser()) {
1571 return;
1572 }
satokf9f01002011-05-19 21:31:50 +09001573 synchronized (mMethodMap) {
1574 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1575 for (int i = 0; i < spans.length; ++i) {
1576 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001577 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001578 mSecureSuggestionSpans.put(ss, currentImi);
1579 }
1580 }
1581 }
1582 }
1583
satoke7c6998e2011-06-03 17:57:59 +09001584 @Override
satokf9f01002011-05-19 21:31:50 +09001585 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001586 if (!calledFromValidUser()) {
1587 return false;
1588 }
satokf9f01002011-05-19 21:31:50 +09001589 synchronized (mMethodMap) {
1590 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1591 // TODO: Do not send the intent if the process of the targetImi is already dead.
1592 if (targetImi != null) {
1593 final String[] suggestions = span.getSuggestions();
1594 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001595 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001596 final Intent intent = new Intent();
1597 // Ensures that only a class in the original IME package will receive the
1598 // notification.
satok42c5a162011-05-26 16:46:14 +09001599 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001600 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1601 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1602 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1603 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001604 final long ident = Binder.clearCallingIdentity();
1605 try {
1606 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1607 } finally {
1608 Binder.restoreCallingIdentity(ident);
1609 }
satokf9f01002011-05-19 21:31:50 +09001610 return true;
1611 }
1612 }
1613 return false;
1614 }
1615
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001616 void updateFromSettingsLocked(boolean enabledMayChange) {
1617 if (enabledMayChange) {
1618 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1619 for (int i=0; i<enabled.size(); i++) {
1620 // We allow the user to select "disabled until used" apps, so if they
1621 // are enabling one of those here we now need to make it enabled.
1622 InputMethodInfo imm = enabled.get(i);
1623 try {
1624 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1625 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1626 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001627 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001628 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001629 if (DEBUG) {
1630 Slog.d(TAG, "Update state(" + imm.getId()
1631 + "): DISABLED_UNTIL_USED -> DEFAULT");
1632 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001633 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1634 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001635 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1636 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001637 }
1638 } catch (RemoteException e) {
1639 }
1640 }
1641 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001642 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1643 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1644 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1645 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001646 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001647 // There is no input method selected, try to choose new applicable input method.
1648 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001649 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001650 }
1651 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001653 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001655 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001656 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001657 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
satokf3db1af2010-11-23 13:34:33 +09001659 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001660 } else {
1661 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001662 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001663 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
satokab751aa2010-09-14 19:17:36 +09001667 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 InputMethodInfo info = mMethodMap.get(id);
1669 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001670 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001672
satokd81e9502012-05-21 12:58:45 +09001673 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001675 final int subtypeCount = info.getSubtypeCount();
1676 if (subtypeCount <= 0) {
1677 return;
satokcd7cd292010-11-20 15:46:23 +09001678 }
satokd81e9502012-05-21 12:58:45 +09001679 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1680 final InputMethodSubtype newSubtype;
1681 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1682 newSubtype = info.getSubtypeAt(subtypeId);
1683 } else {
1684 // If subtype is null, try to find the most applicable one from
1685 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001686 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001687 }
1688 if (newSubtype == null || oldSubtype == null) {
1689 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1690 + ", new subtype = " + newSubtype);
1691 return;
1692 }
1693 if (newSubtype != oldSubtype) {
1694 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1695 if (mCurMethod != null) {
1696 try {
1697 refreshImeWindowVisibilityLocked();
1698 mCurMethod.changeInputMethodSubtype(newSubtype);
1699 } catch (RemoteException e) {
1700 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001701 }
1702 }
1703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 return;
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
satokd81e9502012-05-21 12:58:45 +09001707 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 final long ident = Binder.clearCallingIdentity();
1709 try {
satokab751aa2010-09-14 19:17:36 +09001710 // Set a subtype to this input method.
1711 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001712 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1713 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1714 // because mCurMethodId is stored as a history in
1715 // setSelectedInputMethodAndSubtypeLocked().
1716 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717
1718 if (ActivityManagerNative.isSystemReady()) {
1719 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001720 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001722 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001724 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 } finally {
1726 Binder.restoreCallingIdentity(ident);
1727 }
1728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001729
satok42c5a162011-05-26 16:46:14 +09001730 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 public boolean showSoftInput(IInputMethodClient client, int flags,
1732 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001733 if (!calledFromValidUser()) {
1734 return false;
1735 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001736 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 long ident = Binder.clearCallingIdentity();
1738 try {
1739 synchronized (mMethodMap) {
1740 if (mCurClient == null || client == null
1741 || mCurClient.client.asBinder() != client.asBinder()) {
1742 try {
1743 // We need to check if this is the current client with
1744 // focus in the window manager, to allow this call to
1745 // be made before input is started in it.
1746 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001747 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001748 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001751 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001754
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001755 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758 } finally {
1759 Binder.restoreCallingIdentity(ident);
1760 }
1761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001762
The Android Open Source Project4df24232009-03-05 14:34:35 -08001763 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 mShowRequested = true;
1765 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1766 mShowExplicitlyRequested = true;
1767 }
1768 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1769 mShowExplicitlyRequested = true;
1770 mShowForced = true;
1771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001772
Dianne Hackborncc278702009-09-02 23:07:23 -07001773 if (!mSystemReady) {
1774 return false;
1775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001779 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1781 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1782 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001784 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001785 bindCurrentInputMethodService(
1786 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001787 mVisibleBound = true;
1788 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001791 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // The client has asked to have the input method shown, but
1793 // we have been sitting here too long with a connection to the
1794 // service and no interface received, so let's disconnect/connect
1795 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001798 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001800 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001801 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001802 } else {
1803 if (DEBUG) {
1804 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1805 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001808
The Android Open Source Project4df24232009-03-05 14:34:35 -08001809 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001811
satok42c5a162011-05-26 16:46:14 +09001812 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001813 public boolean hideSoftInput(IInputMethodClient client, int flags,
1814 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001815 if (!calledFromValidUser()) {
1816 return false;
1817 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001818 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 long ident = Binder.clearCallingIdentity();
1820 try {
1821 synchronized (mMethodMap) {
1822 if (mCurClient == null || client == null
1823 || mCurClient.client.asBinder() != client.asBinder()) {
1824 try {
1825 // We need to check if this is the current client with
1826 // focus in the window manager, to allow this call to
1827 // be made before input is started in it.
1828 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001829 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1830 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001831 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001832 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001835 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001839
Joe Onorato8a9b2202010-02-26 18:56:32 -08001840 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001841 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843 } finally {
1844 Binder.restoreCallingIdentity(ident);
1845 }
1846 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001847
The Android Open Source Project4df24232009-03-05 14:34:35 -08001848 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1850 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001851 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 -08001852 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001855 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 -08001856 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001858 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1861 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1862 res = true;
1863 } else {
1864 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001866 if (mHaveConnection && mVisibleBound) {
1867 mContext.unbindService(mVisibleConnection);
1868 mVisibleBound = false;
1869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 mInputShown = false;
1871 mShowRequested = false;
1872 mShowExplicitlyRequested = false;
1873 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001874 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001876
satok42c5a162011-05-26 16:46:14 +09001877 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001878 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1879 int controlFlags, int softInputMode, int windowFlags,
1880 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001881 // Needs to check the validity before clearing calling identity
1882 final boolean calledFromValidUser = calledFromValidUser();
1883
Dianne Hackborn7663d802012-02-24 13:08:49 -08001884 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 long ident = Binder.clearCallingIdentity();
1886 try {
1887 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001889 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001891 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001892
Dianne Hackborn7663d802012-02-24 13:08:49 -08001893 ClientState cs = mClients.get(client.asBinder());
1894 if (cs == null) {
1895 throw new IllegalArgumentException("unknown client "
1896 + client.asBinder());
1897 }
1898
1899 try {
1900 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1901 // Check with the window manager to make sure this client actually
1902 // has a window with focus. If not, reject. This is thread safe
1903 // because if the focus changes some time before or after, the
1904 // next client receiving focus that has any interest in input will
1905 // be calling through here after that change happens.
1906 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1907 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1908 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001910 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001912
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001913 if (!calledFromValidUser) {
1914 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1915 Slog.w(TAG, "If you want to interect with IME, you need "
1916 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1917 hideCurrentInputLocked(0, null);
1918 return null;
1919 }
1920
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001921 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001922 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001923 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001924 if (attribute != null) {
1925 return startInputUncheckedLocked(cs, inputContext, attribute,
1926 controlFlags);
1927 }
1928 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001929 }
1930 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001931
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001932 // Should we auto-show the IME even if the caller has not
1933 // specified what should be done with it?
1934 // We only do this automatically if the window can resize
1935 // to accommodate the IME (so what the user sees will give
1936 // them good context without input information being obscured
1937 // by the IME) or if running on a large screen where there
1938 // is more room for the target window + IME.
1939 final boolean doAutoShow =
1940 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1941 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1942 || mRes.getConfiguration().isLayoutSizeAtLeast(
1943 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001944 final boolean isTextEditor =
1945 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1946
1947 // We want to start input before showing the IME, but after closing
1948 // it. We want to do this after closing it to help the IME disappear
1949 // more quickly (not get stuck behind it initializing itself for the
1950 // new focused input, even if its window wants to hide the IME).
1951 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1954 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001955 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1957 // There is no focus view, and this window will
1958 // be behind any soft input window, so hide the
1959 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001960 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001961 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001963 } else if (isTextEditor && doAutoShow && (softInputMode &
1964 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 // There is a focus view, and we are navigating forward
1966 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001967 // We only do this automatically if the window can resize
1968 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001969 // them good context without input information being obscured
1970 // by the IME) or if running on a large screen where there
1971 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001973 if (attribute != null) {
1974 res = startInputUncheckedLocked(cs, inputContext, attribute,
1975 controlFlags);
1976 didStart = true;
1977 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001978 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980 break;
1981 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1982 // Do nothing.
1983 break;
1984 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1985 if ((softInputMode &
1986 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
1990 break;
1991 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001992 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 break;
1995 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1996 if ((softInputMode &
1997 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001998 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001999 if (attribute != null) {
2000 res = startInputUncheckedLocked(cs, inputContext, attribute,
2001 controlFlags);
2002 didStart = true;
2003 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002004 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
2006 break;
2007 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002008 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002009 if (attribute != null) {
2010 res = startInputUncheckedLocked(cs, inputContext, attribute,
2011 controlFlags);
2012 didStart = true;
2013 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002014 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 break;
2016 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002017
2018 if (!didStart && attribute != null) {
2019 res = startInputUncheckedLocked(cs, inputContext, attribute,
2020 controlFlags);
2021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
2023 } finally {
2024 Binder.restoreCallingIdentity(ident);
2025 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002026
2027 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
satok42c5a162011-05-26 16:46:14 +09002030 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002032 if (!calledFromValidUser()) {
2033 return;
2034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 synchronized (mMethodMap) {
2036 if (mCurClient == null || client == null
2037 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002038 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002039 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041
satok440aab52010-11-25 09:43:11 +09002042 // Always call subtype picker, because subtype picker is a superset of input method
2043 // picker.
satokab751aa2010-09-14 19:17:36 +09002044 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2045 }
2046 }
2047
satok42c5a162011-05-26 16:46:14 +09002048 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 if (!calledFromValidUser()) {
2051 return;
2052 }
satok28203512010-11-24 11:06:49 +09002053 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2054 }
2055
satok42c5a162011-05-26 16:46:14 +09002056 @Override
satok28203512010-11-24 11:06:49 +09002057 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002058 if (!calledFromValidUser()) {
2059 return;
2060 }
satok28203512010-11-24 11:06:49 +09002061 synchronized (mMethodMap) {
2062 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002063 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002064 mMethodMap.get(id), subtype.hashCode()));
2065 } else {
2066 setInputMethod(token, id);
2067 }
2068 }
satokab751aa2010-09-14 19:17:36 +09002069 }
2070
satok42c5a162011-05-26 16:46:14 +09002071 @Override
satokb416a712010-11-25 20:42:14 +09002072 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002073 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002074 if (!calledFromValidUser()) {
2075 return;
2076 }
satokb416a712010-11-25 20:42:14 +09002077 synchronized (mMethodMap) {
2078 if (mCurClient == null || client == null
2079 || mCurClient.client.asBinder() != client.asBinder()) {
2080 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2081 }
satok7fee71f2010-12-17 18:54:26 +09002082 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2083 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002084 }
2085 }
2086
satok4fc87d62011-05-20 16:13:43 +09002087 @Override
satok735cf382010-11-11 20:40:09 +09002088 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002089 if (!calledFromValidUser()) {
2090 return false;
2091 }
satok735cf382010-11-11 20:40:09 +09002092 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002093 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002094 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002095 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002096 lastImi = mMethodMap.get(lastIme.first);
2097 } else {
2098 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002099 }
satok4fc87d62011-05-20 16:13:43 +09002100 String targetLastImiId = null;
2101 int subtypeId = NOT_A_SUBTYPE_ID;
2102 if (lastIme != null && lastImi != null) {
2103 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2104 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2105 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2106 : mCurrentSubtype.hashCode();
2107 // If the last IME is the same as the current IME and the last subtype is not
2108 // defined, there is no need to switch to the last IME.
2109 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2110 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002111 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002112 }
2113 }
2114
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002115 if (TextUtils.isEmpty(targetLastImiId)
2116 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002117 // This is a safety net. If the currentSubtype can't be added to the history
2118 // and the framework couldn't find the last ime, we will make the last ime be
2119 // the most applicable enabled keyboard subtype of the system imes.
2120 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2121 if (enabled != null) {
2122 final int N = enabled.size();
2123 final String locale = mCurrentSubtype == null
2124 ? mRes.getConfiguration().locale.toString()
2125 : mCurrentSubtype.getLocale();
2126 for (int i = 0; i < N; ++i) {
2127 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002128 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002129 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002130 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2131 InputMethodUtils.getSubtypes(imi),
2132 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002133 if (keyboardSubtype != null) {
2134 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002135 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002136 imi, keyboardSubtype.hashCode());
2137 if(keyboardSubtype.getLocale().equals(locale)) {
2138 break;
2139 }
2140 }
2141 }
2142 }
2143 }
2144 }
2145
2146 if (!TextUtils.isEmpty(targetLastImiId)) {
2147 if (DEBUG) {
2148 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2149 + ", from: " + mCurMethodId + ", " + subtypeId);
2150 }
2151 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2152 return true;
2153 } else {
2154 return false;
2155 }
satok735cf382010-11-11 20:40:09 +09002156 }
2157 }
2158
satoke7c6998e2011-06-03 17:57:59 +09002159 @Override
satok688bd472012-02-09 20:09:17 +09002160 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002161 if (!calledFromValidUser()) {
2162 return false;
2163 }
satok688bd472012-02-09 20:09:17 +09002164 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002165 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
satok688bd472012-02-09 20:09:17 +09002166 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2167 if (nextSubtype == null) {
2168 return false;
2169 }
2170 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2171 return true;
2172 }
2173 }
2174
2175 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002176 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2177 if (!calledFromValidUser()) {
2178 return false;
2179 }
2180 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002181 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002182 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2183 if (nextSubtype == null) {
2184 return false;
2185 }
2186 return true;
2187 }
2188 }
2189
2190 @Override
satok68f1b782011-04-11 14:26:04 +09002191 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002192 if (!calledFromValidUser()) {
2193 return null;
2194 }
satok68f1b782011-04-11 14:26:04 +09002195 synchronized (mMethodMap) {
2196 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2197 // TODO: Handle the case of the last IME with no subtypes
2198 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2199 || TextUtils.isEmpty(lastIme.second)) return null;
2200 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2201 if (lastImi == null) return null;
2202 try {
2203 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002204 final int lastSubtypeId =
2205 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002206 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2207 return null;
2208 }
2209 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002210 } catch (NumberFormatException e) {
2211 return null;
2212 }
2213 }
2214 }
2215
satoke7c6998e2011-06-03 17:57:59 +09002216 @Override
satokee5e77c2011-09-02 18:50:15 +09002217 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002218 if (!calledFromValidUser()) {
2219 return;
2220 }
satok91e88122011-07-18 11:11:42 +09002221 // By this IPC call, only a process which shares the same uid with the IME can add
2222 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002223 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002224 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002225 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002226 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002227 final String[] packageInfos;
2228 try {
2229 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2230 } catch (RemoteException e) {
2231 Slog.e(TAG, "Failed to get package infos");
2232 return;
2233 }
satok91e88122011-07-18 11:11:42 +09002234 if (packageInfos != null) {
2235 final int packageNum = packageInfos.length;
2236 for (int i = 0; i < packageNum; ++i) {
2237 if (packageInfos[i].equals(imi.getPackageName())) {
2238 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002239 final long ident = Binder.clearCallingIdentity();
2240 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002241 buildInputMethodListLocked(mMethodList, mMethodMap,
2242 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002243 } finally {
2244 Binder.restoreCallingIdentity(ident);
2245 }
satokee5e77c2011-09-02 18:50:15 +09002246 return;
satok91e88122011-07-18 11:11:42 +09002247 }
2248 }
2249 }
satoke7c6998e2011-06-03 17:57:59 +09002250 }
satokee5e77c2011-09-02 18:50:15 +09002251 return;
satoke7c6998e2011-06-03 17:57:59 +09002252 }
2253
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002254 @Override
2255 public int getInputMethodWindowVisibleHeight() {
2256 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2257 }
2258
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002259 @Override
2260 public void notifyTextCommitted() {
2261 if (DEBUG) {
2262 Slog.d(TAG, "Got the notification of commitText");
2263 }
2264 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2265 if (imi != null) {
2266 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2267 }
2268 }
2269
satok28203512010-11-24 11:06:49 +09002270 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 synchronized (mMethodMap) {
2272 if (token == null) {
2273 if (mContext.checkCallingOrSelfPermission(
2274 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2275 != PackageManager.PERMISSION_GRANTED) {
2276 throw new SecurityException(
2277 "Using null token requires permission "
2278 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2279 }
2280 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002281 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2282 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 return;
2284 }
2285
satokc5933802011-08-31 21:26:04 +09002286 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 try {
satokab751aa2010-09-14 19:17:36 +09002288 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 } finally {
2290 Binder.restoreCallingIdentity(ident);
2291 }
2292 }
2293 }
2294
satok42c5a162011-05-26 16:46:14 +09002295 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002297 if (!calledFromValidUser()) {
2298 return;
2299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 synchronized (mMethodMap) {
2301 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002302 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2303 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 return;
2305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 long ident = Binder.clearCallingIdentity();
2307 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002308 hideCurrentInputLocked(flags, null);
2309 } finally {
2310 Binder.restoreCallingIdentity(ident);
2311 }
2312 }
2313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002314
satok42c5a162011-05-26 16:46:14 +09002315 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002316 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 if (!calledFromValidUser()) {
2318 return;
2319 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002320 synchronized (mMethodMap) {
2321 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002322 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2323 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002324 return;
2325 }
2326 long ident = Binder.clearCallingIdentity();
2327 try {
2328 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 } finally {
2330 Binder.restoreCallingIdentity(ident);
2331 }
2332 }
2333 }
2334
2335 void setEnabledSessionInMainThread(SessionState session) {
2336 if (mEnabledSession != session) {
2337 if (mEnabledSession != null) {
2338 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002339 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 mEnabledSession.method.setSessionEnabled(
2341 mEnabledSession.session, false);
2342 } catch (RemoteException e) {
2343 }
2344 }
2345 mEnabledSession = session;
2346 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002347 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 session.method.setSessionEnabled(
2349 session.session, true);
2350 } catch (RemoteException e) {
2351 }
2352 }
2353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002354
satok42c5a162011-05-26 16:46:14 +09002355 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002357 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 switch (msg.what) {
2359 case MSG_SHOW_IM_PICKER:
2360 showInputMethodMenu();
2361 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002362
satokab751aa2010-09-14 19:17:36 +09002363 case MSG_SHOW_IM_SUBTYPE_PICKER:
2364 showInputMethodSubtypeMenu();
2365 return true;
2366
satok47a44912010-10-06 16:03:58 +09002367 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002368 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002369 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002370 args.recycle();
satok217f5482010-12-15 05:19:19 +09002371 return true;
2372
2373 case MSG_SHOW_IM_CONFIG:
2374 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002375 return true;
2376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 case MSG_UNBIND_INPUT:
2380 try {
2381 ((IInputMethod)msg.obj).unbindInput();
2382 } catch (RemoteException e) {
2383 // There is nothing interesting about the method dying.
2384 }
2385 return true;
2386 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002387 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 try {
2389 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2390 } catch (RemoteException e) {
2391 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002392 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 return true;
2394 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002395 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002397 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002398 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002399 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 } catch (RemoteException e) {
2401 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002402 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 return true;
2404 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002405 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002407 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002408 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002409 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 } catch (RemoteException e) {
2411 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002412 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 return true;
2414 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002415 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002417 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2419 } catch (RemoteException e) {
2420 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002421 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002423 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002424 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002425 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002426 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002428 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002430 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002431 // Dispose the channel if the input method is not local to this process
2432 // because the remote proxy will get its own copy when unparceled.
2433 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002434 channel.dispose();
2435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002437 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002443 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 try {
2445 SessionState session = (SessionState)args.arg1;
2446 setEnabledSessionInMainThread(session);
2447 session.method.startInput((IInputContext)args.arg2,
2448 (EditorInfo)args.arg3);
2449 } catch (RemoteException e) {
2450 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002451 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 return true;
2453 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002454 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 try {
2456 SessionState session = (SessionState)args.arg1;
2457 setEnabledSessionInMainThread(session);
2458 session.method.restartInput((IInputContext)args.arg2,
2459 (EditorInfo)args.arg3);
2460 } catch (RemoteException e) {
2461 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002462 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 case MSG_UNBIND_METHOD:
2468 try {
2469 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2470 } catch (RemoteException e) {
2471 // There is nothing interesting about the last client dying.
2472 }
2473 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002474 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002475 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002476 IInputMethodClient client = (IInputMethodClient)args.arg1;
2477 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002479 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002481 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002482 } finally {
2483 // Dispose the channel if the input method is not local to this process
2484 // because the remote proxy will get its own copy when unparceled.
2485 if (res.channel != null && Binder.isProxy(client)) {
2486 res.channel.dispose();
2487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002489 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002491 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002492 case MSG_SET_ACTIVE:
2493 try {
2494 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2495 } catch (RemoteException e) {
2496 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2497 + ((ClientState)msg.obj).pid + " uid "
2498 + ((ClientState)msg.obj).uid);
2499 }
2500 return true;
satok01038492012-04-09 21:08:27 +09002501
2502 // --------------------------------------------------------------
2503 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2504 mHardKeyboardListener.handleHardKeyboardStatusChange(
2505 msg.arg1 == 1, msg.arg2 == 1);
2506 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 }
2508 return false;
2509 }
2510
satokdc9ddae2011-10-06 12:22:36 +09002511 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002512 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2513 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002514 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002515 if (DEBUG) {
2516 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2517 }
satok723a27e2010-11-11 14:58:11 +09002518 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002519 return true;
2520 }
2521
2522 return false;
2523 }
2524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002526 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002527 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002528 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002529 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 list.clear();
2532 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002533
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 // Use for queryIntentServicesAsUser
2535 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002537 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002539 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002541 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2542 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002543
satoke7c6998e2011-06-03 17:57:59 +09002544 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2545 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 for (int i = 0; i < services.size(); ++i) {
2547 ResolveInfo ri = services.get(i);
2548 ServiceInfo si = ri.serviceInfo;
2549 ComponentName compName = new ComponentName(si.packageName, si.name);
2550 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2551 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002552 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 + ": it does not require the permission "
2554 + android.Manifest.permission.BIND_INPUT_METHOD);
2555 continue;
2556 }
2557
Joe Onorato8a9b2202010-02-26 18:56:32 -08002558 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559
2560 try {
satoke7c6998e2011-06-03 17:57:59 +09002561 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002563 final String id = p.getId();
2564 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565
2566 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002567 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002571 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002573 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 }
2575 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002576
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002577 if (resetDefaultEnabledIme) {
2578 final ArrayList<InputMethodInfo> defaultEnabledIme =
2579 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2580 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2581 final InputMethodInfo imi = defaultEnabledIme.get(i);
2582 if (DEBUG) {
2583 Slog.d(TAG, "--- enable ime = " + imi);
2584 }
2585 setInputMethodEnabledLocked(imi.getId(), true);
2586 }
2587 }
2588
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002589 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002590 if (!TextUtils.isEmpty(defaultImiId)) {
2591 if (!map.containsKey(defaultImiId)) {
2592 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2593 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002594 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002595 }
2596 } else {
2597 // Double check that the default IME is certainly enabled.
2598 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002599 }
2600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002604
satokab751aa2010-09-14 19:17:36 +09002605 private void showInputMethodMenu() {
2606 showInputMethodMenuInternal(false);
2607 }
2608
2609 private void showInputMethodSubtypeMenu() {
2610 showInputMethodMenuInternal(true);
2611 }
2612
satok217f5482010-12-15 05:19:19 +09002613 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002614 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002615 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002616 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2617 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002618 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002619 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002620 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002621 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002622 }
2623
2624 private void showConfigureInputMethods() {
2625 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2626 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2627 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2628 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002629 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002630 }
2631
satok2c93efc2012-04-02 19:33:47 +09002632 private boolean isScreenLocked() {
2633 return mKeyguardManager != null
2634 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2635 }
satokab751aa2010-09-14 19:17:36 +09002636 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002637 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002640 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002641
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002642 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002643 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002644 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002645
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002646 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002647 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002648 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2649 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002650 if (immis == null || immis.size() == 0) {
2651 return;
2652 }
2653
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002654 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655
satok688bd472012-02-09 20:09:17 +09002656 final List<ImeSubtypeListItem> imList =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002657 mSwitchingController.getSortedInputMethodAndSubtypeList(
satok688bd472012-02-09 20:09:17 +09002658 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002659
satokc3690562012-01-10 20:14:43 +09002660 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002661 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002662 if (currentSubtype != null) {
2663 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002664 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2665 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002666 }
2667 }
2668
Ken Wakasa761eb372011-03-04 19:06:18 +09002669 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002670 mIms = new InputMethodInfo[N];
2671 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002672 int checkedItem = 0;
2673 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002674 final ImeSubtypeListItem item = imList.get(i);
2675 mIms[i] = item.mImi;
2676 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002677 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002678 int subtypeId = mSubtypeIds[i];
2679 if ((subtypeId == NOT_A_SUBTYPE_ID)
2680 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2681 || (subtypeId == lastInputMethodSubtypeId)) {
2682 checkedItem = i;
2683 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002686 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002687 com.android.internal.R.styleable.DialogPreference,
2688 com.android.internal.R.attr.alertDialogStyle, 0);
2689 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002690 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002691 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002692 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002694 }
2695 })
2696 .setIcon(a.getDrawable(
2697 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2698 a.recycle();
satok01038492012-04-09 21:08:27 +09002699 final LayoutInflater inflater =
2700 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2701 final View tv = inflater.inflate(
2702 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2703 mDialogBuilder.setCustomTitle(tv);
2704
2705 // Setup layout for a toggle switch of the hardware keyboard
2706 mSwitchingDialogTitleView = tv;
2707 mSwitchingDialogTitleView.findViewById(
2708 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2709 mWindowManagerService.isHardKeyboardAvailable() ?
2710 View.VISIBLE : View.GONE);
2711 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2712 com.android.internal.R.id.hard_keyboard_switch));
2713 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2714 hardKeySwitch.setOnCheckedChangeListener(
2715 new OnCheckedChangeListener() {
2716 @Override
2717 public void onCheckedChanged(
2718 CompoundButton buttonView, boolean isChecked) {
2719 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002720 // Ensure that the input method dialog is dismissed when changing
2721 // the hardware keyboard state.
2722 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002723 }
2724 });
satokd87c2592010-09-29 11:52:06 +09002725
Ken Wakasa05dbb652011-08-22 15:22:43 +09002726 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2727 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2728 checkedItem);
2729
2730 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002731 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002732 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002733 public void onClick(DialogInterface dialog, int which) {
2734 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002735 if (mIms == null || mIms.length <= which
2736 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002737 return;
2738 }
2739 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002740 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002741 adapter.mCheckedItem = which;
2742 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002743 hideInputMethodMenu();
2744 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002745 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002746 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002747 subtypeId = NOT_A_SUBTYPE_ID;
2748 }
2749 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002750 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002753 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754
satokbc81b692011-08-26 16:22:22 +09002755 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002756 mDialogBuilder.setPositiveButton(
2757 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002758 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002759 @Override
satok7f35c8c2010-10-07 21:13:11 +09002760 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002761 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002762 }
2763 });
2764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002766 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 mSwitchingDialog.getWindow().setType(
2768 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002769 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2770 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002771 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 mSwitchingDialog.show();
2773 }
2774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002775
Ken Wakasa05dbb652011-08-22 15:22:43 +09002776 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2777 private final LayoutInflater mInflater;
2778 private final int mTextViewResourceId;
2779 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002780 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002781 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2782 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2783 super(context, textViewResourceId, itemsList);
2784 mTextViewResourceId = textViewResourceId;
2785 mItemsList = itemsList;
2786 mCheckedItem = checkedItem;
2787 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2788 }
2789
2790 @Override
2791 public View getView(int position, View convertView, ViewGroup parent) {
2792 final View view = convertView != null ? convertView
2793 : mInflater.inflate(mTextViewResourceId, null);
2794 if (position < 0 || position >= mItemsList.size()) return view;
2795 final ImeSubtypeListItem item = mItemsList.get(position);
2796 final CharSequence imeName = item.mImeName;
2797 final CharSequence subtypeName = item.mSubtypeName;
2798 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2799 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2800 if (TextUtils.isEmpty(subtypeName)) {
2801 firstTextView.setText(imeName);
2802 secondTextView.setVisibility(View.GONE);
2803 } else {
2804 firstTextView.setText(subtypeName);
2805 secondTextView.setText(imeName);
2806 secondTextView.setVisibility(View.VISIBLE);
2807 }
2808 final RadioButton radioButton =
2809 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2810 radioButton.setChecked(position == mCheckedItem);
2811 return view;
2812 }
2813 }
2814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002816 synchronized (mMethodMap) {
2817 hideInputMethodMenuLocked();
2818 }
2819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002820
The Android Open Source Project10592532009-03-18 17:39:46 -07002821 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002822 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823
The Android Open Source Project10592532009-03-18 17:39:46 -07002824 if (mSwitchingDialog != null) {
2825 mSwitchingDialog.dismiss();
2826 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project10592532009-03-18 17:39:46 -07002829 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002830 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002834
satok42c5a162011-05-26 16:46:14 +09002835 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002837 // TODO: Make this work even for non-current users?
2838 if (!calledFromValidUser()) {
2839 return false;
2840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 synchronized (mMethodMap) {
2842 if (mContext.checkCallingOrSelfPermission(
2843 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2844 != PackageManager.PERMISSION_GRANTED) {
2845 throw new SecurityException(
2846 "Requires permission "
2847 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2848 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 long ident = Binder.clearCallingIdentity();
2851 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002852 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 } finally {
2854 Binder.restoreCallingIdentity(ident);
2855 }
2856 }
2857 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002858
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002859 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2860 // Make sure this is a valid input method.
2861 InputMethodInfo imm = mMethodMap.get(id);
2862 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002863 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002864 }
2865
satokd87c2592010-09-29 11:52:06 +09002866 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2867 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002868
satokd87c2592010-09-29 11:52:06 +09002869 if (enabled) {
2870 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2871 if (pair.first.equals(id)) {
2872 // We are enabling this input method, but it is already enabled.
2873 // Nothing to do. The previous state was enabled.
2874 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002875 }
2876 }
satokd87c2592010-09-29 11:52:06 +09002877 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2878 // Previous state was disabled.
2879 return false;
2880 } else {
2881 StringBuilder builder = new StringBuilder();
2882 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2883 builder, enabledInputMethodsList, id)) {
2884 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002885 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002886 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2887 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2888 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002889 }
2890 // Previous state was enabled.
2891 return true;
2892 } else {
2893 // We are disabling the input method but it is already disabled.
2894 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002895 return false;
2896 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002897 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002898 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002899
satok723a27e2010-11-11 14:58:11 +09002900 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2901 boolean setSubtypeOnly) {
2902 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002903 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002904
2905 // Set Subtype here
2906 if (imi == null || subtypeId < 0) {
2907 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002908 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002909 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002910 if (subtypeId < imi.getSubtypeCount()) {
2911 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2912 mSettings.putSelectedSubtype(subtype.hashCode());
2913 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002914 } else {
2915 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002916 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002917 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002918 }
satokab751aa2010-09-14 19:17:36 +09002919 }
satok723a27e2010-11-11 14:58:11 +09002920
satok4c0e7152012-06-20 20:08:44 +09002921 // Workaround.
2922 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2923 // IMEs are not recognized and considered uninstalled.
2924 // Actually, we can't move everything after SystemReady because
2925 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2926 // the default IME here and try cheking the default IME again in systemReady().
2927 // TODO: Do nothing before system ready and implement a separated logic for
2928 // the encryption lock screen.
2929 // TODO: ASEC should be ready before IMMS is instantiated.
2930 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002931 // Set InputMethod here
2932 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2933 }
2934 }
2935
2936 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2937 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2938 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2939 // newDefaultIme is empty when there is no candidate for the selected IME.
2940 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2941 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2942 if (subtypeHashCode != null) {
2943 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002944 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002945 imi, Integer.valueOf(subtypeHashCode));
2946 } catch (NumberFormatException e) {
2947 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2948 }
2949 }
2950 }
2951 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002952 }
2953
satok4e4569d2010-11-19 18:45:53 +09002954 // If there are no selected shortcuts, tries finding the most applicable ones.
2955 private Pair<InputMethodInfo, InputMethodSubtype>
2956 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2957 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2958 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002959 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002960 boolean foundInSystemIME = false;
2961
2962 // Search applicable subtype for each InputMethodInfo
2963 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002964 final String imiId = imi.getId();
2965 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2966 continue;
2967 }
satokcd7cd292010-11-20 15:46:23 +09002968 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002969 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002970 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002971 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002972 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002973 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002974 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002975 }
satokdf31ae62011-01-15 06:19:44 +09002976 // 2. Search by the system locale from enabledSubtypes.
2977 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002978 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002979 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002980 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002981 }
satoka86f5e42011-09-02 17:12:42 +09002982 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002983 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002984 final ArrayList<InputMethodSubtype> subtypesForSearch =
2985 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002986 ? InputMethodUtils.getSubtypes(imi)
2987 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002988 // 4. Search by the current subtype's locale from all subtypes.
2989 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002990 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002991 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002992 }
2993 // 5. Search by the system locale from all subtypes.
2994 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002995 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002996 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002997 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002998 }
satokcd7cd292010-11-20 15:46:23 +09002999 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003000 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003001 // The current input method is the most applicable IME.
3002 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003003 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003004 break;
satok7599a7f2010-12-22 13:45:23 +09003005 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003006 // The system input method is 2nd applicable IME.
3007 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003008 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003009 if ((imi.getServiceInfo().applicationInfo.flags
3010 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3011 foundInSystemIME = true;
3012 }
satok4e4569d2010-11-19 18:45:53 +09003013 }
3014 }
3015 }
3016 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003017 if (mostApplicableIMI != null) {
3018 Slog.w(TAG, "Most applicable shortcut input method was:"
3019 + mostApplicableIMI.getId());
3020 if (mostApplicableSubtype != null) {
3021 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3022 + "," + mostApplicableSubtype.getMode() + ","
3023 + mostApplicableSubtype.getLocale());
3024 }
3025 }
satok4e4569d2010-11-19 18:45:53 +09003026 }
satokcd7cd292010-11-20 15:46:23 +09003027 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003028 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003029 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003030 } else {
3031 return null;
3032 }
3033 }
3034
satokab751aa2010-09-14 19:17:36 +09003035 /**
3036 * @return Return the current subtype of this input method.
3037 */
satok42c5a162011-05-26 16:46:14 +09003038 @Override
satokab751aa2010-09-14 19:17:36 +09003039 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 // TODO: Make this work even for non-current users?
3041 if (!calledFromValidUser()) {
3042 return null;
3043 }
3044 synchronized (mMethodMap) {
3045 return getCurrentInputMethodSubtypeLocked();
3046 }
3047 }
3048
3049 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003050 if (mCurMethodId == null) {
3051 return null;
3052 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003053 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003054 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3055 if (imi == null || imi.getSubtypeCount() == 0) {
3056 return null;
satok4e4569d2010-11-19 18:45:53 +09003057 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003058 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003059 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3060 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003061 if (subtypeId == NOT_A_SUBTYPE_ID) {
3062 // If there are no selected subtypes, the framework will try to find
3063 // the most applicable subtype from explicitly or implicitly enabled
3064 // subtypes.
3065 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003066 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003067 // If there is only one explicitly or implicitly enabled subtype,
3068 // just returns it.
3069 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3070 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3071 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003072 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003073 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003074 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003076 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003077 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3078 true);
satok4e4569d2010-11-19 18:45:53 +09003079 }
satok3ef8b292010-11-23 06:06:29 +09003080 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003081 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003082 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003083 }
3084 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003085 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003086 }
3087
satokf3db1af2010-11-23 13:34:33 +09003088 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3089 InputMethodSubtype subtype) {
3090 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3091 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3092 } else {
3093 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3094 subtypes.add(subtype);
3095 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3096 }
3097 }
3098
satok4e4569d2010-11-19 18:45:53 +09003099 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003100 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003101 @Override
satok4e4569d2010-11-19 18:45:53 +09003102 public List getShortcutInputMethodsAndSubtypes() {
3103 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003104 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003105 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003106 // If there are no selected shortcut subtypes, the framework will try to find
3107 // the most applicable subtype from all subtypes whose mode is
3108 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003109 Pair<InputMethodInfo, InputMethodSubtype> info =
3110 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003111 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003112 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003113 ret.add(info.first);
3114 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003115 }
satok3da92232011-01-11 22:46:30 +09003116 return ret;
satokf3db1af2010-11-23 13:34:33 +09003117 }
satokf3db1af2010-11-23 13:34:33 +09003118 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3119 ret.add(imi);
3120 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3121 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003122 }
3123 }
satokf3db1af2010-11-23 13:34:33 +09003124 return ret;
satok4e4569d2010-11-19 18:45:53 +09003125 }
3126 }
3127
satok42c5a162011-05-26 16:46:14 +09003128 @Override
satokb66d2872010-11-10 01:04:04 +09003129 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003130 // TODO: Make this work even for non-current users?
3131 if (!calledFromValidUser()) {
3132 return false;
3133 }
satokb66d2872010-11-10 01:04:04 +09003134 synchronized (mMethodMap) {
3135 if (subtype != null && mCurMethodId != null) {
3136 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003137 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003138 if (subtypeId != NOT_A_SUBTYPE_ID) {
3139 setInputMethodLocked(mCurMethodId, subtypeId);
3140 return true;
3141 }
3142 }
3143 return false;
3144 }
3145 }
3146
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003147 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003148 private static class InputMethodFileManager {
3149 private static final String SYSTEM_PATH = "system";
3150 private static final String INPUT_METHOD_PATH = "inputmethod";
3151 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3152 private static final String NODE_SUBTYPES = "subtypes";
3153 private static final String NODE_SUBTYPE = "subtype";
3154 private static final String NODE_IMI = "imi";
3155 private static final String ATTR_ID = "id";
3156 private static final String ATTR_LABEL = "label";
3157 private static final String ATTR_ICON = "icon";
3158 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3159 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3160 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3161 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3162 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3163 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003164 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003165 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003166 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003167 if (methodMap == null) {
3168 throw new NullPointerException("methodMap is null");
3169 }
3170 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003171 final File systemDir = userId == UserHandle.USER_OWNER
3172 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3173 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003174 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3175 if (!inputMethodDir.mkdirs()) {
3176 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3177 }
3178 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3179 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3180 if (!subtypeFile.exists()) {
3181 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003182 writeAdditionalInputMethodSubtypes(
3183 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003184 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003185 readAdditionalInputMethodSubtypes(
3186 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003187 }
3188 }
3189
3190 private void deleteAllInputMethodSubtypes(String imiId) {
3191 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003192 mAdditionalSubtypesMap.remove(imiId);
3193 writeAdditionalInputMethodSubtypes(
3194 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003195 }
3196 }
3197
3198 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003199 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003200 synchronized (mMethodMap) {
3201 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3202 final int N = additionalSubtypes.length;
3203 for (int i = 0; i < N; ++i) {
3204 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003205 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003206 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003207 } else {
3208 Slog.w(TAG, "Duplicated subtype definition found: "
3209 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003210 }
3211 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003212 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3213 writeAdditionalInputMethodSubtypes(
3214 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003215 }
3216 }
3217
3218 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3219 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003220 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003221 }
3222 }
3223
3224 private static void writeAdditionalInputMethodSubtypes(
3225 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3226 HashMap<String, InputMethodInfo> methodMap) {
3227 // Safety net for the case that this function is called before methodMap is set.
3228 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3229 FileOutputStream fos = null;
3230 try {
3231 fos = subtypesFile.startWrite();
3232 final XmlSerializer out = new FastXmlSerializer();
3233 out.setOutput(fos, "utf-8");
3234 out.startDocument(null, true);
3235 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3236 out.startTag(null, NODE_SUBTYPES);
3237 for (String imiId : allSubtypes.keySet()) {
3238 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3239 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3240 continue;
3241 }
3242 out.startTag(null, NODE_IMI);
3243 out.attribute(null, ATTR_ID, imiId);
3244 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3245 final int N = subtypesList.size();
3246 for (int i = 0; i < N; ++i) {
3247 final InputMethodSubtype subtype = subtypesList.get(i);
3248 out.startTag(null, NODE_SUBTYPE);
3249 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3250 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3251 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3252 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3253 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3254 out.attribute(null, ATTR_IS_AUXILIARY,
3255 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3256 out.endTag(null, NODE_SUBTYPE);
3257 }
3258 out.endTag(null, NODE_IMI);
3259 }
3260 out.endTag(null, NODE_SUBTYPES);
3261 out.endDocument();
3262 subtypesFile.finishWrite(fos);
3263 } catch (java.io.IOException e) {
3264 Slog.w(TAG, "Error writing subtypes", e);
3265 if (fos != null) {
3266 subtypesFile.failWrite(fos);
3267 }
3268 }
3269 }
3270
3271 private static void readAdditionalInputMethodSubtypes(
3272 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3273 if (allSubtypes == null || subtypesFile == null) return;
3274 allSubtypes.clear();
3275 FileInputStream fis = null;
3276 try {
3277 fis = subtypesFile.openRead();
3278 final XmlPullParser parser = Xml.newPullParser();
3279 parser.setInput(fis, null);
3280 int type = parser.getEventType();
3281 // Skip parsing until START_TAG
3282 while ((type = parser.next()) != XmlPullParser.START_TAG
3283 && type != XmlPullParser.END_DOCUMENT) {}
3284 String firstNodeName = parser.getName();
3285 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3286 throw new XmlPullParserException("Xml doesn't start with subtypes");
3287 }
3288 final int depth =parser.getDepth();
3289 String currentImiId = null;
3290 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3291 while (((type = parser.next()) != XmlPullParser.END_TAG
3292 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3293 if (type != XmlPullParser.START_TAG)
3294 continue;
3295 final String nodeName = parser.getName();
3296 if (NODE_IMI.equals(nodeName)) {
3297 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3298 if (TextUtils.isEmpty(currentImiId)) {
3299 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3300 continue;
3301 }
3302 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3303 allSubtypes.put(currentImiId, tempSubtypesArray);
3304 } else if (NODE_SUBTYPE.equals(nodeName)) {
3305 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3306 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3307 continue;
3308 }
3309 final int icon = Integer.valueOf(
3310 parser.getAttributeValue(null, ATTR_ICON));
3311 final int label = Integer.valueOf(
3312 parser.getAttributeValue(null, ATTR_LABEL));
3313 final String imeSubtypeLocale =
3314 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3315 final String imeSubtypeMode =
3316 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3317 final String imeSubtypeExtraValue =
3318 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003319 final boolean isAuxiliary = "1".equals(String.valueOf(
3320 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003321 final InputMethodSubtype subtype =
3322 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3323 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3324 tempSubtypesArray.add(subtype);
3325 }
3326 }
3327 } catch (XmlPullParserException e) {
3328 Slog.w(TAG, "Error reading subtypes: " + e);
3329 return;
3330 } catch (java.io.IOException e) {
3331 Slog.w(TAG, "Error reading subtypes: " + e);
3332 return;
3333 } catch (NumberFormatException e) {
3334 Slog.w(TAG, "Error reading subtypes: " + e);
3335 return;
3336 } finally {
3337 if (fis != null) {
3338 try {
3339 fis.close();
3340 } catch (java.io.IOException e1) {
3341 Slog.w(TAG, "Failed to close.");
3342 }
3343 }
3344 }
3345 }
3346 }
3347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 @Override
3349 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3350 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3351 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3354 + Binder.getCallingPid()
3355 + ", uid=" + Binder.getCallingUid());
3356 return;
3357 }
3358
3359 IInputMethod method;
3360 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 synchronized (mMethodMap) {
3365 p.println("Current Input Method Manager state:");
3366 int N = mMethodList.size();
3367 p.println(" Input Methods:");
3368 for (int i=0; i<N; i++) {
3369 InputMethodInfo info = mMethodList.get(i);
3370 p.println(" InputMethod #" + i + ":");
3371 info.dump(p, " ");
3372 }
3373 p.println(" Clients:");
3374 for (ClientState ci : mClients.values()) {
3375 p.println(" Client " + ci + ":");
3376 p.println(" client=" + ci.client);
3377 p.println(" inputContext=" + ci.inputContext);
3378 p.println(" sessionRequested=" + ci.sessionRequested);
3379 p.println(" curSession=" + ci.curSession);
3380 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003381 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003383 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3384 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3386 + " mBoundToMethod=" + mBoundToMethod);
3387 p.println(" mCurToken=" + mCurToken);
3388 p.println(" mCurIntent=" + mCurIntent);
3389 method = mCurMethod;
3390 p.println(" mCurMethod=" + mCurMethod);
3391 p.println(" mEnabledSession=" + mEnabledSession);
3392 p.println(" mShowRequested=" + mShowRequested
3393 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3394 + " mShowForced=" + mShowForced
3395 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003396 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003398
Jeff Brownb88102f2010-09-08 11:49:43 -07003399 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 pw.flush();
3402 try {
3403 client.client.asBinder().dump(fd, args);
3404 } catch (RemoteException e) {
3405 p.println("Input method client dead: " + e);
3406 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003407 } else {
3408 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003410
Jeff Brownb88102f2010-09-08 11:49:43 -07003411 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 pw.flush();
3414 try {
3415 method.asBinder().dump(fd, args);
3416 } catch (RemoteException e) {
3417 p.println("Input method service dead: " + e);
3418 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003419 } else {
3420 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 }
3422 }
3423}