blob: dd081a1aeb39a634a58d18577a7d9185482cbe57 [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 Kataoka8e303cc2013-01-11 15:55:28 +090019import com.android.internal.inputmethod.InputMethodUtils;
20import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070022import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090023import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.view.IInputContext;
25import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070026import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputMethodClient;
28import com.android.internal.view.IInputMethodManager;
29import com.android.internal.view.IInputMethodSession;
30import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080031import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090032import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090042import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090043import android.app.Notification;
44import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070045import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090046import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.ComponentName;
48import android.content.ContentResolver;
49import android.content.Context;
50import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.DialogInterface.OnCancelListener;
52import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090053import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070055import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090056import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.PackageManager;
58import android.content.pm.ResolveInfo;
59import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070060import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Resources;
62import android.content.res.TypedArray;
63import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080064import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090066import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Handler;
68import android.os.IBinder;
69import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090070import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Parcel;
74import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080075import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.ServiceManager;
77import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.provider.Settings;
80import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090081import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070082import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090084import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090085import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.PrintWriterPrinter;
87import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090088import android.util.Slog;
89import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090091import android.view.LayoutInflater;
92import android.view.View;
93import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090095import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.inputmethod.InputBinding;
97import android.view.inputmethod.InputMethod;
98import android.view.inputmethod.InputMethodInfo;
99import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900101import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900102import android.widget.CompoundButton;
103import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900104import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900105import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
satoke7c6998e2011-06-03 17:57:59 +0900108import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900110import java.io.FileInputStream;
111import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.IOException;
113import java.io.PrintWriter;
114import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900115import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900116import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900118import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900120import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900121import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123/**
124 * This class provides a system service that manages input methods.
125 */
126public class InputMethodManagerService extends IInputMethodManager.Stub
127 implements ServiceConnection, Handler.Callback {
128 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700129 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
131 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900132 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900133 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900134 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final int MSG_UNBIND_INPUT = 1000;
137 static final int MSG_BIND_INPUT = 1010;
138 static final int MSG_SHOW_SOFT_INPUT = 1020;
139 static final int MSG_HIDE_SOFT_INPUT = 1030;
140 static final int MSG_ATTACH_TOKEN = 1040;
141 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final int MSG_START_INPUT = 2000;
144 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int MSG_UNBIND_METHOD = 3000;
147 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700148 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
satok01038492012-04-09 21:08:27 +0900150 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
satokf9f01002011-05-19 21:31:50 +0900154 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
155
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900156 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900157 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700167 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900168 private InputMethodFileManager mFileManager;
169 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900170 private final HardKeyboardListener mHardKeyboardListener;
171 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 // All known input methods. mMethodMap also serves as the global
176 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900177 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
178 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900179 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
180 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700182 // Used to bring IME service up to visible adjustment while it is being shown.
183 final ServiceConnection mVisibleConnection = new ServiceConnection() {
184 @Override public void onServiceConnected(ComponentName name, IBinder service) {
185 }
186
187 @Override public void onServiceDisconnected(ComponentName name) {
188 }
189 };
190 boolean mVisibleBound = false;
191
satok7cfc0ed2011-06-20 21:29:36 +0900192 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700193 private NotificationManager mNotificationManager;
194 private KeyguardManager mKeyguardManager;
195 private StatusBarManagerService mStatusBar;
196 private Notification mImeSwitcherNotification;
197 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900198 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900199 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900200 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 class SessionState {
203 final ClientState client;
204 final IInputMethod method;
205 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public String toString() {
209 return "SessionState{uid " + client.uid + " pid " + client.pid
210 + " method " + Integer.toHexString(
211 System.identityHashCode(method))
212 + " session " + Integer.toHexString(
213 System.identityHashCode(session))
214 + "}";
215 }
216
217 SessionState(ClientState _client, IInputMethod _method,
218 IInputMethodSession _session) {
219 client = _client;
220 method = _method;
221 session = _session;
222 }
223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 class ClientState {
226 final IInputMethodClient client;
227 final IInputContext inputContext;
228 final int uid;
229 final int pid;
230 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 boolean sessionRequested;
233 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 @Override
236 public String toString() {
237 return "ClientState{" + Integer.toHexString(
238 System.identityHashCode(this)) + " uid " + uid
239 + " pid " + pid + "}";
240 }
241
242 ClientState(IInputMethodClient _client, IInputContext _inputContext,
243 int _uid, int _pid) {
244 client = _client;
245 inputContext = _inputContext;
246 uid = _uid;
247 pid = _pid;
248 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
249 }
250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final HashMap<IBinder, ClientState> mClients
253 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700256 * Set once the system is ready to run third party code.
257 */
258 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * Id of the currently selected input method.
262 */
263 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * The current binding sequence number, incremented every time there is
267 * a new bind performed.
268 */
269 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The client that is currently bound to an input method.
273 */
274 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700277 * The last window token that gained focus.
278 */
279 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 * The input context last provided by the current client.
283 */
284 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * The attributes last provided by the current client.
288 */
289 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * The input method ID of the input method service that we are currently
293 * connected to or in the process of connecting to.
294 */
295 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
satokab751aa2010-09-14 19:17:36 +0900298 * The current subtype of the current input method.
299 */
300 private InputMethodSubtype mCurrentSubtype;
301
satok4e4569d2010-11-19 18:45:53 +0900302 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900303 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
304 mShortcutInputMethodsAndSubtypes =
305 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900306
307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 * Set to true if our ServiceConnection is currently actively bound to
309 * a service (whether or not we have gotten its IBinder back yet).
310 */
311 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * Set if the client has asked for the input method to be shown.
315 */
316 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * Set if we were explicitly told to show the input method.
320 */
321 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * Set if we were forced to be shown.
325 */
326 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * Set if we last told the input method to show itself.
330 */
331 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * The Intent used to connect to the current input method.
335 */
336 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * The token we have made for the currently active input method, to
340 * identify it in the future.
341 */
342 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * If non-null, this is the input method service we are currently connected
346 * to.
347 */
348 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * Time that we last initiated a bind to the input method, to determine
352 * if we should try to disconnect and reconnect to it.
353 */
354 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Have we called mCurMethod.bindInput()?
358 */
359 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * Currently enabled session. Only touched by service thread, not
363 * protected by a lock.
364 */
365 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * True if the screen is on. The value is true initially.
369 */
370 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
Joe Onorato857fd9b2011-01-27 15:08:35 -0800372 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
373 int mImeWindowVis;
374
Ken Wakasa05dbb652011-08-22 15:22:43 +0900375 private AlertDialog.Builder mDialogBuilder;
376 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900377 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900378 private InputMethodInfo[] mIms;
379 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900380 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900381 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
382 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500383 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800386 String mLastEnabled = "";
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 SettingsObserver(Handler handler) {
389 super(handler);
390 ContentResolver resolver = mContext.getContentResolver();
391 resolver.registerContentObserver(Settings.Secure.getUriFor(
392 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900393 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900394 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
395 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900396 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 @Override public void onChange(boolean selfChange) {
400 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800401 boolean enabledChanged = false;
402 String newEnabled = mSettings.getEnabledInputMethodsStr();
403 if (!mLastEnabled.equals(newEnabled)) {
404 mLastEnabled = newEnabled;
405 enabledChanged = true;
406 }
407 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900412 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
413 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700415 if (mCurClient != null && mCurClient.client != null) {
416 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
417 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
419 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900420
421 @Override
422 public void onReceive(Context context, Intent intent) {
423 final String action = intent.getAction();
424 if (Intent.ACTION_SCREEN_ON.equals(action)) {
425 mScreenOn = true;
426 refreshImeWindowVisibilityLocked();
427 updateActive();
428 return;
429 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
430 mScreenOn = false;
431 setImeWindowVisibilityStatusHiddenLocked();
432 updateActive();
433 return;
434 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
435 hideInputMethodMenu();
436 // No need to updateActive
437 return;
438 } else {
439 Slog.w(TAG, "Unexpected intent " + intent);
440 }
441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900445 private boolean isChangingPackagesOfCurrentUser() {
446 final int userId = getChangingUserId();
447 final boolean retval = userId == mSettings.getCurrentUserId();
448 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900449 if (!retval) {
450 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
451 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900452 }
453 return retval;
454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 if (!isChangingPackagesOfCurrentUser()) {
459 return false;
460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900462 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 final int N = mMethodList.size();
464 if (curInputMethodId != null) {
465 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800466 InputMethodInfo imi = mMethodList.get(i);
467 if (imi.getId().equals(curInputMethodId)) {
468 for (String pkg : packages) {
469 if (imi.getPackageName().equals(pkg)) {
470 if (!doit) {
471 return true;
472 }
satok723a27e2010-11-11 14:58:11 +0900473 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 chooseNewDefaultIMELocked();
475 return true;
476 }
477 }
478 }
479 }
480 }
481 }
482 return false;
483 }
484
485 @Override
486 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 if (!isChangingPackagesOfCurrentUser()) {
488 return;
489 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 synchronized (mMethodMap) {
491 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900492 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800493 final int N = mMethodList.size();
494 if (curInputMethodId != null) {
495 for (int i=0; i<N; i++) {
496 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900497 final String imiId = imi.getId();
498 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800499 curIm = imi;
500 }
satoke7c6998e2011-06-03 17:57:59 +0900501
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900503 if (isPackageModified(imi.getPackageName())) {
504 mFileManager.deleteAllInputMethodSubtypes(imiId);
505 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 if (change == PACKAGE_TEMPORARY_CHANGE
507 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800509 + imi.getComponent());
510 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900515 buildInputMethodListLocked(
516 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800519
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800520 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 int change = isPackageDisappearing(curIm.getPackageName());
522 if (change == PACKAGE_TEMPORARY_CHANGE
523 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 ServiceInfo si = null;
525 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900526 si = mIPackageManager.getServiceInfo(
527 curIm.getComponent(), 0, mSettings.getCurrentUserId());
528 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800529 }
530 if (si == null) {
531 // Uh oh, current input method is no longer around!
532 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800533 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900534 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800535 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 changed = true;
537 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900539 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800540 }
541 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800542 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800543 }
satokab751aa2010-09-14 19:17:36 +0900544
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545 if (curIm == null) {
546 // We currently don't have a default input method... is
547 // one now available?
548 changed = chooseNewDefaultIMELocked();
549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800551 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800552 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554 }
555 }
556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800557
Michael Wright52a53522013-03-14 10:59:38 -0700558 private static class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900559 private final IInputMethod mMethod;
560 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
Jean Chalarde0d32a62011-10-20 20:36:07 +0900562 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900564 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
satoke7c6998e2011-06-03 17:57:59 +0900567 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900569 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
satok01038492012-04-09 21:08:27 +0900573 private class HardKeyboardListener
574 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
575 @Override
576 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
577 mHandler.sendMessage(mHandler.obtainMessage(
578 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
579 }
580
581 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
582 if (DEBUG) {
583 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
584 + enabled);
585 }
586 synchronized(mMethodMap) {
587 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
588 && mSwitchingDialog.isShowing()) {
589 mSwitchingDialogTitleView.findViewById(
590 com.android.internal.R.id.hard_keyboard_section).setVisibility(
591 available ? View.VISIBLE : View.GONE);
592 }
593 }
594 }
595 }
596
597 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900598 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800600 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 mHandler = new Handler(this);
602 mIWindowManager = IWindowManager.Stub.asInterface(
603 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800604 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public void executeMessage(Message msg) {
607 handleMessage(msg);
608 }
Mita Yuned218c72012-12-06 17:18:25 -0800609 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900610 mWindowManagerService = windowManager;
611 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700612 mHasFeature = context.getPackageManager().hasSystemFeature(
613 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900614
satok7cfc0ed2011-06-20 21:29:36 +0900615 mImeSwitcherNotification = new Notification();
616 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
617 mImeSwitcherNotification.when = 0;
618 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
619 mImeSwitcherNotification.tickerText = null;
620 mImeSwitcherNotification.defaults = 0; // please be quiet
621 mImeSwitcherNotification.sound = null;
622 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400623
624 // Tag this notification specially so SystemUI knows it's important
625 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
626
satok7cfc0ed2011-06-20 21:29:36 +0900627 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900628 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900629
630 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900631
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900632 final IntentFilter broadcastFilter = new IntentFilter();
633 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
634 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
635 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
636 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
satok7cfc0ed2011-06-20 21:29:36 +0900638 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 int userId = 0;
640 try {
641 ActivityManagerNative.getDefault().registerUserSwitchObserver(
642 new IUserSwitchObserver.Stub() {
643 @Override
644 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900645 synchronized(mMethodMap) {
646 switchUserLocked(newUserId);
647 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900648 if (reply != null) {
649 try {
650 reply.sendResult(null);
651 } catch (RemoteException e) {
652 }
653 }
654 }
655
656 @Override
657 public void onUserSwitchComplete(int newUserId) throws RemoteException {
658 }
659 });
660 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
661 } catch (RemoteException e) {
662 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
663 }
satok81f8b7c2012-12-04 20:42:56 +0900664 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900665
satokd87c2592010-09-29 11:52:06 +0900666 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900667 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900668 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900669 mFileManager = new InputMethodFileManager(mMethodMap, userId);
670 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900671
672 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900673 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900674 if (DEBUG) {
675 Slog.d(TAG, "Initial default ime = " + defaultImiId);
676 }
satok0a1bcf42012-05-16 19:26:31 +0900677 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
678
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900679 buildInputMethodListLocked(mMethodList, mMethodMap,
680 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900681 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682
satok0a1bcf42012-05-16 19:26:31 +0900683 if (!mImeSelectedOnBoot) {
684 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900685 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800689 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900690
691 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
692 // according to the new system locale.
693 final IntentFilter filter = new IntentFilter();
694 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
695 mContext.registerReceiver(
696 new BroadcastReceiver() {
697 @Override
698 public void onReceive(Context context, Intent intent) {
699 synchronized(mMethodMap) {
700 checkCurrentLocaleChangedLocked();
701 }
702 }
703 }, filter);
704 }
705
satok5b927c432012-05-01 20:09:34 +0900706 private void resetDefaultImeLocked(Context context) {
707 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900708 if (mCurMethodId != null
709 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900710 return;
711 }
712
713 InputMethodInfo defIm = null;
714 for (InputMethodInfo imi : mMethodList) {
715 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900716 if (InputMethodUtils.isValidSystemDefaultIme(
717 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900718 defIm = imi;
719 Slog.i(TAG, "Selected default: " + imi.getId());
720 }
721 }
722 }
723 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900724 defIm = InputMethodUtils.getMostApplicableDefaultIME(
725 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900726 Slog.i(TAG, "No default found, using " + defIm.getId());
727 }
728 if (defIm != null) {
729 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
730 }
731 }
732
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900733 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
734 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900735 if (!mSystemReady) {
736 // not system ready
737 return;
738 }
739 final Locale newLocale = mRes.getConfiguration().locale;
740 if (!updateOnlyWhenLocaleChanged
741 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
742 if (!updateOnlyWhenLocaleChanged) {
743 hideCurrentInputLocked(0, null);
744 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700745 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900746 }
747 if (DEBUG) {
748 Slog.i(TAG, "Locale has been changed to " + newLocale);
749 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900750 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
751 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900752 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900753 if (!updateOnlyWhenLocaleChanged) {
754 final String selectedImiId = mSettings.getSelectedInputMethod();
755 if (TextUtils.isEmpty(selectedImiId)) {
756 // This is the first time of the user switch and
757 // set the current ime to the proper one.
758 resetDefaultImeLocked(mContext);
759 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900760 } else {
761 // If the locale is changed, needs to reset the default ime
762 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900763 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800764 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900765 mLastSystemLocale = newLocale;
766 if (!updateOnlyWhenLocaleChanged) {
767 try {
768 startInputInnerLocked();
769 } catch (RuntimeException e) {
770 Slog.w(TAG, "Unexpected exception", e);
771 }
772 }
773 }
774 }
775
776 private void checkCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900777 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
778 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 }
780
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900781 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900783 // InputMethodFileManager should be reset when the user is changed
784 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900785 final String defaultImiId = mSettings.getSelectedInputMethod();
786 final boolean needsToResetDefaultIme = TextUtils.isEmpty(defaultImiId);
787 if (DEBUG) {
788 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
789 }
790 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
791 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900792 }
793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 @Override
795 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
796 throws RemoteException {
797 try {
798 return super.onTransact(code, data, reply, flags);
799 } catch (RuntimeException e) {
800 // The input method manager only throws security exceptions, so let's
801 // log all others.
802 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800803 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 throw e;
806 }
807 }
808
Dianne Hackborn661cd522011-08-22 00:26:20 -0700809 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700810 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 if (DEBUG) {
812 Slog.d(TAG, "--- systemReady");
813 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700814 if (!mSystemReady) {
815 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 mKeyguardManager =
817 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700818 mNotificationManager = (NotificationManager)
819 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
820 mStatusBar = statusBar;
821 statusBar.setIconVisibility("ime", false);
822 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900823 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
824 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900825 if (mShowOngoingImeSwitcherForPhones) {
826 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
827 mHardKeyboardListener);
828 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900829 buildInputMethodListLocked(mMethodList, mMethodMap,
830 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900831 if (!mImeSelectedOnBoot) {
832 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
833 checkCurrentLocaleChangedLocked();
834 }
835 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 try {
837 startInputInnerLocked();
838 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700840 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700841 }
842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800844
satok15452a42011-10-28 17:58:28 +0900845 private void setImeWindowVisibilityStatusHiddenLocked() {
846 mImeWindowVis = 0;
847 updateImeWindowStatusLocked();
848 }
849
satok3afd6c02011-11-18 08:38:19 +0900850 private void refreshImeWindowVisibilityLocked() {
851 final Configuration conf = mRes.getConfiguration();
852 final boolean haveHardKeyboard = conf.keyboard
853 != Configuration.KEYBOARD_NOKEYS;
854 final boolean hardKeyShown = haveHardKeyboard
855 && conf.hardKeyboardHidden
856 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500857 final boolean isScreenLocked =
858 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
859 final boolean isScreenSecurelyLocked =
860 isScreenLocked && mKeyguardManager.isKeyguardSecure();
861 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
862 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900863 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
864 updateImeWindowStatusLocked();
865 }
866
satok15452a42011-10-28 17:58:28 +0900867 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900868 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700869 }
870
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900871 // ---------------------------------------------------------------------------------------
872 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
873 // 1) it comes from the system process
874 // 2) the calling process' user id is identical to the current user id IMMS thinks.
875 private boolean calledFromValidUser() {
876 final int uid = Binder.getCallingUid();
877 final int userId = UserHandle.getUserId(uid);
878 if (DEBUG) {
879 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
880 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
881 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900882 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900883 }
884 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
885 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900886 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900887
888 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
889 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
890 // must not manage background users' states in any functions.
891 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
892 // by a token.
893 if (mContext.checkCallingOrSelfPermission(
894 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
895 == PackageManager.PERMISSION_GRANTED) {
896 if (DEBUG) {
897 Slog.d(TAG, "--- Access granted because the calling process has "
898 + "the INTERACT_ACROSS_USERS_FULL permission");
899 }
900 return true;
901 }
902 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
903 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900904 }
905
906 private boolean bindCurrentInputMethodService(
907 Intent service, ServiceConnection conn, int flags) {
908 if (service == null || conn == null) {
909 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
910 return false;
911 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800912 return mContext.bindServiceAsUser(service, conn, flags,
913 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900914 }
915
satoke7c6998e2011-06-03 17:57:59 +0900916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900918 // TODO: Make this work even for non-current users?
919 if (!calledFromValidUser()) {
920 return Collections.emptyList();
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 synchronized (mMethodMap) {
923 return new ArrayList<InputMethodInfo>(mMethodList);
924 }
925 }
926
satoke7c6998e2011-06-03 17:57:59 +0900927 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 // TODO: Make this work even for non-current users?
930 if (!calledFromValidUser()) {
931 return Collections.emptyList();
932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900934 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 }
937
satokbb4aa062011-01-19 21:40:27 +0900938 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
939 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
940 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
941 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900943 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900944 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900945 }
946 return enabledInputMethodAndSubtypes;
947 }
948
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900949 /**
950 * @param imi if null, returns enabled subtypes for the current imi
951 * @return enabled subtypes of the specified imi
952 */
satoke7c6998e2011-06-03 17:57:59 +0900953 @Override
satok16331c82010-12-20 23:48:46 +0900954 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
955 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 // TODO: Make this work even for non-current users?
957 if (!calledFromValidUser()) {
958 return Collections.emptyList();
959 }
satok67ddf9c2010-11-17 09:45:54 +0900960 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900961 if (imi == null && mCurMethodId != null) {
962 imi = mMethodMap.get(mCurMethodId);
963 }
964 return mSettings.getEnabledInputMethodSubtypeListLocked(
965 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900966 }
967 }
968
satoke7c6998e2011-06-03 17:57:59 +0900969 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 public void addClient(IInputMethodClient client,
971 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900972 if (!calledFromValidUser()) {
973 return;
974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 synchronized (mMethodMap) {
976 mClients.put(client.asBinder(), new ClientState(client,
977 inputContext, uid, pid));
978 }
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
satoke7c6998e2011-06-03 17:57:59 +0900981 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 if (!calledFromValidUser()) {
984 return;
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 synchronized (mMethodMap) {
987 mClients.remove(client.asBinder());
988 }
989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 void executeOrSendMessage(IInterface target, Message msg) {
992 if (target.asBinder() instanceof Binder) {
993 mCaller.sendMessage(msg);
994 } else {
995 handleMessage(msg);
996 msg.recycle();
997 }
998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800999
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001000 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001002 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 + mCurClient.client.asBinder());
1004 if (mBoundToMethod) {
1005 mBoundToMethod = false;
1006 if (mCurMethod != null) {
1007 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1008 MSG_UNBIND_INPUT, mCurMethod));
1009 }
1010 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001011
1012 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1013 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1015 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1016 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
The Android Open Source Project10592532009-03-18 17:39:46 -07001019 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 private int getImeShowFlags() {
1024 int flags = 0;
1025 if (mShowForced) {
1026 flags |= InputMethod.SHOW_FORCED
1027 | InputMethod.SHOW_EXPLICIT;
1028 } else if (mShowExplicitlyRequested) {
1029 flags |= InputMethod.SHOW_EXPLICIT;
1030 }
1031 return flags;
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 private int getAppShowFlags() {
1035 int flags = 0;
1036 if (mShowForced) {
1037 flags |= InputMethodManager.SHOW_FORCED;
1038 } else if (!mShowExplicitlyRequested) {
1039 flags |= InputMethodManager.SHOW_IMPLICIT;
1040 }
1041 return flags;
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
Dianne Hackborn7663d802012-02-24 13:08:49 -08001044 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (!mBoundToMethod) {
1046 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1047 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1048 mBoundToMethod = true;
1049 }
1050 final SessionState session = mCurClient.curSession;
1051 if (initial) {
1052 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1053 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1054 } else {
1055 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1056 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1057 }
1058 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001059 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001060 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001062 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001066 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 // If no method is currently selected, do nothing.
1068 if (mCurMethodId == null) {
1069 return mNoBinding;
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 ClientState cs = mClients.get(client.asBinder());
1073 if (cs == null) {
1074 throw new IllegalArgumentException("unknown client "
1075 + client.asBinder());
1076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 try {
1079 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1080 // Check with the window manager to make sure this client actually
1081 // has a window with focus. If not, reject. This is thread safe
1082 // because if the focus changes some time before or after, the
1083 // next client receiving focus that has any interest in input will
1084 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001085 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1087 return null;
1088 }
1089 } catch (RemoteException e) {
1090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
Dianne Hackborn7663d802012-02-24 13:08:49 -08001092 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1093 }
1094
1095 InputBindResult startInputUncheckedLocked(ClientState cs,
1096 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1097 // If no method is currently selected, do nothing.
1098 if (mCurMethodId == null) {
1099 return mNoBinding;
1100 }
1101
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001102 if (mCurClient == null) {
1103 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1104 if (DEBUG) {
1105 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1106 }
1107 }
1108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 if (mCurClient != cs) {
1110 // If the client is changing, we need to switch over to the new
1111 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001112 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 + cs.client.asBinder());
1115
1116 // If the screen is on, inform the new client it is active
1117 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001118 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1119 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 // Bump up the sequence for this client and attach it.
1124 mCurSeq++;
1125 if (mCurSeq <= 0) mCurSeq = 1;
1126 mCurClient = cs;
1127 mCurInputContext = inputContext;
1128 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Check if the input method is changing.
1131 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1132 if (cs.curSession != null) {
1133 // Fast case: if we are already connected to the input method,
1134 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001135 return attachNewInputLocked(
1136 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 if (mHaveConnection) {
1139 if (mCurMethod != null) {
1140 if (!cs.sessionRequested) {
1141 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001142 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1144 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001145 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 // Return to client, and we will get back with it when
1148 // we have had a session made for it.
1149 return new InputBindResult(null, mCurId, mCurSeq);
1150 } else if (SystemClock.uptimeMillis()
1151 < (mLastBindTime+TIME_TO_RECONNECT)) {
1152 // In this case we have connected to the service, but
1153 // don't yet have its interface. If it hasn't been too
1154 // long since we did the connection, we'll return to
1155 // the client and wait to get the service interface so
1156 // we can report back. If it has been too long, we want
1157 // to fall through so we can try a disconnect/reconnect
1158 // to see if we can get back in touch with the service.
1159 return new InputBindResult(null, mCurId, mCurSeq);
1160 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1162 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001167 return startInputInnerLocked();
1168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001170 InputBindResult startInputInnerLocked() {
1171 if (mCurMethodId == null) {
1172 return mNoBinding;
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001175 if (!mSystemReady) {
1176 // If the system is not yet ready, we shouldn't be running third
1177 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001178 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1182 if (info == null) {
1183 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001186 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1189 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001190 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1191 com.android.internal.R.string.input_method_binding_label);
1192 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1193 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001194 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001195 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 mLastBindTime = SystemClock.uptimeMillis();
1197 mHaveConnection = true;
1198 mCurId = info.getId();
1199 mCurToken = new Binder();
1200 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001201 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 mIWindowManager.addWindowToken(mCurToken,
1203 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1204 } catch (RemoteException e) {
1205 }
1206 return new InputBindResult(null, mCurId, mCurSeq);
1207 } else {
1208 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001209 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 + mCurIntent);
1211 }
1212 return null;
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
satoke7c6998e2011-06-03 17:57:59 +09001215 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001217 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 if (!calledFromValidUser()) {
1219 return null;
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 synchronized (mMethodMap) {
1222 final long ident = Binder.clearCallingIdentity();
1223 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001224 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 } finally {
1226 Binder.restoreCallingIdentity(ident);
1227 }
1228 }
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
satoke7c6998e2011-06-03 17:57:59 +09001231 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 public void finishInput(IInputMethodClient client) {
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
satoke7c6998e2011-06-03 17:57:59 +09001235 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 public void onServiceConnected(ComponentName name, IBinder service) {
1237 synchronized (mMethodMap) {
1238 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1239 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001240 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001241 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001242 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001243 return;
1244 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001245 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001246 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1247 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001250 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001252 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001253 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255 }
1256 }
1257 }
1258
1259 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1260 synchronized (mMethodMap) {
1261 if (mCurMethod != null && method != null
1262 && mCurMethod.asBinder() == method.asBinder()) {
1263 if (mCurClient != null) {
1264 mCurClient.curSession = new SessionState(mCurClient,
1265 method, session);
1266 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001267 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 if (res.method != null) {
1269 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1270 MSG_BIND_METHOD, mCurClient.client, res));
1271 }
1272 }
1273 }
1274 }
1275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001277 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001278 if (mVisibleBound) {
1279 mContext.unbindService(mVisibleConnection);
1280 mVisibleBound = false;
1281 }
1282
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001283 if (mHaveConnection) {
1284 mContext.unbindService(this);
1285 mHaveConnection = false;
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001288 if (mCurToken != null) {
1289 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001290 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001291 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001292 // The current IME is shown. Hence an IME switch (transition) is happening.
1293 mWindowManagerService.saveLastInputMethodWindowForTransition();
1294 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001295 mIWindowManager.removeWindowToken(mCurToken);
1296 } catch (RemoteException e) {
1297 }
1298 mCurToken = null;
1299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001300
The Android Open Source Project10592532009-03-18 17:39:46 -07001301 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001302 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001304 if (reportToClient && mCurClient != null) {
1305 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1306 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1307 }
1308 }
1309
Devin Taylor0c33ed22010-02-23 13:26:46 -06001310 private void finishSession(SessionState sessionState) {
1311 if (sessionState != null && sessionState.session != null) {
1312 try {
1313 sessionState.session.finishSession();
1314 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001315 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001316 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001317 }
1318 }
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001321 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 if (mCurMethod != null) {
1323 for (ClientState cs : mClients.values()) {
1324 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001325 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 cs.curSession = null;
1327 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001328
1329 finishSession(mEnabledSession);
1330 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 mCurMethod = null;
1332 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001333 if (mStatusBar != null) {
1334 mStatusBar.setIconVisibility("ime", false);
1335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
satoke7c6998e2011-06-03 17:57:59 +09001338 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 public void onServiceDisconnected(ComponentName name) {
1340 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001341 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 + " mCurIntent=" + mCurIntent);
1343 if (mCurMethod != null && mCurIntent != null
1344 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001345 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // We consider this to be a new bind attempt, since the system
1347 // should now try to restart the service for us.
1348 mLastBindTime = SystemClock.uptimeMillis();
1349 mShowRequested = mInputShown;
1350 mInputShown = false;
1351 if (mCurClient != null) {
1352 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1353 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1354 }
1355 }
1356 }
1357 }
1358
satokf9f01002011-05-19 21:31:50 +09001359 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001361 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 long ident = Binder.clearCallingIdentity();
1363 try {
1364 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001365 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 return;
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 synchronized (mMethodMap) {
1370 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001371 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001372 if (mStatusBar != null) {
1373 mStatusBar.setIconVisibility("ime", false);
1374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001376 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001377 CharSequence contentDescription = null;
1378 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001379 // Use PackageManager to load label
1380 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001381 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001382 mIPackageManager.getApplicationInfo(packageName, 0,
1383 mSettings.getCurrentUserId()));
1384 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001385 /* ignore */
1386 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001387 if (mStatusBar != null) {
1388 mStatusBar.setIcon("ime", packageName, iconId, 0,
1389 contentDescription != null
1390 ? contentDescription.toString() : null);
1391 mStatusBar.setIconVisibility("ime", true);
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 }
1395 } finally {
1396 Binder.restoreCallingIdentity(ident);
1397 }
1398 }
1399
satok7cfc0ed2011-06-20 21:29:36 +09001400 private boolean needsToShowImeSwitchOngoingNotification() {
1401 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001402 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001403 synchronized (mMethodMap) {
1404 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1405 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001406 if (N > 2) return true;
1407 if (N < 1) return false;
1408 int nonAuxCount = 0;
1409 int auxCount = 0;
1410 InputMethodSubtype nonAuxSubtype = null;
1411 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001412 for(int i = 0; i < N; ++i) {
1413 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001414 final List<InputMethodSubtype> subtypes =
1415 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001416 final int subtypeCount = subtypes.size();
1417 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001418 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001419 } else {
1420 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001421 final InputMethodSubtype subtype = subtypes.get(j);
1422 if (!subtype.isAuxiliary()) {
1423 ++nonAuxCount;
1424 nonAuxSubtype = subtype;
1425 } else {
1426 ++auxCount;
1427 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001428 }
1429 }
1430 }
satok7cfc0ed2011-06-20 21:29:36 +09001431 }
satokb6359412011-06-28 17:47:41 +09001432 if (nonAuxCount > 1 || auxCount > 1) {
1433 return true;
1434 } else if (nonAuxCount == 1 && auxCount == 1) {
1435 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001436 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1437 || auxSubtype.overridesImplicitlyEnabledSubtype()
1438 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001439 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1440 return false;
1441 }
1442 return true;
1443 }
1444 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001445 }
satok7cfc0ed2011-06-20 21:29:36 +09001446 }
1447
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001448 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001449 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001450 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001451 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001452 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001453 try {
1454 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001455 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001456 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001457 return;
1458 }
1459
1460 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001461 mImeWindowVis = vis;
1462 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001463 if (mStatusBar != null) {
1464 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1465 }
satok7cfc0ed2011-06-20 21:29:36 +09001466 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001467 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1468 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001469 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001470 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001471 final CharSequence title = mRes.getText(
1472 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001473 final CharSequence imiLabel = imi.loadLabel(pm);
1474 final CharSequence summary = mCurrentSubtype != null
1475 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1476 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1477 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001478 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001479 : imiLabel;
1480
satok7cfc0ed2011-06-20 21:29:36 +09001481 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001482 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001483 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001484 if (DEBUG) {
1485 Slog.d(TAG, "--- show notification: label = " + imiLabel
1486 + ", summary = " + summary);
1487 }
1488 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001489 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001490 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001491 mNotificationShown = true;
1492 }
satok7cfc0ed2011-06-20 21:29:36 +09001493 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001494 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001495 if (DEBUG) {
1496 Slog.d(TAG, "--- hide notification");
1497 }
1498 mNotificationManager.cancelAsUser(null,
1499 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001500 mNotificationShown = false;
1501 }
1502 }
satok06487a52010-10-29 11:37:18 +09001503 }
1504 } finally {
1505 Binder.restoreCallingIdentity(ident);
1506 }
1507 }
1508
satoke7c6998e2011-06-03 17:57:59 +09001509 @Override
satokf9f01002011-05-19 21:31:50 +09001510 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001511 if (!calledFromValidUser()) {
1512 return;
1513 }
satokf9f01002011-05-19 21:31:50 +09001514 synchronized (mMethodMap) {
1515 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1516 for (int i = 0; i < spans.length; ++i) {
1517 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001518 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001519 mSecureSuggestionSpans.put(ss, currentImi);
1520 }
1521 }
1522 }
1523 }
1524
satoke7c6998e2011-06-03 17:57:59 +09001525 @Override
satokf9f01002011-05-19 21:31:50 +09001526 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001527 if (!calledFromValidUser()) {
1528 return false;
1529 }
satokf9f01002011-05-19 21:31:50 +09001530 synchronized (mMethodMap) {
1531 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1532 // TODO: Do not send the intent if the process of the targetImi is already dead.
1533 if (targetImi != null) {
1534 final String[] suggestions = span.getSuggestions();
1535 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001536 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001537 final Intent intent = new Intent();
1538 // Ensures that only a class in the original IME package will receive the
1539 // notification.
satok42c5a162011-05-26 16:46:14 +09001540 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001541 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1542 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1543 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1544 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001545 final long ident = Binder.clearCallingIdentity();
1546 try {
1547 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1548 } finally {
1549 Binder.restoreCallingIdentity(ident);
1550 }
satokf9f01002011-05-19 21:31:50 +09001551 return true;
1552 }
1553 }
1554 return false;
1555 }
1556
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001557 void updateFromSettingsLocked(boolean enabledMayChange) {
1558 if (enabledMayChange) {
1559 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1560 for (int i=0; i<enabled.size(); i++) {
1561 // We allow the user to select "disabled until used" apps, so if they
1562 // are enabling one of those here we now need to make it enabled.
1563 InputMethodInfo imm = enabled.get(i);
1564 try {
1565 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1566 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1567 mSettings.getCurrentUserId());
1568 if (ai.enabledSetting
1569 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1570 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1571 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1572 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1573 }
1574 } catch (RemoteException e) {
1575 }
1576 }
1577 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001578 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1579 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1580 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1581 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001582 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001583 // There is no input method selected, try to choose new applicable input method.
1584 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001585 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001586 }
1587 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001589 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001591 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001592 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001593 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
satokf3db1af2010-11-23 13:34:33 +09001595 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001596 } else {
1597 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001598 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001599 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
satokab751aa2010-09-14 19:17:36 +09001603 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 InputMethodInfo info = mMethodMap.get(id);
1605 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001606 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
satokd81e9502012-05-21 12:58:45 +09001609 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001611 final int subtypeCount = info.getSubtypeCount();
1612 if (subtypeCount <= 0) {
1613 return;
satokcd7cd292010-11-20 15:46:23 +09001614 }
satokd81e9502012-05-21 12:58:45 +09001615 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1616 final InputMethodSubtype newSubtype;
1617 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1618 newSubtype = info.getSubtypeAt(subtypeId);
1619 } else {
1620 // If subtype is null, try to find the most applicable one from
1621 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001622 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001623 }
1624 if (newSubtype == null || oldSubtype == null) {
1625 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1626 + ", new subtype = " + newSubtype);
1627 return;
1628 }
1629 if (newSubtype != oldSubtype) {
1630 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1631 if (mCurMethod != null) {
1632 try {
1633 refreshImeWindowVisibilityLocked();
1634 mCurMethod.changeInputMethodSubtype(newSubtype);
1635 } catch (RemoteException e) {
1636 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001637 }
1638 }
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 return;
1641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001642
satokd81e9502012-05-21 12:58:45 +09001643 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 final long ident = Binder.clearCallingIdentity();
1645 try {
satokab751aa2010-09-14 19:17:36 +09001646 // Set a subtype to this input method.
1647 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001648 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1649 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1650 // because mCurMethodId is stored as a history in
1651 // setSelectedInputMethodAndSubtypeLocked().
1652 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653
1654 if (ActivityManagerNative.isSystemReady()) {
1655 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001656 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001658 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001660 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 } finally {
1662 Binder.restoreCallingIdentity(ident);
1663 }
1664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
satok42c5a162011-05-26 16:46:14 +09001666 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001667 public boolean showSoftInput(IInputMethodClient client, int flags,
1668 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001669 if (!calledFromValidUser()) {
1670 return false;
1671 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001672 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 long ident = Binder.clearCallingIdentity();
1674 try {
1675 synchronized (mMethodMap) {
1676 if (mCurClient == null || client == null
1677 || mCurClient.client.asBinder() != client.asBinder()) {
1678 try {
1679 // We need to check if this is the current client with
1680 // focus in the window manager, to allow this call to
1681 // be made before input is started in it.
1682 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001683 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001684 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001687 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001691 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001692 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694 } finally {
1695 Binder.restoreCallingIdentity(ident);
1696 }
1697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001698
The Android Open Source Project4df24232009-03-05 14:34:35 -08001699 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 mShowRequested = true;
1701 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1702 mShowExplicitlyRequested = true;
1703 }
1704 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1705 mShowExplicitlyRequested = true;
1706 mShowForced = true;
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
Dianne Hackborncc278702009-09-02 23:07:23 -07001709 if (!mSystemReady) {
1710 return false;
1711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
The Android Open Source Project4df24232009-03-05 14:34:35 -08001713 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001715 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001716 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1717 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1718 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001720 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001721 bindCurrentInputMethodService(
1722 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001723 mVisibleBound = true;
1724 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001727 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 // The client has asked to have the input method shown, but
1729 // we have been sitting here too long with a connection to the
1730 // service and no interface received, so let's disconnect/connect
1731 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001734 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001736 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001737 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001738 } else {
1739 if (DEBUG) {
1740 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1741 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
The Android Open Source Project4df24232009-03-05 14:34:35 -08001745 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
satok42c5a162011-05-26 16:46:14 +09001748 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001749 public boolean hideSoftInput(IInputMethodClient client, int flags,
1750 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001751 if (!calledFromValidUser()) {
1752 return false;
1753 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001754 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 long ident = Binder.clearCallingIdentity();
1756 try {
1757 synchronized (mMethodMap) {
1758 if (mCurClient == null || client == null
1759 || mCurClient.client.asBinder() != client.asBinder()) {
1760 try {
1761 // We need to check if this is the current client with
1762 // focus in the window manager, to allow this call to
1763 // be made before input is started in it.
1764 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001765 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1766 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001767 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001768 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001771 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001772 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
Joe Onorato8a9b2202010-02-26 18:56:32 -08001776 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779 } finally {
1780 Binder.restoreCallingIdentity(ident);
1781 }
1782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1786 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001787 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 -08001788 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001791 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 -08001792 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001796 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1797 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1798 res = true;
1799 } else {
1800 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001802 if (mHaveConnection && mVisibleBound) {
1803 mContext.unbindService(mVisibleConnection);
1804 mVisibleBound = false;
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 mInputShown = false;
1807 mShowRequested = false;
1808 mShowExplicitlyRequested = false;
1809 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
satok42c5a162011-05-26 16:46:14 +09001813 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001814 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1815 int controlFlags, int softInputMode, int windowFlags,
1816 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001817 // Needs to check the validity before clearing calling identity
1818 final boolean calledFromValidUser = calledFromValidUser();
1819
Dianne Hackborn7663d802012-02-24 13:08:49 -08001820 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 long ident = Binder.clearCallingIdentity();
1822 try {
1823 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001824 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001825 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001827 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828
Dianne Hackborn7663d802012-02-24 13:08:49 -08001829 ClientState cs = mClients.get(client.asBinder());
1830 if (cs == null) {
1831 throw new IllegalArgumentException("unknown client "
1832 + client.asBinder());
1833 }
1834
1835 try {
1836 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1837 // Check with the window manager to make sure this client actually
1838 // has a window with focus. If not, reject. This is thread safe
1839 // because if the focus changes some time before or after, the
1840 // next client receiving focus that has any interest in input will
1841 // be calling through here after that change happens.
1842 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1843 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1844 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001846 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001848
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001849 if (!calledFromValidUser) {
1850 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1851 Slog.w(TAG, "If you want to interect with IME, you need "
1852 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1853 hideCurrentInputLocked(0, null);
1854 return null;
1855 }
1856
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001857 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001858 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001859 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001860 if (attribute != null) {
1861 return startInputUncheckedLocked(cs, inputContext, attribute,
1862 controlFlags);
1863 }
1864 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001865 }
1866 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001867
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001868 // Should we auto-show the IME even if the caller has not
1869 // specified what should be done with it?
1870 // We only do this automatically if the window can resize
1871 // to accommodate the IME (so what the user sees will give
1872 // them good context without input information being obscured
1873 // by the IME) or if running on a large screen where there
1874 // is more room for the target window + IME.
1875 final boolean doAutoShow =
1876 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1877 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1878 || mRes.getConfiguration().isLayoutSizeAtLeast(
1879 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001880 final boolean isTextEditor =
1881 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1882
1883 // We want to start input before showing the IME, but after closing
1884 // it. We want to do this after closing it to help the IME disappear
1885 // more quickly (not get stuck behind it initializing itself for the
1886 // new focused input, even if its window wants to hide the IME).
1887 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1890 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001891 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1893 // There is no focus view, and this window will
1894 // be behind any soft input window, so hide the
1895 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001896 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001897 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001899 } else if (isTextEditor && doAutoShow && (softInputMode &
1900 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 // There is a focus view, and we are navigating forward
1902 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001903 // We only do this automatically if the window can resize
1904 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001905 // them good context without input information being obscured
1906 // by the IME) or if running on a large screen where there
1907 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001908 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001909 if (attribute != null) {
1910 res = startInputUncheckedLocked(cs, inputContext, attribute,
1911 controlFlags);
1912 didStart = true;
1913 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001914 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 }
1916 break;
1917 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1918 // Do nothing.
1919 break;
1920 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1921 if ((softInputMode &
1922 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001924 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 break;
1927 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001928 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001929 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 break;
1931 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1932 if ((softInputMode &
1933 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001934 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001935 if (attribute != null) {
1936 res = startInputUncheckedLocked(cs, inputContext, attribute,
1937 controlFlags);
1938 didStart = true;
1939 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001940 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
1942 break;
1943 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001944 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001945 if (attribute != null) {
1946 res = startInputUncheckedLocked(cs, inputContext, attribute,
1947 controlFlags);
1948 didStart = true;
1949 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001950 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 break;
1952 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001953
1954 if (!didStart && attribute != null) {
1955 res = startInputUncheckedLocked(cs, inputContext, attribute,
1956 controlFlags);
1957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 } finally {
1960 Binder.restoreCallingIdentity(ident);
1961 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001962
1963 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001965
satok42c5a162011-05-26 16:46:14 +09001966 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001968 if (!calledFromValidUser()) {
1969 return;
1970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 synchronized (mMethodMap) {
1972 if (mCurClient == null || client == null
1973 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001974 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001975 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
1977
satok440aab52010-11-25 09:43:11 +09001978 // Always call subtype picker, because subtype picker is a superset of input method
1979 // picker.
satokab751aa2010-09-14 19:17:36 +09001980 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1981 }
1982 }
1983
satok42c5a162011-05-26 16:46:14 +09001984 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001986 if (!calledFromValidUser()) {
1987 return;
1988 }
satok28203512010-11-24 11:06:49 +09001989 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1990 }
1991
satok42c5a162011-05-26 16:46:14 +09001992 @Override
satok28203512010-11-24 11:06:49 +09001993 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001994 if (!calledFromValidUser()) {
1995 return;
1996 }
satok28203512010-11-24 11:06:49 +09001997 synchronized (mMethodMap) {
1998 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001999 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002000 mMethodMap.get(id), subtype.hashCode()));
2001 } else {
2002 setInputMethod(token, id);
2003 }
2004 }
satokab751aa2010-09-14 19:17:36 +09002005 }
2006
satok42c5a162011-05-26 16:46:14 +09002007 @Override
satokb416a712010-11-25 20:42:14 +09002008 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002009 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002010 if (!calledFromValidUser()) {
2011 return;
2012 }
satokb416a712010-11-25 20:42:14 +09002013 synchronized (mMethodMap) {
2014 if (mCurClient == null || client == null
2015 || mCurClient.client.asBinder() != client.asBinder()) {
2016 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2017 }
satok7fee71f2010-12-17 18:54:26 +09002018 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2019 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002020 }
2021 }
2022
satok4fc87d62011-05-20 16:13:43 +09002023 @Override
satok735cf382010-11-11 20:40:09 +09002024 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002025 if (!calledFromValidUser()) {
2026 return false;
2027 }
satok735cf382010-11-11 20:40:09 +09002028 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002029 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002030 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002031 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002032 lastImi = mMethodMap.get(lastIme.first);
2033 } else {
2034 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002035 }
satok4fc87d62011-05-20 16:13:43 +09002036 String targetLastImiId = null;
2037 int subtypeId = NOT_A_SUBTYPE_ID;
2038 if (lastIme != null && lastImi != null) {
2039 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2040 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2041 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2042 : mCurrentSubtype.hashCode();
2043 // If the last IME is the same as the current IME and the last subtype is not
2044 // defined, there is no need to switch to the last IME.
2045 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2046 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002047 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002048 }
2049 }
2050
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002051 if (TextUtils.isEmpty(targetLastImiId)
2052 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002053 // This is a safety net. If the currentSubtype can't be added to the history
2054 // and the framework couldn't find the last ime, we will make the last ime be
2055 // the most applicable enabled keyboard subtype of the system imes.
2056 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2057 if (enabled != null) {
2058 final int N = enabled.size();
2059 final String locale = mCurrentSubtype == null
2060 ? mRes.getConfiguration().locale.toString()
2061 : mCurrentSubtype.getLocale();
2062 for (int i = 0; i < N; ++i) {
2063 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002064 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002065 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002066 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2067 InputMethodUtils.getSubtypes(imi),
2068 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002069 if (keyboardSubtype != null) {
2070 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002071 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002072 imi, keyboardSubtype.hashCode());
2073 if(keyboardSubtype.getLocale().equals(locale)) {
2074 break;
2075 }
2076 }
2077 }
2078 }
2079 }
2080 }
2081
2082 if (!TextUtils.isEmpty(targetLastImiId)) {
2083 if (DEBUG) {
2084 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2085 + ", from: " + mCurMethodId + ", " + subtypeId);
2086 }
2087 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2088 return true;
2089 } else {
2090 return false;
2091 }
satok735cf382010-11-11 20:40:09 +09002092 }
2093 }
2094
satoke7c6998e2011-06-03 17:57:59 +09002095 @Override
satok688bd472012-02-09 20:09:17 +09002096 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002097 if (!calledFromValidUser()) {
2098 return false;
2099 }
satok688bd472012-02-09 20:09:17 +09002100 synchronized (mMethodMap) {
2101 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2102 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2103 if (nextSubtype == null) {
2104 return false;
2105 }
2106 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2107 return true;
2108 }
2109 }
2110
2111 @Override
satok68f1b782011-04-11 14:26:04 +09002112 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002113 if (!calledFromValidUser()) {
2114 return null;
2115 }
satok68f1b782011-04-11 14:26:04 +09002116 synchronized (mMethodMap) {
2117 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2118 // TODO: Handle the case of the last IME with no subtypes
2119 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2120 || TextUtils.isEmpty(lastIme.second)) return null;
2121 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2122 if (lastImi == null) return null;
2123 try {
2124 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002125 final int lastSubtypeId =
2126 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002127 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2128 return null;
2129 }
2130 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002131 } catch (NumberFormatException e) {
2132 return null;
2133 }
2134 }
2135 }
2136
satoke7c6998e2011-06-03 17:57:59 +09002137 @Override
satokee5e77c2011-09-02 18:50:15 +09002138 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002139 if (!calledFromValidUser()) {
2140 return;
2141 }
satok91e88122011-07-18 11:11:42 +09002142 // By this IPC call, only a process which shares the same uid with the IME can add
2143 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002144 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002145 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002146 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002147 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002148 final String[] packageInfos;
2149 try {
2150 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2151 } catch (RemoteException e) {
2152 Slog.e(TAG, "Failed to get package infos");
2153 return;
2154 }
satok91e88122011-07-18 11:11:42 +09002155 if (packageInfos != null) {
2156 final int packageNum = packageInfos.length;
2157 for (int i = 0; i < packageNum; ++i) {
2158 if (packageInfos[i].equals(imi.getPackageName())) {
2159 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002160 final long ident = Binder.clearCallingIdentity();
2161 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002162 buildInputMethodListLocked(mMethodList, mMethodMap,
2163 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002164 } finally {
2165 Binder.restoreCallingIdentity(ident);
2166 }
satokee5e77c2011-09-02 18:50:15 +09002167 return;
satok91e88122011-07-18 11:11:42 +09002168 }
2169 }
2170 }
satoke7c6998e2011-06-03 17:57:59 +09002171 }
satokee5e77c2011-09-02 18:50:15 +09002172 return;
satoke7c6998e2011-06-03 17:57:59 +09002173 }
2174
satok28203512010-11-24 11:06:49 +09002175 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 synchronized (mMethodMap) {
2177 if (token == null) {
2178 if (mContext.checkCallingOrSelfPermission(
2179 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2180 != PackageManager.PERMISSION_GRANTED) {
2181 throw new SecurityException(
2182 "Using null token requires permission "
2183 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2184 }
2185 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002186 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2187 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 return;
2189 }
2190
satokc5933802011-08-31 21:26:04 +09002191 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 try {
satokab751aa2010-09-14 19:17:36 +09002193 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 } finally {
2195 Binder.restoreCallingIdentity(ident);
2196 }
2197 }
2198 }
2199
satok42c5a162011-05-26 16:46:14 +09002200 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002202 if (!calledFromValidUser()) {
2203 return;
2204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 synchronized (mMethodMap) {
2206 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002207 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2208 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 return;
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 long ident = Binder.clearCallingIdentity();
2212 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 hideCurrentInputLocked(flags, null);
2214 } finally {
2215 Binder.restoreCallingIdentity(ident);
2216 }
2217 }
2218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
satok42c5a162011-05-26 16:46:14 +09002220 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002221 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002222 if (!calledFromValidUser()) {
2223 return;
2224 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002225 synchronized (mMethodMap) {
2226 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002227 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2228 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002229 return;
2230 }
2231 long ident = Binder.clearCallingIdentity();
2232 try {
2233 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 } finally {
2235 Binder.restoreCallingIdentity(ident);
2236 }
2237 }
2238 }
2239
2240 void setEnabledSessionInMainThread(SessionState session) {
2241 if (mEnabledSession != session) {
2242 if (mEnabledSession != null) {
2243 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002244 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 mEnabledSession.method.setSessionEnabled(
2246 mEnabledSession.session, false);
2247 } catch (RemoteException e) {
2248 }
2249 }
2250 mEnabledSession = session;
2251 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 session.method.setSessionEnabled(
2254 session.session, true);
2255 } catch (RemoteException e) {
2256 }
2257 }
2258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002259
satok42c5a162011-05-26 16:46:14 +09002260 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002262 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 switch (msg.what) {
2264 case MSG_SHOW_IM_PICKER:
2265 showInputMethodMenu();
2266 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
satokab751aa2010-09-14 19:17:36 +09002268 case MSG_SHOW_IM_SUBTYPE_PICKER:
2269 showInputMethodSubtypeMenu();
2270 return true;
2271
satok47a44912010-10-06 16:03:58 +09002272 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002273 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002274 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002275 args.recycle();
satok217f5482010-12-15 05:19:19 +09002276 return true;
2277
2278 case MSG_SHOW_IM_CONFIG:
2279 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002280 return true;
2281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 case MSG_UNBIND_INPUT:
2285 try {
2286 ((IInputMethod)msg.obj).unbindInput();
2287 } catch (RemoteException e) {
2288 // There is nothing interesting about the method dying.
2289 }
2290 return true;
2291 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002292 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 try {
2294 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2295 } catch (RemoteException e) {
2296 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002297 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 return true;
2299 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002300 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002302 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002303 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002304 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 } catch (RemoteException e) {
2306 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002307 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 return true;
2309 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002310 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002312 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002313 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002314 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 } catch (RemoteException e) {
2316 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002317 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 return true;
2319 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002320 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002322 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2324 } catch (RemoteException e) {
2325 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002326 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 return true;
2328 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002329 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 try {
2331 ((IInputMethod)args.arg1).createSession(
Michael Wright52a53522013-03-14 10:59:38 -07002332 (IInputSessionCallback)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 } catch (RemoteException e) {
2334 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002340 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 try {
2342 SessionState session = (SessionState)args.arg1;
2343 setEnabledSessionInMainThread(session);
2344 session.method.startInput((IInputContext)args.arg2,
2345 (EditorInfo)args.arg3);
2346 } catch (RemoteException e) {
2347 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002348 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 return true;
2350 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002351 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 try {
2353 SessionState session = (SessionState)args.arg1;
2354 setEnabledSessionInMainThread(session);
2355 session.method.restartInput((IInputContext)args.arg2,
2356 (EditorInfo)args.arg3);
2357 } catch (RemoteException e) {
2358 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002359 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 case MSG_UNBIND_METHOD:
2365 try {
2366 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2367 } catch (RemoteException e) {
2368 // There is nothing interesting about the last client dying.
2369 }
2370 return true;
2371 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002372 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 try {
2374 ((IInputMethodClient)args.arg1).onBindMethod(
2375 (InputBindResult)args.arg2);
2376 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002377 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002379 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002381 case MSG_SET_ACTIVE:
2382 try {
2383 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2384 } catch (RemoteException e) {
2385 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2386 + ((ClientState)msg.obj).pid + " uid "
2387 + ((ClientState)msg.obj).uid);
2388 }
2389 return true;
satok01038492012-04-09 21:08:27 +09002390
2391 // --------------------------------------------------------------
2392 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2393 mHardKeyboardListener.handleHardKeyboardStatusChange(
2394 msg.arg1 == 1, msg.arg2 == 1);
2395 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
2397 return false;
2398 }
2399
satokdc9ddae2011-10-06 12:22:36 +09002400 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002401 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2402 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002403 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002404 if (DEBUG) {
2405 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2406 }
satok723a27e2010-11-11 14:58:11 +09002407 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002408 return true;
2409 }
2410
2411 return false;
2412 }
2413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002415 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002417 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2418 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 list.clear();
2421 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002422
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002423 // Use for queryIntentServicesAsUser
2424 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002425 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002426 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002428 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002430 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2431 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002432
satoke7c6998e2011-06-03 17:57:59 +09002433 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2434 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 for (int i = 0; i < services.size(); ++i) {
2436 ResolveInfo ri = services.get(i);
2437 ServiceInfo si = ri.serviceInfo;
2438 ComponentName compName = new ComponentName(si.packageName, si.name);
2439 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2440 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002441 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 + ": it does not require the permission "
2443 + android.Manifest.permission.BIND_INPUT_METHOD);
2444 continue;
2445 }
2446
Joe Onorato8a9b2202010-02-26 18:56:32 -08002447 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448
2449 try {
satoke7c6998e2011-06-03 17:57:59 +09002450 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002452 final String id = p.getId();
2453 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454
2455 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002456 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002460 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002462 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 }
2464 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002465
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002466 if (resetDefaultEnabledIme) {
2467 final ArrayList<InputMethodInfo> defaultEnabledIme =
2468 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2469 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2470 final InputMethodInfo imi = defaultEnabledIme.get(i);
2471 if (DEBUG) {
2472 Slog.d(TAG, "--- enable ime = " + imi);
2473 }
2474 setInputMethodEnabledLocked(imi.getId(), true);
2475 }
2476 }
2477
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002479 if (!TextUtils.isEmpty(defaultImiId)) {
2480 if (!map.containsKey(defaultImiId)) {
2481 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2482 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002483 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002484 }
2485 } else {
2486 // Double check that the default IME is certainly enabled.
2487 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002488 }
2489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002493
satokab751aa2010-09-14 19:17:36 +09002494 private void showInputMethodMenu() {
2495 showInputMethodMenuInternal(false);
2496 }
2497
2498 private void showInputMethodSubtypeMenu() {
2499 showInputMethodMenuInternal(true);
2500 }
2501
satok217f5482010-12-15 05:19:19 +09002502 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002503 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002504 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002505 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2506 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002507 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002508 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002509 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002510 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002511 }
2512
2513 private void showConfigureInputMethods() {
2514 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2515 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2516 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2517 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002518 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002519 }
2520
satok2c93efc2012-04-02 19:33:47 +09002521 private boolean isScreenLocked() {
2522 return mKeyguardManager != null
2523 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2524 }
satokab751aa2010-09-14 19:17:36 +09002525 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002526 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002529 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002530
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002531 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002532 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002533 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002534
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002535 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002536 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2537 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002538 if (immis == null || immis.size() == 0) {
2539 return;
2540 }
2541
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002542 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543
satok688bd472012-02-09 20:09:17 +09002544 final List<ImeSubtypeListItem> imList =
2545 mImListManager.getSortedInputMethodAndSubtypeList(
2546 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002547
satokc3690562012-01-10 20:14:43 +09002548 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002549 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002550 if (currentSubtype != null) {
2551 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002552 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2553 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002554 }
2555 }
2556
Ken Wakasa761eb372011-03-04 19:06:18 +09002557 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002558 mIms = new InputMethodInfo[N];
2559 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002560 int checkedItem = 0;
2561 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002562 final ImeSubtypeListItem item = imList.get(i);
2563 mIms[i] = item.mImi;
2564 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002565 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002566 int subtypeId = mSubtypeIds[i];
2567 if ((subtypeId == NOT_A_SUBTYPE_ID)
2568 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2569 || (subtypeId == lastInputMethodSubtypeId)) {
2570 checkedItem = i;
2571 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002574 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 com.android.internal.R.styleable.DialogPreference,
2576 com.android.internal.R.attr.alertDialogStyle, 0);
2577 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002578 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002579 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002582 }
2583 })
2584 .setIcon(a.getDrawable(
2585 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2586 a.recycle();
satok01038492012-04-09 21:08:27 +09002587 final LayoutInflater inflater =
2588 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2589 final View tv = inflater.inflate(
2590 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2591 mDialogBuilder.setCustomTitle(tv);
2592
2593 // Setup layout for a toggle switch of the hardware keyboard
2594 mSwitchingDialogTitleView = tv;
2595 mSwitchingDialogTitleView.findViewById(
2596 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2597 mWindowManagerService.isHardKeyboardAvailable() ?
2598 View.VISIBLE : View.GONE);
2599 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2600 com.android.internal.R.id.hard_keyboard_switch));
2601 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2602 hardKeySwitch.setOnCheckedChangeListener(
2603 new OnCheckedChangeListener() {
2604 @Override
2605 public void onCheckedChanged(
2606 CompoundButton buttonView, boolean isChecked) {
2607 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002608 // Ensure that the input method dialog is dismissed when changing
2609 // the hardware keyboard state.
2610 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002611 }
2612 });
satokd87c2592010-09-29 11:52:06 +09002613
Ken Wakasa05dbb652011-08-22 15:22:43 +09002614 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2615 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2616 checkedItem);
2617
2618 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002620 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002621 public void onClick(DialogInterface dialog, int which) {
2622 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002623 if (mIms == null || mIms.length <= which
2624 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 return;
2626 }
2627 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002628 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002629 adapter.mCheckedItem = which;
2630 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002631 hideInputMethodMenu();
2632 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002633 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002634 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002635 subtypeId = NOT_A_SUBTYPE_ID;
2636 }
2637 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002638 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002641 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642
satokbc81b692011-08-26 16:22:22 +09002643 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002644 mDialogBuilder.setPositiveButton(
2645 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002646 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002647 @Override
satok7f35c8c2010-10-07 21:13:11 +09002648 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002649 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002650 }
2651 });
2652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002654 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 mSwitchingDialog.getWindow().setType(
2656 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002657 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2658 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002659 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 mSwitchingDialog.show();
2661 }
2662 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002663
satok93d744d2012-05-09 17:14:08 +09002664 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002665 public final CharSequence mImeName;
2666 public final CharSequence mSubtypeName;
2667 public final InputMethodInfo mImi;
2668 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002669 private final boolean mIsSystemLocale;
2670 private final boolean mIsSystemLanguage;
2671
Ken Wakasa05dbb652011-08-22 15:22:43 +09002672 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002673 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002674 mImeName = imeName;
2675 mSubtypeName = subtypeName;
2676 mImi = imi;
2677 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002678 if (TextUtils.isEmpty(subtypeLocale)) {
2679 mIsSystemLocale = false;
2680 mIsSystemLanguage = false;
2681 } else {
2682 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2683 mIsSystemLanguage = mIsSystemLocale
2684 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2685 }
2686 }
2687
2688 @Override
2689 public int compareTo(ImeSubtypeListItem other) {
2690 if (TextUtils.isEmpty(mImeName)) {
2691 return 1;
2692 }
2693 if (TextUtils.isEmpty(other.mImeName)) {
2694 return -1;
2695 }
2696 if (!TextUtils.equals(mImeName, other.mImeName)) {
2697 return mImeName.toString().compareTo(other.mImeName.toString());
2698 }
2699 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2700 return 0;
2701 }
2702 if (mIsSystemLocale) {
2703 return -1;
2704 }
2705 if (other.mIsSystemLocale) {
2706 return 1;
2707 }
2708 if (mIsSystemLanguage) {
2709 return -1;
2710 }
2711 if (other.mIsSystemLanguage) {
2712 return 1;
2713 }
2714 if (TextUtils.isEmpty(mSubtypeName)) {
2715 return 1;
2716 }
2717 if (TextUtils.isEmpty(other.mSubtypeName)) {
2718 return -1;
2719 }
2720 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002721 }
2722 }
2723
2724 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2725 private final LayoutInflater mInflater;
2726 private final int mTextViewResourceId;
2727 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002728 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002729 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2730 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2731 super(context, textViewResourceId, itemsList);
2732 mTextViewResourceId = textViewResourceId;
2733 mItemsList = itemsList;
2734 mCheckedItem = checkedItem;
2735 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2736 }
2737
2738 @Override
2739 public View getView(int position, View convertView, ViewGroup parent) {
2740 final View view = convertView != null ? convertView
2741 : mInflater.inflate(mTextViewResourceId, null);
2742 if (position < 0 || position >= mItemsList.size()) return view;
2743 final ImeSubtypeListItem item = mItemsList.get(position);
2744 final CharSequence imeName = item.mImeName;
2745 final CharSequence subtypeName = item.mSubtypeName;
2746 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2747 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2748 if (TextUtils.isEmpty(subtypeName)) {
2749 firstTextView.setText(imeName);
2750 secondTextView.setVisibility(View.GONE);
2751 } else {
2752 firstTextView.setText(subtypeName);
2753 secondTextView.setText(imeName);
2754 secondTextView.setVisibility(View.VISIBLE);
2755 }
2756 final RadioButton radioButton =
2757 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2758 radioButton.setChecked(position == mCheckedItem);
2759 return view;
2760 }
2761 }
2762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002764 synchronized (mMethodMap) {
2765 hideInputMethodMenuLocked();
2766 }
2767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002768
The Android Open Source Project10592532009-03-18 17:39:46 -07002769 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002770 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771
The Android Open Source Project10592532009-03-18 17:39:46 -07002772 if (mSwitchingDialog != null) {
2773 mSwitchingDialog.dismiss();
2774 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002776
The Android Open Source Project10592532009-03-18 17:39:46 -07002777 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002778 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002782
satok42c5a162011-05-26 16:46:14 +09002783 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002785 // TODO: Make this work even for non-current users?
2786 if (!calledFromValidUser()) {
2787 return false;
2788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 synchronized (mMethodMap) {
2790 if (mContext.checkCallingOrSelfPermission(
2791 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2792 != PackageManager.PERMISSION_GRANTED) {
2793 throw new SecurityException(
2794 "Requires permission "
2795 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2796 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 long ident = Binder.clearCallingIdentity();
2799 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002800 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 } finally {
2802 Binder.restoreCallingIdentity(ident);
2803 }
2804 }
2805 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002806
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002807 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2808 // Make sure this is a valid input method.
2809 InputMethodInfo imm = mMethodMap.get(id);
2810 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002811 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002812 }
2813
satokd87c2592010-09-29 11:52:06 +09002814 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2815 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002816
satokd87c2592010-09-29 11:52:06 +09002817 if (enabled) {
2818 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2819 if (pair.first.equals(id)) {
2820 // We are enabling this input method, but it is already enabled.
2821 // Nothing to do. The previous state was enabled.
2822 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002823 }
2824 }
satokd87c2592010-09-29 11:52:06 +09002825 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2826 // Previous state was disabled.
2827 return false;
2828 } else {
2829 StringBuilder builder = new StringBuilder();
2830 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2831 builder, enabledInputMethodsList, id)) {
2832 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002833 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002834 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2835 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2836 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002837 }
2838 // Previous state was enabled.
2839 return true;
2840 } else {
2841 // We are disabling the input method but it is already disabled.
2842 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002843 return false;
2844 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002845 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002846 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002847
satok723a27e2010-11-11 14:58:11 +09002848 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2849 boolean setSubtypeOnly) {
2850 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002851 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002852
2853 // Set Subtype here
2854 if (imi == null || subtypeId < 0) {
2855 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002856 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002857 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002858 if (subtypeId < imi.getSubtypeCount()) {
2859 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2860 mSettings.putSelectedSubtype(subtype.hashCode());
2861 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002862 } else {
2863 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002864 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002865 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002866 }
satokab751aa2010-09-14 19:17:36 +09002867 }
satok723a27e2010-11-11 14:58:11 +09002868
satok4c0e7152012-06-20 20:08:44 +09002869 // Workaround.
2870 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2871 // IMEs are not recognized and considered uninstalled.
2872 // Actually, we can't move everything after SystemReady because
2873 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2874 // the default IME here and try cheking the default IME again in systemReady().
2875 // TODO: Do nothing before system ready and implement a separated logic for
2876 // the encryption lock screen.
2877 // TODO: ASEC should be ready before IMMS is instantiated.
2878 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002879 // Set InputMethod here
2880 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2881 }
2882 }
2883
2884 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2885 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2886 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2887 // newDefaultIme is empty when there is no candidate for the selected IME.
2888 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2889 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2890 if (subtypeHashCode != null) {
2891 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002892 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002893 imi, Integer.valueOf(subtypeHashCode));
2894 } catch (NumberFormatException e) {
2895 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2896 }
2897 }
2898 }
2899 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002900 }
2901
satok4e4569d2010-11-19 18:45:53 +09002902 // If there are no selected shortcuts, tries finding the most applicable ones.
2903 private Pair<InputMethodInfo, InputMethodSubtype>
2904 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2905 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2906 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002907 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002908 boolean foundInSystemIME = false;
2909
2910 // Search applicable subtype for each InputMethodInfo
2911 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002912 final String imiId = imi.getId();
2913 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2914 continue;
2915 }
satokcd7cd292010-11-20 15:46:23 +09002916 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002917 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002918 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002919 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002920 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002921 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002922 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002923 }
satokdf31ae62011-01-15 06:19:44 +09002924 // 2. Search by the system locale from enabledSubtypes.
2925 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002926 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002927 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002928 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002929 }
satoka86f5e42011-09-02 17:12:42 +09002930 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002931 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002932 final ArrayList<InputMethodSubtype> subtypesForSearch =
2933 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002934 ? InputMethodUtils.getSubtypes(imi)
2935 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002936 // 4. Search by the current subtype's locale from all subtypes.
2937 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002938 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002939 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002940 }
2941 // 5. Search by the system locale from all subtypes.
2942 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002943 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002944 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002945 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002946 }
satokcd7cd292010-11-20 15:46:23 +09002947 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002948 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002949 // The current input method is the most applicable IME.
2950 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002951 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002952 break;
satok7599a7f2010-12-22 13:45:23 +09002953 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002954 // The system input method is 2nd applicable IME.
2955 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002956 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002957 if ((imi.getServiceInfo().applicationInfo.flags
2958 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2959 foundInSystemIME = true;
2960 }
satok4e4569d2010-11-19 18:45:53 +09002961 }
2962 }
2963 }
2964 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002965 if (mostApplicableIMI != null) {
2966 Slog.w(TAG, "Most applicable shortcut input method was:"
2967 + mostApplicableIMI.getId());
2968 if (mostApplicableSubtype != null) {
2969 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2970 + "," + mostApplicableSubtype.getMode() + ","
2971 + mostApplicableSubtype.getLocale());
2972 }
2973 }
satok4e4569d2010-11-19 18:45:53 +09002974 }
satokcd7cd292010-11-20 15:46:23 +09002975 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002976 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002977 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002978 } else {
2979 return null;
2980 }
2981 }
2982
satokab751aa2010-09-14 19:17:36 +09002983 /**
2984 * @return Return the current subtype of this input method.
2985 */
satok42c5a162011-05-26 16:46:14 +09002986 @Override
satokab751aa2010-09-14 19:17:36 +09002987 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002988 // TODO: Make this work even for non-current users?
2989 if (!calledFromValidUser()) {
2990 return null;
2991 }
2992 synchronized (mMethodMap) {
2993 return getCurrentInputMethodSubtypeLocked();
2994 }
2995 }
2996
2997 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002998 if (mCurMethodId == null) {
2999 return null;
3000 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003001 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003002 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3003 if (imi == null || imi.getSubtypeCount() == 0) {
3004 return null;
satok4e4569d2010-11-19 18:45:53 +09003005 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003006 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003007 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3008 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 if (subtypeId == NOT_A_SUBTYPE_ID) {
3010 // If there are no selected subtypes, the framework will try to find
3011 // the most applicable subtype from explicitly or implicitly enabled
3012 // subtypes.
3013 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003014 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003015 // If there is only one explicitly or implicitly enabled subtype,
3016 // just returns it.
3017 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3018 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3019 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003020 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003021 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003022 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003023 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003024 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003025 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3026 true);
satok4e4569d2010-11-19 18:45:53 +09003027 }
satok3ef8b292010-11-23 06:06:29 +09003028 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003029 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003030 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003031 }
3032 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003033 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003034 }
3035
satokf3db1af2010-11-23 13:34:33 +09003036 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3037 InputMethodSubtype subtype) {
3038 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3039 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3040 } else {
3041 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3042 subtypes.add(subtype);
3043 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3044 }
3045 }
3046
satok4e4569d2010-11-19 18:45:53 +09003047 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003048 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003049 @Override
satok4e4569d2010-11-19 18:45:53 +09003050 public List getShortcutInputMethodsAndSubtypes() {
3051 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003052 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003053 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003054 // If there are no selected shortcut subtypes, the framework will try to find
3055 // the most applicable subtype from all subtypes whose mode is
3056 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003057 Pair<InputMethodInfo, InputMethodSubtype> info =
3058 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003059 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003060 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003061 ret.add(info.first);
3062 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003063 }
satok3da92232011-01-11 22:46:30 +09003064 return ret;
satokf3db1af2010-11-23 13:34:33 +09003065 }
satokf3db1af2010-11-23 13:34:33 +09003066 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3067 ret.add(imi);
3068 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3069 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003070 }
3071 }
satokf3db1af2010-11-23 13:34:33 +09003072 return ret;
satok4e4569d2010-11-19 18:45:53 +09003073 }
3074 }
3075
satok42c5a162011-05-26 16:46:14 +09003076 @Override
satokb66d2872010-11-10 01:04:04 +09003077 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003078 // TODO: Make this work even for non-current users?
3079 if (!calledFromValidUser()) {
3080 return false;
3081 }
satokb66d2872010-11-10 01:04:04 +09003082 synchronized (mMethodMap) {
3083 if (subtype != null && mCurMethodId != null) {
3084 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003085 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003086 if (subtypeId != NOT_A_SUBTYPE_ID) {
3087 setInputMethodLocked(mCurMethodId, subtypeId);
3088 return true;
3089 }
3090 }
3091 return false;
3092 }
3093 }
3094
satok688bd472012-02-09 20:09:17 +09003095 private static class InputMethodAndSubtypeListManager {
3096 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003097 // Used to load label
satok688bd472012-02-09 20:09:17 +09003098 private final PackageManager mPm;
3099 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003100 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003101 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3102 mContext = context;
3103 mPm = context.getPackageManager();
3104 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003105 final Locale locale = context.getResources().getConfiguration().locale;
3106 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003107 }
3108
3109 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3110 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3111 new Comparator<InputMethodInfo>() {
3112 @Override
3113 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3114 if (imi2 == null) return 0;
3115 if (imi1 == null) return 1;
3116 if (mPm == null) {
3117 return imi1.getId().compareTo(imi2.getId());
3118 }
3119 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3120 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3121 return imiId1.toString().compareTo(imiId2.toString());
3122 }
3123 });
3124
3125 public ImeSubtypeListItem getNextInputMethod(
3126 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3127 if (imi == null) {
3128 return null;
3129 }
3130 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3131 if (imList.size() <= 1) {
3132 return null;
3133 }
3134 final int N = imList.size();
3135 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003136 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003137 : NOT_A_SUBTYPE_ID;
3138 for (int i = 0; i < N; ++i) {
3139 final ImeSubtypeListItem isli = imList.get(i);
3140 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3141 if (!onlyCurrentIme) {
3142 return imList.get((i + 1) % N);
3143 }
3144 for (int j = 0; j < N - 1; ++j) {
3145 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3146 if (candidate.mImi.equals(imi)) {
3147 return candidate;
3148 }
3149 }
3150 return null;
3151 }
3152 }
3153 return null;
3154 }
3155
3156 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3157 return getSortedInputMethodAndSubtypeList(true, false, false);
3158 }
3159
3160 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3161 boolean inputShown, boolean isScreenLocked) {
3162 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3163 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3164 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3165 if (immis == null || immis.size() == 0) {
3166 return Collections.emptyList();
3167 }
3168 mSortedImmis.clear();
3169 mSortedImmis.putAll(immis);
3170 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3171 if (imi == null) continue;
3172 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3173 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3174 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3175 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3176 }
satok688bd472012-02-09 20:09:17 +09003177 final CharSequence imeLabel = imi.loadLabel(mPm);
3178 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3179 final int subtypeCount = imi.getSubtypeCount();
3180 if (DEBUG) {
3181 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3182 }
3183 for (int j = 0; j < subtypeCount; ++j) {
3184 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3185 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3186 // We show all enabled IMEs and subtypes when an IME is shown.
3187 if (enabledSubtypeSet.contains(subtypeHashCode)
3188 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3189 final CharSequence subtypeLabel =
3190 subtype.overridesImplicitlyEnabledSubtype() ? null
3191 : subtype.getDisplayName(mContext, imi.getPackageName(),
3192 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003193 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3194 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003195
3196 // Removing this subtype from enabledSubtypeSet because we no longer
3197 // need to add an entry of this subtype to imList to avoid duplicated
3198 // entries.
3199 enabledSubtypeSet.remove(subtypeHashCode);
3200 }
3201 }
3202 } else {
satok93d744d2012-05-09 17:14:08 +09003203 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3204 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003205 }
3206 }
satok93d744d2012-05-09 17:14:08 +09003207 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003208 return imList;
3209 }
3210 }
3211
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003212 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003213 private static class InputMethodFileManager {
3214 private static final String SYSTEM_PATH = "system";
3215 private static final String INPUT_METHOD_PATH = "inputmethod";
3216 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3217 private static final String NODE_SUBTYPES = "subtypes";
3218 private static final String NODE_SUBTYPE = "subtype";
3219 private static final String NODE_IMI = "imi";
3220 private static final String ATTR_ID = "id";
3221 private static final String ATTR_LABEL = "label";
3222 private static final String ATTR_ICON = "icon";
3223 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3224 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3225 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3226 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3227 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3228 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003229 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003230 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003231 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003232 if (methodMap == null) {
3233 throw new NullPointerException("methodMap is null");
3234 }
3235 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003236 final File systemDir = userId == UserHandle.USER_OWNER
3237 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3238 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003239 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3240 if (!inputMethodDir.mkdirs()) {
3241 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3242 }
3243 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3244 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3245 if (!subtypeFile.exists()) {
3246 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003247 writeAdditionalInputMethodSubtypes(
3248 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003249 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003250 readAdditionalInputMethodSubtypes(
3251 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003252 }
3253 }
3254
3255 private void deleteAllInputMethodSubtypes(String imiId) {
3256 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003257 mAdditionalSubtypesMap.remove(imiId);
3258 writeAdditionalInputMethodSubtypes(
3259 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003260 }
3261 }
3262
3263 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003264 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003265 synchronized (mMethodMap) {
3266 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3267 final int N = additionalSubtypes.length;
3268 for (int i = 0; i < N; ++i) {
3269 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003270 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003271 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003272 } else {
3273 Slog.w(TAG, "Duplicated subtype definition found: "
3274 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003275 }
3276 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003277 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3278 writeAdditionalInputMethodSubtypes(
3279 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003280 }
3281 }
3282
3283 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3284 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003285 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003286 }
3287 }
3288
3289 private static void writeAdditionalInputMethodSubtypes(
3290 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3291 HashMap<String, InputMethodInfo> methodMap) {
3292 // Safety net for the case that this function is called before methodMap is set.
3293 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3294 FileOutputStream fos = null;
3295 try {
3296 fos = subtypesFile.startWrite();
3297 final XmlSerializer out = new FastXmlSerializer();
3298 out.setOutput(fos, "utf-8");
3299 out.startDocument(null, true);
3300 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3301 out.startTag(null, NODE_SUBTYPES);
3302 for (String imiId : allSubtypes.keySet()) {
3303 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3304 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3305 continue;
3306 }
3307 out.startTag(null, NODE_IMI);
3308 out.attribute(null, ATTR_ID, imiId);
3309 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3310 final int N = subtypesList.size();
3311 for (int i = 0; i < N; ++i) {
3312 final InputMethodSubtype subtype = subtypesList.get(i);
3313 out.startTag(null, NODE_SUBTYPE);
3314 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3315 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3316 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3317 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3318 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3319 out.attribute(null, ATTR_IS_AUXILIARY,
3320 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3321 out.endTag(null, NODE_SUBTYPE);
3322 }
3323 out.endTag(null, NODE_IMI);
3324 }
3325 out.endTag(null, NODE_SUBTYPES);
3326 out.endDocument();
3327 subtypesFile.finishWrite(fos);
3328 } catch (java.io.IOException e) {
3329 Slog.w(TAG, "Error writing subtypes", e);
3330 if (fos != null) {
3331 subtypesFile.failWrite(fos);
3332 }
3333 }
3334 }
3335
3336 private static void readAdditionalInputMethodSubtypes(
3337 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3338 if (allSubtypes == null || subtypesFile == null) return;
3339 allSubtypes.clear();
3340 FileInputStream fis = null;
3341 try {
3342 fis = subtypesFile.openRead();
3343 final XmlPullParser parser = Xml.newPullParser();
3344 parser.setInput(fis, null);
3345 int type = parser.getEventType();
3346 // Skip parsing until START_TAG
3347 while ((type = parser.next()) != XmlPullParser.START_TAG
3348 && type != XmlPullParser.END_DOCUMENT) {}
3349 String firstNodeName = parser.getName();
3350 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3351 throw new XmlPullParserException("Xml doesn't start with subtypes");
3352 }
3353 final int depth =parser.getDepth();
3354 String currentImiId = null;
3355 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3356 while (((type = parser.next()) != XmlPullParser.END_TAG
3357 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3358 if (type != XmlPullParser.START_TAG)
3359 continue;
3360 final String nodeName = parser.getName();
3361 if (NODE_IMI.equals(nodeName)) {
3362 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3363 if (TextUtils.isEmpty(currentImiId)) {
3364 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3365 continue;
3366 }
3367 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3368 allSubtypes.put(currentImiId, tempSubtypesArray);
3369 } else if (NODE_SUBTYPE.equals(nodeName)) {
3370 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3371 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3372 continue;
3373 }
3374 final int icon = Integer.valueOf(
3375 parser.getAttributeValue(null, ATTR_ICON));
3376 final int label = Integer.valueOf(
3377 parser.getAttributeValue(null, ATTR_LABEL));
3378 final String imeSubtypeLocale =
3379 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3380 final String imeSubtypeMode =
3381 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3382 final String imeSubtypeExtraValue =
3383 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003384 final boolean isAuxiliary = "1".equals(String.valueOf(
3385 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003386 final InputMethodSubtype subtype =
3387 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3388 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3389 tempSubtypesArray.add(subtype);
3390 }
3391 }
3392 } catch (XmlPullParserException e) {
3393 Slog.w(TAG, "Error reading subtypes: " + e);
3394 return;
3395 } catch (java.io.IOException e) {
3396 Slog.w(TAG, "Error reading subtypes: " + e);
3397 return;
3398 } catch (NumberFormatException e) {
3399 Slog.w(TAG, "Error reading subtypes: " + e);
3400 return;
3401 } finally {
3402 if (fis != null) {
3403 try {
3404 fis.close();
3405 } catch (java.io.IOException e1) {
3406 Slog.w(TAG, "Failed to close.");
3407 }
3408 }
3409 }
3410 }
3411 }
3412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003414 // Utilities for debug
3415 private static String getStackTrace() {
3416 final StringBuilder sb = new StringBuilder();
3417 try {
3418 throw new RuntimeException();
3419 } catch (RuntimeException e) {
3420 final StackTraceElement[] frames = e.getStackTrace();
3421 // Start at 1 because the first frame is here and we don't care about it
3422 for (int j = 1; j < frames.length; ++j) {
3423 sb.append(frames[j].toString() + "\n");
3424 }
3425 }
3426 return sb.toString();
3427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 @Override
3430 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3431 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3432 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3435 + Binder.getCallingPid()
3436 + ", uid=" + Binder.getCallingUid());
3437 return;
3438 }
3439
3440 IInputMethod method;
3441 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 synchronized (mMethodMap) {
3446 p.println("Current Input Method Manager state:");
3447 int N = mMethodList.size();
3448 p.println(" Input Methods:");
3449 for (int i=0; i<N; i++) {
3450 InputMethodInfo info = mMethodList.get(i);
3451 p.println(" InputMethod #" + i + ":");
3452 info.dump(p, " ");
3453 }
3454 p.println(" Clients:");
3455 for (ClientState ci : mClients.values()) {
3456 p.println(" Client " + ci + ":");
3457 p.println(" client=" + ci.client);
3458 p.println(" inputContext=" + ci.inputContext);
3459 p.println(" sessionRequested=" + ci.sessionRequested);
3460 p.println(" curSession=" + ci.curSession);
3461 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003462 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003464 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3465 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3467 + " mBoundToMethod=" + mBoundToMethod);
3468 p.println(" mCurToken=" + mCurToken);
3469 p.println(" mCurIntent=" + mCurIntent);
3470 method = mCurMethod;
3471 p.println(" mCurMethod=" + mCurMethod);
3472 p.println(" mEnabledSession=" + mEnabledSession);
3473 p.println(" mShowRequested=" + mShowRequested
3474 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3475 + " mShowForced=" + mShowForced
3476 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003477 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003479
Jeff Brownb88102f2010-09-08 11:49:43 -07003480 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 pw.flush();
3483 try {
3484 client.client.asBinder().dump(fd, args);
3485 } catch (RemoteException e) {
3486 p.println("Input method client dead: " + e);
3487 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003488 } else {
3489 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003491
Jeff Brownb88102f2010-09-08 11:49:43 -07003492 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 pw.flush();
3495 try {
3496 method.asBinder().dump(fd, args);
3497 } catch (RemoteException e) {
3498 p.println("Input method service dead: " + e);
3499 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003500 } else {
3501 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 }
3503 }
3504}