blob: 3aa04b3a0663b4f87e8957ba08d884d8c152d824 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090019import com.android.internal.inputmethod.InputMethodUtils;
20import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070022import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090023import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.view.IInputContext;
25import com.android.internal.view.IInputMethod;
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 Kataoka8e303cc2013-01-11 15:55:28 +0900156 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900157 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
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 Kataoka8e303cc2013-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 Kataoka8e303cc2013-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 Kataoka8e303cc2013-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 }
891 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
892 }
893
satoke7c6998e2011-06-03 17:57:59 +0900894 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900896 // TODO: Make this work even for non-current users?
897 if (!calledFromValidUser()) {
898 return Collections.emptyList();
899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 synchronized (mMethodMap) {
901 return new ArrayList<InputMethodInfo>(mMethodList);
902 }
903 }
904
satoke7c6998e2011-06-03 17:57:59 +0900905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 // TODO: Make this work even for non-current users?
908 if (!calledFromValidUser()) {
909 return Collections.emptyList();
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900912 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 }
915
satokbb4aa062011-01-19 21:40:27 +0900916 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
917 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
918 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
919 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900920 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900921 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900922 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900923 }
924 return enabledInputMethodAndSubtypes;
925 }
926
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900927 /**
928 * @param imi if null, returns enabled subtypes for the current imi
929 * @return enabled subtypes of the specified imi
930 */
satoke7c6998e2011-06-03 17:57:59 +0900931 @Override
satok16331c82010-12-20 23:48:46 +0900932 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
933 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900934 // TODO: Make this work even for non-current users?
935 if (!calledFromValidUser()) {
936 return Collections.emptyList();
937 }
satok67ddf9c2010-11-17 09:45:54 +0900938 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900939 if (imi == null && mCurMethodId != null) {
940 imi = mMethodMap.get(mCurMethodId);
941 }
942 return mSettings.getEnabledInputMethodSubtypeListLocked(
943 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900944 }
945 }
946
satoke7c6998e2011-06-03 17:57:59 +0900947 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 public void addClient(IInputMethodClient client,
949 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 if (!calledFromValidUser()) {
951 return;
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 synchronized (mMethodMap) {
954 mClients.put(client.asBinder(), new ClientState(client,
955 inputContext, uid, pid));
956 }
957 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800958
satoke7c6998e2011-06-03 17:57:59 +0900959 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900961 if (!calledFromValidUser()) {
962 return;
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 synchronized (mMethodMap) {
965 mClients.remove(client.asBinder());
966 }
967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 void executeOrSendMessage(IInterface target, Message msg) {
970 if (target.asBinder() instanceof Binder) {
971 mCaller.sendMessage(msg);
972 } else {
973 handleMessage(msg);
974 msg.recycle();
975 }
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700978 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800980 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 + mCurClient.client.asBinder());
982 if (mBoundToMethod) {
983 mBoundToMethod = false;
984 if (mCurMethod != null) {
985 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
986 MSG_UNBIND_INPUT, mCurMethod));
987 }
988 }
Dianne Hackborna6e41342012-05-22 16:30:34 -0700989
990 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
991 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
993 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
994 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
The Android Open Source Project10592532009-03-18 17:39:46 -0700997 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 private int getImeShowFlags() {
1002 int flags = 0;
1003 if (mShowForced) {
1004 flags |= InputMethod.SHOW_FORCED
1005 | InputMethod.SHOW_EXPLICIT;
1006 } else if (mShowExplicitlyRequested) {
1007 flags |= InputMethod.SHOW_EXPLICIT;
1008 }
1009 return flags;
1010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 private int getAppShowFlags() {
1013 int flags = 0;
1014 if (mShowForced) {
1015 flags |= InputMethodManager.SHOW_FORCED;
1016 } else if (!mShowExplicitlyRequested) {
1017 flags |= InputMethodManager.SHOW_IMPLICIT;
1018 }
1019 return flags;
1020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
Dianne Hackborn7663d802012-02-24 13:08:49 -08001022 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 if (!mBoundToMethod) {
1024 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1025 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1026 mBoundToMethod = true;
1027 }
1028 final SessionState session = mCurClient.curSession;
1029 if (initial) {
1030 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1031 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1032 } else {
1033 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1034 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1035 }
1036 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001037 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001038 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001040 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001044 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 // If no method is currently selected, do nothing.
1046 if (mCurMethodId == null) {
1047 return mNoBinding;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 ClientState cs = mClients.get(client.asBinder());
1051 if (cs == null) {
1052 throw new IllegalArgumentException("unknown client "
1053 + client.asBinder());
1054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 try {
1057 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1058 // Check with the window manager to make sure this client actually
1059 // has a window with focus. If not, reject. This is thread safe
1060 // because if the focus changes some time before or after, the
1061 // next client receiving focus that has any interest in input will
1062 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1065 return null;
1066 }
1067 } catch (RemoteException e) {
1068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
Dianne Hackborn7663d802012-02-24 13:08:49 -08001070 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1071 }
1072
1073 InputBindResult startInputUncheckedLocked(ClientState cs,
1074 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1075 // If no method is currently selected, do nothing.
1076 if (mCurMethodId == null) {
1077 return mNoBinding;
1078 }
1079
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001080 if (mCurClient == null) {
1081 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1082 if (DEBUG) {
1083 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1084 }
1085 }
1086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 if (mCurClient != cs) {
1088 // If the client is changing, we need to switch over to the new
1089 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001090 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001091 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 + cs.client.asBinder());
1093
1094 // If the screen is on, inform the new client it is active
1095 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001096 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1097 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 // Bump up the sequence for this client and attach it.
1102 mCurSeq++;
1103 if (mCurSeq <= 0) mCurSeq = 1;
1104 mCurClient = cs;
1105 mCurInputContext = inputContext;
1106 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 // Check if the input method is changing.
1109 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1110 if (cs.curSession != null) {
1111 // Fast case: if we are already connected to the input method,
1112 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001113 return attachNewInputLocked(
1114 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 if (mHaveConnection) {
1117 if (mCurMethod != null) {
1118 if (!cs.sessionRequested) {
1119 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1122 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001123 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125 // Return to client, and we will get back with it when
1126 // we have had a session made for it.
1127 return new InputBindResult(null, mCurId, mCurSeq);
1128 } else if (SystemClock.uptimeMillis()
1129 < (mLastBindTime+TIME_TO_RECONNECT)) {
1130 // In this case we have connected to the service, but
1131 // don't yet have its interface. If it hasn't been too
1132 // long since we did the connection, we'll return to
1133 // the client and wait to get the service interface so
1134 // we can report back. If it has been too long, we want
1135 // to fall through so we can try a disconnect/reconnect
1136 // to see if we can get back in touch with the service.
1137 return new InputBindResult(null, mCurId, mCurSeq);
1138 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1140 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 }
1143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001145 return startInputInnerLocked();
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001148 InputBindResult startInputInnerLocked() {
1149 if (mCurMethodId == null) {
1150 return mNoBinding;
1151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001153 if (!mSystemReady) {
1154 // If the system is not yet ready, we shouldn't be running third
1155 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001156 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1160 if (info == null) {
1161 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001163
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001164 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1167 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001168 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1169 com.android.internal.R.string.input_method_binding_label);
1170 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1171 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001172 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001173 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 mLastBindTime = SystemClock.uptimeMillis();
1175 mHaveConnection = true;
1176 mCurId = info.getId();
1177 mCurToken = new Binder();
1178 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001179 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 mIWindowManager.addWindowToken(mCurToken,
1181 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1182 } catch (RemoteException e) {
1183 }
1184 return new InputBindResult(null, mCurId, mCurSeq);
1185 } else {
1186 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001187 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 + mCurIntent);
1189 }
1190 return null;
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
satoke7c6998e2011-06-03 17:57:59 +09001193 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001195 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001196 if (!calledFromValidUser()) {
1197 return null;
1198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 synchronized (mMethodMap) {
1200 final long ident = Binder.clearCallingIdentity();
1201 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001202 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } finally {
1204 Binder.restoreCallingIdentity(ident);
1205 }
1206 }
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public void finishInput(IInputMethodClient client) {
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
satoke7c6998e2011-06-03 17:57:59 +09001213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 public void onServiceConnected(ComponentName name, IBinder service) {
1215 synchronized (mMethodMap) {
1216 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1217 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001218 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001219 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001220 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001221 return;
1222 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001223 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001224 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1225 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001227 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001228 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001230 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001231 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 }
1234 }
1235 }
1236
1237 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1238 synchronized (mMethodMap) {
1239 if (mCurMethod != null && method != null
1240 && mCurMethod.asBinder() == method.asBinder()) {
1241 if (mCurClient != null) {
1242 mCurClient.curSession = new SessionState(mCurClient,
1243 method, session);
1244 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001245 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 if (res.method != null) {
1247 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1248 MSG_BIND_METHOD, mCurClient.client, res));
1249 }
1250 }
1251 }
1252 }
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001255 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001256 if (mVisibleBound) {
1257 mContext.unbindService(mVisibleConnection);
1258 mVisibleBound = false;
1259 }
1260
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001261 if (mHaveConnection) {
1262 mContext.unbindService(this);
1263 mHaveConnection = false;
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001266 if (mCurToken != null) {
1267 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001268 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001269 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001270 // The current IME is shown. Hence an IME switch (transition) is happening.
1271 mWindowManagerService.saveLastInputMethodWindowForTransition();
1272 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001273 mIWindowManager.removeWindowToken(mCurToken);
1274 } catch (RemoteException e) {
1275 }
1276 mCurToken = null;
1277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project10592532009-03-18 17:39:46 -07001279 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001280 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001282 if (reportToClient && mCurClient != null) {
1283 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1284 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1285 }
1286 }
1287
Devin Taylor0c33ed22010-02-23 13:26:46 -06001288 private void finishSession(SessionState sessionState) {
1289 if (sessionState != null && sessionState.session != null) {
1290 try {
1291 sessionState.session.finishSession();
1292 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001293 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001294 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001295 }
1296 }
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001299 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 if (mCurMethod != null) {
1301 for (ClientState cs : mClients.values()) {
1302 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001303 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 cs.curSession = null;
1305 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001306
1307 finishSession(mEnabledSession);
1308 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 mCurMethod = null;
1310 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001311 if (mStatusBar != null) {
1312 mStatusBar.setIconVisibility("ime", false);
1313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
satoke7c6998e2011-06-03 17:57:59 +09001316 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 public void onServiceDisconnected(ComponentName name) {
1318 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001319 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 + " mCurIntent=" + mCurIntent);
1321 if (mCurMethod != null && mCurIntent != null
1322 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 // We consider this to be a new bind attempt, since the system
1325 // should now try to restart the service for us.
1326 mLastBindTime = SystemClock.uptimeMillis();
1327 mShowRequested = mInputShown;
1328 mInputShown = false;
1329 if (mCurClient != null) {
1330 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1331 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1332 }
1333 }
1334 }
1335 }
1336
satokf9f01002011-05-19 21:31:50 +09001337 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001339 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 long ident = Binder.clearCallingIdentity();
1341 try {
1342 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001343 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 return;
1345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 synchronized (mMethodMap) {
1348 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001349 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001350 if (mStatusBar != null) {
1351 mStatusBar.setIconVisibility("ime", false);
1352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001354 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001355 CharSequence contentDescription = null;
1356 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001357 // Use PackageManager to load label
1358 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001359 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001360 mIPackageManager.getApplicationInfo(packageName, 0,
1361 mSettings.getCurrentUserId()));
1362 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001363 /* ignore */
1364 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001365 if (mStatusBar != null) {
1366 mStatusBar.setIcon("ime", packageName, iconId, 0,
1367 contentDescription != null
1368 ? contentDescription.toString() : null);
1369 mStatusBar.setIconVisibility("ime", true);
1370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373 } finally {
1374 Binder.restoreCallingIdentity(ident);
1375 }
1376 }
1377
satok7cfc0ed2011-06-20 21:29:36 +09001378 private boolean needsToShowImeSwitchOngoingNotification() {
1379 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001380 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001381 synchronized (mMethodMap) {
1382 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1383 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001384 if (N > 2) return true;
1385 if (N < 1) return false;
1386 int nonAuxCount = 0;
1387 int auxCount = 0;
1388 InputMethodSubtype nonAuxSubtype = null;
1389 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001390 for(int i = 0; i < N; ++i) {
1391 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001392 final List<InputMethodSubtype> subtypes =
1393 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001394 final int subtypeCount = subtypes.size();
1395 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001396 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001397 } else {
1398 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001399 final InputMethodSubtype subtype = subtypes.get(j);
1400 if (!subtype.isAuxiliary()) {
1401 ++nonAuxCount;
1402 nonAuxSubtype = subtype;
1403 } else {
1404 ++auxCount;
1405 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001406 }
1407 }
1408 }
satok7cfc0ed2011-06-20 21:29:36 +09001409 }
satokb6359412011-06-28 17:47:41 +09001410 if (nonAuxCount > 1 || auxCount > 1) {
1411 return true;
1412 } else if (nonAuxCount == 1 && auxCount == 1) {
1413 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001414 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1415 || auxSubtype.overridesImplicitlyEnabledSubtype()
1416 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001417 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1418 return false;
1419 }
1420 return true;
1421 }
1422 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001423 }
satok7cfc0ed2011-06-20 21:29:36 +09001424 }
1425
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001426 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001427 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001428 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001429 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001430 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001431 try {
1432 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001433 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001434 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001435 return;
1436 }
1437
1438 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001439 mImeWindowVis = vis;
1440 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001441 if (mStatusBar != null) {
1442 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1443 }
satok7cfc0ed2011-06-20 21:29:36 +09001444 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001445 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1446 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001447 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001448 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001449 final CharSequence title = mRes.getText(
1450 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001451 final CharSequence imiLabel = imi.loadLabel(pm);
1452 final CharSequence summary = mCurrentSubtype != null
1453 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1454 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1455 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001456 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001457 : imiLabel;
1458
satok7cfc0ed2011-06-20 21:29:36 +09001459 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001460 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001461 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001462 if (DEBUG) {
1463 Slog.d(TAG, "--- show notification: label = " + imiLabel
1464 + ", summary = " + summary);
1465 }
1466 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001467 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001468 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001469 mNotificationShown = true;
1470 }
satok7cfc0ed2011-06-20 21:29:36 +09001471 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001472 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001473 if (DEBUG) {
1474 Slog.d(TAG, "--- hide notification");
1475 }
1476 mNotificationManager.cancelAsUser(null,
1477 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001478 mNotificationShown = false;
1479 }
1480 }
satok06487a52010-10-29 11:37:18 +09001481 }
1482 } finally {
1483 Binder.restoreCallingIdentity(ident);
1484 }
1485 }
1486
satoke7c6998e2011-06-03 17:57:59 +09001487 @Override
satokf9f01002011-05-19 21:31:50 +09001488 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001489 if (!calledFromValidUser()) {
1490 return;
1491 }
satokf9f01002011-05-19 21:31:50 +09001492 synchronized (mMethodMap) {
1493 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1494 for (int i = 0; i < spans.length; ++i) {
1495 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001496 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001497 mSecureSuggestionSpans.put(ss, currentImi);
1498 }
1499 }
1500 }
1501 }
1502
satoke7c6998e2011-06-03 17:57:59 +09001503 @Override
satokf9f01002011-05-19 21:31:50 +09001504 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 if (!calledFromValidUser()) {
1506 return false;
1507 }
satokf9f01002011-05-19 21:31:50 +09001508 synchronized (mMethodMap) {
1509 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1510 // TODO: Do not send the intent if the process of the targetImi is already dead.
1511 if (targetImi != null) {
1512 final String[] suggestions = span.getSuggestions();
1513 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001514 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001515 final Intent intent = new Intent();
1516 // Ensures that only a class in the original IME package will receive the
1517 // notification.
satok42c5a162011-05-26 16:46:14 +09001518 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001519 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1520 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1521 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1522 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001523 final long ident = Binder.clearCallingIdentity();
1524 try {
1525 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1526 } finally {
1527 Binder.restoreCallingIdentity(ident);
1528 }
satokf9f01002011-05-19 21:31:50 +09001529 return true;
1530 }
1531 }
1532 return false;
1533 }
1534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001536 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1537 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1538 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1539 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001540 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001541 // There is no input method selected, try to choose new applicable input method.
1542 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001543 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001544 }
1545 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001547 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001549 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001550 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001551 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
satokf3db1af2010-11-23 13:34:33 +09001553 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001554 } else {
1555 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001556 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001557 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
satokab751aa2010-09-14 19:17:36 +09001561 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 InputMethodInfo info = mMethodMap.get(id);
1563 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001564 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001566
satokd81e9502012-05-21 12:58:45 +09001567 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001569 final int subtypeCount = info.getSubtypeCount();
1570 if (subtypeCount <= 0) {
1571 return;
satokcd7cd292010-11-20 15:46:23 +09001572 }
satokd81e9502012-05-21 12:58:45 +09001573 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1574 final InputMethodSubtype newSubtype;
1575 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1576 newSubtype = info.getSubtypeAt(subtypeId);
1577 } else {
1578 // If subtype is null, try to find the most applicable one from
1579 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001580 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001581 }
1582 if (newSubtype == null || oldSubtype == null) {
1583 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1584 + ", new subtype = " + newSubtype);
1585 return;
1586 }
1587 if (newSubtype != oldSubtype) {
1588 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1589 if (mCurMethod != null) {
1590 try {
1591 refreshImeWindowVisibilityLocked();
1592 mCurMethod.changeInputMethodSubtype(newSubtype);
1593 } catch (RemoteException e) {
1594 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001595 }
1596 }
1597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 return;
1599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
satokd81e9502012-05-21 12:58:45 +09001601 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 final long ident = Binder.clearCallingIdentity();
1603 try {
satokab751aa2010-09-14 19:17:36 +09001604 // Set a subtype to this input method.
1605 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001606 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1607 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1608 // because mCurMethodId is stored as a history in
1609 // setSelectedInputMethodAndSubtypeLocked().
1610 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611
1612 if (ActivityManagerNative.isSystemReady()) {
1613 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001614 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001616 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001618 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 } finally {
1620 Binder.restoreCallingIdentity(ident);
1621 }
1622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001623
satok42c5a162011-05-26 16:46:14 +09001624 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001625 public boolean showSoftInput(IInputMethodClient client, int flags,
1626 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001627 if (!calledFromValidUser()) {
1628 return false;
1629 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001630 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 long ident = Binder.clearCallingIdentity();
1632 try {
1633 synchronized (mMethodMap) {
1634 if (mCurClient == null || client == null
1635 || mCurClient.client.asBinder() != client.asBinder()) {
1636 try {
1637 // We need to check if this is the current client with
1638 // focus in the window manager, to allow this call to
1639 // be made before input is started in it.
1640 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001641 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001642 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001645 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
Joe Onorato8a9b2202010-02-26 18:56:32 -08001649 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001650 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 } finally {
1653 Binder.restoreCallingIdentity(ident);
1654 }
1655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project4df24232009-03-05 14:34:35 -08001657 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 mShowRequested = true;
1659 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1660 mShowExplicitlyRequested = true;
1661 }
1662 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1663 mShowExplicitlyRequested = true;
1664 mShowForced = true;
1665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
Dianne Hackborncc278702009-09-02 23:07:23 -07001667 if (!mSystemReady) {
1668 return false;
1669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
The Android Open Source Project4df24232009-03-05 14:34:35 -08001671 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001673 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1674 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1675 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001677 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001678 bindCurrentInputMethodService(
1679 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001680 mVisibleBound = true;
1681 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001682 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001684 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 // The client has asked to have the input method shown, but
1686 // we have been sitting here too long with a connection to the
1687 // service and no interface received, so let's disconnect/connect
1688 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001691 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001693 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001694 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001695 } else {
1696 if (DEBUG) {
1697 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1698 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701
The Android Open Source Project4df24232009-03-05 14:34:35 -08001702 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
satok42c5a162011-05-26 16:46:14 +09001705 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001706 public boolean hideSoftInput(IInputMethodClient client, int flags,
1707 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001708 if (!calledFromValidUser()) {
1709 return false;
1710 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001711 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 long ident = Binder.clearCallingIdentity();
1713 try {
1714 synchronized (mMethodMap) {
1715 if (mCurClient == null || client == null
1716 || mCurClient.client.asBinder() != client.asBinder()) {
1717 try {
1718 // We need to check if this is the current client with
1719 // focus in the window manager, to allow this call to
1720 // be made before input is started in it.
1721 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001722 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1723 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001724 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001728 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001729 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
Joe Onorato8a9b2202010-02-26 18:56:32 -08001733 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001734 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736 } finally {
1737 Binder.restoreCallingIdentity(ident);
1738 }
1739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
The Android Open Source Project4df24232009-03-05 14:34:35 -08001741 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1743 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001744 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001746 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001749 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001751 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001753 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1756 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1757 res = true;
1758 } else {
1759 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001761 if (mHaveConnection && mVisibleBound) {
1762 mContext.unbindService(mVisibleConnection);
1763 mVisibleBound = false;
1764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 mInputShown = false;
1766 mShowRequested = false;
1767 mShowExplicitlyRequested = false;
1768 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001771
satok42c5a162011-05-26 16:46:14 +09001772 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001773 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1774 int controlFlags, int softInputMode, int windowFlags,
1775 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001776 // Needs to check the validity before clearing calling identity
1777 final boolean calledFromValidUser = calledFromValidUser();
1778
Dianne Hackborn7663d802012-02-24 13:08:49 -08001779 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 long ident = Binder.clearCallingIdentity();
1781 try {
1782 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001783 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001784 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001786 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
Dianne Hackborn7663d802012-02-24 13:08:49 -08001788 ClientState cs = mClients.get(client.asBinder());
1789 if (cs == null) {
1790 throw new IllegalArgumentException("unknown client "
1791 + client.asBinder());
1792 }
1793
1794 try {
1795 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1796 // Check with the window manager to make sure this client actually
1797 // has a window with focus. If not, reject. This is thread safe
1798 // because if the focus changes some time before or after, the
1799 // next client receiving focus that has any interest in input will
1800 // be calling through here after that change happens.
1801 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1802 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1803 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001805 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001808 if (!calledFromValidUser) {
1809 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1810 Slog.w(TAG, "If you want to interect with IME, you need "
1811 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1812 hideCurrentInputLocked(0, null);
1813 return null;
1814 }
1815
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001816 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001817 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001818 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001819 if (attribute != null) {
1820 return startInputUncheckedLocked(cs, inputContext, attribute,
1821 controlFlags);
1822 }
1823 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001824 }
1825 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001827 // Should we auto-show the IME even if the caller has not
1828 // specified what should be done with it?
1829 // We only do this automatically if the window can resize
1830 // to accommodate the IME (so what the user sees will give
1831 // them good context without input information being obscured
1832 // by the IME) or if running on a large screen where there
1833 // is more room for the target window + IME.
1834 final boolean doAutoShow =
1835 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1836 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1837 || mRes.getConfiguration().isLayoutSizeAtLeast(
1838 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001839 final boolean isTextEditor =
1840 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1841
1842 // We want to start input before showing the IME, but after closing
1843 // it. We want to do this after closing it to help the IME disappear
1844 // more quickly (not get stuck behind it initializing itself for the
1845 // new focused input, even if its window wants to hide the IME).
1846 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1849 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001850 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1852 // There is no focus view, and this window will
1853 // be behind any soft input window, so hide the
1854 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001855 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001858 } else if (isTextEditor && doAutoShow && (softInputMode &
1859 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 // There is a focus view, and we are navigating forward
1861 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001862 // We only do this automatically if the window can resize
1863 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001864 // them good context without input information being obscured
1865 // by the IME) or if running on a large screen where there
1866 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001867 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001868 if (attribute != null) {
1869 res = startInputUncheckedLocked(cs, inputContext, attribute,
1870 controlFlags);
1871 didStart = true;
1872 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001873 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
1875 break;
1876 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1877 // Do nothing.
1878 break;
1879 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1880 if ((softInputMode &
1881 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001882 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001883 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 break;
1886 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001887 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001888 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 break;
1890 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1891 if ((softInputMode &
1892 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001893 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001894 if (attribute != null) {
1895 res = startInputUncheckedLocked(cs, inputContext, attribute,
1896 controlFlags);
1897 didStart = true;
1898 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001899 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
1901 break;
1902 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001903 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001904 if (attribute != null) {
1905 res = startInputUncheckedLocked(cs, inputContext, attribute,
1906 controlFlags);
1907 didStart = true;
1908 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001909 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 break;
1911 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001912
1913 if (!didStart && attribute != null) {
1914 res = startInputUncheckedLocked(cs, inputContext, attribute,
1915 controlFlags);
1916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918 } finally {
1919 Binder.restoreCallingIdentity(ident);
1920 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001921
1922 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001924
satok42c5a162011-05-26 16:46:14 +09001925 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001927 if (!calledFromValidUser()) {
1928 return;
1929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 synchronized (mMethodMap) {
1931 if (mCurClient == null || client == null
1932 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001933 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001934 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936
satok440aab52010-11-25 09:43:11 +09001937 // Always call subtype picker, because subtype picker is a superset of input method
1938 // picker.
satokab751aa2010-09-14 19:17:36 +09001939 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1940 }
1941 }
1942
satok42c5a162011-05-26 16:46:14 +09001943 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001945 if (!calledFromValidUser()) {
1946 return;
1947 }
satok28203512010-11-24 11:06:49 +09001948 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1949 }
1950
satok42c5a162011-05-26 16:46:14 +09001951 @Override
satok28203512010-11-24 11:06:49 +09001952 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001953 if (!calledFromValidUser()) {
1954 return;
1955 }
satok28203512010-11-24 11:06:49 +09001956 synchronized (mMethodMap) {
1957 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001958 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09001959 mMethodMap.get(id), subtype.hashCode()));
1960 } else {
1961 setInputMethod(token, id);
1962 }
1963 }
satokab751aa2010-09-14 19:17:36 +09001964 }
1965
satok42c5a162011-05-26 16:46:14 +09001966 @Override
satokb416a712010-11-25 20:42:14 +09001967 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001968 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001969 if (!calledFromValidUser()) {
1970 return;
1971 }
satokb416a712010-11-25 20:42:14 +09001972 synchronized (mMethodMap) {
1973 if (mCurClient == null || client == null
1974 || mCurClient.client.asBinder() != client.asBinder()) {
1975 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1976 }
satok7fee71f2010-12-17 18:54:26 +09001977 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1978 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001979 }
1980 }
1981
satok4fc87d62011-05-20 16:13:43 +09001982 @Override
satok735cf382010-11-11 20:40:09 +09001983 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001984 if (!calledFromValidUser()) {
1985 return false;
1986 }
satok735cf382010-11-11 20:40:09 +09001987 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001988 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001989 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001990 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001991 lastImi = mMethodMap.get(lastIme.first);
1992 } else {
1993 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001994 }
satok4fc87d62011-05-20 16:13:43 +09001995 String targetLastImiId = null;
1996 int subtypeId = NOT_A_SUBTYPE_ID;
1997 if (lastIme != null && lastImi != null) {
1998 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1999 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2000 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2001 : mCurrentSubtype.hashCode();
2002 // If the last IME is the same as the current IME and the last subtype is not
2003 // defined, there is no need to switch to the last IME.
2004 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2005 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002006 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002007 }
2008 }
2009
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002010 if (TextUtils.isEmpty(targetLastImiId)
2011 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002012 // This is a safety net. If the currentSubtype can't be added to the history
2013 // and the framework couldn't find the last ime, we will make the last ime be
2014 // the most applicable enabled keyboard subtype of the system imes.
2015 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2016 if (enabled != null) {
2017 final int N = enabled.size();
2018 final String locale = mCurrentSubtype == null
2019 ? mRes.getConfiguration().locale.toString()
2020 : mCurrentSubtype.getLocale();
2021 for (int i = 0; i < N; ++i) {
2022 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002023 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002024 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002025 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2026 InputMethodUtils.getSubtypes(imi),
2027 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002028 if (keyboardSubtype != null) {
2029 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002030 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002031 imi, keyboardSubtype.hashCode());
2032 if(keyboardSubtype.getLocale().equals(locale)) {
2033 break;
2034 }
2035 }
2036 }
2037 }
2038 }
2039 }
2040
2041 if (!TextUtils.isEmpty(targetLastImiId)) {
2042 if (DEBUG) {
2043 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2044 + ", from: " + mCurMethodId + ", " + subtypeId);
2045 }
2046 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2047 return true;
2048 } else {
2049 return false;
2050 }
satok735cf382010-11-11 20:40:09 +09002051 }
2052 }
2053
satoke7c6998e2011-06-03 17:57:59 +09002054 @Override
satok688bd472012-02-09 20:09:17 +09002055 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002056 if (!calledFromValidUser()) {
2057 return false;
2058 }
satok688bd472012-02-09 20:09:17 +09002059 synchronized (mMethodMap) {
2060 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2061 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2062 if (nextSubtype == null) {
2063 return false;
2064 }
2065 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2066 return true;
2067 }
2068 }
2069
2070 @Override
satok68f1b782011-04-11 14:26:04 +09002071 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002072 if (!calledFromValidUser()) {
2073 return null;
2074 }
satok68f1b782011-04-11 14:26:04 +09002075 synchronized (mMethodMap) {
2076 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2077 // TODO: Handle the case of the last IME with no subtypes
2078 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2079 || TextUtils.isEmpty(lastIme.second)) return null;
2080 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2081 if (lastImi == null) return null;
2082 try {
2083 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002084 final int lastSubtypeId =
2085 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002086 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2087 return null;
2088 }
2089 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002090 } catch (NumberFormatException e) {
2091 return null;
2092 }
2093 }
2094 }
2095
satoke7c6998e2011-06-03 17:57:59 +09002096 @Override
satokee5e77c2011-09-02 18:50:15 +09002097 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002098 if (!calledFromValidUser()) {
2099 return;
2100 }
satok91e88122011-07-18 11:11:42 +09002101 // By this IPC call, only a process which shares the same uid with the IME can add
2102 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002103 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002104 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002105 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002106 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002107 final String[] packageInfos;
2108 try {
2109 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2110 } catch (RemoteException e) {
2111 Slog.e(TAG, "Failed to get package infos");
2112 return;
2113 }
satok91e88122011-07-18 11:11:42 +09002114 if (packageInfos != null) {
2115 final int packageNum = packageInfos.length;
2116 for (int i = 0; i < packageNum; ++i) {
2117 if (packageInfos[i].equals(imi.getPackageName())) {
2118 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002119 final long ident = Binder.clearCallingIdentity();
2120 try {
2121 buildInputMethodListLocked(mMethodList, mMethodMap);
2122 } finally {
2123 Binder.restoreCallingIdentity(ident);
2124 }
satokee5e77c2011-09-02 18:50:15 +09002125 return;
satok91e88122011-07-18 11:11:42 +09002126 }
2127 }
2128 }
satoke7c6998e2011-06-03 17:57:59 +09002129 }
satokee5e77c2011-09-02 18:50:15 +09002130 return;
satoke7c6998e2011-06-03 17:57:59 +09002131 }
2132
satok28203512010-11-24 11:06:49 +09002133 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 synchronized (mMethodMap) {
2135 if (token == null) {
2136 if (mContext.checkCallingOrSelfPermission(
2137 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2138 != PackageManager.PERMISSION_GRANTED) {
2139 throw new SecurityException(
2140 "Using null token requires permission "
2141 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2142 }
2143 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002144 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2145 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 return;
2147 }
2148
satokc5933802011-08-31 21:26:04 +09002149 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 try {
satokab751aa2010-09-14 19:17:36 +09002151 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 } finally {
2153 Binder.restoreCallingIdentity(ident);
2154 }
2155 }
2156 }
2157
satok42c5a162011-05-26 16:46:14 +09002158 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002160 if (!calledFromValidUser()) {
2161 return;
2162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 synchronized (mMethodMap) {
2164 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002165 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2166 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 return;
2168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 long ident = Binder.clearCallingIdentity();
2170 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002171 hideCurrentInputLocked(flags, null);
2172 } finally {
2173 Binder.restoreCallingIdentity(ident);
2174 }
2175 }
2176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002177
satok42c5a162011-05-26 16:46:14 +09002178 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002179 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002180 if (!calledFromValidUser()) {
2181 return;
2182 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 synchronized (mMethodMap) {
2184 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002185 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2186 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 return;
2188 }
2189 long ident = Binder.clearCallingIdentity();
2190 try {
2191 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 } finally {
2193 Binder.restoreCallingIdentity(ident);
2194 }
2195 }
2196 }
2197
2198 void setEnabledSessionInMainThread(SessionState session) {
2199 if (mEnabledSession != session) {
2200 if (mEnabledSession != null) {
2201 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002202 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 mEnabledSession.method.setSessionEnabled(
2204 mEnabledSession.session, false);
2205 } catch (RemoteException e) {
2206 }
2207 }
2208 mEnabledSession = session;
2209 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002210 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 session.method.setSessionEnabled(
2212 session.session, true);
2213 } catch (RemoteException e) {
2214 }
2215 }
2216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
satok42c5a162011-05-26 16:46:14 +09002218 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002220 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 switch (msg.what) {
2222 case MSG_SHOW_IM_PICKER:
2223 showInputMethodMenu();
2224 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002225
satokab751aa2010-09-14 19:17:36 +09002226 case MSG_SHOW_IM_SUBTYPE_PICKER:
2227 showInputMethodSubtypeMenu();
2228 return true;
2229
satok47a44912010-10-06 16:03:58 +09002230 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002231 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002232 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002233 args.recycle();
satok217f5482010-12-15 05:19:19 +09002234 return true;
2235
2236 case MSG_SHOW_IM_CONFIG:
2237 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002238 return true;
2239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 case MSG_UNBIND_INPUT:
2243 try {
2244 ((IInputMethod)msg.obj).unbindInput();
2245 } catch (RemoteException e) {
2246 // There is nothing interesting about the method dying.
2247 }
2248 return true;
2249 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002250 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 try {
2252 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2253 } catch (RemoteException e) {
2254 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002255 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 return true;
2257 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002258 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002260 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2261 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 } catch (RemoteException e) {
2263 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002264 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 return true;
2266 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002267 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002269 ((IInputMethod)args.arg1).hideSoftInput(0,
2270 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 } catch (RemoteException e) {
2272 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002273 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 return true;
2275 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002276 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002278 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2280 } catch (RemoteException e) {
2281 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002282 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 return true;
2284 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002285 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 try {
2287 ((IInputMethod)args.arg1).createSession(
2288 (IInputMethodCallback)args.arg2);
2289 } catch (RemoteException e) {
2290 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002291 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002296 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 try {
2298 SessionState session = (SessionState)args.arg1;
2299 setEnabledSessionInMainThread(session);
2300 session.method.startInput((IInputContext)args.arg2,
2301 (EditorInfo)args.arg3);
2302 } catch (RemoteException e) {
2303 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002304 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 return true;
2306 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002307 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 try {
2309 SessionState session = (SessionState)args.arg1;
2310 setEnabledSessionInMainThread(session);
2311 session.method.restartInput((IInputContext)args.arg2,
2312 (EditorInfo)args.arg3);
2313 } catch (RemoteException e) {
2314 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002315 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 case MSG_UNBIND_METHOD:
2321 try {
2322 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2323 } catch (RemoteException e) {
2324 // There is nothing interesting about the last client dying.
2325 }
2326 return true;
2327 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002328 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 try {
2330 ((IInputMethodClient)args.arg1).onBindMethod(
2331 (InputBindResult)args.arg2);
2332 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002333 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002337 case MSG_SET_ACTIVE:
2338 try {
2339 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2340 } catch (RemoteException e) {
2341 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2342 + ((ClientState)msg.obj).pid + " uid "
2343 + ((ClientState)msg.obj).uid);
2344 }
2345 return true;
satok01038492012-04-09 21:08:27 +09002346
2347 // --------------------------------------------------------------
2348 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2349 mHardKeyboardListener.handleHardKeyboardStatusChange(
2350 msg.arg1 == 1, msg.arg2 == 1);
2351 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353 return false;
2354 }
2355
satokdc9ddae2011-10-06 12:22:36 +09002356 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002357 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2358 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002359 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002360 if (DEBUG) {
2361 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2362 }
satok723a27e2010-11-11 14:58:11 +09002363 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002364 return true;
2365 }
2366
2367 return false;
2368 }
2369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2371 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002372 if (DEBUG) {
2373 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 list.clear();
2376 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002377
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002378 // Use for queryIntentServicesAsUser
2379 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002380 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002381 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002383 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002385 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
satoke7c6998e2011-06-03 17:57:59 +09002387 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2388 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 for (int i = 0; i < services.size(); ++i) {
2390 ResolveInfo ri = services.get(i);
2391 ServiceInfo si = ri.serviceInfo;
2392 ComponentName compName = new ComponentName(si.packageName, si.name);
2393 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2394 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002395 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 + ": it does not require the permission "
2397 + android.Manifest.permission.BIND_INPUT_METHOD);
2398 continue;
2399 }
2400
Joe Onorato8a9b2202010-02-26 18:56:32 -08002401 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402
2403 try {
satoke7c6998e2011-06-03 17:57:59 +09002404 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002406 final String id = p.getId();
2407 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408
satok5b927c432012-05-01 20:09:34 +09002409 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002410 // by default
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002411 if (InputMethodUtils.isDefaultEnabledIme(mSystemReady, p, mContext)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002412 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002413 }
2414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002416 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002420 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002422 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 }
2424 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002425
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002427 if (!TextUtils.isEmpty(defaultImiId)) {
2428 if (!map.containsKey(defaultImiId)) {
2429 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2430 if (chooseNewDefaultIMELocked()) {
2431 updateFromSettingsLocked();
2432 }
2433 } else {
2434 // Double check that the default IME is certainly enabled.
2435 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002436 }
2437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002441
satokab751aa2010-09-14 19:17:36 +09002442 private void showInputMethodMenu() {
2443 showInputMethodMenuInternal(false);
2444 }
2445
2446 private void showInputMethodSubtypeMenu() {
2447 showInputMethodMenuInternal(true);
2448 }
2449
satok217f5482010-12-15 05:19:19 +09002450 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002451 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002452 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002453 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2454 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002455 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002456 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002457 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002458 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002459 }
2460
2461 private void showConfigureInputMethods() {
2462 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2463 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2464 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2465 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002466 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002467 }
2468
satok2c93efc2012-04-02 19:33:47 +09002469 private boolean isScreenLocked() {
2470 return mKeyguardManager != null
2471 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2472 }
satokab751aa2010-09-14 19:17:36 +09002473 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002474 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002477 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002478
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002479 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002480 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002481 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002482
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002483 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002484 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2485 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002486 if (immis == null || immis.size() == 0) {
2487 return;
2488 }
2489
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002490 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491
satok688bd472012-02-09 20:09:17 +09002492 final List<ImeSubtypeListItem> imList =
2493 mImListManager.getSortedInputMethodAndSubtypeList(
2494 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002495
satokc3690562012-01-10 20:14:43 +09002496 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002497 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002498 if (currentSubtype != null) {
2499 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002500 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2501 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002502 }
2503 }
2504
Ken Wakasa761eb372011-03-04 19:06:18 +09002505 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002506 mIms = new InputMethodInfo[N];
2507 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002508 int checkedItem = 0;
2509 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002510 final ImeSubtypeListItem item = imList.get(i);
2511 mIms[i] = item.mImi;
2512 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002513 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002514 int subtypeId = mSubtypeIds[i];
2515 if ((subtypeId == NOT_A_SUBTYPE_ID)
2516 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2517 || (subtypeId == lastInputMethodSubtypeId)) {
2518 checkedItem = i;
2519 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002522 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002523 com.android.internal.R.styleable.DialogPreference,
2524 com.android.internal.R.attr.alertDialogStyle, 0);
2525 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002526 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002527 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002528 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002530 }
2531 })
2532 .setIcon(a.getDrawable(
2533 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2534 a.recycle();
satok01038492012-04-09 21:08:27 +09002535 final LayoutInflater inflater =
2536 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2537 final View tv = inflater.inflate(
2538 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2539 mDialogBuilder.setCustomTitle(tv);
2540
2541 // Setup layout for a toggle switch of the hardware keyboard
2542 mSwitchingDialogTitleView = tv;
2543 mSwitchingDialogTitleView.findViewById(
2544 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2545 mWindowManagerService.isHardKeyboardAvailable() ?
2546 View.VISIBLE : View.GONE);
2547 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2548 com.android.internal.R.id.hard_keyboard_switch));
2549 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2550 hardKeySwitch.setOnCheckedChangeListener(
2551 new OnCheckedChangeListener() {
2552 @Override
2553 public void onCheckedChanged(
2554 CompoundButton buttonView, boolean isChecked) {
2555 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2556 }
2557 });
satokd87c2592010-09-29 11:52:06 +09002558
Ken Wakasa05dbb652011-08-22 15:22:43 +09002559 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2560 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2561 checkedItem);
2562
2563 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002564 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002565 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002566 public void onClick(DialogInterface dialog, int which) {
2567 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002568 if (mIms == null || mIms.length <= which
2569 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002570 return;
2571 }
2572 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002573 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002574 adapter.mCheckedItem = which;
2575 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002576 hideInputMethodMenu();
2577 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002578 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002579 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002580 subtypeId = NOT_A_SUBTYPE_ID;
2581 }
2582 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002583 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002586 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
satokbc81b692011-08-26 16:22:22 +09002588 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002589 mDialogBuilder.setPositiveButton(
2590 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002591 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002592 @Override
satok7f35c8c2010-10-07 21:13:11 +09002593 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002594 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002595 }
2596 });
2597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002599 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 mSwitchingDialog.getWindow().setType(
2601 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002602 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2603 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002604 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 mSwitchingDialog.show();
2606 }
2607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002608
satok93d744d2012-05-09 17:14:08 +09002609 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002610 public final CharSequence mImeName;
2611 public final CharSequence mSubtypeName;
2612 public final InputMethodInfo mImi;
2613 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002614 private final boolean mIsSystemLocale;
2615 private final boolean mIsSystemLanguage;
2616
Ken Wakasa05dbb652011-08-22 15:22:43 +09002617 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002618 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002619 mImeName = imeName;
2620 mSubtypeName = subtypeName;
2621 mImi = imi;
2622 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002623 if (TextUtils.isEmpty(subtypeLocale)) {
2624 mIsSystemLocale = false;
2625 mIsSystemLanguage = false;
2626 } else {
2627 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2628 mIsSystemLanguage = mIsSystemLocale
2629 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2630 }
2631 }
2632
2633 @Override
2634 public int compareTo(ImeSubtypeListItem other) {
2635 if (TextUtils.isEmpty(mImeName)) {
2636 return 1;
2637 }
2638 if (TextUtils.isEmpty(other.mImeName)) {
2639 return -1;
2640 }
2641 if (!TextUtils.equals(mImeName, other.mImeName)) {
2642 return mImeName.toString().compareTo(other.mImeName.toString());
2643 }
2644 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2645 return 0;
2646 }
2647 if (mIsSystemLocale) {
2648 return -1;
2649 }
2650 if (other.mIsSystemLocale) {
2651 return 1;
2652 }
2653 if (mIsSystemLanguage) {
2654 return -1;
2655 }
2656 if (other.mIsSystemLanguage) {
2657 return 1;
2658 }
2659 if (TextUtils.isEmpty(mSubtypeName)) {
2660 return 1;
2661 }
2662 if (TextUtils.isEmpty(other.mSubtypeName)) {
2663 return -1;
2664 }
2665 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002666 }
2667 }
2668
2669 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2670 private final LayoutInflater mInflater;
2671 private final int mTextViewResourceId;
2672 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002673 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002674 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2675 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2676 super(context, textViewResourceId, itemsList);
2677 mTextViewResourceId = textViewResourceId;
2678 mItemsList = itemsList;
2679 mCheckedItem = checkedItem;
2680 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2681 }
2682
2683 @Override
2684 public View getView(int position, View convertView, ViewGroup parent) {
2685 final View view = convertView != null ? convertView
2686 : mInflater.inflate(mTextViewResourceId, null);
2687 if (position < 0 || position >= mItemsList.size()) return view;
2688 final ImeSubtypeListItem item = mItemsList.get(position);
2689 final CharSequence imeName = item.mImeName;
2690 final CharSequence subtypeName = item.mSubtypeName;
2691 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2692 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2693 if (TextUtils.isEmpty(subtypeName)) {
2694 firstTextView.setText(imeName);
2695 secondTextView.setVisibility(View.GONE);
2696 } else {
2697 firstTextView.setText(subtypeName);
2698 secondTextView.setText(imeName);
2699 secondTextView.setVisibility(View.VISIBLE);
2700 }
2701 final RadioButton radioButton =
2702 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2703 radioButton.setChecked(position == mCheckedItem);
2704 return view;
2705 }
2706 }
2707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002709 synchronized (mMethodMap) {
2710 hideInputMethodMenuLocked();
2711 }
2712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002713
The Android Open Source Project10592532009-03-18 17:39:46 -07002714 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002715 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
The Android Open Source Project10592532009-03-18 17:39:46 -07002717 if (mSwitchingDialog != null) {
2718 mSwitchingDialog.dismiss();
2719 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002721
The Android Open Source Project10592532009-03-18 17:39:46 -07002722 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002723 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002727
satok42c5a162011-05-26 16:46:14 +09002728 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002730 // TODO: Make this work even for non-current users?
2731 if (!calledFromValidUser()) {
2732 return false;
2733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 synchronized (mMethodMap) {
2735 if (mContext.checkCallingOrSelfPermission(
2736 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2737 != PackageManager.PERMISSION_GRANTED) {
2738 throw new SecurityException(
2739 "Requires permission "
2740 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2741 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 long ident = Binder.clearCallingIdentity();
2744 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002745 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 } finally {
2747 Binder.restoreCallingIdentity(ident);
2748 }
2749 }
2750 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002751
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002752 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2753 // Make sure this is a valid input method.
2754 InputMethodInfo imm = mMethodMap.get(id);
2755 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002756 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002757 }
2758
satokd87c2592010-09-29 11:52:06 +09002759 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2760 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002761
satokd87c2592010-09-29 11:52:06 +09002762 if (enabled) {
2763 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2764 if (pair.first.equals(id)) {
2765 // We are enabling this input method, but it is already enabled.
2766 // Nothing to do. The previous state was enabled.
2767 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002768 }
2769 }
satokd87c2592010-09-29 11:52:06 +09002770 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2771 // Previous state was disabled.
2772 return false;
2773 } else {
2774 StringBuilder builder = new StringBuilder();
2775 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2776 builder, enabledInputMethodsList, id)) {
2777 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002778 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002779 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2780 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2781 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002782 }
2783 // Previous state was enabled.
2784 return true;
2785 } else {
2786 // We are disabling the input method but it is already disabled.
2787 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002788 return false;
2789 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002790 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002791 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002792
satok723a27e2010-11-11 14:58:11 +09002793 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2794 boolean setSubtypeOnly) {
2795 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002796 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002797
2798 // Set Subtype here
2799 if (imi == null || subtypeId < 0) {
2800 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002801 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002802 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002803 if (subtypeId < imi.getSubtypeCount()) {
2804 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2805 mSettings.putSelectedSubtype(subtype.hashCode());
2806 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002807 } else {
2808 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002809 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002810 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002811 }
satokab751aa2010-09-14 19:17:36 +09002812 }
satok723a27e2010-11-11 14:58:11 +09002813
satok4c0e7152012-06-20 20:08:44 +09002814 // Workaround.
2815 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2816 // IMEs are not recognized and considered uninstalled.
2817 // Actually, we can't move everything after SystemReady because
2818 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2819 // the default IME here and try cheking the default IME again in systemReady().
2820 // TODO: Do nothing before system ready and implement a separated logic for
2821 // the encryption lock screen.
2822 // TODO: ASEC should be ready before IMMS is instantiated.
2823 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002824 // Set InputMethod here
2825 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2826 }
2827 }
2828
2829 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2830 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2831 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2832 // newDefaultIme is empty when there is no candidate for the selected IME.
2833 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2834 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2835 if (subtypeHashCode != null) {
2836 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002837 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002838 imi, Integer.valueOf(subtypeHashCode));
2839 } catch (NumberFormatException e) {
2840 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2841 }
2842 }
2843 }
2844 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002845 }
2846
satok4e4569d2010-11-19 18:45:53 +09002847 // If there are no selected shortcuts, tries finding the most applicable ones.
2848 private Pair<InputMethodInfo, InputMethodSubtype>
2849 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2850 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2851 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002852 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002853 boolean foundInSystemIME = false;
2854
2855 // Search applicable subtype for each InputMethodInfo
2856 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002857 final String imiId = imi.getId();
2858 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2859 continue;
2860 }
satokcd7cd292010-11-20 15:46:23 +09002861 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002862 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002863 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002864 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002865 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002866 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002867 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002868 }
satokdf31ae62011-01-15 06:19:44 +09002869 // 2. Search by the system locale from enabledSubtypes.
2870 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002871 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002872 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002873 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002874 }
satoka86f5e42011-09-02 17:12:42 +09002875 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002876 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002877 final ArrayList<InputMethodSubtype> subtypesForSearch =
2878 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002879 ? InputMethodUtils.getSubtypes(imi)
2880 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002881 // 4. Search by the current subtype's locale from all subtypes.
2882 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002883 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002884 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002885 }
2886 // 5. Search by the system locale from all subtypes.
2887 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002888 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002889 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002890 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002891 }
satokcd7cd292010-11-20 15:46:23 +09002892 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002893 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002894 // The current input method is the most applicable IME.
2895 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002896 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002897 break;
satok7599a7f2010-12-22 13:45:23 +09002898 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002899 // The system input method is 2nd applicable IME.
2900 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002901 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002902 if ((imi.getServiceInfo().applicationInfo.flags
2903 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2904 foundInSystemIME = true;
2905 }
satok4e4569d2010-11-19 18:45:53 +09002906 }
2907 }
2908 }
2909 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002910 if (mostApplicableIMI != null) {
2911 Slog.w(TAG, "Most applicable shortcut input method was:"
2912 + mostApplicableIMI.getId());
2913 if (mostApplicableSubtype != null) {
2914 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2915 + "," + mostApplicableSubtype.getMode() + ","
2916 + mostApplicableSubtype.getLocale());
2917 }
2918 }
satok4e4569d2010-11-19 18:45:53 +09002919 }
satokcd7cd292010-11-20 15:46:23 +09002920 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002921 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002922 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002923 } else {
2924 return null;
2925 }
2926 }
2927
satokab751aa2010-09-14 19:17:36 +09002928 /**
2929 * @return Return the current subtype of this input method.
2930 */
satok42c5a162011-05-26 16:46:14 +09002931 @Override
satokab751aa2010-09-14 19:17:36 +09002932 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002933 // TODO: Make this work even for non-current users?
2934 if (!calledFromValidUser()) {
2935 return null;
2936 }
2937 synchronized (mMethodMap) {
2938 return getCurrentInputMethodSubtypeLocked();
2939 }
2940 }
2941
2942 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002943 if (mCurMethodId == null) {
2944 return null;
2945 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002946 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002947 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2948 if (imi == null || imi.getSubtypeCount() == 0) {
2949 return null;
satok4e4569d2010-11-19 18:45:53 +09002950 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002951 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002952 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2953 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002954 if (subtypeId == NOT_A_SUBTYPE_ID) {
2955 // If there are no selected subtypes, the framework will try to find
2956 // the most applicable subtype from explicitly or implicitly enabled
2957 // subtypes.
2958 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002959 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002960 // If there is only one explicitly or implicitly enabled subtype,
2961 // just returns it.
2962 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2963 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2964 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002965 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002966 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002967 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002968 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002969 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002970 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2971 true);
satok4e4569d2010-11-19 18:45:53 +09002972 }
satok3ef8b292010-11-23 06:06:29 +09002973 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002974 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002975 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09002976 }
2977 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002978 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09002979 }
2980
satokf3db1af2010-11-23 13:34:33 +09002981 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2982 InputMethodSubtype subtype) {
2983 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2984 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2985 } else {
2986 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2987 subtypes.add(subtype);
2988 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2989 }
2990 }
2991
satok4e4569d2010-11-19 18:45:53 +09002992 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002993 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002994 @Override
satok4e4569d2010-11-19 18:45:53 +09002995 public List getShortcutInputMethodsAndSubtypes() {
2996 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002997 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002998 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002999 // If there are no selected shortcut subtypes, the framework will try to find
3000 // the most applicable subtype from all subtypes whose mode is
3001 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003002 Pair<InputMethodInfo, InputMethodSubtype> info =
3003 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003004 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003005 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003006 ret.add(info.first);
3007 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003008 }
satok3da92232011-01-11 22:46:30 +09003009 return ret;
satokf3db1af2010-11-23 13:34:33 +09003010 }
satokf3db1af2010-11-23 13:34:33 +09003011 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3012 ret.add(imi);
3013 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3014 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003015 }
3016 }
satokf3db1af2010-11-23 13:34:33 +09003017 return ret;
satok4e4569d2010-11-19 18:45:53 +09003018 }
3019 }
3020
satok42c5a162011-05-26 16:46:14 +09003021 @Override
satokb66d2872010-11-10 01:04:04 +09003022 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003023 // TODO: Make this work even for non-current users?
3024 if (!calledFromValidUser()) {
3025 return false;
3026 }
satokb66d2872010-11-10 01:04:04 +09003027 synchronized (mMethodMap) {
3028 if (subtype != null && mCurMethodId != null) {
3029 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003030 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003031 if (subtypeId != NOT_A_SUBTYPE_ID) {
3032 setInputMethodLocked(mCurMethodId, subtypeId);
3033 return true;
3034 }
3035 }
3036 return false;
3037 }
3038 }
3039
satok688bd472012-02-09 20:09:17 +09003040 private static class InputMethodAndSubtypeListManager {
3041 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 // Used to load label
satok688bd472012-02-09 20:09:17 +09003043 private final PackageManager mPm;
3044 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003045 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003046 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3047 mContext = context;
3048 mPm = context.getPackageManager();
3049 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003050 final Locale locale = context.getResources().getConfiguration().locale;
3051 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003052 }
3053
3054 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3055 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3056 new Comparator<InputMethodInfo>() {
3057 @Override
3058 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3059 if (imi2 == null) return 0;
3060 if (imi1 == null) return 1;
3061 if (mPm == null) {
3062 return imi1.getId().compareTo(imi2.getId());
3063 }
3064 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3065 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3066 return imiId1.toString().compareTo(imiId2.toString());
3067 }
3068 });
3069
3070 public ImeSubtypeListItem getNextInputMethod(
3071 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3072 if (imi == null) {
3073 return null;
3074 }
3075 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3076 if (imList.size() <= 1) {
3077 return null;
3078 }
3079 final int N = imList.size();
3080 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003081 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003082 : NOT_A_SUBTYPE_ID;
3083 for (int i = 0; i < N; ++i) {
3084 final ImeSubtypeListItem isli = imList.get(i);
3085 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3086 if (!onlyCurrentIme) {
3087 return imList.get((i + 1) % N);
3088 }
3089 for (int j = 0; j < N - 1; ++j) {
3090 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3091 if (candidate.mImi.equals(imi)) {
3092 return candidate;
3093 }
3094 }
3095 return null;
3096 }
3097 }
3098 return null;
3099 }
3100
3101 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3102 return getSortedInputMethodAndSubtypeList(true, false, false);
3103 }
3104
3105 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3106 boolean inputShown, boolean isScreenLocked) {
3107 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3108 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3109 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3110 if (immis == null || immis.size() == 0) {
3111 return Collections.emptyList();
3112 }
3113 mSortedImmis.clear();
3114 mSortedImmis.putAll(immis);
3115 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3116 if (imi == null) continue;
3117 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3118 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3119 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3120 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3121 }
satok688bd472012-02-09 20:09:17 +09003122 final CharSequence imeLabel = imi.loadLabel(mPm);
3123 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3124 final int subtypeCount = imi.getSubtypeCount();
3125 if (DEBUG) {
3126 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3127 }
3128 for (int j = 0; j < subtypeCount; ++j) {
3129 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3130 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3131 // We show all enabled IMEs and subtypes when an IME is shown.
3132 if (enabledSubtypeSet.contains(subtypeHashCode)
3133 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3134 final CharSequence subtypeLabel =
3135 subtype.overridesImplicitlyEnabledSubtype() ? null
3136 : subtype.getDisplayName(mContext, imi.getPackageName(),
3137 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003138 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3139 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003140
3141 // Removing this subtype from enabledSubtypeSet because we no longer
3142 // need to add an entry of this subtype to imList to avoid duplicated
3143 // entries.
3144 enabledSubtypeSet.remove(subtypeHashCode);
3145 }
3146 }
3147 } else {
satok93d744d2012-05-09 17:14:08 +09003148 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3149 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003150 }
3151 }
satok93d744d2012-05-09 17:14:08 +09003152 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003153 return imList;
3154 }
3155 }
3156
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003157 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003158 private static class InputMethodFileManager {
3159 private static final String SYSTEM_PATH = "system";
3160 private static final String INPUT_METHOD_PATH = "inputmethod";
3161 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3162 private static final String NODE_SUBTYPES = "subtypes";
3163 private static final String NODE_SUBTYPE = "subtype";
3164 private static final String NODE_IMI = "imi";
3165 private static final String ATTR_ID = "id";
3166 private static final String ATTR_LABEL = "label";
3167 private static final String ATTR_ICON = "icon";
3168 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3169 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3170 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3171 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3172 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3173 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003174 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003175 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003176 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003177 if (methodMap == null) {
3178 throw new NullPointerException("methodMap is null");
3179 }
3180 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003181 final File systemDir = userId == UserHandle.USER_OWNER
3182 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3183 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003184 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3185 if (!inputMethodDir.mkdirs()) {
3186 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3187 }
3188 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3189 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3190 if (!subtypeFile.exists()) {
3191 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003192 writeAdditionalInputMethodSubtypes(
3193 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003194 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003195 readAdditionalInputMethodSubtypes(
3196 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003197 }
3198 }
3199
3200 private void deleteAllInputMethodSubtypes(String imiId) {
3201 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003202 mAdditionalSubtypesMap.remove(imiId);
3203 writeAdditionalInputMethodSubtypes(
3204 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003205 }
3206 }
3207
3208 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003209 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003210 synchronized (mMethodMap) {
3211 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3212 final int N = additionalSubtypes.length;
3213 for (int i = 0; i < N; ++i) {
3214 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003215 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003216 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003217 } else {
3218 Slog.w(TAG, "Duplicated subtype definition found: "
3219 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003220 }
3221 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003222 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3223 writeAdditionalInputMethodSubtypes(
3224 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003225 }
3226 }
3227
3228 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3229 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003230 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003231 }
3232 }
3233
3234 private static void writeAdditionalInputMethodSubtypes(
3235 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3236 HashMap<String, InputMethodInfo> methodMap) {
3237 // Safety net for the case that this function is called before methodMap is set.
3238 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3239 FileOutputStream fos = null;
3240 try {
3241 fos = subtypesFile.startWrite();
3242 final XmlSerializer out = new FastXmlSerializer();
3243 out.setOutput(fos, "utf-8");
3244 out.startDocument(null, true);
3245 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3246 out.startTag(null, NODE_SUBTYPES);
3247 for (String imiId : allSubtypes.keySet()) {
3248 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3249 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3250 continue;
3251 }
3252 out.startTag(null, NODE_IMI);
3253 out.attribute(null, ATTR_ID, imiId);
3254 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3255 final int N = subtypesList.size();
3256 for (int i = 0; i < N; ++i) {
3257 final InputMethodSubtype subtype = subtypesList.get(i);
3258 out.startTag(null, NODE_SUBTYPE);
3259 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3260 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3261 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3262 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3263 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3264 out.attribute(null, ATTR_IS_AUXILIARY,
3265 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3266 out.endTag(null, NODE_SUBTYPE);
3267 }
3268 out.endTag(null, NODE_IMI);
3269 }
3270 out.endTag(null, NODE_SUBTYPES);
3271 out.endDocument();
3272 subtypesFile.finishWrite(fos);
3273 } catch (java.io.IOException e) {
3274 Slog.w(TAG, "Error writing subtypes", e);
3275 if (fos != null) {
3276 subtypesFile.failWrite(fos);
3277 }
3278 }
3279 }
3280
3281 private static void readAdditionalInputMethodSubtypes(
3282 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3283 if (allSubtypes == null || subtypesFile == null) return;
3284 allSubtypes.clear();
3285 FileInputStream fis = null;
3286 try {
3287 fis = subtypesFile.openRead();
3288 final XmlPullParser parser = Xml.newPullParser();
3289 parser.setInput(fis, null);
3290 int type = parser.getEventType();
3291 // Skip parsing until START_TAG
3292 while ((type = parser.next()) != XmlPullParser.START_TAG
3293 && type != XmlPullParser.END_DOCUMENT) {}
3294 String firstNodeName = parser.getName();
3295 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3296 throw new XmlPullParserException("Xml doesn't start with subtypes");
3297 }
3298 final int depth =parser.getDepth();
3299 String currentImiId = null;
3300 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3301 while (((type = parser.next()) != XmlPullParser.END_TAG
3302 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3303 if (type != XmlPullParser.START_TAG)
3304 continue;
3305 final String nodeName = parser.getName();
3306 if (NODE_IMI.equals(nodeName)) {
3307 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3308 if (TextUtils.isEmpty(currentImiId)) {
3309 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3310 continue;
3311 }
3312 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3313 allSubtypes.put(currentImiId, tempSubtypesArray);
3314 } else if (NODE_SUBTYPE.equals(nodeName)) {
3315 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3316 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3317 continue;
3318 }
3319 final int icon = Integer.valueOf(
3320 parser.getAttributeValue(null, ATTR_ICON));
3321 final int label = Integer.valueOf(
3322 parser.getAttributeValue(null, ATTR_LABEL));
3323 final String imeSubtypeLocale =
3324 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3325 final String imeSubtypeMode =
3326 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3327 final String imeSubtypeExtraValue =
3328 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003329 final boolean isAuxiliary = "1".equals(String.valueOf(
3330 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003331 final InputMethodSubtype subtype =
3332 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3333 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3334 tempSubtypesArray.add(subtype);
3335 }
3336 }
3337 } catch (XmlPullParserException e) {
3338 Slog.w(TAG, "Error reading subtypes: " + e);
3339 return;
3340 } catch (java.io.IOException e) {
3341 Slog.w(TAG, "Error reading subtypes: " + e);
3342 return;
3343 } catch (NumberFormatException e) {
3344 Slog.w(TAG, "Error reading subtypes: " + e);
3345 return;
3346 } finally {
3347 if (fis != null) {
3348 try {
3349 fis.close();
3350 } catch (java.io.IOException e1) {
3351 Slog.w(TAG, "Failed to close.");
3352 }
3353 }
3354 }
3355 }
3356 }
3357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003359 // Utilities for debug
3360 private static String getStackTrace() {
3361 final StringBuilder sb = new StringBuilder();
3362 try {
3363 throw new RuntimeException();
3364 } catch (RuntimeException e) {
3365 final StackTraceElement[] frames = e.getStackTrace();
3366 // Start at 1 because the first frame is here and we don't care about it
3367 for (int j = 1; j < frames.length; ++j) {
3368 sb.append(frames[j].toString() + "\n");
3369 }
3370 }
3371 return sb.toString();
3372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 @Override
3375 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3376 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3377 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3380 + Binder.getCallingPid()
3381 + ", uid=" + Binder.getCallingUid());
3382 return;
3383 }
3384
3385 IInputMethod method;
3386 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 synchronized (mMethodMap) {
3391 p.println("Current Input Method Manager state:");
3392 int N = mMethodList.size();
3393 p.println(" Input Methods:");
3394 for (int i=0; i<N; i++) {
3395 InputMethodInfo info = mMethodList.get(i);
3396 p.println(" InputMethod #" + i + ":");
3397 info.dump(p, " ");
3398 }
3399 p.println(" Clients:");
3400 for (ClientState ci : mClients.values()) {
3401 p.println(" Client " + ci + ":");
3402 p.println(" client=" + ci.client);
3403 p.println(" inputContext=" + ci.inputContext);
3404 p.println(" sessionRequested=" + ci.sessionRequested);
3405 p.println(" curSession=" + ci.curSession);
3406 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003407 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003409 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3410 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3412 + " mBoundToMethod=" + mBoundToMethod);
3413 p.println(" mCurToken=" + mCurToken);
3414 p.println(" mCurIntent=" + mCurIntent);
3415 method = mCurMethod;
3416 p.println(" mCurMethod=" + mCurMethod);
3417 p.println(" mEnabledSession=" + mEnabledSession);
3418 p.println(" mShowRequested=" + mShowRequested
3419 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3420 + " mShowForced=" + mShowForced
3421 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003422 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003424
Jeff Brownb88102f2010-09-08 11:49:43 -07003425 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 pw.flush();
3428 try {
3429 client.client.asBinder().dump(fd, args);
3430 } catch (RemoteException e) {
3431 p.println("Input method client dead: " + e);
3432 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003433 } else {
3434 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003436
Jeff Brownb88102f2010-09-08 11:49:43 -07003437 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 pw.flush();
3440 try {
3441 method.asBinder().dump(fd, args);
3442 } catch (RemoteException e) {
3443 p.println("Input method service dead: " + e);
3444 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003445 } else {
3446 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 }
3448 }
3449}