blob: 593b9bf99920b489f4d133a936c42c9e80f15262 [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 Kataoka8e303cc52013-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;
26import com.android.internal.view.IInputMethodCallback;
27import 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 Kataoka8e303cc52013-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 Kataoka8e303cc52013-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;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900167 private InputMethodFileManager mFileManager;
168 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900169 private final HardKeyboardListener mHardKeyboardListener;
170 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // All known input methods. mMethodMap also serves as the global
175 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900176 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
177 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900178 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
179 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700181 // Used to bring IME service up to visible adjustment while it is being shown.
182 final ServiceConnection mVisibleConnection = new ServiceConnection() {
183 @Override public void onServiceConnected(ComponentName name, IBinder service) {
184 }
185
186 @Override public void onServiceDisconnected(ComponentName name) {
187 }
188 };
189 boolean mVisibleBound = false;
190
satok7cfc0ed2011-06-20 21:29:36 +0900191 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700192 private NotificationManager mNotificationManager;
193 private KeyguardManager mKeyguardManager;
194 private StatusBarManagerService mStatusBar;
195 private Notification mImeSwitcherNotification;
196 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900197 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900198 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900199 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 class SessionState {
202 final ClientState client;
203 final IInputMethod method;
204 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "SessionState{uid " + client.uid + " pid " + client.pid
209 + " method " + Integer.toHexString(
210 System.identityHashCode(method))
211 + " session " + Integer.toHexString(
212 System.identityHashCode(session))
213 + "}";
214 }
215
216 SessionState(ClientState _client, IInputMethod _method,
217 IInputMethodSession _session) {
218 client = _client;
219 method = _method;
220 session = _session;
221 }
222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 class ClientState {
225 final IInputMethodClient client;
226 final IInputContext inputContext;
227 final int uid;
228 final int pid;
229 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 boolean sessionRequested;
232 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 @Override
235 public String toString() {
236 return "ClientState{" + Integer.toHexString(
237 System.identityHashCode(this)) + " uid " + uid
238 + " pid " + pid + "}";
239 }
240
241 ClientState(IInputMethodClient _client, IInputContext _inputContext,
242 int _uid, int _pid) {
243 client = _client;
244 inputContext = _inputContext;
245 uid = _uid;
246 pid = _pid;
247 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final HashMap<IBinder, ClientState> mClients
252 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700255 * Set once the system is ready to run third party code.
256 */
257 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Id of the currently selected input method.
261 */
262 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * The current binding sequence number, incremented every time there is
266 * a new bind performed.
267 */
268 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The client that is currently bound to an input method.
272 */
273 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700276 * The last window token that gained focus.
277 */
278 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * The input context last provided by the current client.
282 */
283 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The attributes last provided by the current client.
287 */
288 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The input method ID of the input method service that we are currently
292 * connected to or in the process of connecting to.
293 */
294 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
satokab751aa2010-09-14 19:17:36 +0900297 * The current subtype of the current input method.
298 */
299 private InputMethodSubtype mCurrentSubtype;
300
satok4e4569d2010-11-19 18:45:53 +0900301 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900302 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
303 mShortcutInputMethodsAndSubtypes =
304 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900305
306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * Set to true if our ServiceConnection is currently actively bound to
308 * a service (whether or not we have gotten its IBinder back yet).
309 */
310 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if the client has asked for the input method to be shown.
314 */
315 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if we were explicitly told to show the input method.
319 */
320 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we were forced to be shown.
324 */
325 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we last told the input method to show itself.
329 */
330 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The Intent used to connect to the current input method.
334 */
335 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The token we have made for the currently active input method, to
339 * identify it in the future.
340 */
341 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * If non-null, this is the input method service we are currently connected
345 * to.
346 */
347 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Time that we last initiated a bind to the input method, to determine
351 * if we should try to disconnect and reconnect to it.
352 */
353 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Have we called mCurMethod.bindInput()?
357 */
358 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Currently enabled session. Only touched by service thread, not
362 * protected by a lock.
363 */
364 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * True if the screen is on. The value is true initially.
368 */
369 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Joe Onorato857fd9b2011-01-27 15:08:35 -0800371 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
372 int mImeWindowVis;
373
Ken Wakasa05dbb652011-08-22 15:22:43 +0900374 private AlertDialog.Builder mDialogBuilder;
375 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900376 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900377 private InputMethodInfo[] mIms;
378 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900379 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900380 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
381 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500382 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 class SettingsObserver extends ContentObserver {
385 SettingsObserver(Handler handler) {
386 super(handler);
387 ContentResolver resolver = mContext.getContentResolver();
388 resolver.registerContentObserver(Settings.Secure.getUriFor(
389 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900390 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900391 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
392 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900393 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 @Override public void onChange(boolean selfChange) {
397 synchronized (mMethodMap) {
398 updateFromSettingsLocked();
399 }
400 }
401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900403 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
404 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700406 if (mCurClient != null && mCurClient.client != null) {
407 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
408 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900411
412 @Override
413 public void onReceive(Context context, Intent intent) {
414 final String action = intent.getAction();
415 if (Intent.ACTION_SCREEN_ON.equals(action)) {
416 mScreenOn = true;
417 refreshImeWindowVisibilityLocked();
418 updateActive();
419 return;
420 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
421 mScreenOn = false;
422 setImeWindowVisibilityStatusHiddenLocked();
423 updateActive();
424 return;
425 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
426 hideInputMethodMenu();
427 // No need to updateActive
428 return;
429 } else {
430 Slog.w(TAG, "Unexpected intent " + intent);
431 }
432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800435 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900436 private boolean isChangingPackagesOfCurrentUser() {
437 final int userId = getChangingUserId();
438 final boolean retval = userId == mSettings.getCurrentUserId();
439 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900440 if (!retval) {
441 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
442 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443 }
444 return retval;
445 }
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900449 if (!isChangingPackagesOfCurrentUser()) {
450 return false;
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900453 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 final int N = mMethodList.size();
455 if (curInputMethodId != null) {
456 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 InputMethodInfo imi = mMethodList.get(i);
458 if (imi.getId().equals(curInputMethodId)) {
459 for (String pkg : packages) {
460 if (imi.getPackageName().equals(pkg)) {
461 if (!doit) {
462 return true;
463 }
satok723a27e2010-11-11 14:58:11 +0900464 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 chooseNewDefaultIMELocked();
466 return true;
467 }
468 }
469 }
470 }
471 }
472 }
473 return false;
474 }
475
476 @Override
477 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900478 if (!isChangingPackagesOfCurrentUser()) {
479 return;
480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 synchronized (mMethodMap) {
482 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900483 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 final int N = mMethodList.size();
485 if (curInputMethodId != null) {
486 for (int i=0; i<N; i++) {
487 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900488 final String imiId = imi.getId();
489 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 curIm = imi;
491 }
satoke7c6998e2011-06-03 17:57:59 +0900492
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800493 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900494 if (isPackageModified(imi.getPackageName())) {
495 mFileManager.deleteAllInputMethodSubtypes(imiId);
496 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 if (change == PACKAGE_TEMPORARY_CHANGE
498 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800499 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800500 + imi.getComponent());
501 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 }
504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800505
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 buildInputMethodListLocked(mMethodList, mMethodMap);
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800510 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 int change = isPackageDisappearing(curIm.getPackageName());
512 if (change == PACKAGE_TEMPORARY_CHANGE
513 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800514 ServiceInfo si = null;
515 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900516 si = mIPackageManager.getServiceInfo(
517 curIm.getComponent(), 0, mSettings.getCurrentUserId());
518 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 }
520 if (si == null) {
521 // Uh oh, current input method is no longer around!
522 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900524 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800525 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800526 changed = true;
527 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900529 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 }
531 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800532 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 }
satokab751aa2010-09-14 19:17:36 +0900534
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800535 if (curIm == null) {
536 // We currently don't have a default input method... is
537 // one now available?
538 changed = chooseNewDefaultIMELocked();
539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800540
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800541 if (changed) {
542 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
545 }
546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Jean Chalarde0d32a62011-10-20 20:36:07 +0900548 private static class MethodCallback extends IInputMethodCallback.Stub {
549 private final IInputMethod mMethod;
550 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800551
Jean Chalarde0d32a62011-10-20 20:36:07 +0900552 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900554 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
satoke7c6998e2011-06-03 17:57:59 +0900557 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public void finishedEvent(int seq, boolean handled) throws RemoteException {
559 }
560
satoke7c6998e2011-06-03 17:57:59 +0900561 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900563 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
satok01038492012-04-09 21:08:27 +0900567 private class HardKeyboardListener
568 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
569 @Override
570 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
571 mHandler.sendMessage(mHandler.obtainMessage(
572 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
573 }
574
575 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
576 if (DEBUG) {
577 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
578 + enabled);
579 }
580 synchronized(mMethodMap) {
581 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
582 && mSwitchingDialog.isShowing()) {
583 mSwitchingDialogTitleView.findViewById(
584 com.android.internal.R.id.hard_keyboard_section).setVisibility(
585 available ? View.VISIBLE : View.GONE);
586 }
587 }
588 }
589 }
590
591 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900592 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800594 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 mHandler = new Handler(this);
596 mIWindowManager = IWindowManager.Stub.asInterface(
597 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800598 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900599 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public void executeMessage(Message msg) {
601 handleMessage(msg);
602 }
Mita Yuned218c72012-12-06 17:18:25 -0800603 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900604 mWindowManagerService = windowManager;
605 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900606
satok7cfc0ed2011-06-20 21:29:36 +0900607 mImeSwitcherNotification = new Notification();
608 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
609 mImeSwitcherNotification.when = 0;
610 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
611 mImeSwitcherNotification.tickerText = null;
612 mImeSwitcherNotification.defaults = 0; // please be quiet
613 mImeSwitcherNotification.sound = null;
614 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400615
616 // Tag this notification specially so SystemUI knows it's important
617 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
618
satok7cfc0ed2011-06-20 21:29:36 +0900619 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900620 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900621
622 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900623
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900624 final IntentFilter broadcastFilter = new IntentFilter();
625 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
626 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
627 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
628 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
satok7cfc0ed2011-06-20 21:29:36 +0900630 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900631 int userId = 0;
632 try {
633 ActivityManagerNative.getDefault().registerUserSwitchObserver(
634 new IUserSwitchObserver.Stub() {
635 @Override
636 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900637 synchronized(mMethodMap) {
638 switchUserLocked(newUserId);
639 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 if (reply != null) {
641 try {
642 reply.sendResult(null);
643 } catch (RemoteException e) {
644 }
645 }
646 }
647
648 @Override
649 public void onUserSwitchComplete(int newUserId) throws RemoteException {
650 }
651 });
652 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
653 } catch (RemoteException e) {
654 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
655 }
satok81f8b7c2012-12-04 20:42:56 +0900656 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900657
satokd87c2592010-09-29 11:52:06 +0900658 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900659 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900660 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900661 mFileManager = new InputMethodFileManager(mMethodMap, userId);
662 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900663
664 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900666 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900669 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670
satok0a1bcf42012-05-16 19:26:31 +0900671 if (!mImeSelectedOnBoot) {
672 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900673 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 mSettingsObserver = new SettingsObserver(mHandler);
677 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900678
679 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
680 // according to the new system locale.
681 final IntentFilter filter = new IntentFilter();
682 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
683 mContext.registerReceiver(
684 new BroadcastReceiver() {
685 @Override
686 public void onReceive(Context context, Intent intent) {
687 synchronized(mMethodMap) {
688 checkCurrentLocaleChangedLocked();
689 }
690 }
691 }, filter);
692 }
693
satok5b927c432012-05-01 20:09:34 +0900694 private void resetDefaultImeLocked(Context context) {
695 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900696 if (mCurMethodId != null
697 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900698 return;
699 }
700
701 InputMethodInfo defIm = null;
702 for (InputMethodInfo imi : mMethodList) {
703 if (defIm == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900704 if (InputMethodUtils.isValidSystemDefaultIme(
705 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900706 defIm = imi;
707 Slog.i(TAG, "Selected default: " + imi.getId());
708 }
709 }
710 }
711 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900712 defIm = InputMethodUtils.getMostApplicableDefaultIME(
713 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900714 Slog.i(TAG, "No default found, using " + defIm.getId());
715 }
716 if (defIm != null) {
717 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
718 }
719 }
720
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900721 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
722 if (!mSystemReady) {
723 // not system ready
724 return;
725 }
726 final Locale newLocale = mRes.getConfiguration().locale;
727 if (!updateOnlyWhenLocaleChanged
728 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
729 if (!updateOnlyWhenLocaleChanged) {
730 hideCurrentInputLocked(0, null);
731 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700732 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900733 }
734 if (DEBUG) {
735 Slog.i(TAG, "Locale has been changed to " + newLocale);
736 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900737 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
738 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900739 buildInputMethodListLocked(mMethodList, mMethodMap);
740 if (!updateOnlyWhenLocaleChanged) {
741 final String selectedImiId = mSettings.getSelectedInputMethod();
742 if (TextUtils.isEmpty(selectedImiId)) {
743 // This is the first time of the user switch and
744 // set the current ime to the proper one.
745 resetDefaultImeLocked(mContext);
746 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900747 } else {
748 // If the locale is changed, needs to reset the default ime
749 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900750 }
751 updateFromSettingsLocked();
752 mLastSystemLocale = newLocale;
753 if (!updateOnlyWhenLocaleChanged) {
754 try {
755 startInputInnerLocked();
756 } catch (RuntimeException e) {
757 Slog.w(TAG, "Unexpected exception", e);
758 }
759 }
760 }
761 }
762
763 private void checkCurrentLocaleChangedLocked() {
764 resetAllInternalStateLocked(true);
765 }
766
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900767 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900768 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900769 // InputMethodFileManager should be reset when the user is changed
770 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 resetAllInternalStateLocked(false);
772 }
773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 @Override
775 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
776 throws RemoteException {
777 try {
778 return super.onTransact(code, data, reply, flags);
779 } catch (RuntimeException e) {
780 // The input method manager only throws security exceptions, so let's
781 // log all others.
782 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785 throw e;
786 }
787 }
788
Dianne Hackborn661cd522011-08-22 00:26:20 -0700789 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700790 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900791 if (DEBUG) {
792 Slog.d(TAG, "--- systemReady");
793 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700794 if (!mSystemReady) {
795 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900796 mKeyguardManager =
797 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700798 mNotificationManager = (NotificationManager)
799 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
800 mStatusBar = statusBar;
801 statusBar.setIconVisibility("ime", false);
802 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900803 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
804 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900805 if (mShowOngoingImeSwitcherForPhones) {
806 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
807 mHardKeyboardListener);
808 }
satok0a1bcf42012-05-16 19:26:31 +0900809 buildInputMethodListLocked(mMethodList, mMethodMap);
810 if (!mImeSelectedOnBoot) {
811 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
812 checkCurrentLocaleChangedLocked();
813 }
814 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700815 try {
816 startInputInnerLocked();
817 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700819 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700820 }
821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823
satok15452a42011-10-28 17:58:28 +0900824 private void setImeWindowVisibilityStatusHiddenLocked() {
825 mImeWindowVis = 0;
826 updateImeWindowStatusLocked();
827 }
828
satok3afd6c02011-11-18 08:38:19 +0900829 private void refreshImeWindowVisibilityLocked() {
830 final Configuration conf = mRes.getConfiguration();
831 final boolean haveHardKeyboard = conf.keyboard
832 != Configuration.KEYBOARD_NOKEYS;
833 final boolean hardKeyShown = haveHardKeyboard
834 && conf.hardKeyboardHidden
835 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500836 final boolean isScreenLocked =
837 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
838 final boolean isScreenSecurelyLocked =
839 isScreenLocked && mKeyguardManager.isKeyguardSecure();
840 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
841 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900842 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
843 updateImeWindowStatusLocked();
844 }
845
satok15452a42011-10-28 17:58:28 +0900846 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900847 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700848 }
849
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900850 // ---------------------------------------------------------------------------------------
851 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
852 // 1) it comes from the system process
853 // 2) the calling process' user id is identical to the current user id IMMS thinks.
854 private boolean calledFromValidUser() {
855 final int uid = Binder.getCallingUid();
856 final int userId = UserHandle.getUserId(uid);
857 if (DEBUG) {
858 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
859 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
860 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900861 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900862 }
863 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
864 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900865 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900866
867 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
868 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
869 // must not manage background users' states in any functions.
870 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
871 // by a token.
872 if (mContext.checkCallingOrSelfPermission(
873 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
874 == PackageManager.PERMISSION_GRANTED) {
875 if (DEBUG) {
876 Slog.d(TAG, "--- Access granted because the calling process has "
877 + "the INTERACT_ACROSS_USERS_FULL permission");
878 }
879 return true;
880 }
881 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
882 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900883 }
884
885 private boolean bindCurrentInputMethodService(
886 Intent service, ServiceConnection conn, int flags) {
887 if (service == null || conn == null) {
888 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
889 return false;
890 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800891 return mContext.bindServiceAsUser(service, conn, flags,
892 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900893 }
894
satoke7c6998e2011-06-03 17:57:59 +0900895 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 // TODO: Make this work even for non-current users?
898 if (!calledFromValidUser()) {
899 return Collections.emptyList();
900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 synchronized (mMethodMap) {
902 return new ArrayList<InputMethodInfo>(mMethodList);
903 }
904 }
905
satoke7c6998e2011-06-03 17:57:59 +0900906 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 // TODO: Make this work even for non-current users?
909 if (!calledFromValidUser()) {
910 return Collections.emptyList();
911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900913 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 }
915 }
916
satokbb4aa062011-01-19 21:40:27 +0900917 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
918 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
919 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
920 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900921 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900922 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900923 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900924 }
925 return enabledInputMethodAndSubtypes;
926 }
927
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900928 /**
929 * @param imi if null, returns enabled subtypes for the current imi
930 * @return enabled subtypes of the specified imi
931 */
satoke7c6998e2011-06-03 17:57:59 +0900932 @Override
satok16331c82010-12-20 23:48:46 +0900933 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
934 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 // TODO: Make this work even for non-current users?
936 if (!calledFromValidUser()) {
937 return Collections.emptyList();
938 }
satok67ddf9c2010-11-17 09:45:54 +0900939 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900940 if (imi == null && mCurMethodId != null) {
941 imi = mMethodMap.get(mCurMethodId);
942 }
943 return mSettings.getEnabledInputMethodSubtypeListLocked(
944 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900945 }
946 }
947
satoke7c6998e2011-06-03 17:57:59 +0900948 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 public void addClient(IInputMethodClient client,
950 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900951 if (!calledFromValidUser()) {
952 return;
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 synchronized (mMethodMap) {
955 mClients.put(client.asBinder(), new ClientState(client,
956 inputContext, uid, pid));
957 }
958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800959
satoke7c6998e2011-06-03 17:57:59 +0900960 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 if (!calledFromValidUser()) {
963 return;
964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 synchronized (mMethodMap) {
966 mClients.remove(client.asBinder());
967 }
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 void executeOrSendMessage(IInterface target, Message msg) {
971 if (target.asBinder() instanceof Binder) {
972 mCaller.sendMessage(msg);
973 } else {
974 handleMessage(msg);
975 msg.recycle();
976 }
977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800981 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 + mCurClient.client.asBinder());
983 if (mBoundToMethod) {
984 mBoundToMethod = false;
985 if (mCurMethod != null) {
986 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
987 MSG_UNBIND_INPUT, mCurMethod));
988 }
989 }
Dianne Hackborna6e41342012-05-22 16:30:34 -0700990
991 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
992 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
994 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
995 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
The Android Open Source Project10592532009-03-18 17:39:46 -0700998 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 private int getImeShowFlags() {
1003 int flags = 0;
1004 if (mShowForced) {
1005 flags |= InputMethod.SHOW_FORCED
1006 | InputMethod.SHOW_EXPLICIT;
1007 } else if (mShowExplicitlyRequested) {
1008 flags |= InputMethod.SHOW_EXPLICIT;
1009 }
1010 return flags;
1011 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 private int getAppShowFlags() {
1014 int flags = 0;
1015 if (mShowForced) {
1016 flags |= InputMethodManager.SHOW_FORCED;
1017 } else if (!mShowExplicitlyRequested) {
1018 flags |= InputMethodManager.SHOW_IMPLICIT;
1019 }
1020 return flags;
1021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
Dianne Hackborn7663d802012-02-24 13:08:49 -08001023 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (!mBoundToMethod) {
1025 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1026 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1027 mBoundToMethod = true;
1028 }
1029 final SessionState session = mCurClient.curSession;
1030 if (initial) {
1031 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1032 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1033 } else {
1034 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1035 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1036 }
1037 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001038 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001039 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001041 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001045 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 // If no method is currently selected, do nothing.
1047 if (mCurMethodId == null) {
1048 return mNoBinding;
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 ClientState cs = mClients.get(client.asBinder());
1052 if (cs == null) {
1053 throw new IllegalArgumentException("unknown client "
1054 + client.asBinder());
1055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 try {
1058 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1059 // Check with the window manager to make sure this client actually
1060 // has a window with focus. If not, reject. This is thread safe
1061 // because if the focus changes some time before or after, the
1062 // next client receiving focus that has any interest in input will
1063 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1066 return null;
1067 }
1068 } catch (RemoteException e) {
1069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
Dianne Hackborn7663d802012-02-24 13:08:49 -08001071 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1072 }
1073
1074 InputBindResult startInputUncheckedLocked(ClientState cs,
1075 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1076 // If no method is currently selected, do nothing.
1077 if (mCurMethodId == null) {
1078 return mNoBinding;
1079 }
1080
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001081 if (mCurClient == null) {
1082 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1083 if (DEBUG) {
1084 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1085 }
1086 }
1087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 if (mCurClient != cs) {
1089 // If the client is changing, we need to switch over to the new
1090 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001091 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 + cs.client.asBinder());
1094
1095 // If the screen is on, inform the new client it is active
1096 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001097 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1098 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 // Bump up the sequence for this client and attach it.
1103 mCurSeq++;
1104 if (mCurSeq <= 0) mCurSeq = 1;
1105 mCurClient = cs;
1106 mCurInputContext = inputContext;
1107 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 // Check if the input method is changing.
1110 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1111 if (cs.curSession != null) {
1112 // Fast case: if we are already connected to the input method,
1113 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001114 return attachNewInputLocked(
1115 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117 if (mHaveConnection) {
1118 if (mCurMethod != null) {
1119 if (!cs.sessionRequested) {
1120 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1123 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001124 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126 // Return to client, and we will get back with it when
1127 // we have had a session made for it.
1128 return new InputBindResult(null, mCurId, mCurSeq);
1129 } else if (SystemClock.uptimeMillis()
1130 < (mLastBindTime+TIME_TO_RECONNECT)) {
1131 // In this case we have connected to the service, but
1132 // don't yet have its interface. If it hasn't been too
1133 // long since we did the connection, we'll return to
1134 // the client and wait to get the service interface so
1135 // we can report back. If it has been too long, we want
1136 // to fall through so we can try a disconnect/reconnect
1137 // to see if we can get back in touch with the service.
1138 return new InputBindResult(null, mCurId, mCurSeq);
1139 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1141 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 }
1143 }
1144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001145
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001146 return startInputInnerLocked();
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001149 InputBindResult startInputInnerLocked() {
1150 if (mCurMethodId == null) {
1151 return mNoBinding;
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001154 if (!mSystemReady) {
1155 // If the system is not yet ready, we shouldn't be running third
1156 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001157 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1161 if (info == null) {
1162 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001164
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001165 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1168 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001169 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1170 com.android.internal.R.string.input_method_binding_label);
1171 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1172 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001173 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001174 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 mLastBindTime = SystemClock.uptimeMillis();
1176 mHaveConnection = true;
1177 mCurId = info.getId();
1178 mCurToken = new Binder();
1179 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 mIWindowManager.addWindowToken(mCurToken,
1182 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1183 } catch (RemoteException e) {
1184 }
1185 return new InputBindResult(null, mCurId, mCurSeq);
1186 } else {
1187 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 + mCurIntent);
1190 }
1191 return null;
1192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
satoke7c6998e2011-06-03 17:57:59 +09001194 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001196 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001197 if (!calledFromValidUser()) {
1198 return null;
1199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 synchronized (mMethodMap) {
1201 final long ident = Binder.clearCallingIdentity();
1202 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001203 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } finally {
1205 Binder.restoreCallingIdentity(ident);
1206 }
1207 }
1208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
satoke7c6998e2011-06-03 17:57:59 +09001210 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 public void finishInput(IInputMethodClient client) {
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
satoke7c6998e2011-06-03 17:57:59 +09001214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 public void onServiceConnected(ComponentName name, IBinder service) {
1216 synchronized (mMethodMap) {
1217 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1218 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001219 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001221 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001222 return;
1223 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001224 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001225 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1226 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001229 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001231 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001232 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 }
1235 }
1236 }
1237
1238 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1239 synchronized (mMethodMap) {
1240 if (mCurMethod != null && method != null
1241 && mCurMethod.asBinder() == method.asBinder()) {
1242 if (mCurClient != null) {
1243 mCurClient.curSession = new SessionState(mCurClient,
1244 method, session);
1245 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001246 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 if (res.method != null) {
1248 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1249 MSG_BIND_METHOD, mCurClient.client, res));
1250 }
1251 }
1252 }
1253 }
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001256 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001257 if (mVisibleBound) {
1258 mContext.unbindService(mVisibleConnection);
1259 mVisibleBound = false;
1260 }
1261
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001262 if (mHaveConnection) {
1263 mContext.unbindService(this);
1264 mHaveConnection = false;
1265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001267 if (mCurToken != null) {
1268 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001270 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001271 // The current IME is shown. Hence an IME switch (transition) is happening.
1272 mWindowManagerService.saveLastInputMethodWindowForTransition();
1273 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001274 mIWindowManager.removeWindowToken(mCurToken);
1275 } catch (RemoteException e) {
1276 }
1277 mCurToken = null;
1278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001279
The Android Open Source Project10592532009-03-18 17:39:46 -07001280 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001281 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001283 if (reportToClient && mCurClient != null) {
1284 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1285 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1286 }
1287 }
1288
Devin Taylor0c33ed22010-02-23 13:26:46 -06001289 private void finishSession(SessionState sessionState) {
1290 if (sessionState != null && sessionState.session != null) {
1291 try {
1292 sessionState.session.finishSession();
1293 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001294 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001295 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001296 }
1297 }
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001300 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 if (mCurMethod != null) {
1302 for (ClientState cs : mClients.values()) {
1303 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001304 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 cs.curSession = null;
1306 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001307
1308 finishSession(mEnabledSession);
1309 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 mCurMethod = null;
1311 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001312 if (mStatusBar != null) {
1313 mStatusBar.setIconVisibility("ime", false);
1314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
satoke7c6998e2011-06-03 17:57:59 +09001317 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 public void onServiceDisconnected(ComponentName name) {
1319 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001320 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 + " mCurIntent=" + mCurIntent);
1322 if (mCurMethod != null && mCurIntent != null
1323 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001324 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // We consider this to be a new bind attempt, since the system
1326 // should now try to restart the service for us.
1327 mLastBindTime = SystemClock.uptimeMillis();
1328 mShowRequested = mInputShown;
1329 mInputShown = false;
1330 if (mCurClient != null) {
1331 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1332 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1333 }
1334 }
1335 }
1336 }
1337
satokf9f01002011-05-19 21:31:50 +09001338 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001340 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 long ident = Binder.clearCallingIdentity();
1342 try {
1343 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001344 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 return;
1346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 synchronized (mMethodMap) {
1349 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001350 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001351 if (mStatusBar != null) {
1352 mStatusBar.setIconVisibility("ime", false);
1353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001355 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001356 CharSequence contentDescription = null;
1357 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001358 // Use PackageManager to load label
1359 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001360 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001361 mIPackageManager.getApplicationInfo(packageName, 0,
1362 mSettings.getCurrentUserId()));
1363 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001364 /* ignore */
1365 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001366 if (mStatusBar != null) {
1367 mStatusBar.setIcon("ime", packageName, iconId, 0,
1368 contentDescription != null
1369 ? contentDescription.toString() : null);
1370 mStatusBar.setIconVisibility("ime", true);
1371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373 }
1374 } finally {
1375 Binder.restoreCallingIdentity(ident);
1376 }
1377 }
1378
satok7cfc0ed2011-06-20 21:29:36 +09001379 private boolean needsToShowImeSwitchOngoingNotification() {
1380 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001381 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001382 synchronized (mMethodMap) {
1383 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1384 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001385 if (N > 2) return true;
1386 if (N < 1) return false;
1387 int nonAuxCount = 0;
1388 int auxCount = 0;
1389 InputMethodSubtype nonAuxSubtype = null;
1390 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001391 for(int i = 0; i < N; ++i) {
1392 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001393 final List<InputMethodSubtype> subtypes =
1394 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001395 final int subtypeCount = subtypes.size();
1396 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001397 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001398 } else {
1399 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001400 final InputMethodSubtype subtype = subtypes.get(j);
1401 if (!subtype.isAuxiliary()) {
1402 ++nonAuxCount;
1403 nonAuxSubtype = subtype;
1404 } else {
1405 ++auxCount;
1406 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001407 }
1408 }
1409 }
satok7cfc0ed2011-06-20 21:29:36 +09001410 }
satokb6359412011-06-28 17:47:41 +09001411 if (nonAuxCount > 1 || auxCount > 1) {
1412 return true;
1413 } else if (nonAuxCount == 1 && auxCount == 1) {
1414 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001415 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1416 || auxSubtype.overridesImplicitlyEnabledSubtype()
1417 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001418 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1419 return false;
1420 }
1421 return true;
1422 }
1423 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001424 }
satok7cfc0ed2011-06-20 21:29:36 +09001425 }
1426
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001427 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001428 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001429 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001430 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001431 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001432 try {
1433 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001434 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001435 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001436 return;
1437 }
1438
1439 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001440 mImeWindowVis = vis;
1441 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001442 if (mStatusBar != null) {
1443 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1444 }
satok7cfc0ed2011-06-20 21:29:36 +09001445 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001446 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1447 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001448 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001449 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001450 final CharSequence title = mRes.getText(
1451 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001452 final CharSequence imiLabel = imi.loadLabel(pm);
1453 final CharSequence summary = mCurrentSubtype != null
1454 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1455 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1456 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001457 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001458 : imiLabel;
1459
satok7cfc0ed2011-06-20 21:29:36 +09001460 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001461 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001462 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001463 if (DEBUG) {
1464 Slog.d(TAG, "--- show notification: label = " + imiLabel
1465 + ", summary = " + summary);
1466 }
1467 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001468 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001469 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001470 mNotificationShown = true;
1471 }
satok7cfc0ed2011-06-20 21:29:36 +09001472 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001473 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001474 if (DEBUG) {
1475 Slog.d(TAG, "--- hide notification");
1476 }
1477 mNotificationManager.cancelAsUser(null,
1478 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001479 mNotificationShown = false;
1480 }
1481 }
satok06487a52010-10-29 11:37:18 +09001482 }
1483 } finally {
1484 Binder.restoreCallingIdentity(ident);
1485 }
1486 }
1487
satoke7c6998e2011-06-03 17:57:59 +09001488 @Override
satokf9f01002011-05-19 21:31:50 +09001489 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001490 if (!calledFromValidUser()) {
1491 return;
1492 }
satokf9f01002011-05-19 21:31:50 +09001493 synchronized (mMethodMap) {
1494 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1495 for (int i = 0; i < spans.length; ++i) {
1496 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001497 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001498 mSecureSuggestionSpans.put(ss, currentImi);
1499 }
1500 }
1501 }
1502 }
1503
satoke7c6998e2011-06-03 17:57:59 +09001504 @Override
satokf9f01002011-05-19 21:31:50 +09001505 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001506 if (!calledFromValidUser()) {
1507 return false;
1508 }
satokf9f01002011-05-19 21:31:50 +09001509 synchronized (mMethodMap) {
1510 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1511 // TODO: Do not send the intent if the process of the targetImi is already dead.
1512 if (targetImi != null) {
1513 final String[] suggestions = span.getSuggestions();
1514 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001515 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001516 final Intent intent = new Intent();
1517 // Ensures that only a class in the original IME package will receive the
1518 // notification.
satok42c5a162011-05-26 16:46:14 +09001519 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001520 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1521 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1522 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1523 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001524 final long ident = Binder.clearCallingIdentity();
1525 try {
1526 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1527 } finally {
1528 Binder.restoreCallingIdentity(ident);
1529 }
satokf9f01002011-05-19 21:31:50 +09001530 return true;
1531 }
1532 }
1533 return false;
1534 }
1535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001537 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1538 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1539 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1540 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001541 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001542 // There is no input method selected, try to choose new applicable input method.
1543 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001544 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001545 }
1546 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001548 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001550 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001551 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001552 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
satokf3db1af2010-11-23 13:34:33 +09001554 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001555 } else {
1556 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001557 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001558 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
satokab751aa2010-09-14 19:17:36 +09001562 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 InputMethodInfo info = mMethodMap.get(id);
1564 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001565 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001567
satokd81e9502012-05-21 12:58:45 +09001568 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001570 final int subtypeCount = info.getSubtypeCount();
1571 if (subtypeCount <= 0) {
1572 return;
satokcd7cd292010-11-20 15:46:23 +09001573 }
satokd81e9502012-05-21 12:58:45 +09001574 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1575 final InputMethodSubtype newSubtype;
1576 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1577 newSubtype = info.getSubtypeAt(subtypeId);
1578 } else {
1579 // If subtype is null, try to find the most applicable one from
1580 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001581 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001582 }
1583 if (newSubtype == null || oldSubtype == null) {
1584 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1585 + ", new subtype = " + newSubtype);
1586 return;
1587 }
1588 if (newSubtype != oldSubtype) {
1589 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1590 if (mCurMethod != null) {
1591 try {
1592 refreshImeWindowVisibilityLocked();
1593 mCurMethod.changeInputMethodSubtype(newSubtype);
1594 } catch (RemoteException e) {
1595 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001596 }
1597 }
1598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 return;
1600 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001601
satokd81e9502012-05-21 12:58:45 +09001602 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 final long ident = Binder.clearCallingIdentity();
1604 try {
satokab751aa2010-09-14 19:17:36 +09001605 // Set a subtype to this input method.
1606 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001607 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1608 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1609 // because mCurMethodId is stored as a history in
1610 // setSelectedInputMethodAndSubtypeLocked().
1611 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612
1613 if (ActivityManagerNative.isSystemReady()) {
1614 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001615 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001617 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001619 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } finally {
1621 Binder.restoreCallingIdentity(ident);
1622 }
1623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
satok42c5a162011-05-26 16:46:14 +09001625 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001626 public boolean showSoftInput(IInputMethodClient client, int flags,
1627 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 if (!calledFromValidUser()) {
1629 return false;
1630 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001631 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 long ident = Binder.clearCallingIdentity();
1633 try {
1634 synchronized (mMethodMap) {
1635 if (mCurClient == null || client == null
1636 || mCurClient.client.asBinder() != client.asBinder()) {
1637 try {
1638 // We need to check if this is the current client with
1639 // focus in the window manager, to allow this call to
1640 // be made before input is started in it.
1641 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001642 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001643 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001646 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
1648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001649
Joe Onorato8a9b2202010-02-26 18:56:32 -08001650 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001651 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653 } finally {
1654 Binder.restoreCallingIdentity(ident);
1655 }
1656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
The Android Open Source Project4df24232009-03-05 14:34:35 -08001658 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 mShowRequested = true;
1660 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1661 mShowExplicitlyRequested = true;
1662 }
1663 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1664 mShowExplicitlyRequested = true;
1665 mShowForced = true;
1666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
Dianne Hackborncc278702009-09-02 23:07:23 -07001668 if (!mSystemReady) {
1669 return false;
1670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
The Android Open Source Project4df24232009-03-05 14:34:35 -08001672 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001674 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1675 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1676 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001678 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001679 bindCurrentInputMethodService(
1680 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001681 mVisibleBound = true;
1682 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001683 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001685 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 // The client has asked to have the input method shown, but
1687 // we have been sitting here too long with a connection to the
1688 // service and no interface received, so let's disconnect/connect
1689 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001692 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001694 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001695 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001696 } else {
1697 if (DEBUG) {
1698 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1699 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
The Android Open Source Project4df24232009-03-05 14:34:35 -08001703 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
satok42c5a162011-05-26 16:46:14 +09001706 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001707 public boolean hideSoftInput(IInputMethodClient client, int flags,
1708 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001709 if (!calledFromValidUser()) {
1710 return false;
1711 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001712 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 long ident = Binder.clearCallingIdentity();
1714 try {
1715 synchronized (mMethodMap) {
1716 if (mCurClient == null || client == null
1717 || mCurClient.client.asBinder() != client.asBinder()) {
1718 try {
1719 // We need to check if this is the current client with
1720 // focus in the window manager, to allow this call to
1721 // be made before input is started in it.
1722 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001723 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1724 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001725 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001726 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001729 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737 } finally {
1738 Binder.restoreCallingIdentity(ident);
1739 }
1740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1744 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001745 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001747 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
1749 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001750 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001752 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1757 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1758 res = true;
1759 } else {
1760 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001762 if (mHaveConnection && mVisibleBound) {
1763 mContext.unbindService(mVisibleConnection);
1764 mVisibleBound = false;
1765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 mInputShown = false;
1767 mShowRequested = false;
1768 mShowExplicitlyRequested = false;
1769 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001772
satok42c5a162011-05-26 16:46:14 +09001773 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001774 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1775 int controlFlags, int softInputMode, int windowFlags,
1776 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001777 // Needs to check the validity before clearing calling identity
1778 final boolean calledFromValidUser = calledFromValidUser();
1779
Dianne Hackborn7663d802012-02-24 13:08:49 -08001780 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 long ident = Binder.clearCallingIdentity();
1782 try {
1783 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001784 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001785 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001787 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
Dianne Hackborn7663d802012-02-24 13:08:49 -08001789 ClientState cs = mClients.get(client.asBinder());
1790 if (cs == null) {
1791 throw new IllegalArgumentException("unknown client "
1792 + client.asBinder());
1793 }
1794
1795 try {
1796 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1797 // Check with the window manager to make sure this client actually
1798 // has a window with focus. If not, reject. This is thread safe
1799 // because if the focus changes some time before or after, the
1800 // next client receiving focus that has any interest in input will
1801 // be calling through here after that change happens.
1802 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1803 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1804 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001806 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001808
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001809 if (!calledFromValidUser) {
1810 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1811 Slog.w(TAG, "If you want to interect with IME, you need "
1812 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1813 hideCurrentInputLocked(0, null);
1814 return null;
1815 }
1816
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001817 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001818 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001819 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001820 if (attribute != null) {
1821 return startInputUncheckedLocked(cs, inputContext, attribute,
1822 controlFlags);
1823 }
1824 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001825 }
1826 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001828 // Should we auto-show the IME even if the caller has not
1829 // specified what should be done with it?
1830 // We only do this automatically if the window can resize
1831 // to accommodate the IME (so what the user sees will give
1832 // them good context without input information being obscured
1833 // by the IME) or if running on a large screen where there
1834 // is more room for the target window + IME.
1835 final boolean doAutoShow =
1836 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1837 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1838 || mRes.getConfiguration().isLayoutSizeAtLeast(
1839 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001840 final boolean isTextEditor =
1841 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1842
1843 // We want to start input before showing the IME, but after closing
1844 // it. We want to do this after closing it to help the IME disappear
1845 // more quickly (not get stuck behind it initializing itself for the
1846 // new focused input, even if its window wants to hide the IME).
1847 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1850 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001851 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1853 // There is no focus view, and this window will
1854 // be behind any soft input window, so hide the
1855 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001856 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001857 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001859 } else if (isTextEditor && doAutoShow && (softInputMode &
1860 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 // There is a focus view, and we are navigating forward
1862 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001863 // We only do this automatically if the window can resize
1864 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001865 // them good context without input information being obscured
1866 // by the IME) or if running on a large screen where there
1867 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001869 if (attribute != null) {
1870 res = startInputUncheckedLocked(cs, inputContext, attribute,
1871 controlFlags);
1872 didStart = true;
1873 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001874 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876 break;
1877 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1878 // Do nothing.
1879 break;
1880 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1881 if ((softInputMode &
1882 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001883 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001884 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
1886 break;
1887 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001889 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 break;
1891 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1892 if ((softInputMode &
1893 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001894 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001895 if (attribute != null) {
1896 res = startInputUncheckedLocked(cs, inputContext, attribute,
1897 controlFlags);
1898 didStart = true;
1899 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001900 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 }
1902 break;
1903 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001904 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001905 if (attribute != null) {
1906 res = startInputUncheckedLocked(cs, inputContext, attribute,
1907 controlFlags);
1908 didStart = true;
1909 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001910 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 break;
1912 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001913
1914 if (!didStart && attribute != null) {
1915 res = startInputUncheckedLocked(cs, inputContext, attribute,
1916 controlFlags);
1917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 } finally {
1920 Binder.restoreCallingIdentity(ident);
1921 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001922
1923 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001925
satok42c5a162011-05-26 16:46:14 +09001926 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001928 if (!calledFromValidUser()) {
1929 return;
1930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 synchronized (mMethodMap) {
1932 if (mCurClient == null || client == null
1933 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001934 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001935 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937
satok440aab52010-11-25 09:43:11 +09001938 // Always call subtype picker, because subtype picker is a superset of input method
1939 // picker.
satokab751aa2010-09-14 19:17:36 +09001940 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1941 }
1942 }
1943
satok42c5a162011-05-26 16:46:14 +09001944 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001946 if (!calledFromValidUser()) {
1947 return;
1948 }
satok28203512010-11-24 11:06:49 +09001949 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1950 }
1951
satok42c5a162011-05-26 16:46:14 +09001952 @Override
satok28203512010-11-24 11:06:49 +09001953 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001954 if (!calledFromValidUser()) {
1955 return;
1956 }
satok28203512010-11-24 11:06:49 +09001957 synchronized (mMethodMap) {
1958 if (subtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001959 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09001960 mMethodMap.get(id), subtype.hashCode()));
1961 } else {
1962 setInputMethod(token, id);
1963 }
1964 }
satokab751aa2010-09-14 19:17:36 +09001965 }
1966
satok42c5a162011-05-26 16:46:14 +09001967 @Override
satokb416a71e2010-11-25 20:42:14 +09001968 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001969 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001970 if (!calledFromValidUser()) {
1971 return;
1972 }
satokb416a71e2010-11-25 20:42:14 +09001973 synchronized (mMethodMap) {
1974 if (mCurClient == null || client == null
1975 || mCurClient.client.asBinder() != client.asBinder()) {
1976 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1977 }
satok7fee71f2010-12-17 18:54:26 +09001978 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1979 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001980 }
1981 }
1982
satok4fc87d62011-05-20 16:13:43 +09001983 @Override
satok735cf382010-11-11 20:40:09 +09001984 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001985 if (!calledFromValidUser()) {
1986 return false;
1987 }
satok735cf382010-11-11 20:40:09 +09001988 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001989 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001990 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001991 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001992 lastImi = mMethodMap.get(lastIme.first);
1993 } else {
1994 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001995 }
satok4fc87d62011-05-20 16:13:43 +09001996 String targetLastImiId = null;
1997 int subtypeId = NOT_A_SUBTYPE_ID;
1998 if (lastIme != null && lastImi != null) {
1999 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2000 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2001 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2002 : mCurrentSubtype.hashCode();
2003 // If the last IME is the same as the current IME and the last subtype is not
2004 // defined, there is no need to switch to the last IME.
2005 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2006 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002007 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002008 }
2009 }
2010
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002011 if (TextUtils.isEmpty(targetLastImiId)
2012 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002013 // This is a safety net. If the currentSubtype can't be added to the history
2014 // and the framework couldn't find the last ime, we will make the last ime be
2015 // the most applicable enabled keyboard subtype of the system imes.
2016 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2017 if (enabled != null) {
2018 final int N = enabled.size();
2019 final String locale = mCurrentSubtype == null
2020 ? mRes.getConfiguration().locale.toString()
2021 : mCurrentSubtype.getLocale();
2022 for (int i = 0; i < N; ++i) {
2023 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002024 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002025 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002026 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2027 InputMethodUtils.getSubtypes(imi),
2028 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002029 if (keyboardSubtype != null) {
2030 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002031 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002032 imi, keyboardSubtype.hashCode());
2033 if(keyboardSubtype.getLocale().equals(locale)) {
2034 break;
2035 }
2036 }
2037 }
2038 }
2039 }
2040 }
2041
2042 if (!TextUtils.isEmpty(targetLastImiId)) {
2043 if (DEBUG) {
2044 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2045 + ", from: " + mCurMethodId + ", " + subtypeId);
2046 }
2047 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2048 return true;
2049 } else {
2050 return false;
2051 }
satok735cf382010-11-11 20:40:09 +09002052 }
2053 }
2054
satoke7c6998e2011-06-03 17:57:59 +09002055 @Override
satok688bd472012-02-09 20:09:17 +09002056 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002057 if (!calledFromValidUser()) {
2058 return false;
2059 }
satok688bd472012-02-09 20:09:17 +09002060 synchronized (mMethodMap) {
2061 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2062 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2063 if (nextSubtype == null) {
2064 return false;
2065 }
2066 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2067 return true;
2068 }
2069 }
2070
2071 @Override
satok68f1b782011-04-11 14:26:04 +09002072 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002073 if (!calledFromValidUser()) {
2074 return null;
2075 }
satok68f1b782011-04-11 14:26:04 +09002076 synchronized (mMethodMap) {
2077 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2078 // TODO: Handle the case of the last IME with no subtypes
2079 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2080 || TextUtils.isEmpty(lastIme.second)) return null;
2081 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2082 if (lastImi == null) return null;
2083 try {
2084 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002085 final int lastSubtypeId =
2086 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002087 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2088 return null;
2089 }
2090 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002091 } catch (NumberFormatException e) {
2092 return null;
2093 }
2094 }
2095 }
2096
satoke7c6998e2011-06-03 17:57:59 +09002097 @Override
satokee5e77c2011-09-02 18:50:15 +09002098 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002099 if (!calledFromValidUser()) {
2100 return;
2101 }
satok91e88122011-07-18 11:11:42 +09002102 // By this IPC call, only a process which shares the same uid with the IME can add
2103 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002104 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002105 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002106 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002107 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 final String[] packageInfos;
2109 try {
2110 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2111 } catch (RemoteException e) {
2112 Slog.e(TAG, "Failed to get package infos");
2113 return;
2114 }
satok91e88122011-07-18 11:11:42 +09002115 if (packageInfos != null) {
2116 final int packageNum = packageInfos.length;
2117 for (int i = 0; i < packageNum; ++i) {
2118 if (packageInfos[i].equals(imi.getPackageName())) {
2119 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002120 final long ident = Binder.clearCallingIdentity();
2121 try {
2122 buildInputMethodListLocked(mMethodList, mMethodMap);
2123 } finally {
2124 Binder.restoreCallingIdentity(ident);
2125 }
satokee5e77c2011-09-02 18:50:15 +09002126 return;
satok91e88122011-07-18 11:11:42 +09002127 }
2128 }
2129 }
satoke7c6998e2011-06-03 17:57:59 +09002130 }
satokee5e77c2011-09-02 18:50:15 +09002131 return;
satoke7c6998e2011-06-03 17:57:59 +09002132 }
2133
satok28203512010-11-24 11:06:49 +09002134 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 synchronized (mMethodMap) {
2136 if (token == null) {
2137 if (mContext.checkCallingOrSelfPermission(
2138 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2139 != PackageManager.PERMISSION_GRANTED) {
2140 throw new SecurityException(
2141 "Using null token requires permission "
2142 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2143 }
2144 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002145 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2146 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 return;
2148 }
2149
satokc5933802011-08-31 21:26:04 +09002150 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 try {
satokab751aa2010-09-14 19:17:36 +09002152 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 } finally {
2154 Binder.restoreCallingIdentity(ident);
2155 }
2156 }
2157 }
2158
satok42c5a162011-05-26 16:46:14 +09002159 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002161 if (!calledFromValidUser()) {
2162 return;
2163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 synchronized (mMethodMap) {
2165 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002166 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2167 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 return;
2169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 long ident = Binder.clearCallingIdentity();
2171 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002172 hideCurrentInputLocked(flags, null);
2173 } finally {
2174 Binder.restoreCallingIdentity(ident);
2175 }
2176 }
2177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
satok42c5a162011-05-26 16:46:14 +09002179 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002180 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002181 if (!calledFromValidUser()) {
2182 return;
2183 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 synchronized (mMethodMap) {
2185 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002186 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2187 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002188 return;
2189 }
2190 long ident = Binder.clearCallingIdentity();
2191 try {
2192 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 } finally {
2194 Binder.restoreCallingIdentity(ident);
2195 }
2196 }
2197 }
2198
2199 void setEnabledSessionInMainThread(SessionState session) {
2200 if (mEnabledSession != session) {
2201 if (mEnabledSession != null) {
2202 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002203 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mEnabledSession.method.setSessionEnabled(
2205 mEnabledSession.session, false);
2206 } catch (RemoteException e) {
2207 }
2208 }
2209 mEnabledSession = session;
2210 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002211 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 session.method.setSessionEnabled(
2213 session.session, true);
2214 } catch (RemoteException e) {
2215 }
2216 }
2217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002218
satok42c5a162011-05-26 16:46:14 +09002219 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002221 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 switch (msg.what) {
2223 case MSG_SHOW_IM_PICKER:
2224 showInputMethodMenu();
2225 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002226
satokab751aa2010-09-14 19:17:36 +09002227 case MSG_SHOW_IM_SUBTYPE_PICKER:
2228 showInputMethodSubtypeMenu();
2229 return true;
2230
satok47a44912010-10-06 16:03:58 +09002231 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002232 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002233 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002234 args.recycle();
satok217f5482010-12-15 05:19:19 +09002235 return true;
2236
2237 case MSG_SHOW_IM_CONFIG:
2238 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002239 return true;
2240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 case MSG_UNBIND_INPUT:
2244 try {
2245 ((IInputMethod)msg.obj).unbindInput();
2246 } catch (RemoteException e) {
2247 // There is nothing interesting about the method dying.
2248 }
2249 return true;
2250 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002251 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 try {
2253 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2254 } catch (RemoteException e) {
2255 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002256 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 return true;
2258 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002259 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2262 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 } catch (RemoteException e) {
2264 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002265 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 return true;
2267 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002268 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002270 ((IInputMethod)args.arg1).hideSoftInput(0,
2271 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 } catch (RemoteException e) {
2273 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002274 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 return true;
2276 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002277 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002279 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2281 } catch (RemoteException e) {
2282 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002283 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 return true;
2285 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002286 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 try {
2288 ((IInputMethod)args.arg1).createSession(
2289 (IInputMethodCallback)args.arg2);
2290 } catch (RemoteException e) {
2291 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002292 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002297 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 try {
2299 SessionState session = (SessionState)args.arg1;
2300 setEnabledSessionInMainThread(session);
2301 session.method.startInput((IInputContext)args.arg2,
2302 (EditorInfo)args.arg3);
2303 } catch (RemoteException e) {
2304 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 return true;
2307 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002308 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 try {
2310 SessionState session = (SessionState)args.arg1;
2311 setEnabledSessionInMainThread(session);
2312 session.method.restartInput((IInputContext)args.arg2,
2313 (EditorInfo)args.arg3);
2314 } catch (RemoteException e) {
2315 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002316 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 case MSG_UNBIND_METHOD:
2322 try {
2323 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2324 } catch (RemoteException e) {
2325 // There is nothing interesting about the last client dying.
2326 }
2327 return true;
2328 case MSG_BIND_METHOD:
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 ((IInputMethodClient)args.arg1).onBindMethod(
2332 (InputBindResult)args.arg2);
2333 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002338 case MSG_SET_ACTIVE:
2339 try {
2340 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2341 } catch (RemoteException e) {
2342 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2343 + ((ClientState)msg.obj).pid + " uid "
2344 + ((ClientState)msg.obj).uid);
2345 }
2346 return true;
satok01038492012-04-09 21:08:27 +09002347
2348 // --------------------------------------------------------------
2349 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2350 mHardKeyboardListener.handleHardKeyboardStatusChange(
2351 msg.arg1 == 1, msg.arg2 == 1);
2352 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 return false;
2355 }
2356
satokdc9ddae2011-10-06 12:22:36 +09002357 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002358 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2359 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002360 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002361 if (DEBUG) {
2362 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2363 }
satok723a27e2010-11-11 14:58:11 +09002364 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002365 return true;
2366 }
2367
2368 return false;
2369 }
2370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2372 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002373 if (DEBUG) {
2374 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 list.clear();
2377 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002378
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002379 // Use for queryIntentServicesAsUser
2380 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002381 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002382 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002384 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002386 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002387
satoke7c6998e2011-06-03 17:57:59 +09002388 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2389 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 for (int i = 0; i < services.size(); ++i) {
2391 ResolveInfo ri = services.get(i);
2392 ServiceInfo si = ri.serviceInfo;
2393 ComponentName compName = new ComponentName(si.packageName, si.name);
2394 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2395 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002396 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 + ": it does not require the permission "
2398 + android.Manifest.permission.BIND_INPUT_METHOD);
2399 continue;
2400 }
2401
Joe Onorato8a9b2202010-02-26 18:56:32 -08002402 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403
2404 try {
satoke7c6998e2011-06-03 17:57:59 +09002405 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002407 final String id = p.getId();
2408 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409
satok5b927c432012-05-01 20:09:34 +09002410 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002411 // by default
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002412 if (InputMethodUtils.isDefaultEnabledIme(mSystemReady, p, mContext)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002413 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002414 }
2415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002417 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002421 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002423 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 }
2425 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002426
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002427 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002428 if (!TextUtils.isEmpty(defaultImiId)) {
2429 if (!map.containsKey(defaultImiId)) {
2430 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2431 if (chooseNewDefaultIMELocked()) {
2432 updateFromSettingsLocked();
2433 }
2434 } else {
2435 // Double check that the default IME is certainly enabled.
2436 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002437 }
2438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002442
satokab751aa2010-09-14 19:17:36 +09002443 private void showInputMethodMenu() {
2444 showInputMethodMenuInternal(false);
2445 }
2446
2447 private void showInputMethodSubtypeMenu() {
2448 showInputMethodMenuInternal(true);
2449 }
2450
satok217f5482010-12-15 05:19:19 +09002451 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002452 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002453 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002454 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2455 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002456 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002457 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002458 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002459 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002460 }
2461
2462 private void showConfigureInputMethods() {
2463 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2464 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2465 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2466 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002467 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002468 }
2469
satok2c93efc2012-04-02 19:33:47 +09002470 private boolean isScreenLocked() {
2471 return mKeyguardManager != null
2472 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2473 }
satokab751aa2010-09-14 19:17:36 +09002474 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002475 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002478 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002479
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002480 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002481 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002482 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002483
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002484 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002485 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2486 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002487 if (immis == null || immis.size() == 0) {
2488 return;
2489 }
2490
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002491 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492
satok688bd472012-02-09 20:09:17 +09002493 final List<ImeSubtypeListItem> imList =
2494 mImListManager.getSortedInputMethodAndSubtypeList(
2495 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002496
satokc3690562012-01-10 20:14:43 +09002497 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002498 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002499 if (currentSubtype != null) {
2500 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002501 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2502 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002503 }
2504 }
2505
Ken Wakasa761eb372011-03-04 19:06:18 +09002506 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002507 mIms = new InputMethodInfo[N];
2508 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002509 int checkedItem = 0;
2510 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002511 final ImeSubtypeListItem item = imList.get(i);
2512 mIms[i] = item.mImi;
2513 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002514 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002515 int subtypeId = mSubtypeIds[i];
2516 if ((subtypeId == NOT_A_SUBTYPE_ID)
2517 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2518 || (subtypeId == lastInputMethodSubtypeId)) {
2519 checkedItem = i;
2520 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002523 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002524 com.android.internal.R.styleable.DialogPreference,
2525 com.android.internal.R.attr.alertDialogStyle, 0);
2526 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002527 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002528 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002529 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002531 }
2532 })
2533 .setIcon(a.getDrawable(
2534 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2535 a.recycle();
satok01038492012-04-09 21:08:27 +09002536 final LayoutInflater inflater =
2537 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2538 final View tv = inflater.inflate(
2539 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2540 mDialogBuilder.setCustomTitle(tv);
2541
2542 // Setup layout for a toggle switch of the hardware keyboard
2543 mSwitchingDialogTitleView = tv;
2544 mSwitchingDialogTitleView.findViewById(
2545 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2546 mWindowManagerService.isHardKeyboardAvailable() ?
2547 View.VISIBLE : View.GONE);
2548 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2549 com.android.internal.R.id.hard_keyboard_switch));
2550 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2551 hardKeySwitch.setOnCheckedChangeListener(
2552 new OnCheckedChangeListener() {
2553 @Override
2554 public void onCheckedChanged(
2555 CompoundButton buttonView, boolean isChecked) {
2556 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002557 // Ensure that the input method dialog is dismissed when changing
2558 // the hardware keyboard state.
2559 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002560 }
2561 });
satokd87c2592010-09-29 11:52:06 +09002562
Ken Wakasa05dbb652011-08-22 15:22:43 +09002563 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2564 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2565 checkedItem);
2566
2567 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002568 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002569 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002570 public void onClick(DialogInterface dialog, int which) {
2571 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002572 if (mIms == null || mIms.length <= which
2573 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002574 return;
2575 }
2576 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002577 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002578 adapter.mCheckedItem = which;
2579 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 hideInputMethodMenu();
2581 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002582 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002583 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002584 subtypeId = NOT_A_SUBTYPE_ID;
2585 }
2586 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002587 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002590 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591
satokbc81b692011-08-26 16:22:22 +09002592 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002593 mDialogBuilder.setPositiveButton(
2594 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002595 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002596 @Override
satok7f35c8c2010-10-07 21:13:11 +09002597 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002598 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002599 }
2600 });
2601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002603 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 mSwitchingDialog.getWindow().setType(
2605 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002606 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2607 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002608 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 mSwitchingDialog.show();
2610 }
2611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002612
satok93d744d2012-05-09 17:14:08 +09002613 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002614 public final CharSequence mImeName;
2615 public final CharSequence mSubtypeName;
2616 public final InputMethodInfo mImi;
2617 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002618 private final boolean mIsSystemLocale;
2619 private final boolean mIsSystemLanguage;
2620
Ken Wakasa05dbb652011-08-22 15:22:43 +09002621 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002622 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002623 mImeName = imeName;
2624 mSubtypeName = subtypeName;
2625 mImi = imi;
2626 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002627 if (TextUtils.isEmpty(subtypeLocale)) {
2628 mIsSystemLocale = false;
2629 mIsSystemLanguage = false;
2630 } else {
2631 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2632 mIsSystemLanguage = mIsSystemLocale
2633 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2634 }
2635 }
2636
2637 @Override
2638 public int compareTo(ImeSubtypeListItem other) {
2639 if (TextUtils.isEmpty(mImeName)) {
2640 return 1;
2641 }
2642 if (TextUtils.isEmpty(other.mImeName)) {
2643 return -1;
2644 }
2645 if (!TextUtils.equals(mImeName, other.mImeName)) {
2646 return mImeName.toString().compareTo(other.mImeName.toString());
2647 }
2648 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2649 return 0;
2650 }
2651 if (mIsSystemLocale) {
2652 return -1;
2653 }
2654 if (other.mIsSystemLocale) {
2655 return 1;
2656 }
2657 if (mIsSystemLanguage) {
2658 return -1;
2659 }
2660 if (other.mIsSystemLanguage) {
2661 return 1;
2662 }
2663 if (TextUtils.isEmpty(mSubtypeName)) {
2664 return 1;
2665 }
2666 if (TextUtils.isEmpty(other.mSubtypeName)) {
2667 return -1;
2668 }
2669 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002670 }
2671 }
2672
2673 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2674 private final LayoutInflater mInflater;
2675 private final int mTextViewResourceId;
2676 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002677 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002678 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2679 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2680 super(context, textViewResourceId, itemsList);
2681 mTextViewResourceId = textViewResourceId;
2682 mItemsList = itemsList;
2683 mCheckedItem = checkedItem;
2684 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2685 }
2686
2687 @Override
2688 public View getView(int position, View convertView, ViewGroup parent) {
2689 final View view = convertView != null ? convertView
2690 : mInflater.inflate(mTextViewResourceId, null);
2691 if (position < 0 || position >= mItemsList.size()) return view;
2692 final ImeSubtypeListItem item = mItemsList.get(position);
2693 final CharSequence imeName = item.mImeName;
2694 final CharSequence subtypeName = item.mSubtypeName;
2695 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2696 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2697 if (TextUtils.isEmpty(subtypeName)) {
2698 firstTextView.setText(imeName);
2699 secondTextView.setVisibility(View.GONE);
2700 } else {
2701 firstTextView.setText(subtypeName);
2702 secondTextView.setText(imeName);
2703 secondTextView.setVisibility(View.VISIBLE);
2704 }
2705 final RadioButton radioButton =
2706 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2707 radioButton.setChecked(position == mCheckedItem);
2708 return view;
2709 }
2710 }
2711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002713 synchronized (mMethodMap) {
2714 hideInputMethodMenuLocked();
2715 }
2716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002717
The Android Open Source Project10592532009-03-18 17:39:46 -07002718 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002719 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720
The Android Open Source Project10592532009-03-18 17:39:46 -07002721 if (mSwitchingDialog != null) {
2722 mSwitchingDialog.dismiss();
2723 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002725
The Android Open Source Project10592532009-03-18 17:39:46 -07002726 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002727 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002731
satok42c5a162011-05-26 16:46:14 +09002732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002734 // TODO: Make this work even for non-current users?
2735 if (!calledFromValidUser()) {
2736 return false;
2737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 synchronized (mMethodMap) {
2739 if (mContext.checkCallingOrSelfPermission(
2740 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2741 != PackageManager.PERMISSION_GRANTED) {
2742 throw new SecurityException(
2743 "Requires permission "
2744 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2745 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 long ident = Binder.clearCallingIdentity();
2748 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002749 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 } finally {
2751 Binder.restoreCallingIdentity(ident);
2752 }
2753 }
2754 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002755
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002756 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2757 // Make sure this is a valid input method.
2758 InputMethodInfo imm = mMethodMap.get(id);
2759 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002760 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002761 }
2762
satokd87c2592010-09-29 11:52:06 +09002763 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2764 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002765
satokd87c2592010-09-29 11:52:06 +09002766 if (enabled) {
2767 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2768 if (pair.first.equals(id)) {
2769 // We are enabling this input method, but it is already enabled.
2770 // Nothing to do. The previous state was enabled.
2771 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002772 }
2773 }
satokd87c2592010-09-29 11:52:06 +09002774 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2775 // Previous state was disabled.
2776 return false;
2777 } else {
2778 StringBuilder builder = new StringBuilder();
2779 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2780 builder, enabledInputMethodsList, id)) {
2781 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002783 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2784 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2785 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002786 }
2787 // Previous state was enabled.
2788 return true;
2789 } else {
2790 // We are disabling the input method but it is already disabled.
2791 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002792 return false;
2793 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002794 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002795 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002796
satok723a27e2010-11-11 14:58:11 +09002797 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2798 boolean setSubtypeOnly) {
2799 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002800 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002801
2802 // Set Subtype here
2803 if (imi == null || subtypeId < 0) {
2804 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002805 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002806 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002807 if (subtypeId < imi.getSubtypeCount()) {
2808 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2809 mSettings.putSelectedSubtype(subtype.hashCode());
2810 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002811 } else {
2812 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002813 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002814 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002815 }
satokab751aa2010-09-14 19:17:36 +09002816 }
satok723a27e2010-11-11 14:58:11 +09002817
satok4c0e7152012-06-20 20:08:44 +09002818 // Workaround.
2819 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2820 // IMEs are not recognized and considered uninstalled.
2821 // Actually, we can't move everything after SystemReady because
2822 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2823 // the default IME here and try cheking the default IME again in systemReady().
2824 // TODO: Do nothing before system ready and implement a separated logic for
2825 // the encryption lock screen.
2826 // TODO: ASEC should be ready before IMMS is instantiated.
2827 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002828 // Set InputMethod here
2829 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2830 }
2831 }
2832
2833 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2834 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2835 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2836 // newDefaultIme is empty when there is no candidate for the selected IME.
2837 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2838 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2839 if (subtypeHashCode != null) {
2840 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002841 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002842 imi, Integer.valueOf(subtypeHashCode));
2843 } catch (NumberFormatException e) {
2844 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2845 }
2846 }
2847 }
2848 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002849 }
2850
satok4e4569d2010-11-19 18:45:53 +09002851 // If there are no selected shortcuts, tries finding the most applicable ones.
2852 private Pair<InputMethodInfo, InputMethodSubtype>
2853 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2854 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2855 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002856 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002857 boolean foundInSystemIME = false;
2858
2859 // Search applicable subtype for each InputMethodInfo
2860 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002861 final String imiId = imi.getId();
2862 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2863 continue;
2864 }
satokcd7cd292010-11-20 15:46:23 +09002865 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002866 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002867 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002868 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002869 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002870 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002871 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002872 }
satokdf31ae62011-01-15 06:19:44 +09002873 // 2. Search by the system locale from enabledSubtypes.
2874 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002875 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002876 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002877 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002878 }
satoka86f5e42011-09-02 17:12:42 +09002879 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002880 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002881 final ArrayList<InputMethodSubtype> subtypesForSearch =
2882 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002883 ? InputMethodUtils.getSubtypes(imi)
2884 : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002885 // 4. Search by the current subtype's locale from all subtypes.
2886 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002887 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002888 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002889 }
2890 // 5. Search by the system locale from all subtypes.
2891 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002892 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002893 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002894 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002895 }
satokcd7cd292010-11-20 15:46:23 +09002896 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002897 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002898 // The current input method is the most applicable IME.
2899 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002900 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002901 break;
satok7599a7fb2010-12-22 13:45:23 +09002902 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002903 // The system input method is 2nd applicable IME.
2904 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002905 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002906 if ((imi.getServiceInfo().applicationInfo.flags
2907 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2908 foundInSystemIME = true;
2909 }
satok4e4569d2010-11-19 18:45:53 +09002910 }
2911 }
2912 }
2913 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002914 if (mostApplicableIMI != null) {
2915 Slog.w(TAG, "Most applicable shortcut input method was:"
2916 + mostApplicableIMI.getId());
2917 if (mostApplicableSubtype != null) {
2918 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2919 + "," + mostApplicableSubtype.getMode() + ","
2920 + mostApplicableSubtype.getLocale());
2921 }
2922 }
satok4e4569d2010-11-19 18:45:53 +09002923 }
satokcd7cd292010-11-20 15:46:23 +09002924 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002925 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002926 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002927 } else {
2928 return null;
2929 }
2930 }
2931
satokab751aa2010-09-14 19:17:36 +09002932 /**
2933 * @return Return the current subtype of this input method.
2934 */
satok42c5a162011-05-26 16:46:14 +09002935 @Override
satokab751aa2010-09-14 19:17:36 +09002936 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002937 // TODO: Make this work even for non-current users?
2938 if (!calledFromValidUser()) {
2939 return null;
2940 }
2941 synchronized (mMethodMap) {
2942 return getCurrentInputMethodSubtypeLocked();
2943 }
2944 }
2945
2946 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002947 if (mCurMethodId == null) {
2948 return null;
2949 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002950 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002951 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2952 if (imi == null || imi.getSubtypeCount() == 0) {
2953 return null;
satok4e4569d2010-11-19 18:45:53 +09002954 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002955 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002956 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2957 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002958 if (subtypeId == NOT_A_SUBTYPE_ID) {
2959 // If there are no selected subtypes, the framework will try to find
2960 // the most applicable subtype from explicitly or implicitly enabled
2961 // subtypes.
2962 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002963 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002964 // If there is only one explicitly or implicitly enabled subtype,
2965 // just returns it.
2966 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2967 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2968 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002969 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002970 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002971 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002972 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002973 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002974 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2975 true);
satok4e4569d2010-11-19 18:45:53 +09002976 }
satok3ef8b292010-11-23 06:06:29 +09002977 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002978 } else {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002979 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09002980 }
2981 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002982 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09002983 }
2984
satokf3db1af2010-11-23 13:34:33 +09002985 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2986 InputMethodSubtype subtype) {
2987 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2988 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2989 } else {
2990 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2991 subtypes.add(subtype);
2992 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2993 }
2994 }
2995
satok4e4569d2010-11-19 18:45:53 +09002996 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002997 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002998 @Override
satok4e4569d2010-11-19 18:45:53 +09002999 public List getShortcutInputMethodsAndSubtypes() {
3000 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003001 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003002 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003003 // If there are no selected shortcut subtypes, the framework will try to find
3004 // the most applicable subtype from all subtypes whose mode is
3005 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003006 Pair<InputMethodInfo, InputMethodSubtype> info =
3007 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003008 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003009 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003010 ret.add(info.first);
3011 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003012 }
satok3da92232011-01-11 22:46:30 +09003013 return ret;
satokf3db1af2010-11-23 13:34:33 +09003014 }
satokf3db1af2010-11-23 13:34:33 +09003015 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3016 ret.add(imi);
3017 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3018 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003019 }
3020 }
satokf3db1af2010-11-23 13:34:33 +09003021 return ret;
satok4e4569d2010-11-19 18:45:53 +09003022 }
3023 }
3024
satok42c5a162011-05-26 16:46:14 +09003025 @Override
satokb66d2872010-11-10 01:04:04 +09003026 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003027 // TODO: Make this work even for non-current users?
3028 if (!calledFromValidUser()) {
3029 return false;
3030 }
satokb66d2872010-11-10 01:04:04 +09003031 synchronized (mMethodMap) {
3032 if (subtype != null && mCurMethodId != null) {
3033 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003034 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003035 if (subtypeId != NOT_A_SUBTYPE_ID) {
3036 setInputMethodLocked(mCurMethodId, subtypeId);
3037 return true;
3038 }
3039 }
3040 return false;
3041 }
3042 }
3043
satok688bd472012-02-09 20:09:17 +09003044 private static class InputMethodAndSubtypeListManager {
3045 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 // Used to load label
satok688bd472012-02-09 20:09:17 +09003047 private final PackageManager mPm;
3048 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003049 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003050 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3051 mContext = context;
3052 mPm = context.getPackageManager();
3053 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003054 final Locale locale = context.getResources().getConfiguration().locale;
3055 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003056 }
3057
3058 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3059 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3060 new Comparator<InputMethodInfo>() {
3061 @Override
3062 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3063 if (imi2 == null) return 0;
3064 if (imi1 == null) return 1;
3065 if (mPm == null) {
3066 return imi1.getId().compareTo(imi2.getId());
3067 }
3068 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3069 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3070 return imiId1.toString().compareTo(imiId2.toString());
3071 }
3072 });
3073
3074 public ImeSubtypeListItem getNextInputMethod(
3075 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3076 if (imi == null) {
3077 return null;
3078 }
3079 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3080 if (imList.size() <= 1) {
3081 return null;
3082 }
3083 final int N = imList.size();
3084 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003085 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003086 : NOT_A_SUBTYPE_ID;
3087 for (int i = 0; i < N; ++i) {
3088 final ImeSubtypeListItem isli = imList.get(i);
3089 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3090 if (!onlyCurrentIme) {
3091 return imList.get((i + 1) % N);
3092 }
3093 for (int j = 0; j < N - 1; ++j) {
3094 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3095 if (candidate.mImi.equals(imi)) {
3096 return candidate;
3097 }
3098 }
3099 return null;
3100 }
3101 }
3102 return null;
3103 }
3104
3105 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3106 return getSortedInputMethodAndSubtypeList(true, false, false);
3107 }
3108
3109 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3110 boolean inputShown, boolean isScreenLocked) {
3111 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3112 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3113 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3114 if (immis == null || immis.size() == 0) {
3115 return Collections.emptyList();
3116 }
3117 mSortedImmis.clear();
3118 mSortedImmis.putAll(immis);
3119 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3120 if (imi == null) continue;
3121 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3122 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3123 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3124 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3125 }
satok688bd472012-02-09 20:09:17 +09003126 final CharSequence imeLabel = imi.loadLabel(mPm);
3127 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3128 final int subtypeCount = imi.getSubtypeCount();
3129 if (DEBUG) {
3130 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3131 }
3132 for (int j = 0; j < subtypeCount; ++j) {
3133 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3134 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3135 // We show all enabled IMEs and subtypes when an IME is shown.
3136 if (enabledSubtypeSet.contains(subtypeHashCode)
3137 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3138 final CharSequence subtypeLabel =
3139 subtype.overridesImplicitlyEnabledSubtype() ? null
3140 : subtype.getDisplayName(mContext, imi.getPackageName(),
3141 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003142 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3143 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003144
3145 // Removing this subtype from enabledSubtypeSet because we no longer
3146 // need to add an entry of this subtype to imList to avoid duplicated
3147 // entries.
3148 enabledSubtypeSet.remove(subtypeHashCode);
3149 }
3150 }
3151 } else {
satok93d744d2012-05-09 17:14:08 +09003152 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3153 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003154 }
3155 }
satok93d744d2012-05-09 17:14:08 +09003156 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003157 return imList;
3158 }
3159 }
3160
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003161 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003162 private static class InputMethodFileManager {
3163 private static final String SYSTEM_PATH = "system";
3164 private static final String INPUT_METHOD_PATH = "inputmethod";
3165 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3166 private static final String NODE_SUBTYPES = "subtypes";
3167 private static final String NODE_SUBTYPE = "subtype";
3168 private static final String NODE_IMI = "imi";
3169 private static final String ATTR_ID = "id";
3170 private static final String ATTR_LABEL = "label";
3171 private static final String ATTR_ICON = "icon";
3172 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3173 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3174 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3175 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3176 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3177 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003178 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003179 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003180 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003181 if (methodMap == null) {
3182 throw new NullPointerException("methodMap is null");
3183 }
3184 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003185 final File systemDir = userId == UserHandle.USER_OWNER
3186 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3187 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003188 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3189 if (!inputMethodDir.mkdirs()) {
3190 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3191 }
3192 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3193 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3194 if (!subtypeFile.exists()) {
3195 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003196 writeAdditionalInputMethodSubtypes(
3197 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003198 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003199 readAdditionalInputMethodSubtypes(
3200 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003201 }
3202 }
3203
3204 private void deleteAllInputMethodSubtypes(String imiId) {
3205 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003206 mAdditionalSubtypesMap.remove(imiId);
3207 writeAdditionalInputMethodSubtypes(
3208 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003209 }
3210 }
3211
3212 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003213 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003214 synchronized (mMethodMap) {
3215 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3216 final int N = additionalSubtypes.length;
3217 for (int i = 0; i < N; ++i) {
3218 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003219 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003220 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003221 } else {
3222 Slog.w(TAG, "Duplicated subtype definition found: "
3223 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003224 }
3225 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003226 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3227 writeAdditionalInputMethodSubtypes(
3228 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003229 }
3230 }
3231
3232 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3233 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003234 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003235 }
3236 }
3237
3238 private static void writeAdditionalInputMethodSubtypes(
3239 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3240 HashMap<String, InputMethodInfo> methodMap) {
3241 // Safety net for the case that this function is called before methodMap is set.
3242 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3243 FileOutputStream fos = null;
3244 try {
3245 fos = subtypesFile.startWrite();
3246 final XmlSerializer out = new FastXmlSerializer();
3247 out.setOutput(fos, "utf-8");
3248 out.startDocument(null, true);
3249 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3250 out.startTag(null, NODE_SUBTYPES);
3251 for (String imiId : allSubtypes.keySet()) {
3252 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3253 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3254 continue;
3255 }
3256 out.startTag(null, NODE_IMI);
3257 out.attribute(null, ATTR_ID, imiId);
3258 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3259 final int N = subtypesList.size();
3260 for (int i = 0; i < N; ++i) {
3261 final InputMethodSubtype subtype = subtypesList.get(i);
3262 out.startTag(null, NODE_SUBTYPE);
3263 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3264 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3265 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3266 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3267 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3268 out.attribute(null, ATTR_IS_AUXILIARY,
3269 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3270 out.endTag(null, NODE_SUBTYPE);
3271 }
3272 out.endTag(null, NODE_IMI);
3273 }
3274 out.endTag(null, NODE_SUBTYPES);
3275 out.endDocument();
3276 subtypesFile.finishWrite(fos);
3277 } catch (java.io.IOException e) {
3278 Slog.w(TAG, "Error writing subtypes", e);
3279 if (fos != null) {
3280 subtypesFile.failWrite(fos);
3281 }
3282 }
3283 }
3284
3285 private static void readAdditionalInputMethodSubtypes(
3286 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3287 if (allSubtypes == null || subtypesFile == null) return;
3288 allSubtypes.clear();
3289 FileInputStream fis = null;
3290 try {
3291 fis = subtypesFile.openRead();
3292 final XmlPullParser parser = Xml.newPullParser();
3293 parser.setInput(fis, null);
3294 int type = parser.getEventType();
3295 // Skip parsing until START_TAG
3296 while ((type = parser.next()) != XmlPullParser.START_TAG
3297 && type != XmlPullParser.END_DOCUMENT) {}
3298 String firstNodeName = parser.getName();
3299 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3300 throw new XmlPullParserException("Xml doesn't start with subtypes");
3301 }
3302 final int depth =parser.getDepth();
3303 String currentImiId = null;
3304 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3305 while (((type = parser.next()) != XmlPullParser.END_TAG
3306 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3307 if (type != XmlPullParser.START_TAG)
3308 continue;
3309 final String nodeName = parser.getName();
3310 if (NODE_IMI.equals(nodeName)) {
3311 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3312 if (TextUtils.isEmpty(currentImiId)) {
3313 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3314 continue;
3315 }
3316 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3317 allSubtypes.put(currentImiId, tempSubtypesArray);
3318 } else if (NODE_SUBTYPE.equals(nodeName)) {
3319 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3320 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3321 continue;
3322 }
3323 final int icon = Integer.valueOf(
3324 parser.getAttributeValue(null, ATTR_ICON));
3325 final int label = Integer.valueOf(
3326 parser.getAttributeValue(null, ATTR_LABEL));
3327 final String imeSubtypeLocale =
3328 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3329 final String imeSubtypeMode =
3330 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3331 final String imeSubtypeExtraValue =
3332 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003333 final boolean isAuxiliary = "1".equals(String.valueOf(
3334 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003335 final InputMethodSubtype subtype =
3336 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3337 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3338 tempSubtypesArray.add(subtype);
3339 }
3340 }
3341 } catch (XmlPullParserException e) {
3342 Slog.w(TAG, "Error reading subtypes: " + e);
3343 return;
3344 } catch (java.io.IOException e) {
3345 Slog.w(TAG, "Error reading subtypes: " + e);
3346 return;
3347 } catch (NumberFormatException e) {
3348 Slog.w(TAG, "Error reading subtypes: " + e);
3349 return;
3350 } finally {
3351 if (fis != null) {
3352 try {
3353 fis.close();
3354 } catch (java.io.IOException e1) {
3355 Slog.w(TAG, "Failed to close.");
3356 }
3357 }
3358 }
3359 }
3360 }
3361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003363 // Utilities for debug
3364 private static String getStackTrace() {
3365 final StringBuilder sb = new StringBuilder();
3366 try {
3367 throw new RuntimeException();
3368 } catch (RuntimeException e) {
3369 final StackTraceElement[] frames = e.getStackTrace();
3370 // Start at 1 because the first frame is here and we don't care about it
3371 for (int j = 1; j < frames.length; ++j) {
3372 sb.append(frames[j].toString() + "\n");
3373 }
3374 }
3375 return sb.toString();
3376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 @Override
3379 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3380 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3381 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3384 + Binder.getCallingPid()
3385 + ", uid=" + Binder.getCallingUid());
3386 return;
3387 }
3388
3389 IInputMethod method;
3390 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 synchronized (mMethodMap) {
3395 p.println("Current Input Method Manager state:");
3396 int N = mMethodList.size();
3397 p.println(" Input Methods:");
3398 for (int i=0; i<N; i++) {
3399 InputMethodInfo info = mMethodList.get(i);
3400 p.println(" InputMethod #" + i + ":");
3401 info.dump(p, " ");
3402 }
3403 p.println(" Clients:");
3404 for (ClientState ci : mClients.values()) {
3405 p.println(" Client " + ci + ":");
3406 p.println(" client=" + ci.client);
3407 p.println(" inputContext=" + ci.inputContext);
3408 p.println(" sessionRequested=" + ci.sessionRequested);
3409 p.println(" curSession=" + ci.curSession);
3410 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003411 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003413 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3414 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3416 + " mBoundToMethod=" + mBoundToMethod);
3417 p.println(" mCurToken=" + mCurToken);
3418 p.println(" mCurIntent=" + mCurIntent);
3419 method = mCurMethod;
3420 p.println(" mCurMethod=" + mCurMethod);
3421 p.println(" mEnabledSession=" + mEnabledSession);
3422 p.println(" mShowRequested=" + mShowRequested
3423 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3424 + " mShowForced=" + mShowForced
3425 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003426 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003428
Jeff Brownb88102f2010-09-08 11:49:43 -07003429 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 pw.flush();
3432 try {
3433 client.client.asBinder().dump(fd, args);
3434 } catch (RemoteException e) {
3435 p.println("Input method client dead: " + e);
3436 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003437 } else {
3438 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003440
Jeff Brownb88102f2010-09-08 11:49:43 -07003441 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 pw.flush();
3444 try {
3445 method.asBinder().dump(fd, args);
3446 } catch (RemoteException e) {
3447 p.println("Input method service dead: " + e);
3448 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003449 } else {
3450 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 }
3452 }
3453}