blob: 18a2b2b9aa5d7edf4326b98dcff645880fbfc8c5 [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 {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800385 String mLastEnabled = "";
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 SettingsObserver(Handler handler) {
388 super(handler);
389 ContentResolver resolver = mContext.getContentResolver();
390 resolver.registerContentObserver(Settings.Secure.getUriFor(
391 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900392 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900393 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
394 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900395 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override public void onChange(boolean selfChange) {
399 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800400 boolean enabledChanged = false;
401 String newEnabled = mSettings.getEnabledInputMethodsStr();
402 if (!mLastEnabled.equals(newEnabled)) {
403 mLastEnabled = newEnabled;
404 enabledChanged = true;
405 }
406 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408 }
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900411 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
412 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700414 if (mCurClient != null && mCurClient.client != null) {
415 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
416 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900419
420 @Override
421 public void onReceive(Context context, Intent intent) {
422 final String action = intent.getAction();
423 if (Intent.ACTION_SCREEN_ON.equals(action)) {
424 mScreenOn = true;
425 refreshImeWindowVisibilityLocked();
426 updateActive();
427 return;
428 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
429 mScreenOn = false;
430 setImeWindowVisibilityStatusHiddenLocked();
431 updateActive();
432 return;
433 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
434 hideInputMethodMenu();
435 // No need to updateActive
436 return;
437 } else {
438 Slog.w(TAG, "Unexpected intent " + intent);
439 }
440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900444 private boolean isChangingPackagesOfCurrentUser() {
445 final int userId = getChangingUserId();
446 final boolean retval = userId == mSettings.getCurrentUserId();
447 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900448 if (!retval) {
449 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
450 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 }
452 return retval;
453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 if (!isChangingPackagesOfCurrentUser()) {
458 return false;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900461 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final int N = mMethodList.size();
463 if (curInputMethodId != null) {
464 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 InputMethodInfo imi = mMethodList.get(i);
466 if (imi.getId().equals(curInputMethodId)) {
467 for (String pkg : packages) {
468 if (imi.getPackageName().equals(pkg)) {
469 if (!doit) {
470 return true;
471 }
satok723a27e2010-11-11 14:58:11 +0900472 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 chooseNewDefaultIMELocked();
474 return true;
475 }
476 }
477 }
478 }
479 }
480 }
481 return false;
482 }
483
484 @Override
485 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900486 if (!isChangingPackagesOfCurrentUser()) {
487 return;
488 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 synchronized (mMethodMap) {
490 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800492 final int N = mMethodList.size();
493 if (curInputMethodId != null) {
494 for (int i=0; i<N; i++) {
495 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900496 final String imiId = imi.getId();
497 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 curIm = imi;
499 }
satoke7c6998e2011-06-03 17:57:59 +0900500
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900502 if (isPackageModified(imi.getPackageName())) {
503 mFileManager.deleteAllInputMethodSubtypes(imiId);
504 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 if (change == PACKAGE_TEMPORARY_CHANGE
506 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 + imi.getComponent());
509 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900514 buildInputMethodListLocked(
515 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800520 int change = isPackageDisappearing(curIm.getPackageName());
521 if (change == PACKAGE_TEMPORARY_CHANGE
522 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 ServiceInfo si = null;
524 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900525 si = mIPackageManager.getServiceInfo(
526 curIm.getComponent(), 0, mSettings.getCurrentUserId());
527 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 }
529 if (si == null) {
530 // Uh oh, current input method is no longer around!
531 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900533 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 changed = true;
536 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900538 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 }
540 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 }
satokab751aa2010-09-14 19:17:36 +0900543
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800544 if (curIm == null) {
545 // We currently don't have a default input method... is
546 // one now available?
547 changed = chooseNewDefaultIMELocked();
548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800551 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553 }
554 }
555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Jean Chalarde0d32a62011-10-20 20:36:07 +0900557 private static class MethodCallback extends IInputMethodCallback.Stub {
558 private final IInputMethod mMethod;
559 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
Jean Chalarde0d32a62011-10-20 20:36:07 +0900561 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900563 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
satoke7c6998e2011-06-03 17:57:59 +0900566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void finishedEvent(int seq, boolean handled) throws RemoteException {
568 }
569
satoke7c6998e2011-06-03 17:57:59 +0900570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900572 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800575
satok01038492012-04-09 21:08:27 +0900576 private class HardKeyboardListener
577 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
578 @Override
579 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
580 mHandler.sendMessage(mHandler.obtainMessage(
581 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
582 }
583
584 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
585 if (DEBUG) {
586 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
587 + enabled);
588 }
589 synchronized(mMethodMap) {
590 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
591 && mSwitchingDialog.isShowing()) {
592 mSwitchingDialogTitleView.findViewById(
593 com.android.internal.R.id.hard_keyboard_section).setVisibility(
594 available ? View.VISIBLE : View.GONE);
595 }
596 }
597 }
598 }
599
600 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900601 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800603 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 mHandler = new Handler(this);
605 mIWindowManager = IWindowManager.Stub.asInterface(
606 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800607 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900608 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public void executeMessage(Message msg) {
610 handleMessage(msg);
611 }
Mita Yuned218c72012-12-06 17:18:25 -0800612 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900613 mWindowManagerService = windowManager;
614 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900615
satok7cfc0ed2011-06-20 21:29:36 +0900616 mImeSwitcherNotification = new Notification();
617 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
618 mImeSwitcherNotification.when = 0;
619 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
620 mImeSwitcherNotification.tickerText = null;
621 mImeSwitcherNotification.defaults = 0; // please be quiet
622 mImeSwitcherNotification.sound = null;
623 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400624
625 // Tag this notification specially so SystemUI knows it's important
626 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
627
satok7cfc0ed2011-06-20 21:29:36 +0900628 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900629 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900630
631 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900632
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 final IntentFilter broadcastFilter = new IntentFilter();
634 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
635 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
636 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
637 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
satok7cfc0ed2011-06-20 21:29:36 +0900639 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 int userId = 0;
641 try {
642 ActivityManagerNative.getDefault().registerUserSwitchObserver(
643 new IUserSwitchObserver.Stub() {
644 @Override
645 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900646 synchronized(mMethodMap) {
647 switchUserLocked(newUserId);
648 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900649 if (reply != null) {
650 try {
651 reply.sendResult(null);
652 } catch (RemoteException e) {
653 }
654 }
655 }
656
657 @Override
658 public void onUserSwitchComplete(int newUserId) throws RemoteException {
659 }
660 });
661 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
662 } catch (RemoteException e) {
663 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
664 }
satok81f8b7c2012-12-04 20:42:56 +0900665 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900666
satokd87c2592010-09-29 11:52:06 +0900667 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900668 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900670 mFileManager = new InputMethodFileManager(mMethodMap, userId);
671 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900672
673 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900675 if (DEBUG) {
676 Slog.d(TAG, "Initial default ime = " + defaultImiId);
677 }
satok0a1bcf42012-05-16 19:26:31 +0900678 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
679
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900680 buildInputMethodListLocked(mMethodList, mMethodMap,
681 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900682 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
satok0a1bcf42012-05-16 19:26:31 +0900684 if (!mImeSelectedOnBoot) {
685 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900686 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800690 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900691
692 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
693 // according to the new system locale.
694 final IntentFilter filter = new IntentFilter();
695 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
696 mContext.registerReceiver(
697 new BroadcastReceiver() {
698 @Override
699 public void onReceive(Context context, Intent intent) {
700 synchronized(mMethodMap) {
701 checkCurrentLocaleChangedLocked();
702 }
703 }
704 }, filter);
705 }
706
satok5b927c432012-05-01 20:09:34 +0900707 private void resetDefaultImeLocked(Context context) {
708 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900709 if (mCurMethodId != null
710 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900711 return;
712 }
713
714 InputMethodInfo defIm = null;
715 for (InputMethodInfo imi : mMethodList) {
716 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900717 if (InputMethodUtils.isValidSystemDefaultIme(
718 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900719 defIm = imi;
720 Slog.i(TAG, "Selected default: " + imi.getId());
721 }
722 }
723 }
724 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900725 defIm = InputMethodUtils.getMostApplicableDefaultIME(
726 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900727 Slog.i(TAG, "No default found, using " + defIm.getId());
728 }
729 if (defIm != null) {
730 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
731 }
732 }
733
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900734 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
735 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 if (!mSystemReady) {
737 // not system ready
738 return;
739 }
740 final Locale newLocale = mRes.getConfiguration().locale;
741 if (!updateOnlyWhenLocaleChanged
742 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
743 if (!updateOnlyWhenLocaleChanged) {
744 hideCurrentInputLocked(0, null);
745 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700746 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900747 }
748 if (DEBUG) {
749 Slog.i(TAG, "Locale has been changed to " + newLocale);
750 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900751 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
752 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900753 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 if (!updateOnlyWhenLocaleChanged) {
755 final String selectedImiId = mSettings.getSelectedInputMethod();
756 if (TextUtils.isEmpty(selectedImiId)) {
757 // This is the first time of the user switch and
758 // set the current ime to the proper one.
759 resetDefaultImeLocked(mContext);
760 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900761 } else {
762 // If the locale is changed, needs to reset the default ime
763 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900764 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800765 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900766 mLastSystemLocale = newLocale;
767 if (!updateOnlyWhenLocaleChanged) {
768 try {
769 startInputInnerLocked();
770 } catch (RuntimeException e) {
771 Slog.w(TAG, "Unexpected exception", e);
772 }
773 }
774 }
775 }
776
777 private void checkCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900778 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
779 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 }
781
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900782 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900783 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900784 // InputMethodFileManager should be reset when the user is changed
785 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900786 final String defaultImiId = mSettings.getSelectedInputMethod();
787 final boolean needsToResetDefaultIme = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900788 // For secondary users, the list of enabled IMEs may not have been updated since the
789 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
790 // not be empty even if the IME has been uninstalled by the primary user.
791 // Even in such cases, IMMS works fine because it will find the most applicable
792 // IME for that user.
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900793 if (DEBUG) {
794 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
795 }
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900796 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900797 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 }
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 @Override
801 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
802 throws RemoteException {
803 try {
804 return super.onTransact(code, data, reply, flags);
805 } catch (RuntimeException e) {
806 // The input method manager only throws security exceptions, so let's
807 // log all others.
808 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811 throw e;
812 }
813 }
814
Dianne Hackborn661cd522011-08-22 00:26:20 -0700815 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700816 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 if (DEBUG) {
818 Slog.d(TAG, "--- systemReady");
819 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700820 if (!mSystemReady) {
821 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900822 mKeyguardManager =
823 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700824 mNotificationManager = (NotificationManager)
825 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
826 mStatusBar = statusBar;
827 statusBar.setIconVisibility("ime", false);
828 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900829 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
830 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900831 if (mShowOngoingImeSwitcherForPhones) {
832 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
833 mHardKeyboardListener);
834 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900835 buildInputMethodListLocked(mMethodList, mMethodMap,
836 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900837 if (!mImeSelectedOnBoot) {
838 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
839 checkCurrentLocaleChangedLocked();
840 }
841 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700842 try {
843 startInputInnerLocked();
844 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700846 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700847 }
848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800850
satok15452a42011-10-28 17:58:28 +0900851 private void setImeWindowVisibilityStatusHiddenLocked() {
852 mImeWindowVis = 0;
853 updateImeWindowStatusLocked();
854 }
855
satok3afd6c02011-11-18 08:38:19 +0900856 private void refreshImeWindowVisibilityLocked() {
857 final Configuration conf = mRes.getConfiguration();
858 final boolean haveHardKeyboard = conf.keyboard
859 != Configuration.KEYBOARD_NOKEYS;
860 final boolean hardKeyShown = haveHardKeyboard
861 && conf.hardKeyboardHidden
862 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500863 final boolean isScreenLocked =
864 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
865 final boolean isScreenSecurelyLocked =
866 isScreenLocked && mKeyguardManager.isKeyguardSecure();
867 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
868 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900869 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
870 updateImeWindowStatusLocked();
871 }
872
satok15452a42011-10-28 17:58:28 +0900873 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900874 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700875 }
876
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900877 // ---------------------------------------------------------------------------------------
878 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
879 // 1) it comes from the system process
880 // 2) the calling process' user id is identical to the current user id IMMS thinks.
881 private boolean calledFromValidUser() {
882 final int uid = Binder.getCallingUid();
883 final int userId = UserHandle.getUserId(uid);
884 if (DEBUG) {
885 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
886 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
887 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900888 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900889 }
890 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
891 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900893
894 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
895 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
896 // must not manage background users' states in any functions.
897 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
898 // by a token.
899 if (mContext.checkCallingOrSelfPermission(
900 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
901 == PackageManager.PERMISSION_GRANTED) {
902 if (DEBUG) {
903 Slog.d(TAG, "--- Access granted because the calling process has "
904 + "the INTERACT_ACROSS_USERS_FULL permission");
905 }
906 return true;
907 }
908 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
909 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900910 }
911
912 private boolean bindCurrentInputMethodService(
913 Intent service, ServiceConnection conn, int flags) {
914 if (service == null || conn == null) {
915 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
916 return false;
917 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800918 return mContext.bindServiceAsUser(service, conn, flags,
919 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900920 }
921
satoke7c6998e2011-06-03 17:57:59 +0900922 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900924 // TODO: Make this work even for non-current users?
925 if (!calledFromValidUser()) {
926 return Collections.emptyList();
927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 synchronized (mMethodMap) {
929 return new ArrayList<InputMethodInfo>(mMethodList);
930 }
931 }
932
satoke7c6998e2011-06-03 17:57:59 +0900933 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 // TODO: Make this work even for non-current users?
936 if (!calledFromValidUser()) {
937 return Collections.emptyList();
938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900940 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942 }
943
satokbb4aa062011-01-19 21:40:27 +0900944 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
945 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
946 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
947 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900949 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900950 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900951 }
952 return enabledInputMethodAndSubtypes;
953 }
954
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900955 /**
956 * @param imi if null, returns enabled subtypes for the current imi
957 * @return enabled subtypes of the specified imi
958 */
satoke7c6998e2011-06-03 17:57:59 +0900959 @Override
satok16331c82010-12-20 23:48:46 +0900960 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
961 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 // TODO: Make this work even for non-current users?
963 if (!calledFromValidUser()) {
964 return Collections.emptyList();
965 }
satok67ddf9c2010-11-17 09:45:54 +0900966 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900967 if (imi == null && mCurMethodId != null) {
968 imi = mMethodMap.get(mCurMethodId);
969 }
970 return mSettings.getEnabledInputMethodSubtypeListLocked(
971 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900972 }
973 }
974
satoke7c6998e2011-06-03 17:57:59 +0900975 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 public void addClient(IInputMethodClient client,
977 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 if (!calledFromValidUser()) {
979 return;
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 synchronized (mMethodMap) {
982 mClients.put(client.asBinder(), new ClientState(client,
983 inputContext, uid, pid));
984 }
985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
satoke7c6998e2011-06-03 17:57:59 +0900987 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 if (!calledFromValidUser()) {
990 return;
991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 synchronized (mMethodMap) {
993 mClients.remove(client.asBinder());
994 }
995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 void executeOrSendMessage(IInterface target, Message msg) {
998 if (target.asBinder() instanceof Binder) {
999 mCaller.sendMessage(msg);
1000 } else {
1001 handleMessage(msg);
1002 msg.recycle();
1003 }
1004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001006 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001008 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 + mCurClient.client.asBinder());
1010 if (mBoundToMethod) {
1011 mBoundToMethod = false;
1012 if (mCurMethod != null) {
1013 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1014 MSG_UNBIND_INPUT, mCurMethod));
1015 }
1016 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001017
1018 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1019 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1021 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1022 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
The Android Open Source Project10592532009-03-18 17:39:46 -07001025 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 private int getImeShowFlags() {
1030 int flags = 0;
1031 if (mShowForced) {
1032 flags |= InputMethod.SHOW_FORCED
1033 | InputMethod.SHOW_EXPLICIT;
1034 } else if (mShowExplicitlyRequested) {
1035 flags |= InputMethod.SHOW_EXPLICIT;
1036 }
1037 return flags;
1038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 private int getAppShowFlags() {
1041 int flags = 0;
1042 if (mShowForced) {
1043 flags |= InputMethodManager.SHOW_FORCED;
1044 } else if (!mShowExplicitlyRequested) {
1045 flags |= InputMethodManager.SHOW_IMPLICIT;
1046 }
1047 return flags;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
Dianne Hackborn7663d802012-02-24 13:08:49 -08001050 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 if (!mBoundToMethod) {
1052 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1053 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1054 mBoundToMethod = true;
1055 }
1056 final SessionState session = mCurClient.curSession;
1057 if (initial) {
1058 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1059 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1060 } else {
1061 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1062 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1063 }
1064 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001065 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001066 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001068 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001072 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 // If no method is currently selected, do nothing.
1074 if (mCurMethodId == null) {
1075 return mNoBinding;
1076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 ClientState cs = mClients.get(client.asBinder());
1079 if (cs == null) {
1080 throw new IllegalArgumentException("unknown client "
1081 + client.asBinder());
1082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 try {
1085 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1086 // Check with the window manager to make sure this client actually
1087 // has a window with focus. If not, reject. This is thread safe
1088 // because if the focus changes some time before or after, the
1089 // next client receiving focus that has any interest in input will
1090 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001091 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1093 return null;
1094 }
1095 } catch (RemoteException e) {
1096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001097
Dianne Hackborn7663d802012-02-24 13:08:49 -08001098 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1099 }
1100
1101 InputBindResult startInputUncheckedLocked(ClientState cs,
1102 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1103 // If no method is currently selected, do nothing.
1104 if (mCurMethodId == null) {
1105 return mNoBinding;
1106 }
1107
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001108 if (mCurClient == null) {
1109 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1110 if (DEBUG) {
1111 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1112 }
1113 }
1114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 if (mCurClient != cs) {
1116 // If the client is changing, we need to switch over to the new
1117 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001118 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 + cs.client.asBinder());
1121
1122 // If the screen is on, inform the new client it is active
1123 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001124 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1125 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 // Bump up the sequence for this client and attach it.
1130 mCurSeq++;
1131 if (mCurSeq <= 0) mCurSeq = 1;
1132 mCurClient = cs;
1133 mCurInputContext = inputContext;
1134 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 // Check if the input method is changing.
1137 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1138 if (cs.curSession != null) {
1139 // Fast case: if we are already connected to the input method,
1140 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001141 return attachNewInputLocked(
1142 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144 if (mHaveConnection) {
1145 if (mCurMethod != null) {
1146 if (!cs.sessionRequested) {
1147 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001148 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1150 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001151 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 // Return to client, and we will get back with it when
1154 // we have had a session made for it.
1155 return new InputBindResult(null, mCurId, mCurSeq);
1156 } else if (SystemClock.uptimeMillis()
1157 < (mLastBindTime+TIME_TO_RECONNECT)) {
1158 // In this case we have connected to the service, but
1159 // don't yet have its interface. If it hasn't been too
1160 // long since we did the connection, we'll return to
1161 // the client and wait to get the service interface so
1162 // we can report back. If it has been too long, we want
1163 // to fall through so we can try a disconnect/reconnect
1164 // to see if we can get back in touch with the service.
1165 return new InputBindResult(null, mCurId, mCurSeq);
1166 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1168 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001173 return startInputInnerLocked();
1174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001176 InputBindResult startInputInnerLocked() {
1177 if (mCurMethodId == null) {
1178 return mNoBinding;
1179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001181 if (!mSystemReady) {
1182 // If the system is not yet ready, we shouldn't be running third
1183 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001184 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1188 if (info == null) {
1189 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001192 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1195 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001196 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1197 com.android.internal.R.string.input_method_binding_label);
1198 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1199 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001200 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001201 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 mLastBindTime = SystemClock.uptimeMillis();
1203 mHaveConnection = true;
1204 mCurId = info.getId();
1205 mCurToken = new Binder();
1206 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 mIWindowManager.addWindowToken(mCurToken,
1209 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1210 } catch (RemoteException e) {
1211 }
1212 return new InputBindResult(null, mCurId, mCurSeq);
1213 } else {
1214 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 + mCurIntent);
1217 }
1218 return null;
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001223 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001224 if (!calledFromValidUser()) {
1225 return null;
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 synchronized (mMethodMap) {
1228 final long ident = Binder.clearCallingIdentity();
1229 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001230 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 } finally {
1232 Binder.restoreCallingIdentity(ident);
1233 }
1234 }
1235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236
satoke7c6998e2011-06-03 17:57:59 +09001237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 public void finishInput(IInputMethodClient client) {
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
satoke7c6998e2011-06-03 17:57:59 +09001241 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 public void onServiceConnected(ComponentName name, IBinder service) {
1243 synchronized (mMethodMap) {
1244 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1245 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001246 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001247 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001248 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001249 return;
1250 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001252 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1253 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001255 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001256 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001258 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001259 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 }
1262 }
1263 }
1264
1265 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1266 synchronized (mMethodMap) {
1267 if (mCurMethod != null && method != null
1268 && mCurMethod.asBinder() == method.asBinder()) {
1269 if (mCurClient != null) {
1270 mCurClient.curSession = new SessionState(mCurClient,
1271 method, session);
1272 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001273 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 if (res.method != null) {
1275 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1276 MSG_BIND_METHOD, mCurClient.client, res));
1277 }
1278 }
1279 }
1280 }
1281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001283 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001284 if (mVisibleBound) {
1285 mContext.unbindService(mVisibleConnection);
1286 mVisibleBound = false;
1287 }
1288
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001289 if (mHaveConnection) {
1290 mContext.unbindService(this);
1291 mHaveConnection = false;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001294 if (mCurToken != null) {
1295 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001297 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001298 // The current IME is shown. Hence an IME switch (transition) is happening.
1299 mWindowManagerService.saveLastInputMethodWindowForTransition();
1300 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001301 mIWindowManager.removeWindowToken(mCurToken);
1302 } catch (RemoteException e) {
1303 }
1304 mCurToken = null;
1305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
The Android Open Source Project10592532009-03-18 17:39:46 -07001307 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001308 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001310 if (reportToClient && mCurClient != null) {
1311 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1312 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1313 }
1314 }
1315
Devin Taylor0c33ed22010-02-23 13:26:46 -06001316 private void finishSession(SessionState sessionState) {
1317 if (sessionState != null && sessionState.session != null) {
1318 try {
1319 sessionState.session.finishSession();
1320 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001321 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001322 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001323 }
1324 }
1325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001327 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 if (mCurMethod != null) {
1329 for (ClientState cs : mClients.values()) {
1330 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001331 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 cs.curSession = null;
1333 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001334
1335 finishSession(mEnabledSession);
1336 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 mCurMethod = null;
1338 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001339 if (mStatusBar != null) {
1340 mStatusBar.setIconVisibility("ime", false);
1341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
satoke7c6998e2011-06-03 17:57:59 +09001344 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 public void onServiceDisconnected(ComponentName name) {
1346 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001347 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 + " mCurIntent=" + mCurIntent);
1349 if (mCurMethod != null && mCurIntent != null
1350 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001351 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 // We consider this to be a new bind attempt, since the system
1353 // should now try to restart the service for us.
1354 mLastBindTime = SystemClock.uptimeMillis();
1355 mShowRequested = mInputShown;
1356 mInputShown = false;
1357 if (mCurClient != null) {
1358 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1359 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1360 }
1361 }
1362 }
1363 }
1364
satokf9f01002011-05-19 21:31:50 +09001365 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001367 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 long ident = Binder.clearCallingIdentity();
1369 try {
1370 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001371 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 return;
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 synchronized (mMethodMap) {
1376 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001378 if (mStatusBar != null) {
1379 mStatusBar.setIconVisibility("ime", false);
1380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001383 CharSequence contentDescription = null;
1384 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001385 // Use PackageManager to load label
1386 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001387 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001388 mIPackageManager.getApplicationInfo(packageName, 0,
1389 mSettings.getCurrentUserId()));
1390 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001391 /* ignore */
1392 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001393 if (mStatusBar != null) {
1394 mStatusBar.setIcon("ime", packageName, iconId, 0,
1395 contentDescription != null
1396 ? contentDescription.toString() : null);
1397 mStatusBar.setIconVisibility("ime", true);
1398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400 }
1401 } finally {
1402 Binder.restoreCallingIdentity(ident);
1403 }
1404 }
1405
satok7cfc0ed2011-06-20 21:29:36 +09001406 private boolean needsToShowImeSwitchOngoingNotification() {
1407 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001408 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001409 synchronized (mMethodMap) {
1410 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1411 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001412 if (N > 2) return true;
1413 if (N < 1) return false;
1414 int nonAuxCount = 0;
1415 int auxCount = 0;
1416 InputMethodSubtype nonAuxSubtype = null;
1417 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001418 for(int i = 0; i < N; ++i) {
1419 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001420 final List<InputMethodSubtype> subtypes =
1421 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001422 final int subtypeCount = subtypes.size();
1423 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001424 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001425 } else {
1426 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001427 final InputMethodSubtype subtype = subtypes.get(j);
1428 if (!subtype.isAuxiliary()) {
1429 ++nonAuxCount;
1430 nonAuxSubtype = subtype;
1431 } else {
1432 ++auxCount;
1433 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001434 }
1435 }
1436 }
satok7cfc0ed2011-06-20 21:29:36 +09001437 }
satokb6359412011-06-28 17:47:41 +09001438 if (nonAuxCount > 1 || auxCount > 1) {
1439 return true;
1440 } else if (nonAuxCount == 1 && auxCount == 1) {
1441 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001442 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1443 || auxSubtype.overridesImplicitlyEnabledSubtype()
1444 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001445 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1446 return false;
1447 }
1448 return true;
1449 }
1450 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001451 }
satok7cfc0ed2011-06-20 21:29:36 +09001452 }
1453
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001454 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001455 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001456 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001457 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001458 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001459 try {
1460 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001461 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001462 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001463 return;
1464 }
1465
1466 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001467 mImeWindowVis = vis;
1468 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001469 if (mStatusBar != null) {
1470 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1471 }
satok7cfc0ed2011-06-20 21:29:36 +09001472 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001473 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1474 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001476 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001477 final CharSequence title = mRes.getText(
1478 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001479 final CharSequence imiLabel = imi.loadLabel(pm);
1480 final CharSequence summary = mCurrentSubtype != null
1481 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1482 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1483 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001484 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001485 : imiLabel;
1486
satok7cfc0ed2011-06-20 21:29:36 +09001487 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001488 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001489 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001490 if (DEBUG) {
1491 Slog.d(TAG, "--- show notification: label = " + imiLabel
1492 + ", summary = " + summary);
1493 }
1494 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001495 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001496 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001497 mNotificationShown = true;
1498 }
satok7cfc0ed2011-06-20 21:29:36 +09001499 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001500 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001501 if (DEBUG) {
1502 Slog.d(TAG, "--- hide notification");
1503 }
1504 mNotificationManager.cancelAsUser(null,
1505 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001506 mNotificationShown = false;
1507 }
1508 }
satok06487a52010-10-29 11:37:18 +09001509 }
1510 } finally {
1511 Binder.restoreCallingIdentity(ident);
1512 }
1513 }
1514
satoke7c6998e2011-06-03 17:57:59 +09001515 @Override
satokf9f01002011-05-19 21:31:50 +09001516 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001517 if (!calledFromValidUser()) {
1518 return;
1519 }
satokf9f01002011-05-19 21:31:50 +09001520 synchronized (mMethodMap) {
1521 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1522 for (int i = 0; i < spans.length; ++i) {
1523 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001524 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001525 mSecureSuggestionSpans.put(ss, currentImi);
1526 }
1527 }
1528 }
1529 }
1530
satoke7c6998e2011-06-03 17:57:59 +09001531 @Override
satokf9f01002011-05-19 21:31:50 +09001532 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001533 if (!calledFromValidUser()) {
1534 return false;
1535 }
satokf9f01002011-05-19 21:31:50 +09001536 synchronized (mMethodMap) {
1537 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1538 // TODO: Do not send the intent if the process of the targetImi is already dead.
1539 if (targetImi != null) {
1540 final String[] suggestions = span.getSuggestions();
1541 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001542 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001543 final Intent intent = new Intent();
1544 // Ensures that only a class in the original IME package will receive the
1545 // notification.
satok42c5a162011-05-26 16:46:14 +09001546 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001547 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1548 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1549 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1550 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001551 final long ident = Binder.clearCallingIdentity();
1552 try {
1553 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1554 } finally {
1555 Binder.restoreCallingIdentity(ident);
1556 }
satokf9f01002011-05-19 21:31:50 +09001557 return true;
1558 }
1559 }
1560 return false;
1561 }
1562
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001563 void updateFromSettingsLocked(boolean enabledMayChange) {
1564 if (enabledMayChange) {
1565 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1566 for (int i=0; i<enabled.size(); i++) {
1567 // We allow the user to select "disabled until used" apps, so if they
1568 // are enabling one of those here we now need to make it enabled.
1569 InputMethodInfo imm = enabled.get(i);
1570 try {
1571 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1572 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1573 mSettings.getCurrentUserId());
1574 if (ai.enabledSetting
1575 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1576 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1577 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1578 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1579 }
1580 } catch (RemoteException e) {
1581 }
1582 }
1583 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1585 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1586 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1587 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001588 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001589 // There is no input method selected, try to choose new applicable input method.
1590 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001591 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001592 }
1593 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001595 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001597 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001598 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001599 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
satokf3db1af2010-11-23 13:34:33 +09001601 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001602 } else {
1603 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001604 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001605 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
satokab751aa2010-09-14 19:17:36 +09001609 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 InputMethodInfo info = mMethodMap.get(id);
1611 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001612 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
satokd81e9502012-05-21 12:58:45 +09001615 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001617 final int subtypeCount = info.getSubtypeCount();
1618 if (subtypeCount <= 0) {
1619 return;
satokcd7cd292010-11-20 15:46:23 +09001620 }
satokd81e9502012-05-21 12:58:45 +09001621 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1622 final InputMethodSubtype newSubtype;
1623 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1624 newSubtype = info.getSubtypeAt(subtypeId);
1625 } else {
1626 // If subtype is null, try to find the most applicable one from
1627 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001629 }
1630 if (newSubtype == null || oldSubtype == null) {
1631 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1632 + ", new subtype = " + newSubtype);
1633 return;
1634 }
1635 if (newSubtype != oldSubtype) {
1636 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1637 if (mCurMethod != null) {
1638 try {
1639 refreshImeWindowVisibilityLocked();
1640 mCurMethod.changeInputMethodSubtype(newSubtype);
1641 } catch (RemoteException e) {
1642 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001643 }
1644 }
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 return;
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
satokd81e9502012-05-21 12:58:45 +09001649 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 final long ident = Binder.clearCallingIdentity();
1651 try {
satokab751aa2010-09-14 19:17:36 +09001652 // Set a subtype to this input method.
1653 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001654 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1655 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1656 // because mCurMethodId is stored as a history in
1657 // setSelectedInputMethodAndSubtypeLocked().
1658 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659
1660 if (ActivityManagerNative.isSystemReady()) {
1661 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001662 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001664 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001666 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 } finally {
1668 Binder.restoreCallingIdentity(ident);
1669 }
1670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
satok42c5a162011-05-26 16:46:14 +09001672 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001673 public boolean showSoftInput(IInputMethodClient client, int flags,
1674 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001675 if (!calledFromValidUser()) {
1676 return false;
1677 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001678 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 long ident = Binder.clearCallingIdentity();
1680 try {
1681 synchronized (mMethodMap) {
1682 if (mCurClient == null || client == null
1683 || mCurClient.client.asBinder() != client.asBinder()) {
1684 try {
1685 // We need to check if this is the current client with
1686 // focus in the window manager, to allow this call to
1687 // be made before input is started in it.
1688 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001689 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001690 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
1692 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001693 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
Joe Onorato8a9b2202010-02-26 18:56:32 -08001697 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700 } finally {
1701 Binder.restoreCallingIdentity(ident);
1702 }
1703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
The Android Open Source Project4df24232009-03-05 14:34:35 -08001705 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 mShowRequested = true;
1707 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1708 mShowExplicitlyRequested = true;
1709 }
1710 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1711 mShowExplicitlyRequested = true;
1712 mShowForced = true;
1713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001714
Dianne Hackborncc278702009-09-02 23:07:23 -07001715 if (!mSystemReady) {
1716 return false;
1717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001718
The Android Open Source Project4df24232009-03-05 14:34:35 -08001719 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001721 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1722 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1723 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001725 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001726 bindCurrentInputMethodService(
1727 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001728 mVisibleBound = true;
1729 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001732 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 // The client has asked to have the input method shown, but
1734 // we have been sitting here too long with a connection to the
1735 // service and no interface received, so let's disconnect/connect
1736 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001739 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001741 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001742 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001743 } else {
1744 if (DEBUG) {
1745 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1746 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
The Android Open Source Project4df24232009-03-05 14:34:35 -08001750 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
satok42c5a162011-05-26 16:46:14 +09001753 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 public boolean hideSoftInput(IInputMethodClient client, int flags,
1755 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001756 if (!calledFromValidUser()) {
1757 return false;
1758 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001759 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 long ident = Binder.clearCallingIdentity();
1761 try {
1762 synchronized (mMethodMap) {
1763 if (mCurClient == null || client == null
1764 || mCurClient.client.asBinder() != client.asBinder()) {
1765 try {
1766 // We need to check if this is the current client with
1767 // focus in the window manager, to allow this call to
1768 // be made before input is started in it.
1769 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001770 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1771 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001772 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001773 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001776 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001780
Joe Onorato8a9b2202010-02-26 18:56:32 -08001781 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 } finally {
1785 Binder.restoreCallingIdentity(ident);
1786 }
1787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1791 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001792 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001797 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001799 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001801 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001803 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1804 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1805 res = true;
1806 } else {
1807 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001809 if (mHaveConnection && mVisibleBound) {
1810 mContext.unbindService(mVisibleConnection);
1811 mVisibleBound = false;
1812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 mInputShown = false;
1814 mShowRequested = false;
1815 mShowExplicitlyRequested = false;
1816 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001817 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
satok42c5a162011-05-26 16:46:14 +09001820 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001821 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1822 int controlFlags, int softInputMode, int windowFlags,
1823 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001824 // Needs to check the validity before clearing calling identity
1825 final boolean calledFromValidUser = calledFromValidUser();
1826
Dianne Hackborn7663d802012-02-24 13:08:49 -08001827 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 long ident = Binder.clearCallingIdentity();
1829 try {
1830 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001831 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001832 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001834 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
Dianne Hackborn7663d802012-02-24 13:08:49 -08001836 ClientState cs = mClients.get(client.asBinder());
1837 if (cs == null) {
1838 throw new IllegalArgumentException("unknown client "
1839 + client.asBinder());
1840 }
1841
1842 try {
1843 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1844 // Check with the window manager to make sure this client actually
1845 // has a window with focus. If not, reject. This is thread safe
1846 // because if the focus changes some time before or after, the
1847 // next client receiving focus that has any interest in input will
1848 // be calling through here after that change happens.
1849 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1850 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1851 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001853 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001856 if (!calledFromValidUser) {
1857 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1858 Slog.w(TAG, "If you want to interect with IME, you need "
1859 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1860 hideCurrentInputLocked(0, null);
1861 return null;
1862 }
1863
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001864 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001865 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001866 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001867 if (attribute != null) {
1868 return startInputUncheckedLocked(cs, inputContext, attribute,
1869 controlFlags);
1870 }
1871 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001872 }
1873 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001875 // Should we auto-show the IME even if the caller has not
1876 // specified what should be done with it?
1877 // We only do this automatically if the window can resize
1878 // to accommodate the IME (so what the user sees will give
1879 // them good context without input information being obscured
1880 // by the IME) or if running on a large screen where there
1881 // is more room for the target window + IME.
1882 final boolean doAutoShow =
1883 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1884 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1885 || mRes.getConfiguration().isLayoutSizeAtLeast(
1886 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001887 final boolean isTextEditor =
1888 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1889
1890 // We want to start input before showing the IME, but after closing
1891 // it. We want to do this after closing it to help the IME disappear
1892 // more quickly (not get stuck behind it initializing itself for the
1893 // new focused input, even if its window wants to hide the IME).
1894 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1897 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001898 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1900 // There is no focus view, and this window will
1901 // be behind any soft input window, so hide the
1902 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001903 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001904 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001906 } else if (isTextEditor && doAutoShow && (softInputMode &
1907 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 // There is a focus view, and we are navigating forward
1909 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001910 // We only do this automatically if the window can resize
1911 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001912 // them good context without input information being obscured
1913 // by the IME) or if running on a large screen where there
1914 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001915 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001916 if (attribute != null) {
1917 res = startInputUncheckedLocked(cs, inputContext, attribute,
1918 controlFlags);
1919 didStart = true;
1920 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001921 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 break;
1924 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1925 // Do nothing.
1926 break;
1927 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1928 if ((softInputMode &
1929 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001930 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001931 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 break;
1934 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001936 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 break;
1938 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1939 if ((softInputMode &
1940 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001942 if (attribute != null) {
1943 res = startInputUncheckedLocked(cs, inputContext, attribute,
1944 controlFlags);
1945 didStart = true;
1946 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001947 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
1949 break;
1950 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001951 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001952 if (attribute != null) {
1953 res = startInputUncheckedLocked(cs, inputContext, attribute,
1954 controlFlags);
1955 didStart = true;
1956 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001957 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 break;
1959 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001960
1961 if (!didStart && attribute != null) {
1962 res = startInputUncheckedLocked(cs, inputContext, attribute,
1963 controlFlags);
1964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 } finally {
1967 Binder.restoreCallingIdentity(ident);
1968 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001969
1970 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001972
satok42c5a162011-05-26 16:46:14 +09001973 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001975 if (!calledFromValidUser()) {
1976 return;
1977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 synchronized (mMethodMap) {
1979 if (mCurClient == null || client == null
1980 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001981 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001982 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984
satok440aab52010-11-25 09:43:11 +09001985 // Always call subtype picker, because subtype picker is a superset of input method
1986 // picker.
satokab751aa2010-09-14 19:17:36 +09001987 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1988 }
1989 }
1990
satok42c5a162011-05-26 16:46:14 +09001991 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001993 if (!calledFromValidUser()) {
1994 return;
1995 }
satok28203512010-11-24 11:06:49 +09001996 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1997 }
1998
satok42c5a162011-05-26 16:46:14 +09001999 @Override
satok28203512010-11-24 11:06:49 +09002000 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002001 if (!calledFromValidUser()) {
2002 return;
2003 }
satok28203512010-11-24 11:06:49 +09002004 synchronized (mMethodMap) {
2005 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002006 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002007 mMethodMap.get(id), subtype.hashCode()));
2008 } else {
2009 setInputMethod(token, id);
2010 }
2011 }
satokab751aa2010-09-14 19:17:36 +09002012 }
2013
satok42c5a162011-05-26 16:46:14 +09002014 @Override
satokb416a712010-11-25 20:42:14 +09002015 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002016 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002017 if (!calledFromValidUser()) {
2018 return;
2019 }
satokb416a712010-11-25 20:42:14 +09002020 synchronized (mMethodMap) {
2021 if (mCurClient == null || client == null
2022 || mCurClient.client.asBinder() != client.asBinder()) {
2023 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2024 }
satok7fee71f2010-12-17 18:54:26 +09002025 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2026 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002027 }
2028 }
2029
satok4fc87d62011-05-20 16:13:43 +09002030 @Override
satok735cf382010-11-11 20:40:09 +09002031 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002032 if (!calledFromValidUser()) {
2033 return false;
2034 }
satok735cf382010-11-11 20:40:09 +09002035 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002036 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002037 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002038 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002039 lastImi = mMethodMap.get(lastIme.first);
2040 } else {
2041 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002042 }
satok4fc87d62011-05-20 16:13:43 +09002043 String targetLastImiId = null;
2044 int subtypeId = NOT_A_SUBTYPE_ID;
2045 if (lastIme != null && lastImi != null) {
2046 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2047 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2048 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2049 : mCurrentSubtype.hashCode();
2050 // If the last IME is the same as the current IME and the last subtype is not
2051 // defined, there is no need to switch to the last IME.
2052 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2053 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002054 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002055 }
2056 }
2057
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002058 if (TextUtils.isEmpty(targetLastImiId)
2059 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002060 // This is a safety net. If the currentSubtype can't be added to the history
2061 // and the framework couldn't find the last ime, we will make the last ime be
2062 // the most applicable enabled keyboard subtype of the system imes.
2063 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2064 if (enabled != null) {
2065 final int N = enabled.size();
2066 final String locale = mCurrentSubtype == null
2067 ? mRes.getConfiguration().locale.toString()
2068 : mCurrentSubtype.getLocale();
2069 for (int i = 0; i < N; ++i) {
2070 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002071 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002072 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002073 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2074 InputMethodUtils.getSubtypes(imi),
2075 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002076 if (keyboardSubtype != null) {
2077 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002078 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002079 imi, keyboardSubtype.hashCode());
2080 if(keyboardSubtype.getLocale().equals(locale)) {
2081 break;
2082 }
2083 }
2084 }
2085 }
2086 }
2087 }
2088
2089 if (!TextUtils.isEmpty(targetLastImiId)) {
2090 if (DEBUG) {
2091 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2092 + ", from: " + mCurMethodId + ", " + subtypeId);
2093 }
2094 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2095 return true;
2096 } else {
2097 return false;
2098 }
satok735cf382010-11-11 20:40:09 +09002099 }
2100 }
2101
satoke7c6998e2011-06-03 17:57:59 +09002102 @Override
satok688bd472012-02-09 20:09:17 +09002103 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002104 if (!calledFromValidUser()) {
2105 return false;
2106 }
satok688bd472012-02-09 20:09:17 +09002107 synchronized (mMethodMap) {
2108 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2109 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2110 if (nextSubtype == null) {
2111 return false;
2112 }
2113 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2114 return true;
2115 }
2116 }
2117
2118 @Override
satok68f1b782011-04-11 14:26:04 +09002119 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002120 if (!calledFromValidUser()) {
2121 return null;
2122 }
satok68f1b782011-04-11 14:26:04 +09002123 synchronized (mMethodMap) {
2124 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2125 // TODO: Handle the case of the last IME with no subtypes
2126 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2127 || TextUtils.isEmpty(lastIme.second)) return null;
2128 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2129 if (lastImi == null) return null;
2130 try {
2131 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002132 final int lastSubtypeId =
2133 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002134 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2135 return null;
2136 }
2137 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002138 } catch (NumberFormatException e) {
2139 return null;
2140 }
2141 }
2142 }
2143
satoke7c6998e2011-06-03 17:57:59 +09002144 @Override
satokee5e77c2011-09-02 18:50:15 +09002145 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002146 if (!calledFromValidUser()) {
2147 return;
2148 }
satok91e88122011-07-18 11:11:42 +09002149 // By this IPC call, only a process which shares the same uid with the IME can add
2150 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002151 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002152 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002153 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002154 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002155 final String[] packageInfos;
2156 try {
2157 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2158 } catch (RemoteException e) {
2159 Slog.e(TAG, "Failed to get package infos");
2160 return;
2161 }
satok91e88122011-07-18 11:11:42 +09002162 if (packageInfos != null) {
2163 final int packageNum = packageInfos.length;
2164 for (int i = 0; i < packageNum; ++i) {
2165 if (packageInfos[i].equals(imi.getPackageName())) {
2166 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002167 final long ident = Binder.clearCallingIdentity();
2168 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002169 buildInputMethodListLocked(mMethodList, mMethodMap,
2170 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002171 } finally {
2172 Binder.restoreCallingIdentity(ident);
2173 }
satokee5e77c2011-09-02 18:50:15 +09002174 return;
satok91e88122011-07-18 11:11:42 +09002175 }
2176 }
2177 }
satoke7c6998e2011-06-03 17:57:59 +09002178 }
satokee5e77c2011-09-02 18:50:15 +09002179 return;
satoke7c6998e2011-06-03 17:57:59 +09002180 }
2181
satok28203512010-11-24 11:06:49 +09002182 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 synchronized (mMethodMap) {
2184 if (token == null) {
2185 if (mContext.checkCallingOrSelfPermission(
2186 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2187 != PackageManager.PERMISSION_GRANTED) {
2188 throw new SecurityException(
2189 "Using null token requires permission "
2190 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2191 }
2192 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002193 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2194 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 return;
2196 }
2197
satokc5933802011-08-31 21:26:04 +09002198 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 try {
satokab751aa2010-09-14 19:17:36 +09002200 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 } finally {
2202 Binder.restoreCallingIdentity(ident);
2203 }
2204 }
2205 }
2206
satok42c5a162011-05-26 16:46:14 +09002207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002209 if (!calledFromValidUser()) {
2210 return;
2211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 synchronized (mMethodMap) {
2213 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002214 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2215 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 return;
2217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 long ident = Binder.clearCallingIdentity();
2219 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 hideCurrentInputLocked(flags, null);
2221 } finally {
2222 Binder.restoreCallingIdentity(ident);
2223 }
2224 }
2225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002226
satok42c5a162011-05-26 16:46:14 +09002227 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002228 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002229 if (!calledFromValidUser()) {
2230 return;
2231 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002232 synchronized (mMethodMap) {
2233 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002234 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2235 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002236 return;
2237 }
2238 long ident = Binder.clearCallingIdentity();
2239 try {
2240 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 } finally {
2242 Binder.restoreCallingIdentity(ident);
2243 }
2244 }
2245 }
2246
2247 void setEnabledSessionInMainThread(SessionState session) {
2248 if (mEnabledSession != session) {
2249 if (mEnabledSession != null) {
2250 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002251 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 mEnabledSession.method.setSessionEnabled(
2253 mEnabledSession.session, false);
2254 } catch (RemoteException e) {
2255 }
2256 }
2257 mEnabledSession = session;
2258 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002259 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 session.method.setSessionEnabled(
2261 session.session, true);
2262 } catch (RemoteException e) {
2263 }
2264 }
2265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002266
satok42c5a162011-05-26 16:46:14 +09002267 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002269 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 switch (msg.what) {
2271 case MSG_SHOW_IM_PICKER:
2272 showInputMethodMenu();
2273 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002274
satokab751aa2010-09-14 19:17:36 +09002275 case MSG_SHOW_IM_SUBTYPE_PICKER:
2276 showInputMethodSubtypeMenu();
2277 return true;
2278
satok47a44912010-10-06 16:03:58 +09002279 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002280 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002281 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002282 args.recycle();
satok217f5482010-12-15 05:19:19 +09002283 return true;
2284
2285 case MSG_SHOW_IM_CONFIG:
2286 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002287 return true;
2288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 case MSG_UNBIND_INPUT:
2292 try {
2293 ((IInputMethod)msg.obj).unbindInput();
2294 } catch (RemoteException e) {
2295 // There is nothing interesting about the method dying.
2296 }
2297 return true;
2298 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002299 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 try {
2301 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
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_SHOW_SOFT_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 {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002309 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2310 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 } catch (RemoteException e) {
2312 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002313 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 return true;
2315 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002316 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002318 ((IInputMethod)args.arg1).hideSoftInput(0,
2319 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 } catch (RemoteException e) {
2321 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002322 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 return true;
2324 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002325 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002327 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2329 } catch (RemoteException e) {
2330 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002331 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 return true;
2333 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002334 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 try {
2336 ((IInputMethod)args.arg1).createSession(
2337 (IInputMethodCallback)args.arg2);
2338 } catch (RemoteException e) {
2339 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002340 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002345 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 try {
2347 SessionState session = (SessionState)args.arg1;
2348 setEnabledSessionInMainThread(session);
2349 session.method.startInput((IInputContext)args.arg2,
2350 (EditorInfo)args.arg3);
2351 } catch (RemoteException e) {
2352 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002353 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 return true;
2355 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002356 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 try {
2358 SessionState session = (SessionState)args.arg1;
2359 setEnabledSessionInMainThread(session);
2360 session.method.restartInput((IInputContext)args.arg2,
2361 (EditorInfo)args.arg3);
2362 } catch (RemoteException e) {
2363 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002364 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 case MSG_UNBIND_METHOD:
2370 try {
2371 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2372 } catch (RemoteException e) {
2373 // There is nothing interesting about the last client dying.
2374 }
2375 return true;
2376 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002377 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 try {
2379 ((IInputMethodClient)args.arg1).onBindMethod(
2380 (InputBindResult)args.arg2);
2381 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002384 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002386 case MSG_SET_ACTIVE:
2387 try {
2388 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2389 } catch (RemoteException e) {
2390 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2391 + ((ClientState)msg.obj).pid + " uid "
2392 + ((ClientState)msg.obj).uid);
2393 }
2394 return true;
satok01038492012-04-09 21:08:27 +09002395
2396 // --------------------------------------------------------------
2397 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2398 mHardKeyboardListener.handleHardKeyboardStatusChange(
2399 msg.arg1 == 1, msg.arg2 == 1);
2400 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402 return false;
2403 }
2404
satokdc9ddae2011-10-06 12:22:36 +09002405 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002406 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2407 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002408 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002409 if (DEBUG) {
2410 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2411 }
satok723a27e2010-11-11 14:58:11 +09002412 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002413 return true;
2414 }
2415
2416 return false;
2417 }
2418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002420 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002422 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2423 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 list.clear();
2426 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002427
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002428 // Use for queryIntentServicesAsUser
2429 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002431 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002433 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002435 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2436 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002437
satoke7c6998e2011-06-03 17:57:59 +09002438 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2439 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 for (int i = 0; i < services.size(); ++i) {
2441 ResolveInfo ri = services.get(i);
2442 ServiceInfo si = ri.serviceInfo;
2443 ComponentName compName = new ComponentName(si.packageName, si.name);
2444 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2445 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002446 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 + ": it does not require the permission "
2448 + android.Manifest.permission.BIND_INPUT_METHOD);
2449 continue;
2450 }
2451
Joe Onorato8a9b2202010-02-26 18:56:32 -08002452 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453
2454 try {
satoke7c6998e2011-06-03 17:57:59 +09002455 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002457 final String id = p.getId();
2458 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459
2460 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002461 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002465 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002467 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
2469 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002470
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002471 if (resetDefaultEnabledIme) {
2472 final ArrayList<InputMethodInfo> defaultEnabledIme =
2473 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2474 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2475 final InputMethodInfo imi = defaultEnabledIme.get(i);
2476 if (DEBUG) {
2477 Slog.d(TAG, "--- enable ime = " + imi);
2478 }
2479 setInputMethodEnabledLocked(imi.getId(), true);
2480 }
2481 }
2482
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002483 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002484 if (!TextUtils.isEmpty(defaultImiId)) {
2485 if (!map.containsKey(defaultImiId)) {
2486 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2487 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002488 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002489 }
2490 } else {
2491 // Double check that the default IME is certainly enabled.
2492 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002493 }
2494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002498
satokab751aa2010-09-14 19:17:36 +09002499 private void showInputMethodMenu() {
2500 showInputMethodMenuInternal(false);
2501 }
2502
2503 private void showInputMethodSubtypeMenu() {
2504 showInputMethodMenuInternal(true);
2505 }
2506
satok217f5482010-12-15 05:19:19 +09002507 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002508 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002509 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002510 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2511 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002512 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002513 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002514 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002515 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002516 }
2517
2518 private void showConfigureInputMethods() {
2519 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2520 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2521 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2522 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002523 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002524 }
2525
satok2c93efc2012-04-02 19:33:47 +09002526 private boolean isScreenLocked() {
2527 return mKeyguardManager != null
2528 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2529 }
satokab751aa2010-09-14 19:17:36 +09002530 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002531 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002534 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002535
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002537 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002538 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002539
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002540 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002541 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2542 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002543 if (immis == null || immis.size() == 0) {
2544 return;
2545 }
2546
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002547 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548
satok688bd472012-02-09 20:09:17 +09002549 final List<ImeSubtypeListItem> imList =
2550 mImListManager.getSortedInputMethodAndSubtypeList(
2551 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002552
satokc3690562012-01-10 20:14:43 +09002553 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002554 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002555 if (currentSubtype != null) {
2556 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002557 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2558 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002559 }
2560 }
2561
Ken Wakasa761eb372011-03-04 19:06:18 +09002562 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002563 mIms = new InputMethodInfo[N];
2564 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002565 int checkedItem = 0;
2566 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002567 final ImeSubtypeListItem item = imList.get(i);
2568 mIms[i] = item.mImi;
2569 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002570 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002571 int subtypeId = mSubtypeIds[i];
2572 if ((subtypeId == NOT_A_SUBTYPE_ID)
2573 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2574 || (subtypeId == lastInputMethodSubtypeId)) {
2575 checkedItem = i;
2576 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002579 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 com.android.internal.R.styleable.DialogPreference,
2581 com.android.internal.R.attr.alertDialogStyle, 0);
2582 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002583 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002584 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002585 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002587 }
2588 })
2589 .setIcon(a.getDrawable(
2590 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2591 a.recycle();
satok01038492012-04-09 21:08:27 +09002592 final LayoutInflater inflater =
2593 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2594 final View tv = inflater.inflate(
2595 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2596 mDialogBuilder.setCustomTitle(tv);
2597
2598 // Setup layout for a toggle switch of the hardware keyboard
2599 mSwitchingDialogTitleView = tv;
2600 mSwitchingDialogTitleView.findViewById(
2601 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2602 mWindowManagerService.isHardKeyboardAvailable() ?
2603 View.VISIBLE : View.GONE);
2604 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2605 com.android.internal.R.id.hard_keyboard_switch));
2606 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2607 hardKeySwitch.setOnCheckedChangeListener(
2608 new OnCheckedChangeListener() {
2609 @Override
2610 public void onCheckedChanged(
2611 CompoundButton buttonView, boolean isChecked) {
2612 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002613 // Ensure that the input method dialog is dismissed when changing
2614 // the hardware keyboard state.
2615 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002616 }
2617 });
satokd87c2592010-09-29 11:52:06 +09002618
Ken Wakasa05dbb652011-08-22 15:22:43 +09002619 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2620 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2621 checkedItem);
2622
2623 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002624 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002625 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002626 public void onClick(DialogInterface dialog, int which) {
2627 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002628 if (mIms == null || mIms.length <= which
2629 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002630 return;
2631 }
2632 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002633 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002634 adapter.mCheckedItem = which;
2635 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002636 hideInputMethodMenu();
2637 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002638 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002639 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002640 subtypeId = NOT_A_SUBTYPE_ID;
2641 }
2642 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002643 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002646 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647
satokbc81b692011-08-26 16:22:22 +09002648 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002649 mDialogBuilder.setPositiveButton(
2650 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002651 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002652 @Override
satok7f35c8c2010-10-07 21:13:11 +09002653 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002654 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002655 }
2656 });
2657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002659 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 mSwitchingDialog.getWindow().setType(
2661 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002662 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2663 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002664 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 mSwitchingDialog.show();
2666 }
2667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002668
satok93d744d2012-05-09 17:14:08 +09002669 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002670 public final CharSequence mImeName;
2671 public final CharSequence mSubtypeName;
2672 public final InputMethodInfo mImi;
2673 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002674 private final boolean mIsSystemLocale;
2675 private final boolean mIsSystemLanguage;
2676
Ken Wakasa05dbb652011-08-22 15:22:43 +09002677 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002678 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002679 mImeName = imeName;
2680 mSubtypeName = subtypeName;
2681 mImi = imi;
2682 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002683 if (TextUtils.isEmpty(subtypeLocale)) {
2684 mIsSystemLocale = false;
2685 mIsSystemLanguage = false;
2686 } else {
2687 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2688 mIsSystemLanguage = mIsSystemLocale
2689 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2690 }
2691 }
2692
2693 @Override
2694 public int compareTo(ImeSubtypeListItem other) {
2695 if (TextUtils.isEmpty(mImeName)) {
2696 return 1;
2697 }
2698 if (TextUtils.isEmpty(other.mImeName)) {
2699 return -1;
2700 }
2701 if (!TextUtils.equals(mImeName, other.mImeName)) {
2702 return mImeName.toString().compareTo(other.mImeName.toString());
2703 }
2704 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2705 return 0;
2706 }
2707 if (mIsSystemLocale) {
2708 return -1;
2709 }
2710 if (other.mIsSystemLocale) {
2711 return 1;
2712 }
2713 if (mIsSystemLanguage) {
2714 return -1;
2715 }
2716 if (other.mIsSystemLanguage) {
2717 return 1;
2718 }
2719 if (TextUtils.isEmpty(mSubtypeName)) {
2720 return 1;
2721 }
2722 if (TextUtils.isEmpty(other.mSubtypeName)) {
2723 return -1;
2724 }
2725 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002726 }
2727 }
2728
2729 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2730 private final LayoutInflater mInflater;
2731 private final int mTextViewResourceId;
2732 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002733 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002734 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2735 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2736 super(context, textViewResourceId, itemsList);
2737 mTextViewResourceId = textViewResourceId;
2738 mItemsList = itemsList;
2739 mCheckedItem = checkedItem;
2740 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2741 }
2742
2743 @Override
2744 public View getView(int position, View convertView, ViewGroup parent) {
2745 final View view = convertView != null ? convertView
2746 : mInflater.inflate(mTextViewResourceId, null);
2747 if (position < 0 || position >= mItemsList.size()) return view;
2748 final ImeSubtypeListItem item = mItemsList.get(position);
2749 final CharSequence imeName = item.mImeName;
2750 final CharSequence subtypeName = item.mSubtypeName;
2751 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2752 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2753 if (TextUtils.isEmpty(subtypeName)) {
2754 firstTextView.setText(imeName);
2755 secondTextView.setVisibility(View.GONE);
2756 } else {
2757 firstTextView.setText(subtypeName);
2758 secondTextView.setText(imeName);
2759 secondTextView.setVisibility(View.VISIBLE);
2760 }
2761 final RadioButton radioButton =
2762 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2763 radioButton.setChecked(position == mCheckedItem);
2764 return view;
2765 }
2766 }
2767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002769 synchronized (mMethodMap) {
2770 hideInputMethodMenuLocked();
2771 }
2772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project10592532009-03-18 17:39:46 -07002774 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002775 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
The Android Open Source Project10592532009-03-18 17:39:46 -07002777 if (mSwitchingDialog != null) {
2778 mSwitchingDialog.dismiss();
2779 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002781
The Android Open Source Project10592532009-03-18 17:39:46 -07002782 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002783 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002787
satok42c5a162011-05-26 16:46:14 +09002788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002790 // TODO: Make this work even for non-current users?
2791 if (!calledFromValidUser()) {
2792 return false;
2793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 synchronized (mMethodMap) {
2795 if (mContext.checkCallingOrSelfPermission(
2796 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2797 != PackageManager.PERMISSION_GRANTED) {
2798 throw new SecurityException(
2799 "Requires permission "
2800 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2801 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 long ident = Binder.clearCallingIdentity();
2804 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002805 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 } finally {
2807 Binder.restoreCallingIdentity(ident);
2808 }
2809 }
2810 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002811
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002812 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2813 // Make sure this is a valid input method.
2814 InputMethodInfo imm = mMethodMap.get(id);
2815 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002816 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002817 }
2818
satokd87c2592010-09-29 11:52:06 +09002819 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2820 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002821
satokd87c2592010-09-29 11:52:06 +09002822 if (enabled) {
2823 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2824 if (pair.first.equals(id)) {
2825 // We are enabling this input method, but it is already enabled.
2826 // Nothing to do. The previous state was enabled.
2827 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002828 }
2829 }
satokd87c2592010-09-29 11:52:06 +09002830 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2831 // Previous state was disabled.
2832 return false;
2833 } else {
2834 StringBuilder builder = new StringBuilder();
2835 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2836 builder, enabledInputMethodsList, id)) {
2837 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002838 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002839 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2840 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2841 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002842 }
2843 // Previous state was enabled.
2844 return true;
2845 } else {
2846 // We are disabling the input method but it is already disabled.
2847 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848 return false;
2849 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002850 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002851 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002852
satok723a27e2010-11-11 14:58:11 +09002853 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2854 boolean setSubtypeOnly) {
2855 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002856 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002857
2858 // Set Subtype here
2859 if (imi == null || subtypeId < 0) {
2860 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002861 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002862 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002863 if (subtypeId < imi.getSubtypeCount()) {
2864 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2865 mSettings.putSelectedSubtype(subtype.hashCode());
2866 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002867 } else {
2868 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002869 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002870 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002871 }
satokab751aa2010-09-14 19:17:36 +09002872 }
satok723a27e2010-11-11 14:58:11 +09002873
satok4c0e7152012-06-20 20:08:44 +09002874 // Workaround.
2875 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2876 // IMEs are not recognized and considered uninstalled.
2877 // Actually, we can't move everything after SystemReady because
2878 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2879 // the default IME here and try cheking the default IME again in systemReady().
2880 // TODO: Do nothing before system ready and implement a separated logic for
2881 // the encryption lock screen.
2882 // TODO: ASEC should be ready before IMMS is instantiated.
2883 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002884 // Set InputMethod here
2885 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2886 }
2887 }
2888
2889 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2890 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2891 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2892 // newDefaultIme is empty when there is no candidate for the selected IME.
2893 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2894 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2895 if (subtypeHashCode != null) {
2896 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002897 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002898 imi, Integer.valueOf(subtypeHashCode));
2899 } catch (NumberFormatException e) {
2900 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2901 }
2902 }
2903 }
2904 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002905 }
2906
satok4e4569d2010-11-19 18:45:53 +09002907 // If there are no selected shortcuts, tries finding the most applicable ones.
2908 private Pair<InputMethodInfo, InputMethodSubtype>
2909 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2910 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2911 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002912 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002913 boolean foundInSystemIME = false;
2914
2915 // Search applicable subtype for each InputMethodInfo
2916 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002917 final String imiId = imi.getId();
2918 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2919 continue;
2920 }
satokcd7cd292010-11-20 15:46:23 +09002921 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002922 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002923 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002924 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002925 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002926 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002927 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002928 }
satokdf31ae62011-01-15 06:19:44 +09002929 // 2. Search by the system locale from enabledSubtypes.
2930 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002931 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002932 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002933 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002934 }
satoka86f5e42011-09-02 17:12:42 +09002935 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002936 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002937 final ArrayList<InputMethodSubtype> subtypesForSearch =
2938 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002939 ? InputMethodUtils.getSubtypes(imi)
2940 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002941 // 4. Search by the current subtype's locale from all subtypes.
2942 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002943 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002944 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002945 }
2946 // 5. Search by the system locale from all subtypes.
2947 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002948 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002949 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002950 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002951 }
satokcd7cd292010-11-20 15:46:23 +09002952 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002953 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002954 // The current input method is the most applicable IME.
2955 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002956 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002957 break;
satok7599a7f2010-12-22 13:45:23 +09002958 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002959 // The system input method is 2nd applicable IME.
2960 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002961 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002962 if ((imi.getServiceInfo().applicationInfo.flags
2963 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2964 foundInSystemIME = true;
2965 }
satok4e4569d2010-11-19 18:45:53 +09002966 }
2967 }
2968 }
2969 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002970 if (mostApplicableIMI != null) {
2971 Slog.w(TAG, "Most applicable shortcut input method was:"
2972 + mostApplicableIMI.getId());
2973 if (mostApplicableSubtype != null) {
2974 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2975 + "," + mostApplicableSubtype.getMode() + ","
2976 + mostApplicableSubtype.getLocale());
2977 }
2978 }
satok4e4569d2010-11-19 18:45:53 +09002979 }
satokcd7cd292010-11-20 15:46:23 +09002980 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002981 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002982 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002983 } else {
2984 return null;
2985 }
2986 }
2987
satokab751aa2010-09-14 19:17:36 +09002988 /**
2989 * @return Return the current subtype of this input method.
2990 */
satok42c5a162011-05-26 16:46:14 +09002991 @Override
satokab751aa2010-09-14 19:17:36 +09002992 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002993 // TODO: Make this work even for non-current users?
2994 if (!calledFromValidUser()) {
2995 return null;
2996 }
2997 synchronized (mMethodMap) {
2998 return getCurrentInputMethodSubtypeLocked();
2999 }
3000 }
3001
3002 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003003 if (mCurMethodId == null) {
3004 return null;
3005 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003006 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003007 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3008 if (imi == null || imi.getSubtypeCount() == 0) {
3009 return null;
satok4e4569d2010-11-19 18:45:53 +09003010 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003011 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003012 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3013 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003014 if (subtypeId == NOT_A_SUBTYPE_ID) {
3015 // If there are no selected subtypes, the framework will try to find
3016 // the most applicable subtype from explicitly or implicitly enabled
3017 // subtypes.
3018 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003019 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 // If there is only one explicitly or implicitly enabled subtype,
3021 // just returns it.
3022 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3023 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3024 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003025 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003026 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003027 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003028 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003029 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003030 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3031 true);
satok4e4569d2010-11-19 18:45:53 +09003032 }
satok3ef8b292010-11-23 06:06:29 +09003033 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003034 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003035 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003036 }
3037 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003039 }
3040
satokf3db1af2010-11-23 13:34:33 +09003041 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3042 InputMethodSubtype subtype) {
3043 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3044 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3045 } else {
3046 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3047 subtypes.add(subtype);
3048 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3049 }
3050 }
3051
satok4e4569d2010-11-19 18:45:53 +09003052 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003053 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003054 @Override
satok4e4569d2010-11-19 18:45:53 +09003055 public List getShortcutInputMethodsAndSubtypes() {
3056 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003057 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003058 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003059 // If there are no selected shortcut subtypes, the framework will try to find
3060 // the most applicable subtype from all subtypes whose mode is
3061 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003062 Pair<InputMethodInfo, InputMethodSubtype> info =
3063 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003064 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003065 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003066 ret.add(info.first);
3067 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003068 }
satok3da92232011-01-11 22:46:30 +09003069 return ret;
satokf3db1af2010-11-23 13:34:33 +09003070 }
satokf3db1af2010-11-23 13:34:33 +09003071 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3072 ret.add(imi);
3073 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3074 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003075 }
3076 }
satokf3db1af2010-11-23 13:34:33 +09003077 return ret;
satok4e4569d2010-11-19 18:45:53 +09003078 }
3079 }
3080
satok42c5a162011-05-26 16:46:14 +09003081 @Override
satokb66d2872010-11-10 01:04:04 +09003082 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003083 // TODO: Make this work even for non-current users?
3084 if (!calledFromValidUser()) {
3085 return false;
3086 }
satokb66d2872010-11-10 01:04:04 +09003087 synchronized (mMethodMap) {
3088 if (subtype != null && mCurMethodId != null) {
3089 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003090 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003091 if (subtypeId != NOT_A_SUBTYPE_ID) {
3092 setInputMethodLocked(mCurMethodId, subtypeId);
3093 return true;
3094 }
3095 }
3096 return false;
3097 }
3098 }
3099
satok688bd472012-02-09 20:09:17 +09003100 private static class InputMethodAndSubtypeListManager {
3101 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003102 // Used to load label
satok688bd472012-02-09 20:09:17 +09003103 private final PackageManager mPm;
3104 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003105 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003106 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3107 mContext = context;
3108 mPm = context.getPackageManager();
3109 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003110 final Locale locale = context.getResources().getConfiguration().locale;
3111 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003112 }
3113
3114 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3115 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3116 new Comparator<InputMethodInfo>() {
3117 @Override
3118 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3119 if (imi2 == null) return 0;
3120 if (imi1 == null) return 1;
3121 if (mPm == null) {
3122 return imi1.getId().compareTo(imi2.getId());
3123 }
3124 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3125 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3126 return imiId1.toString().compareTo(imiId2.toString());
3127 }
3128 });
3129
3130 public ImeSubtypeListItem getNextInputMethod(
3131 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3132 if (imi == null) {
3133 return null;
3134 }
3135 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3136 if (imList.size() <= 1) {
3137 return null;
3138 }
3139 final int N = imList.size();
3140 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003141 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003142 : NOT_A_SUBTYPE_ID;
3143 for (int i = 0; i < N; ++i) {
3144 final ImeSubtypeListItem isli = imList.get(i);
3145 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3146 if (!onlyCurrentIme) {
3147 return imList.get((i + 1) % N);
3148 }
3149 for (int j = 0; j < N - 1; ++j) {
3150 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3151 if (candidate.mImi.equals(imi)) {
3152 return candidate;
3153 }
3154 }
3155 return null;
3156 }
3157 }
3158 return null;
3159 }
3160
3161 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3162 return getSortedInputMethodAndSubtypeList(true, false, false);
3163 }
3164
3165 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3166 boolean inputShown, boolean isScreenLocked) {
3167 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3168 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3169 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3170 if (immis == null || immis.size() == 0) {
3171 return Collections.emptyList();
3172 }
3173 mSortedImmis.clear();
3174 mSortedImmis.putAll(immis);
3175 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3176 if (imi == null) continue;
3177 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3178 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3179 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3180 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3181 }
satok688bd472012-02-09 20:09:17 +09003182 final CharSequence imeLabel = imi.loadLabel(mPm);
3183 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3184 final int subtypeCount = imi.getSubtypeCount();
3185 if (DEBUG) {
3186 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3187 }
3188 for (int j = 0; j < subtypeCount; ++j) {
3189 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3190 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3191 // We show all enabled IMEs and subtypes when an IME is shown.
3192 if (enabledSubtypeSet.contains(subtypeHashCode)
3193 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3194 final CharSequence subtypeLabel =
3195 subtype.overridesImplicitlyEnabledSubtype() ? null
3196 : subtype.getDisplayName(mContext, imi.getPackageName(),
3197 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003198 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3199 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003200
3201 // Removing this subtype from enabledSubtypeSet because we no longer
3202 // need to add an entry of this subtype to imList to avoid duplicated
3203 // entries.
3204 enabledSubtypeSet.remove(subtypeHashCode);
3205 }
3206 }
3207 } else {
satok93d744d2012-05-09 17:14:08 +09003208 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3209 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003210 }
3211 }
satok93d744d2012-05-09 17:14:08 +09003212 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003213 return imList;
3214 }
3215 }
3216
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003217 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003218 private static class InputMethodFileManager {
3219 private static final String SYSTEM_PATH = "system";
3220 private static final String INPUT_METHOD_PATH = "inputmethod";
3221 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3222 private static final String NODE_SUBTYPES = "subtypes";
3223 private static final String NODE_SUBTYPE = "subtype";
3224 private static final String NODE_IMI = "imi";
3225 private static final String ATTR_ID = "id";
3226 private static final String ATTR_LABEL = "label";
3227 private static final String ATTR_ICON = "icon";
3228 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3229 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3230 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3231 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3232 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3233 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003234 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003235 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003236 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003237 if (methodMap == null) {
3238 throw new NullPointerException("methodMap is null");
3239 }
3240 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003241 final File systemDir = userId == UserHandle.USER_OWNER
3242 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3243 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003244 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3245 if (!inputMethodDir.mkdirs()) {
3246 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3247 }
3248 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3249 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3250 if (!subtypeFile.exists()) {
3251 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003252 writeAdditionalInputMethodSubtypes(
3253 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003254 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003255 readAdditionalInputMethodSubtypes(
3256 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003257 }
3258 }
3259
3260 private void deleteAllInputMethodSubtypes(String imiId) {
3261 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003262 mAdditionalSubtypesMap.remove(imiId);
3263 writeAdditionalInputMethodSubtypes(
3264 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003265 }
3266 }
3267
3268 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003269 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003270 synchronized (mMethodMap) {
3271 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3272 final int N = additionalSubtypes.length;
3273 for (int i = 0; i < N; ++i) {
3274 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003275 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003276 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003277 } else {
3278 Slog.w(TAG, "Duplicated subtype definition found: "
3279 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003280 }
3281 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003282 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3283 writeAdditionalInputMethodSubtypes(
3284 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003285 }
3286 }
3287
3288 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3289 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003290 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003291 }
3292 }
3293
3294 private static void writeAdditionalInputMethodSubtypes(
3295 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3296 HashMap<String, InputMethodInfo> methodMap) {
3297 // Safety net for the case that this function is called before methodMap is set.
3298 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3299 FileOutputStream fos = null;
3300 try {
3301 fos = subtypesFile.startWrite();
3302 final XmlSerializer out = new FastXmlSerializer();
3303 out.setOutput(fos, "utf-8");
3304 out.startDocument(null, true);
3305 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3306 out.startTag(null, NODE_SUBTYPES);
3307 for (String imiId : allSubtypes.keySet()) {
3308 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3309 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3310 continue;
3311 }
3312 out.startTag(null, NODE_IMI);
3313 out.attribute(null, ATTR_ID, imiId);
3314 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3315 final int N = subtypesList.size();
3316 for (int i = 0; i < N; ++i) {
3317 final InputMethodSubtype subtype = subtypesList.get(i);
3318 out.startTag(null, NODE_SUBTYPE);
3319 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3320 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3321 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3322 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3323 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3324 out.attribute(null, ATTR_IS_AUXILIARY,
3325 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3326 out.endTag(null, NODE_SUBTYPE);
3327 }
3328 out.endTag(null, NODE_IMI);
3329 }
3330 out.endTag(null, NODE_SUBTYPES);
3331 out.endDocument();
3332 subtypesFile.finishWrite(fos);
3333 } catch (java.io.IOException e) {
3334 Slog.w(TAG, "Error writing subtypes", e);
3335 if (fos != null) {
3336 subtypesFile.failWrite(fos);
3337 }
3338 }
3339 }
3340
3341 private static void readAdditionalInputMethodSubtypes(
3342 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3343 if (allSubtypes == null || subtypesFile == null) return;
3344 allSubtypes.clear();
3345 FileInputStream fis = null;
3346 try {
3347 fis = subtypesFile.openRead();
3348 final XmlPullParser parser = Xml.newPullParser();
3349 parser.setInput(fis, null);
3350 int type = parser.getEventType();
3351 // Skip parsing until START_TAG
3352 while ((type = parser.next()) != XmlPullParser.START_TAG
3353 && type != XmlPullParser.END_DOCUMENT) {}
3354 String firstNodeName = parser.getName();
3355 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3356 throw new XmlPullParserException("Xml doesn't start with subtypes");
3357 }
3358 final int depth =parser.getDepth();
3359 String currentImiId = null;
3360 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3361 while (((type = parser.next()) != XmlPullParser.END_TAG
3362 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3363 if (type != XmlPullParser.START_TAG)
3364 continue;
3365 final String nodeName = parser.getName();
3366 if (NODE_IMI.equals(nodeName)) {
3367 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3368 if (TextUtils.isEmpty(currentImiId)) {
3369 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3370 continue;
3371 }
3372 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3373 allSubtypes.put(currentImiId, tempSubtypesArray);
3374 } else if (NODE_SUBTYPE.equals(nodeName)) {
3375 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3376 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3377 continue;
3378 }
3379 final int icon = Integer.valueOf(
3380 parser.getAttributeValue(null, ATTR_ICON));
3381 final int label = Integer.valueOf(
3382 parser.getAttributeValue(null, ATTR_LABEL));
3383 final String imeSubtypeLocale =
3384 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3385 final String imeSubtypeMode =
3386 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3387 final String imeSubtypeExtraValue =
3388 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003389 final boolean isAuxiliary = "1".equals(String.valueOf(
3390 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003391 final InputMethodSubtype subtype =
3392 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3393 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3394 tempSubtypesArray.add(subtype);
3395 }
3396 }
3397 } catch (XmlPullParserException e) {
3398 Slog.w(TAG, "Error reading subtypes: " + e);
3399 return;
3400 } catch (java.io.IOException e) {
3401 Slog.w(TAG, "Error reading subtypes: " + e);
3402 return;
3403 } catch (NumberFormatException e) {
3404 Slog.w(TAG, "Error reading subtypes: " + e);
3405 return;
3406 } finally {
3407 if (fis != null) {
3408 try {
3409 fis.close();
3410 } catch (java.io.IOException e1) {
3411 Slog.w(TAG, "Failed to close.");
3412 }
3413 }
3414 }
3415 }
3416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003419 // Utilities for debug
3420 private static String getStackTrace() {
3421 final StringBuilder sb = new StringBuilder();
3422 try {
3423 throw new RuntimeException();
3424 } catch (RuntimeException e) {
3425 final StackTraceElement[] frames = e.getStackTrace();
3426 // Start at 1 because the first frame is here and we don't care about it
3427 for (int j = 1; j < frames.length; ++j) {
3428 sb.append(frames[j].toString() + "\n");
3429 }
3430 }
3431 return sb.toString();
3432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 @Override
3435 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3436 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3437 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3440 + Binder.getCallingPid()
3441 + ", uid=" + Binder.getCallingUid());
3442 return;
3443 }
3444
3445 IInputMethod method;
3446 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 synchronized (mMethodMap) {
3451 p.println("Current Input Method Manager state:");
3452 int N = mMethodList.size();
3453 p.println(" Input Methods:");
3454 for (int i=0; i<N; i++) {
3455 InputMethodInfo info = mMethodList.get(i);
3456 p.println(" InputMethod #" + i + ":");
3457 info.dump(p, " ");
3458 }
3459 p.println(" Clients:");
3460 for (ClientState ci : mClients.values()) {
3461 p.println(" Client " + ci + ":");
3462 p.println(" client=" + ci.client);
3463 p.println(" inputContext=" + ci.inputContext);
3464 p.println(" sessionRequested=" + ci.sessionRequested);
3465 p.println(" curSession=" + ci.curSession);
3466 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003467 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003469 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3470 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3472 + " mBoundToMethod=" + mBoundToMethod);
3473 p.println(" mCurToken=" + mCurToken);
3474 p.println(" mCurIntent=" + mCurIntent);
3475 method = mCurMethod;
3476 p.println(" mCurMethod=" + mCurMethod);
3477 p.println(" mEnabledSession=" + mEnabledSession);
3478 p.println(" mShowRequested=" + mShowRequested
3479 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3480 + " mShowForced=" + mShowForced
3481 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003482 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003484
Jeff Brownb88102f2010-09-08 11:49:43 -07003485 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 pw.flush();
3488 try {
3489 client.client.asBinder().dump(fd, args);
3490 } catch (RemoteException e) {
3491 p.println("Input method client dead: " + e);
3492 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003493 } else {
3494 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003496
Jeff Brownb88102f2010-09-08 11:49:43 -07003497 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 pw.flush();
3500 try {
3501 method.asBinder().dump(fd, args);
3502 } catch (RemoteException e) {
3503 p.println("Input method service dead: " + e);
3504 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003505 } else {
3506 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508 }
3509}