blob: 1a8641badfa34f83c1f39285dd2005a269a8613a [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 Kataokaf1367b72013-01-25 17:20:12 +0900734 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900735 if (!mSystemReady) {
736 // not system ready
737 return;
738 }
739 final Locale newLocale = mRes.getConfiguration().locale;
740 if (!updateOnlyWhenLocaleChanged
741 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
742 if (!updateOnlyWhenLocaleChanged) {
743 hideCurrentInputLocked(0, null);
744 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700745 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900746 }
747 if (DEBUG) {
748 Slog.i(TAG, "Locale has been changed to " + newLocale);
749 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900750 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
751 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900752 buildInputMethodListLocked(mMethodList, mMethodMap,
753 updateOnlyWhenLocaleChanged /* 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() {
778 resetAllInternalStateLocked(true);
779 }
780
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900781 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900783 // InputMethodFileManager should be reset when the user is changed
784 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900785 resetAllInternalStateLocked(false);
786 }
787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 @Override
789 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
790 throws RemoteException {
791 try {
792 return super.onTransact(code, data, reply, flags);
793 } catch (RuntimeException e) {
794 // The input method manager only throws security exceptions, so let's
795 // log all others.
796 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800797 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799 throw e;
800 }
801 }
802
Dianne Hackborn661cd522011-08-22 00:26:20 -0700803 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700804 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900805 if (DEBUG) {
806 Slog.d(TAG, "--- systemReady");
807 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700808 if (!mSystemReady) {
809 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900810 mKeyguardManager =
811 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700812 mNotificationManager = (NotificationManager)
813 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
814 mStatusBar = statusBar;
815 statusBar.setIconVisibility("ime", false);
816 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900817 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
818 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900819 if (mShowOngoingImeSwitcherForPhones) {
820 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
821 mHardKeyboardListener);
822 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900823 buildInputMethodListLocked(mMethodList, mMethodMap,
824 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900825 if (!mImeSelectedOnBoot) {
826 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
827 checkCurrentLocaleChangedLocked();
828 }
829 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700830 try {
831 startInputInnerLocked();
832 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700834 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700835 }
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800838
satok15452a42011-10-28 17:58:28 +0900839 private void setImeWindowVisibilityStatusHiddenLocked() {
840 mImeWindowVis = 0;
841 updateImeWindowStatusLocked();
842 }
843
satok3afd6c02011-11-18 08:38:19 +0900844 private void refreshImeWindowVisibilityLocked() {
845 final Configuration conf = mRes.getConfiguration();
846 final boolean haveHardKeyboard = conf.keyboard
847 != Configuration.KEYBOARD_NOKEYS;
848 final boolean hardKeyShown = haveHardKeyboard
849 && conf.hardKeyboardHidden
850 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500851 final boolean isScreenLocked =
852 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
853 final boolean isScreenSecurelyLocked =
854 isScreenLocked && mKeyguardManager.isKeyguardSecure();
855 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
856 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900857 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
858 updateImeWindowStatusLocked();
859 }
860
satok15452a42011-10-28 17:58:28 +0900861 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900862 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700863 }
864
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900865 // ---------------------------------------------------------------------------------------
866 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
867 // 1) it comes from the system process
868 // 2) the calling process' user id is identical to the current user id IMMS thinks.
869 private boolean calledFromValidUser() {
870 final int uid = Binder.getCallingUid();
871 final int userId = UserHandle.getUserId(uid);
872 if (DEBUG) {
873 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
874 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
875 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900876 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900877 }
878 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
879 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900880 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900881
882 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
883 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
884 // must not manage background users' states in any functions.
885 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
886 // by a token.
887 if (mContext.checkCallingOrSelfPermission(
888 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
889 == PackageManager.PERMISSION_GRANTED) {
890 if (DEBUG) {
891 Slog.d(TAG, "--- Access granted because the calling process has "
892 + "the INTERACT_ACROSS_USERS_FULL permission");
893 }
894 return true;
895 }
896 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
897 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900898 }
899
900 private boolean bindCurrentInputMethodService(
901 Intent service, ServiceConnection conn, int flags) {
902 if (service == null || conn == null) {
903 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
904 return false;
905 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800906 return mContext.bindServiceAsUser(service, conn, flags,
907 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 }
909
satoke7c6998e2011-06-03 17:57:59 +0900910 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 // TODO: Make this work even for non-current users?
913 if (!calledFromValidUser()) {
914 return Collections.emptyList();
915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 synchronized (mMethodMap) {
917 return new ArrayList<InputMethodInfo>(mMethodList);
918 }
919 }
920
satoke7c6998e2011-06-03 17:57:59 +0900921 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900923 // TODO: Make this work even for non-current users?
924 if (!calledFromValidUser()) {
925 return Collections.emptyList();
926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900928 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 }
930 }
931
satokbb4aa062011-01-19 21:40:27 +0900932 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
933 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
934 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
935 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900936 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900937 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900938 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900939 }
940 return enabledInputMethodAndSubtypes;
941 }
942
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900943 /**
944 * @param imi if null, returns enabled subtypes for the current imi
945 * @return enabled subtypes of the specified imi
946 */
satoke7c6998e2011-06-03 17:57:59 +0900947 @Override
satok16331c82010-12-20 23:48:46 +0900948 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
949 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 // TODO: Make this work even for non-current users?
951 if (!calledFromValidUser()) {
952 return Collections.emptyList();
953 }
satok67ddf9c2010-11-17 09:45:54 +0900954 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900955 if (imi == null && mCurMethodId != null) {
956 imi = mMethodMap.get(mCurMethodId);
957 }
958 return mSettings.getEnabledInputMethodSubtypeListLocked(
959 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900960 }
961 }
962
satoke7c6998e2011-06-03 17:57:59 +0900963 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 public void addClient(IInputMethodClient client,
965 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 if (!calledFromValidUser()) {
967 return;
968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 synchronized (mMethodMap) {
970 mClients.put(client.asBinder(), new ClientState(client,
971 inputContext, uid, pid));
972 }
973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800974
satoke7c6998e2011-06-03 17:57:59 +0900975 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!calledFromValidUser()) {
978 return;
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
981 mClients.remove(client.asBinder());
982 }
983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 void executeOrSendMessage(IInterface target, Message msg) {
986 if (target.asBinder() instanceof Binder) {
987 mCaller.sendMessage(msg);
988 } else {
989 handleMessage(msg);
990 msg.recycle();
991 }
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700994 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800996 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 + mCurClient.client.asBinder());
998 if (mBoundToMethod) {
999 mBoundToMethod = false;
1000 if (mCurMethod != null) {
1001 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1002 MSG_UNBIND_INPUT, mCurMethod));
1003 }
1004 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001005
1006 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1007 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1009 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1010 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001012
The Android Open Source Project10592532009-03-18 17:39:46 -07001013 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 private int getImeShowFlags() {
1018 int flags = 0;
1019 if (mShowForced) {
1020 flags |= InputMethod.SHOW_FORCED
1021 | InputMethod.SHOW_EXPLICIT;
1022 } else if (mShowExplicitlyRequested) {
1023 flags |= InputMethod.SHOW_EXPLICIT;
1024 }
1025 return flags;
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 private int getAppShowFlags() {
1029 int flags = 0;
1030 if (mShowForced) {
1031 flags |= InputMethodManager.SHOW_FORCED;
1032 } else if (!mShowExplicitlyRequested) {
1033 flags |= InputMethodManager.SHOW_IMPLICIT;
1034 }
1035 return flags;
1036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
Dianne Hackborn7663d802012-02-24 13:08:49 -08001038 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (!mBoundToMethod) {
1040 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1041 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1042 mBoundToMethod = true;
1043 }
1044 final SessionState session = mCurClient.curSession;
1045 if (initial) {
1046 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1047 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1048 } else {
1049 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1050 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1051 }
1052 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001053 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001054 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001056 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001060 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 // If no method is currently selected, do nothing.
1062 if (mCurMethodId == null) {
1063 return mNoBinding;
1064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 ClientState cs = mClients.get(client.asBinder());
1067 if (cs == null) {
1068 throw new IllegalArgumentException("unknown client "
1069 + client.asBinder());
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 try {
1073 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1074 // Check with the window manager to make sure this client actually
1075 // has a window with focus. If not, reject. This is thread safe
1076 // because if the focus changes some time before or after, the
1077 // next client receiving focus that has any interest in input will
1078 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001079 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1081 return null;
1082 }
1083 } catch (RemoteException e) {
1084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
Dianne Hackborn7663d802012-02-24 13:08:49 -08001086 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1087 }
1088
1089 InputBindResult startInputUncheckedLocked(ClientState cs,
1090 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1091 // If no method is currently selected, do nothing.
1092 if (mCurMethodId == null) {
1093 return mNoBinding;
1094 }
1095
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001096 if (mCurClient == null) {
1097 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1098 if (DEBUG) {
1099 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1100 }
1101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (mCurClient != cs) {
1104 // If the client is changing, we need to switch over to the new
1105 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001106 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001107 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 + cs.client.asBinder());
1109
1110 // If the screen is on, inform the new client it is active
1111 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001112 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1113 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 // Bump up the sequence for this client and attach it.
1118 mCurSeq++;
1119 if (mCurSeq <= 0) mCurSeq = 1;
1120 mCurClient = cs;
1121 mCurInputContext = inputContext;
1122 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // Check if the input method is changing.
1125 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1126 if (cs.curSession != null) {
1127 // Fast case: if we are already connected to the input method,
1128 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001129 return attachNewInputLocked(
1130 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 if (mHaveConnection) {
1133 if (mCurMethod != null) {
1134 if (!cs.sessionRequested) {
1135 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001136 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1138 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001139 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 // Return to client, and we will get back with it when
1142 // we have had a session made for it.
1143 return new InputBindResult(null, mCurId, mCurSeq);
1144 } else if (SystemClock.uptimeMillis()
1145 < (mLastBindTime+TIME_TO_RECONNECT)) {
1146 // In this case we have connected to the service, but
1147 // don't yet have its interface. If it hasn't been too
1148 // long since we did the connection, we'll return to
1149 // the client and wait to get the service interface so
1150 // we can report back. If it has been too long, we want
1151 // to fall through so we can try a disconnect/reconnect
1152 // to see if we can get back in touch with the service.
1153 return new InputBindResult(null, mCurId, mCurSeq);
1154 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1156 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 }
1159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001161 return startInputInnerLocked();
1162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001163
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001164 InputBindResult startInputInnerLocked() {
1165 if (mCurMethodId == null) {
1166 return mNoBinding;
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001169 if (!mSystemReady) {
1170 // If the system is not yet ready, we shouldn't be running third
1171 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001172 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1176 if (info == null) {
1177 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001180 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1183 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001184 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1185 com.android.internal.R.string.input_method_binding_label);
1186 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1187 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001188 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001189 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 mLastBindTime = SystemClock.uptimeMillis();
1191 mHaveConnection = true;
1192 mCurId = info.getId();
1193 mCurToken = new Binder();
1194 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001195 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 mIWindowManager.addWindowToken(mCurToken,
1197 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1198 } catch (RemoteException e) {
1199 }
1200 return new InputBindResult(null, mCurId, mCurSeq);
1201 } else {
1202 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 + mCurIntent);
1205 }
1206 return null;
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001211 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (!calledFromValidUser()) {
1213 return null;
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
1216 final long ident = Binder.clearCallingIdentity();
1217 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001218 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 } finally {
1220 Binder.restoreCallingIdentity(ident);
1221 }
1222 }
1223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001224
satoke7c6998e2011-06-03 17:57:59 +09001225 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 public void finishInput(IInputMethodClient client) {
1227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001228
satoke7c6998e2011-06-03 17:57:59 +09001229 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 public void onServiceConnected(ComponentName name, IBinder service) {
1231 synchronized (mMethodMap) {
1232 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1233 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001234 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001235 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001236 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001237 return;
1238 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001240 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1241 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001244 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001246 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001247 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 }
1250 }
1251 }
1252
1253 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1254 synchronized (mMethodMap) {
1255 if (mCurMethod != null && method != null
1256 && mCurMethod.asBinder() == method.asBinder()) {
1257 if (mCurClient != null) {
1258 mCurClient.curSession = new SessionState(mCurClient,
1259 method, session);
1260 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001261 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 if (res.method != null) {
1263 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1264 MSG_BIND_METHOD, mCurClient.client, res));
1265 }
1266 }
1267 }
1268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001271 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001272 if (mVisibleBound) {
1273 mContext.unbindService(mVisibleConnection);
1274 mVisibleBound = false;
1275 }
1276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001277 if (mHaveConnection) {
1278 mContext.unbindService(this);
1279 mHaveConnection = false;
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001282 if (mCurToken != null) {
1283 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001284 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001285 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001286 // The current IME is shown. Hence an IME switch (transition) is happening.
1287 mWindowManagerService.saveLastInputMethodWindowForTransition();
1288 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001289 mIWindowManager.removeWindowToken(mCurToken);
1290 } catch (RemoteException e) {
1291 }
1292 mCurToken = null;
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Project10592532009-03-18 17:39:46 -07001295 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001296 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001298 if (reportToClient && mCurClient != null) {
1299 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1300 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1301 }
1302 }
1303
Devin Taylor0c33ed22010-02-23 13:26:46 -06001304 private void finishSession(SessionState sessionState) {
1305 if (sessionState != null && sessionState.session != null) {
1306 try {
1307 sessionState.session.finishSession();
1308 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001309 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001310 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001311 }
1312 }
1313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001315 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if (mCurMethod != null) {
1317 for (ClientState cs : mClients.values()) {
1318 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001319 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 cs.curSession = null;
1321 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001322
1323 finishSession(mEnabledSession);
1324 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 mCurMethod = null;
1326 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001327 if (mStatusBar != null) {
1328 mStatusBar.setIconVisibility("ime", false);
1329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
satoke7c6998e2011-06-03 17:57:59 +09001332 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 public void onServiceDisconnected(ComponentName name) {
1334 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 + " mCurIntent=" + mCurIntent);
1337 if (mCurMethod != null && mCurIntent != null
1338 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001339 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // We consider this to be a new bind attempt, since the system
1341 // should now try to restart the service for us.
1342 mLastBindTime = SystemClock.uptimeMillis();
1343 mShowRequested = mInputShown;
1344 mInputShown = false;
1345 if (mCurClient != null) {
1346 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1347 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1348 }
1349 }
1350 }
1351 }
1352
satokf9f01002011-05-19 21:31:50 +09001353 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001355 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 long ident = Binder.clearCallingIdentity();
1357 try {
1358 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001359 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 return;
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 synchronized (mMethodMap) {
1364 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001365 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001366 if (mStatusBar != null) {
1367 mStatusBar.setIconVisibility("ime", false);
1368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001371 CharSequence contentDescription = null;
1372 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001373 // Use PackageManager to load label
1374 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001375 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001376 mIPackageManager.getApplicationInfo(packageName, 0,
1377 mSettings.getCurrentUserId()));
1378 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001379 /* ignore */
1380 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001381 if (mStatusBar != null) {
1382 mStatusBar.setIcon("ime", packageName, iconId, 0,
1383 contentDescription != null
1384 ? contentDescription.toString() : null);
1385 mStatusBar.setIconVisibility("ime", true);
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 }
1389 } finally {
1390 Binder.restoreCallingIdentity(ident);
1391 }
1392 }
1393
satok7cfc0ed2011-06-20 21:29:36 +09001394 private boolean needsToShowImeSwitchOngoingNotification() {
1395 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001396 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001397 synchronized (mMethodMap) {
1398 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1399 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001400 if (N > 2) return true;
1401 if (N < 1) return false;
1402 int nonAuxCount = 0;
1403 int auxCount = 0;
1404 InputMethodSubtype nonAuxSubtype = null;
1405 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001406 for(int i = 0; i < N; ++i) {
1407 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001408 final List<InputMethodSubtype> subtypes =
1409 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001410 final int subtypeCount = subtypes.size();
1411 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001412 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001413 } else {
1414 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001415 final InputMethodSubtype subtype = subtypes.get(j);
1416 if (!subtype.isAuxiliary()) {
1417 ++nonAuxCount;
1418 nonAuxSubtype = subtype;
1419 } else {
1420 ++auxCount;
1421 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001422 }
1423 }
1424 }
satok7cfc0ed2011-06-20 21:29:36 +09001425 }
satokb6359412011-06-28 17:47:41 +09001426 if (nonAuxCount > 1 || auxCount > 1) {
1427 return true;
1428 } else if (nonAuxCount == 1 && auxCount == 1) {
1429 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001430 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1431 || auxSubtype.overridesImplicitlyEnabledSubtype()
1432 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001433 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1434 return false;
1435 }
1436 return true;
1437 }
1438 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001439 }
satok7cfc0ed2011-06-20 21:29:36 +09001440 }
1441
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001442 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001443 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001444 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001445 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001446 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001447 try {
1448 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001449 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001450 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001451 return;
1452 }
1453
1454 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001455 mImeWindowVis = vis;
1456 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001457 if (mStatusBar != null) {
1458 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1459 }
satok7cfc0ed2011-06-20 21:29:36 +09001460 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001461 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1462 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001463 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001464 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001465 final CharSequence title = mRes.getText(
1466 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001467 final CharSequence imiLabel = imi.loadLabel(pm);
1468 final CharSequence summary = mCurrentSubtype != null
1469 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1470 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1471 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001472 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001473 : imiLabel;
1474
satok7cfc0ed2011-06-20 21:29:36 +09001475 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001476 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001477 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001478 if (DEBUG) {
1479 Slog.d(TAG, "--- show notification: label = " + imiLabel
1480 + ", summary = " + summary);
1481 }
1482 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001483 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001484 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001485 mNotificationShown = true;
1486 }
satok7cfc0ed2011-06-20 21:29:36 +09001487 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001488 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001489 if (DEBUG) {
1490 Slog.d(TAG, "--- hide notification");
1491 }
1492 mNotificationManager.cancelAsUser(null,
1493 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001494 mNotificationShown = false;
1495 }
1496 }
satok06487a52010-10-29 11:37:18 +09001497 }
1498 } finally {
1499 Binder.restoreCallingIdentity(ident);
1500 }
1501 }
1502
satoke7c6998e2011-06-03 17:57:59 +09001503 @Override
satokf9f01002011-05-19 21:31:50 +09001504 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 if (!calledFromValidUser()) {
1506 return;
1507 }
satokf9f01002011-05-19 21:31:50 +09001508 synchronized (mMethodMap) {
1509 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1510 for (int i = 0; i < spans.length; ++i) {
1511 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001512 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001513 mSecureSuggestionSpans.put(ss, currentImi);
1514 }
1515 }
1516 }
1517 }
1518
satoke7c6998e2011-06-03 17:57:59 +09001519 @Override
satokf9f01002011-05-19 21:31:50 +09001520 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001521 if (!calledFromValidUser()) {
1522 return false;
1523 }
satokf9f01002011-05-19 21:31:50 +09001524 synchronized (mMethodMap) {
1525 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1526 // TODO: Do not send the intent if the process of the targetImi is already dead.
1527 if (targetImi != null) {
1528 final String[] suggestions = span.getSuggestions();
1529 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001530 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001531 final Intent intent = new Intent();
1532 // Ensures that only a class in the original IME package will receive the
1533 // notification.
satok42c5a162011-05-26 16:46:14 +09001534 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001535 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1536 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1537 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1538 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001539 final long ident = Binder.clearCallingIdentity();
1540 try {
1541 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
satokf9f01002011-05-19 21:31:50 +09001545 return true;
1546 }
1547 }
1548 return false;
1549 }
1550
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001551 void updateFromSettingsLocked(boolean enabledMayChange) {
1552 if (enabledMayChange) {
1553 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1554 for (int i=0; i<enabled.size(); i++) {
1555 // We allow the user to select "disabled until used" apps, so if they
1556 // are enabling one of those here we now need to make it enabled.
1557 InputMethodInfo imm = enabled.get(i);
1558 try {
1559 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1560 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1561 mSettings.getCurrentUserId());
1562 if (ai.enabledSetting
1563 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1564 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1565 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1566 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1567 }
1568 } catch (RemoteException e) {
1569 }
1570 }
1571 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001572 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1573 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1574 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1575 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001576 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001577 // There is no input method selected, try to choose new applicable input method.
1578 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001579 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001580 }
1581 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001583 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001585 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001586 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001587 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
satokf3db1af2010-11-23 13:34:33 +09001589 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 } else {
1591 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001592 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001593 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
satokab751aa2010-09-14 19:17:36 +09001597 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 InputMethodInfo info = mMethodMap.get(id);
1599 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001600 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
satokd81e9502012-05-21 12:58:45 +09001603 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001605 final int subtypeCount = info.getSubtypeCount();
1606 if (subtypeCount <= 0) {
1607 return;
satokcd7cd292010-11-20 15:46:23 +09001608 }
satokd81e9502012-05-21 12:58:45 +09001609 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1610 final InputMethodSubtype newSubtype;
1611 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1612 newSubtype = info.getSubtypeAt(subtypeId);
1613 } else {
1614 // If subtype is null, try to find the most applicable one from
1615 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001616 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001617 }
1618 if (newSubtype == null || oldSubtype == null) {
1619 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1620 + ", new subtype = " + newSubtype);
1621 return;
1622 }
1623 if (newSubtype != oldSubtype) {
1624 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1625 if (mCurMethod != null) {
1626 try {
1627 refreshImeWindowVisibilityLocked();
1628 mCurMethod.changeInputMethodSubtype(newSubtype);
1629 } catch (RemoteException e) {
1630 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001631 }
1632 }
1633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 return;
1635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001636
satokd81e9502012-05-21 12:58:45 +09001637 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 final long ident = Binder.clearCallingIdentity();
1639 try {
satokab751aa2010-09-14 19:17:36 +09001640 // Set a subtype to this input method.
1641 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001642 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1643 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1644 // because mCurMethodId is stored as a history in
1645 // setSelectedInputMethodAndSubtypeLocked().
1646 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647
1648 if (ActivityManagerNative.isSystemReady()) {
1649 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001650 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001652 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001654 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 } finally {
1656 Binder.restoreCallingIdentity(ident);
1657 }
1658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
satok42c5a162011-05-26 16:46:14 +09001660 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001661 public boolean showSoftInput(IInputMethodClient client, int flags,
1662 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001663 if (!calledFromValidUser()) {
1664 return false;
1665 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001666 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 long ident = Binder.clearCallingIdentity();
1668 try {
1669 synchronized (mMethodMap) {
1670 if (mCurClient == null || client == null
1671 || mCurClient.client.asBinder() != client.asBinder()) {
1672 try {
1673 // We need to check if this is the current client with
1674 // focus in the window manager, to allow this call to
1675 // be made before input is started in it.
1676 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001677 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001678 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001681 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001684
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688 } finally {
1689 Binder.restoreCallingIdentity(ident);
1690 }
1691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
The Android Open Source Project4df24232009-03-05 14:34:35 -08001693 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 mShowRequested = true;
1695 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1696 mShowExplicitlyRequested = true;
1697 }
1698 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1699 mShowExplicitlyRequested = true;
1700 mShowForced = true;
1701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
Dianne Hackborncc278702009-09-02 23:07:23 -07001703 if (!mSystemReady) {
1704 return false;
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
The Android Open Source Project4df24232009-03-05 14:34:35 -08001707 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001709 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1710 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1711 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001713 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001714 bindCurrentInputMethodService(
1715 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001716 mVisibleBound = true;
1717 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001718 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001720 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 // The client has asked to have the input method shown, but
1722 // we have been sitting here too long with a connection to the
1723 // service and no interface received, so let's disconnect/connect
1724 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001727 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001729 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001730 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001731 } else {
1732 if (DEBUG) {
1733 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1734 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
The Android Open Source Project4df24232009-03-05 14:34:35 -08001738 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
satok42c5a162011-05-26 16:46:14 +09001741 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 public boolean hideSoftInput(IInputMethodClient client, int flags,
1743 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001744 if (!calledFromValidUser()) {
1745 return false;
1746 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001747 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 long ident = Binder.clearCallingIdentity();
1749 try {
1750 synchronized (mMethodMap) {
1751 if (mCurClient == null || client == null
1752 || mCurClient.client.asBinder() != client.asBinder()) {
1753 try {
1754 // We need to check if this is the current client with
1755 // focus in the window manager, to allow this call to
1756 // be made before input is started in it.
1757 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001758 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1759 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001760 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001761 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001764 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001765 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
Joe Onorato8a9b2202010-02-26 18:56:32 -08001769 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772 } finally {
1773 Binder.restoreCallingIdentity(ident);
1774 }
1775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1779 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001780 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001791 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1792 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1793 res = true;
1794 } else {
1795 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001797 if (mHaveConnection && mVisibleBound) {
1798 mContext.unbindService(mVisibleConnection);
1799 mVisibleBound = false;
1800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 mInputShown = false;
1802 mShowRequested = false;
1803 mShowExplicitlyRequested = false;
1804 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001805 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807
satok42c5a162011-05-26 16:46:14 +09001808 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001809 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1810 int controlFlags, int softInputMode, int windowFlags,
1811 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001812 // Needs to check the validity before clearing calling identity
1813 final boolean calledFromValidUser = calledFromValidUser();
1814
Dianne Hackborn7663d802012-02-24 13:08:49 -08001815 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 long ident = Binder.clearCallingIdentity();
1817 try {
1818 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001819 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001820 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001822 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001823
Dianne Hackborn7663d802012-02-24 13:08:49 -08001824 ClientState cs = mClients.get(client.asBinder());
1825 if (cs == null) {
1826 throw new IllegalArgumentException("unknown client "
1827 + client.asBinder());
1828 }
1829
1830 try {
1831 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1832 // Check with the window manager to make sure this client actually
1833 // has a window with focus. If not, reject. This is thread safe
1834 // because if the focus changes some time before or after, the
1835 // next client receiving focus that has any interest in input will
1836 // be calling through here after that change happens.
1837 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1838 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1839 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001841 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001844 if (!calledFromValidUser) {
1845 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1846 Slog.w(TAG, "If you want to interect with IME, you need "
1847 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1848 hideCurrentInputLocked(0, null);
1849 return null;
1850 }
1851
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001852 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001853 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001854 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001855 if (attribute != null) {
1856 return startInputUncheckedLocked(cs, inputContext, attribute,
1857 controlFlags);
1858 }
1859 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001860 }
1861 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001862
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001863 // Should we auto-show the IME even if the caller has not
1864 // specified what should be done with it?
1865 // We only do this automatically if the window can resize
1866 // to accommodate the IME (so what the user sees will give
1867 // them good context without input information being obscured
1868 // by the IME) or if running on a large screen where there
1869 // is more room for the target window + IME.
1870 final boolean doAutoShow =
1871 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1872 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1873 || mRes.getConfiguration().isLayoutSizeAtLeast(
1874 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001875 final boolean isTextEditor =
1876 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1877
1878 // We want to start input before showing the IME, but after closing
1879 // it. We want to do this after closing it to help the IME disappear
1880 // more quickly (not get stuck behind it initializing itself for the
1881 // new focused input, even if its window wants to hide the IME).
1882 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1885 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001886 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1888 // There is no focus view, and this window will
1889 // be behind any soft input window, so hide the
1890 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001891 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001892 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001894 } else if (isTextEditor && doAutoShow && (softInputMode &
1895 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 // There is a focus view, and we are navigating forward
1897 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001898 // We only do this automatically if the window can resize
1899 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001900 // them good context without input information being obscured
1901 // by the IME) or if running on a large screen where there
1902 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001903 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001904 if (attribute != null) {
1905 res = startInputUncheckedLocked(cs, inputContext, attribute,
1906 controlFlags);
1907 didStart = true;
1908 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001909 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
1911 break;
1912 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1913 // Do nothing.
1914 break;
1915 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1916 if ((softInputMode &
1917 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001918 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001919 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921 break;
1922 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001924 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 break;
1926 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1927 if ((softInputMode &
1928 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001930 if (attribute != null) {
1931 res = startInputUncheckedLocked(cs, inputContext, attribute,
1932 controlFlags);
1933 didStart = true;
1934 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937 break;
1938 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001939 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001940 if (attribute != null) {
1941 res = startInputUncheckedLocked(cs, inputContext, attribute,
1942 controlFlags);
1943 didStart = true;
1944 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001945 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 break;
1947 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001948
1949 if (!didStart && attribute != null) {
1950 res = startInputUncheckedLocked(cs, inputContext, attribute,
1951 controlFlags);
1952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 } finally {
1955 Binder.restoreCallingIdentity(ident);
1956 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001957
1958 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
satok42c5a162011-05-26 16:46:14 +09001961 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001963 if (!calledFromValidUser()) {
1964 return;
1965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 synchronized (mMethodMap) {
1967 if (mCurClient == null || client == null
1968 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001969 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001970 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972
satok440aab52010-11-25 09:43:11 +09001973 // Always call subtype picker, because subtype picker is a superset of input method
1974 // picker.
satokab751aa2010-09-14 19:17:36 +09001975 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1976 }
1977 }
1978
satok42c5a162011-05-26 16:46:14 +09001979 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001981 if (!calledFromValidUser()) {
1982 return;
1983 }
satok28203512010-11-24 11:06:49 +09001984 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1985 }
1986
satok42c5a162011-05-26 16:46:14 +09001987 @Override
satok28203512010-11-24 11:06:49 +09001988 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001989 if (!calledFromValidUser()) {
1990 return;
1991 }
satok28203512010-11-24 11:06:49 +09001992 synchronized (mMethodMap) {
1993 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001994 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09001995 mMethodMap.get(id), subtype.hashCode()));
1996 } else {
1997 setInputMethod(token, id);
1998 }
1999 }
satokab751aa2010-09-14 19:17:36 +09002000 }
2001
satok42c5a162011-05-26 16:46:14 +09002002 @Override
satokb416a712010-11-25 20:42:14 +09002003 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002004 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002005 if (!calledFromValidUser()) {
2006 return;
2007 }
satokb416a712010-11-25 20:42:14 +09002008 synchronized (mMethodMap) {
2009 if (mCurClient == null || client == null
2010 || mCurClient.client.asBinder() != client.asBinder()) {
2011 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2012 }
satok7fee71f2010-12-17 18:54:26 +09002013 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2014 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002015 }
2016 }
2017
satok4fc87d62011-05-20 16:13:43 +09002018 @Override
satok735cf382010-11-11 20:40:09 +09002019 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 if (!calledFromValidUser()) {
2021 return false;
2022 }
satok735cf382010-11-11 20:40:09 +09002023 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002024 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002025 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002026 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002027 lastImi = mMethodMap.get(lastIme.first);
2028 } else {
2029 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002030 }
satok4fc87d62011-05-20 16:13:43 +09002031 String targetLastImiId = null;
2032 int subtypeId = NOT_A_SUBTYPE_ID;
2033 if (lastIme != null && lastImi != null) {
2034 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2035 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2036 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2037 : mCurrentSubtype.hashCode();
2038 // If the last IME is the same as the current IME and the last subtype is not
2039 // defined, there is no need to switch to the last IME.
2040 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2041 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002042 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002043 }
2044 }
2045
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002046 if (TextUtils.isEmpty(targetLastImiId)
2047 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002048 // This is a safety net. If the currentSubtype can't be added to the history
2049 // and the framework couldn't find the last ime, we will make the last ime be
2050 // the most applicable enabled keyboard subtype of the system imes.
2051 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2052 if (enabled != null) {
2053 final int N = enabled.size();
2054 final String locale = mCurrentSubtype == null
2055 ? mRes.getConfiguration().locale.toString()
2056 : mCurrentSubtype.getLocale();
2057 for (int i = 0; i < N; ++i) {
2058 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002059 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002060 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002061 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2062 InputMethodUtils.getSubtypes(imi),
2063 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002064 if (keyboardSubtype != null) {
2065 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002066 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002067 imi, keyboardSubtype.hashCode());
2068 if(keyboardSubtype.getLocale().equals(locale)) {
2069 break;
2070 }
2071 }
2072 }
2073 }
2074 }
2075 }
2076
2077 if (!TextUtils.isEmpty(targetLastImiId)) {
2078 if (DEBUG) {
2079 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2080 + ", from: " + mCurMethodId + ", " + subtypeId);
2081 }
2082 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2083 return true;
2084 } else {
2085 return false;
2086 }
satok735cf382010-11-11 20:40:09 +09002087 }
2088 }
2089
satoke7c6998e2011-06-03 17:57:59 +09002090 @Override
satok688bd472012-02-09 20:09:17 +09002091 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002092 if (!calledFromValidUser()) {
2093 return false;
2094 }
satok688bd472012-02-09 20:09:17 +09002095 synchronized (mMethodMap) {
2096 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2097 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2098 if (nextSubtype == null) {
2099 return false;
2100 }
2101 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2102 return true;
2103 }
2104 }
2105
2106 @Override
satok68f1b782011-04-11 14:26:04 +09002107 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 if (!calledFromValidUser()) {
2109 return null;
2110 }
satok68f1b782011-04-11 14:26:04 +09002111 synchronized (mMethodMap) {
2112 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2113 // TODO: Handle the case of the last IME with no subtypes
2114 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2115 || TextUtils.isEmpty(lastIme.second)) return null;
2116 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2117 if (lastImi == null) return null;
2118 try {
2119 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002120 final int lastSubtypeId =
2121 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002122 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2123 return null;
2124 }
2125 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002126 } catch (NumberFormatException e) {
2127 return null;
2128 }
2129 }
2130 }
2131
satoke7c6998e2011-06-03 17:57:59 +09002132 @Override
satokee5e77c2011-09-02 18:50:15 +09002133 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002134 if (!calledFromValidUser()) {
2135 return;
2136 }
satok91e88122011-07-18 11:11:42 +09002137 // By this IPC call, only a process which shares the same uid with the IME can add
2138 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002139 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002140 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002141 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002142 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 final String[] packageInfos;
2144 try {
2145 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2146 } catch (RemoteException e) {
2147 Slog.e(TAG, "Failed to get package infos");
2148 return;
2149 }
satok91e88122011-07-18 11:11:42 +09002150 if (packageInfos != null) {
2151 final int packageNum = packageInfos.length;
2152 for (int i = 0; i < packageNum; ++i) {
2153 if (packageInfos[i].equals(imi.getPackageName())) {
2154 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002155 final long ident = Binder.clearCallingIdentity();
2156 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002157 buildInputMethodListLocked(mMethodList, mMethodMap,
2158 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002159 } finally {
2160 Binder.restoreCallingIdentity(ident);
2161 }
satokee5e77c2011-09-02 18:50:15 +09002162 return;
satok91e88122011-07-18 11:11:42 +09002163 }
2164 }
2165 }
satoke7c6998e2011-06-03 17:57:59 +09002166 }
satokee5e77c2011-09-02 18:50:15 +09002167 return;
satoke7c6998e2011-06-03 17:57:59 +09002168 }
2169
satok28203512010-11-24 11:06:49 +09002170 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 synchronized (mMethodMap) {
2172 if (token == null) {
2173 if (mContext.checkCallingOrSelfPermission(
2174 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2175 != PackageManager.PERMISSION_GRANTED) {
2176 throw new SecurityException(
2177 "Using null token requires permission "
2178 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2179 }
2180 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002181 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2182 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 return;
2184 }
2185
satokc5933802011-08-31 21:26:04 +09002186 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 try {
satokab751aa2010-09-14 19:17:36 +09002188 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 } finally {
2190 Binder.restoreCallingIdentity(ident);
2191 }
2192 }
2193 }
2194
satok42c5a162011-05-26 16:46:14 +09002195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002197 if (!calledFromValidUser()) {
2198 return;
2199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 synchronized (mMethodMap) {
2201 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002202 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2203 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 return;
2205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 long ident = Binder.clearCallingIdentity();
2207 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 hideCurrentInputLocked(flags, null);
2209 } finally {
2210 Binder.restoreCallingIdentity(ident);
2211 }
2212 }
2213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002214
satok42c5a162011-05-26 16:46:14 +09002215 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002216 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002217 if (!calledFromValidUser()) {
2218 return;
2219 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 synchronized (mMethodMap) {
2221 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002222 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2223 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002224 return;
2225 }
2226 long ident = Binder.clearCallingIdentity();
2227 try {
2228 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 } finally {
2230 Binder.restoreCallingIdentity(ident);
2231 }
2232 }
2233 }
2234
2235 void setEnabledSessionInMainThread(SessionState session) {
2236 if (mEnabledSession != session) {
2237 if (mEnabledSession != null) {
2238 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002239 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 mEnabledSession.method.setSessionEnabled(
2241 mEnabledSession.session, false);
2242 } catch (RemoteException e) {
2243 }
2244 }
2245 mEnabledSession = session;
2246 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002247 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 session.method.setSessionEnabled(
2249 session.session, true);
2250 } catch (RemoteException e) {
2251 }
2252 }
2253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
satok42c5a162011-05-26 16:46:14 +09002255 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002257 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 switch (msg.what) {
2259 case MSG_SHOW_IM_PICKER:
2260 showInputMethodMenu();
2261 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002262
satokab751aa2010-09-14 19:17:36 +09002263 case MSG_SHOW_IM_SUBTYPE_PICKER:
2264 showInputMethodSubtypeMenu();
2265 return true;
2266
satok47a44912010-10-06 16:03:58 +09002267 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002268 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002269 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002270 args.recycle();
satok217f5482010-12-15 05:19:19 +09002271 return true;
2272
2273 case MSG_SHOW_IM_CONFIG:
2274 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002275 return true;
2276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 case MSG_UNBIND_INPUT:
2280 try {
2281 ((IInputMethod)msg.obj).unbindInput();
2282 } catch (RemoteException e) {
2283 // There is nothing interesting about the method dying.
2284 }
2285 return true;
2286 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002287 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 try {
2289 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2290 } catch (RemoteException e) {
2291 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002292 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 return true;
2294 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002295 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002297 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2298 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 } catch (RemoteException e) {
2300 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002301 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 return true;
2303 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002304 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002306 ((IInputMethod)args.arg1).hideSoftInput(0,
2307 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 } catch (RemoteException e) {
2309 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002310 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 return true;
2312 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002313 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002315 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2317 } catch (RemoteException e) {
2318 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002319 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 return true;
2321 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002322 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 try {
2324 ((IInputMethod)args.arg1).createSession(
2325 (IInputMethodCallback)args.arg2);
2326 } catch (RemoteException e) {
2327 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002328 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002333 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 try {
2335 SessionState session = (SessionState)args.arg1;
2336 setEnabledSessionInMainThread(session);
2337 session.method.startInput((IInputContext)args.arg2,
2338 (EditorInfo)args.arg3);
2339 } catch (RemoteException e) {
2340 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002341 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 return true;
2343 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 try {
2346 SessionState session = (SessionState)args.arg1;
2347 setEnabledSessionInMainThread(session);
2348 session.method.restartInput((IInputContext)args.arg2,
2349 (EditorInfo)args.arg3);
2350 } catch (RemoteException e) {
2351 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002352 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 case MSG_UNBIND_METHOD:
2358 try {
2359 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2360 } catch (RemoteException e) {
2361 // There is nothing interesting about the last client dying.
2362 }
2363 return true;
2364 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002365 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 try {
2367 ((IInputMethodClient)args.arg1).onBindMethod(
2368 (InputBindResult)args.arg2);
2369 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002372 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002374 case MSG_SET_ACTIVE:
2375 try {
2376 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2377 } catch (RemoteException e) {
2378 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2379 + ((ClientState)msg.obj).pid + " uid "
2380 + ((ClientState)msg.obj).uid);
2381 }
2382 return true;
satok01038492012-04-09 21:08:27 +09002383
2384 // --------------------------------------------------------------
2385 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2386 mHardKeyboardListener.handleHardKeyboardStatusChange(
2387 msg.arg1 == 1, msg.arg2 == 1);
2388 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390 return false;
2391 }
2392
satokdc9ddae2011-10-06 12:22:36 +09002393 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002394 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2395 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002396 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002397 if (DEBUG) {
2398 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2399 }
satok723a27e2010-11-11 14:58:11 +09002400 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002401 return true;
2402 }
2403
2404 return false;
2405 }
2406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002408 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002409 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002410 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2411 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 list.clear();
2414 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002415
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 // Use for queryIntentServicesAsUser
2417 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002418 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002419 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002423 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2424 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002425
satoke7c6998e2011-06-03 17:57:59 +09002426 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2427 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 for (int i = 0; i < services.size(); ++i) {
2429 ResolveInfo ri = services.get(i);
2430 ServiceInfo si = ri.serviceInfo;
2431 ComponentName compName = new ComponentName(si.packageName, si.name);
2432 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2433 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002434 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 + ": it does not require the permission "
2436 + android.Manifest.permission.BIND_INPUT_METHOD);
2437 continue;
2438 }
2439
Joe Onorato8a9b2202010-02-26 18:56:32 -08002440 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441
2442 try {
satoke7c6998e2011-06-03 17:57:59 +09002443 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002445 final String id = p.getId();
2446 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447
2448 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002449 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002453 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002455 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
2457 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002458
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002459 if (resetDefaultEnabledIme) {
2460 final ArrayList<InputMethodInfo> defaultEnabledIme =
2461 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2462 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2463 final InputMethodInfo imi = defaultEnabledIme.get(i);
2464 if (DEBUG) {
2465 Slog.d(TAG, "--- enable ime = " + imi);
2466 }
2467 setInputMethodEnabledLocked(imi.getId(), true);
2468 }
2469 }
2470
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002471 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002472 if (!TextUtils.isEmpty(defaultImiId)) {
2473 if (!map.containsKey(defaultImiId)) {
2474 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2475 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002476 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002477 }
2478 } else {
2479 // Double check that the default IME is certainly enabled.
2480 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002481 }
2482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002486
satokab751aa2010-09-14 19:17:36 +09002487 private void showInputMethodMenu() {
2488 showInputMethodMenuInternal(false);
2489 }
2490
2491 private void showInputMethodSubtypeMenu() {
2492 showInputMethodMenuInternal(true);
2493 }
2494
satok217f5482010-12-15 05:19:19 +09002495 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002496 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002497 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002498 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2499 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002500 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002501 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002502 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002503 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002504 }
2505
2506 private void showConfigureInputMethods() {
2507 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2508 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2509 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2510 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002511 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002512 }
2513
satok2c93efc2012-04-02 19:33:47 +09002514 private boolean isScreenLocked() {
2515 return mKeyguardManager != null
2516 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2517 }
satokab751aa2010-09-14 19:17:36 +09002518 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002519 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002522 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002523
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002524 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002525 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002526 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002527
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002528 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002529 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2530 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002531 if (immis == null || immis.size() == 0) {
2532 return;
2533 }
2534
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002535 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536
satok688bd472012-02-09 20:09:17 +09002537 final List<ImeSubtypeListItem> imList =
2538 mImListManager.getSortedInputMethodAndSubtypeList(
2539 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002540
satokc3690562012-01-10 20:14:43 +09002541 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002542 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002543 if (currentSubtype != null) {
2544 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002545 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2546 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002547 }
2548 }
2549
Ken Wakasa761eb372011-03-04 19:06:18 +09002550 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002551 mIms = new InputMethodInfo[N];
2552 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002553 int checkedItem = 0;
2554 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002555 final ImeSubtypeListItem item = imList.get(i);
2556 mIms[i] = item.mImi;
2557 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002558 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002559 int subtypeId = mSubtypeIds[i];
2560 if ((subtypeId == NOT_A_SUBTYPE_ID)
2561 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2562 || (subtypeId == lastInputMethodSubtypeId)) {
2563 checkedItem = i;
2564 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002567 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002568 com.android.internal.R.styleable.DialogPreference,
2569 com.android.internal.R.attr.alertDialogStyle, 0);
2570 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002571 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002572 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002573 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 }
2576 })
2577 .setIcon(a.getDrawable(
2578 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2579 a.recycle();
satok01038492012-04-09 21:08:27 +09002580 final LayoutInflater inflater =
2581 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2582 final View tv = inflater.inflate(
2583 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2584 mDialogBuilder.setCustomTitle(tv);
2585
2586 // Setup layout for a toggle switch of the hardware keyboard
2587 mSwitchingDialogTitleView = tv;
2588 mSwitchingDialogTitleView.findViewById(
2589 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2590 mWindowManagerService.isHardKeyboardAvailable() ?
2591 View.VISIBLE : View.GONE);
2592 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2593 com.android.internal.R.id.hard_keyboard_switch));
2594 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2595 hardKeySwitch.setOnCheckedChangeListener(
2596 new OnCheckedChangeListener() {
2597 @Override
2598 public void onCheckedChanged(
2599 CompoundButton buttonView, boolean isChecked) {
2600 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002601 // Ensure that the input method dialog is dismissed when changing
2602 // the hardware keyboard state.
2603 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002604 }
2605 });
satokd87c2592010-09-29 11:52:06 +09002606
Ken Wakasa05dbb652011-08-22 15:22:43 +09002607 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2608 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2609 checkedItem);
2610
2611 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002612 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002613 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002614 public void onClick(DialogInterface dialog, int which) {
2615 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002616 if (mIms == null || mIms.length <= which
2617 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002618 return;
2619 }
2620 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002621 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002622 adapter.mCheckedItem = which;
2623 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002624 hideInputMethodMenu();
2625 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002626 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002627 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002628 subtypeId = NOT_A_SUBTYPE_ID;
2629 }
2630 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002631 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002634 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635
satokbc81b692011-08-26 16:22:22 +09002636 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002637 mDialogBuilder.setPositiveButton(
2638 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002639 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002640 @Override
satok7f35c8c2010-10-07 21:13:11 +09002641 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002642 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002643 }
2644 });
2645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002647 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 mSwitchingDialog.getWindow().setType(
2649 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002650 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2651 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002652 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 mSwitchingDialog.show();
2654 }
2655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002656
satok93d744d2012-05-09 17:14:08 +09002657 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002658 public final CharSequence mImeName;
2659 public final CharSequence mSubtypeName;
2660 public final InputMethodInfo mImi;
2661 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002662 private final boolean mIsSystemLocale;
2663 private final boolean mIsSystemLanguage;
2664
Ken Wakasa05dbb652011-08-22 15:22:43 +09002665 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002666 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002667 mImeName = imeName;
2668 mSubtypeName = subtypeName;
2669 mImi = imi;
2670 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002671 if (TextUtils.isEmpty(subtypeLocale)) {
2672 mIsSystemLocale = false;
2673 mIsSystemLanguage = false;
2674 } else {
2675 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2676 mIsSystemLanguage = mIsSystemLocale
2677 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2678 }
2679 }
2680
2681 @Override
2682 public int compareTo(ImeSubtypeListItem other) {
2683 if (TextUtils.isEmpty(mImeName)) {
2684 return 1;
2685 }
2686 if (TextUtils.isEmpty(other.mImeName)) {
2687 return -1;
2688 }
2689 if (!TextUtils.equals(mImeName, other.mImeName)) {
2690 return mImeName.toString().compareTo(other.mImeName.toString());
2691 }
2692 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2693 return 0;
2694 }
2695 if (mIsSystemLocale) {
2696 return -1;
2697 }
2698 if (other.mIsSystemLocale) {
2699 return 1;
2700 }
2701 if (mIsSystemLanguage) {
2702 return -1;
2703 }
2704 if (other.mIsSystemLanguage) {
2705 return 1;
2706 }
2707 if (TextUtils.isEmpty(mSubtypeName)) {
2708 return 1;
2709 }
2710 if (TextUtils.isEmpty(other.mSubtypeName)) {
2711 return -1;
2712 }
2713 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002714 }
2715 }
2716
2717 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2718 private final LayoutInflater mInflater;
2719 private final int mTextViewResourceId;
2720 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002721 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002722 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2723 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2724 super(context, textViewResourceId, itemsList);
2725 mTextViewResourceId = textViewResourceId;
2726 mItemsList = itemsList;
2727 mCheckedItem = checkedItem;
2728 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2729 }
2730
2731 @Override
2732 public View getView(int position, View convertView, ViewGroup parent) {
2733 final View view = convertView != null ? convertView
2734 : mInflater.inflate(mTextViewResourceId, null);
2735 if (position < 0 || position >= mItemsList.size()) return view;
2736 final ImeSubtypeListItem item = mItemsList.get(position);
2737 final CharSequence imeName = item.mImeName;
2738 final CharSequence subtypeName = item.mSubtypeName;
2739 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2740 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2741 if (TextUtils.isEmpty(subtypeName)) {
2742 firstTextView.setText(imeName);
2743 secondTextView.setVisibility(View.GONE);
2744 } else {
2745 firstTextView.setText(subtypeName);
2746 secondTextView.setText(imeName);
2747 secondTextView.setVisibility(View.VISIBLE);
2748 }
2749 final RadioButton radioButton =
2750 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2751 radioButton.setChecked(position == mCheckedItem);
2752 return view;
2753 }
2754 }
2755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002757 synchronized (mMethodMap) {
2758 hideInputMethodMenuLocked();
2759 }
2760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002761
The Android Open Source Project10592532009-03-18 17:39:46 -07002762 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
The Android Open Source Project10592532009-03-18 17:39:46 -07002765 if (mSwitchingDialog != null) {
2766 mSwitchingDialog.dismiss();
2767 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002769
The Android Open Source Project10592532009-03-18 17:39:46 -07002770 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002771 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002775
satok42c5a162011-05-26 16:46:14 +09002776 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002778 // TODO: Make this work even for non-current users?
2779 if (!calledFromValidUser()) {
2780 return false;
2781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 synchronized (mMethodMap) {
2783 if (mContext.checkCallingOrSelfPermission(
2784 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2785 != PackageManager.PERMISSION_GRANTED) {
2786 throw new SecurityException(
2787 "Requires permission "
2788 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2789 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 long ident = Binder.clearCallingIdentity();
2792 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002793 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 } finally {
2795 Binder.restoreCallingIdentity(ident);
2796 }
2797 }
2798 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002799
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002800 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2801 // Make sure this is a valid input method.
2802 InputMethodInfo imm = mMethodMap.get(id);
2803 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002804 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002805 }
2806
satokd87c2592010-09-29 11:52:06 +09002807 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2808 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002809
satokd87c2592010-09-29 11:52:06 +09002810 if (enabled) {
2811 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2812 if (pair.first.equals(id)) {
2813 // We are enabling this input method, but it is already enabled.
2814 // Nothing to do. The previous state was enabled.
2815 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002816 }
2817 }
satokd87c2592010-09-29 11:52:06 +09002818 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2819 // Previous state was disabled.
2820 return false;
2821 } else {
2822 StringBuilder builder = new StringBuilder();
2823 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2824 builder, enabledInputMethodsList, id)) {
2825 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002826 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002827 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2828 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2829 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002830 }
2831 // Previous state was enabled.
2832 return true;
2833 } else {
2834 // We are disabling the input method but it is already disabled.
2835 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002836 return false;
2837 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002839 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002840
satok723a27e2010-11-11 14:58:11 +09002841 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2842 boolean setSubtypeOnly) {
2843 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002844 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002845
2846 // Set Subtype here
2847 if (imi == null || subtypeId < 0) {
2848 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002849 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002850 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002851 if (subtypeId < imi.getSubtypeCount()) {
2852 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2853 mSettings.putSelectedSubtype(subtype.hashCode());
2854 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002855 } else {
2856 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002857 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002858 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002859 }
satokab751aa2010-09-14 19:17:36 +09002860 }
satok723a27e2010-11-11 14:58:11 +09002861
satok4c0e7152012-06-20 20:08:44 +09002862 // Workaround.
2863 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2864 // IMEs are not recognized and considered uninstalled.
2865 // Actually, we can't move everything after SystemReady because
2866 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2867 // the default IME here and try cheking the default IME again in systemReady().
2868 // TODO: Do nothing before system ready and implement a separated logic for
2869 // the encryption lock screen.
2870 // TODO: ASEC should be ready before IMMS is instantiated.
2871 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002872 // Set InputMethod here
2873 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2874 }
2875 }
2876
2877 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2878 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2879 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2880 // newDefaultIme is empty when there is no candidate for the selected IME.
2881 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2882 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2883 if (subtypeHashCode != null) {
2884 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002885 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002886 imi, Integer.valueOf(subtypeHashCode));
2887 } catch (NumberFormatException e) {
2888 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2889 }
2890 }
2891 }
2892 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002893 }
2894
satok4e4569d2010-11-19 18:45:53 +09002895 // If there are no selected shortcuts, tries finding the most applicable ones.
2896 private Pair<InputMethodInfo, InputMethodSubtype>
2897 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2898 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2899 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002900 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002901 boolean foundInSystemIME = false;
2902
2903 // Search applicable subtype for each InputMethodInfo
2904 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002905 final String imiId = imi.getId();
2906 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2907 continue;
2908 }
satokcd7cd292010-11-20 15:46:23 +09002909 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002910 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002911 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002912 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002913 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002914 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002915 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002916 }
satokdf31ae62011-01-15 06:19:44 +09002917 // 2. Search by the system locale from enabledSubtypes.
2918 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002919 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002920 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002921 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002922 }
satoka86f5e42011-09-02 17:12:42 +09002923 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002924 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002925 final ArrayList<InputMethodSubtype> subtypesForSearch =
2926 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002927 ? InputMethodUtils.getSubtypes(imi)
2928 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002929 // 4. Search by the current subtype's locale from all subtypes.
2930 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002931 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002932 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002933 }
2934 // 5. Search by the system locale from all subtypes.
2935 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002936 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002937 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002938 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002939 }
satokcd7cd292010-11-20 15:46:23 +09002940 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002941 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002942 // The current input method is the most applicable IME.
2943 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002944 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002945 break;
satok7599a7f2010-12-22 13:45:23 +09002946 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002947 // The system input method is 2nd applicable IME.
2948 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002949 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002950 if ((imi.getServiceInfo().applicationInfo.flags
2951 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2952 foundInSystemIME = true;
2953 }
satok4e4569d2010-11-19 18:45:53 +09002954 }
2955 }
2956 }
2957 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002958 if (mostApplicableIMI != null) {
2959 Slog.w(TAG, "Most applicable shortcut input method was:"
2960 + mostApplicableIMI.getId());
2961 if (mostApplicableSubtype != null) {
2962 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2963 + "," + mostApplicableSubtype.getMode() + ","
2964 + mostApplicableSubtype.getLocale());
2965 }
2966 }
satok4e4569d2010-11-19 18:45:53 +09002967 }
satokcd7cd292010-11-20 15:46:23 +09002968 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002969 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002970 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002971 } else {
2972 return null;
2973 }
2974 }
2975
satokab751aa2010-09-14 19:17:36 +09002976 /**
2977 * @return Return the current subtype of this input method.
2978 */
satok42c5a162011-05-26 16:46:14 +09002979 @Override
satokab751aa2010-09-14 19:17:36 +09002980 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002981 // TODO: Make this work even for non-current users?
2982 if (!calledFromValidUser()) {
2983 return null;
2984 }
2985 synchronized (mMethodMap) {
2986 return getCurrentInputMethodSubtypeLocked();
2987 }
2988 }
2989
2990 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002991 if (mCurMethodId == null) {
2992 return null;
2993 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002994 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002995 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2996 if (imi == null || imi.getSubtypeCount() == 0) {
2997 return null;
satok4e4569d2010-11-19 18:45:53 +09002998 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002999 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003000 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3001 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003002 if (subtypeId == NOT_A_SUBTYPE_ID) {
3003 // If there are no selected subtypes, the framework will try to find
3004 // the most applicable subtype from explicitly or implicitly enabled
3005 // subtypes.
3006 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003007 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003008 // If there is only one explicitly or implicitly enabled subtype,
3009 // just returns it.
3010 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3011 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3012 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003013 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003014 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003015 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003016 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003017 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003018 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3019 true);
satok4e4569d2010-11-19 18:45:53 +09003020 }
satok3ef8b292010-11-23 06:06:29 +09003021 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003022 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003023 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003024 }
3025 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003026 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003027 }
3028
satokf3db1af2010-11-23 13:34:33 +09003029 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3030 InputMethodSubtype subtype) {
3031 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3032 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3033 } else {
3034 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3035 subtypes.add(subtype);
3036 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3037 }
3038 }
3039
satok4e4569d2010-11-19 18:45:53 +09003040 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003041 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003042 @Override
satok4e4569d2010-11-19 18:45:53 +09003043 public List getShortcutInputMethodsAndSubtypes() {
3044 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003045 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003046 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003047 // If there are no selected shortcut subtypes, the framework will try to find
3048 // the most applicable subtype from all subtypes whose mode is
3049 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003050 Pair<InputMethodInfo, InputMethodSubtype> info =
3051 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003052 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003053 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003054 ret.add(info.first);
3055 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003056 }
satok3da92232011-01-11 22:46:30 +09003057 return ret;
satokf3db1af2010-11-23 13:34:33 +09003058 }
satokf3db1af2010-11-23 13:34:33 +09003059 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3060 ret.add(imi);
3061 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3062 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003063 }
3064 }
satokf3db1af2010-11-23 13:34:33 +09003065 return ret;
satok4e4569d2010-11-19 18:45:53 +09003066 }
3067 }
3068
satok42c5a162011-05-26 16:46:14 +09003069 @Override
satokb66d2872010-11-10 01:04:04 +09003070 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003071 // TODO: Make this work even for non-current users?
3072 if (!calledFromValidUser()) {
3073 return false;
3074 }
satokb66d2872010-11-10 01:04:04 +09003075 synchronized (mMethodMap) {
3076 if (subtype != null && mCurMethodId != null) {
3077 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003078 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003079 if (subtypeId != NOT_A_SUBTYPE_ID) {
3080 setInputMethodLocked(mCurMethodId, subtypeId);
3081 return true;
3082 }
3083 }
3084 return false;
3085 }
3086 }
3087
satok688bd472012-02-09 20:09:17 +09003088 private static class InputMethodAndSubtypeListManager {
3089 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003090 // Used to load label
satok688bd472012-02-09 20:09:17 +09003091 private final PackageManager mPm;
3092 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003093 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003094 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3095 mContext = context;
3096 mPm = context.getPackageManager();
3097 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003098 final Locale locale = context.getResources().getConfiguration().locale;
3099 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003100 }
3101
3102 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3103 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3104 new Comparator<InputMethodInfo>() {
3105 @Override
3106 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3107 if (imi2 == null) return 0;
3108 if (imi1 == null) return 1;
3109 if (mPm == null) {
3110 return imi1.getId().compareTo(imi2.getId());
3111 }
3112 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3113 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3114 return imiId1.toString().compareTo(imiId2.toString());
3115 }
3116 });
3117
3118 public ImeSubtypeListItem getNextInputMethod(
3119 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3120 if (imi == null) {
3121 return null;
3122 }
3123 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3124 if (imList.size() <= 1) {
3125 return null;
3126 }
3127 final int N = imList.size();
3128 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003129 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003130 : NOT_A_SUBTYPE_ID;
3131 for (int i = 0; i < N; ++i) {
3132 final ImeSubtypeListItem isli = imList.get(i);
3133 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3134 if (!onlyCurrentIme) {
3135 return imList.get((i + 1) % N);
3136 }
3137 for (int j = 0; j < N - 1; ++j) {
3138 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3139 if (candidate.mImi.equals(imi)) {
3140 return candidate;
3141 }
3142 }
3143 return null;
3144 }
3145 }
3146 return null;
3147 }
3148
3149 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3150 return getSortedInputMethodAndSubtypeList(true, false, false);
3151 }
3152
3153 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3154 boolean inputShown, boolean isScreenLocked) {
3155 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3156 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3157 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3158 if (immis == null || immis.size() == 0) {
3159 return Collections.emptyList();
3160 }
3161 mSortedImmis.clear();
3162 mSortedImmis.putAll(immis);
3163 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3164 if (imi == null) continue;
3165 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3166 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3167 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3168 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3169 }
satok688bd472012-02-09 20:09:17 +09003170 final CharSequence imeLabel = imi.loadLabel(mPm);
3171 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3172 final int subtypeCount = imi.getSubtypeCount();
3173 if (DEBUG) {
3174 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3175 }
3176 for (int j = 0; j < subtypeCount; ++j) {
3177 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3178 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3179 // We show all enabled IMEs and subtypes when an IME is shown.
3180 if (enabledSubtypeSet.contains(subtypeHashCode)
3181 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3182 final CharSequence subtypeLabel =
3183 subtype.overridesImplicitlyEnabledSubtype() ? null
3184 : subtype.getDisplayName(mContext, imi.getPackageName(),
3185 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003186 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3187 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003188
3189 // Removing this subtype from enabledSubtypeSet because we no longer
3190 // need to add an entry of this subtype to imList to avoid duplicated
3191 // entries.
3192 enabledSubtypeSet.remove(subtypeHashCode);
3193 }
3194 }
3195 } else {
satok93d744d2012-05-09 17:14:08 +09003196 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3197 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003198 }
3199 }
satok93d744d2012-05-09 17:14:08 +09003200 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003201 return imList;
3202 }
3203 }
3204
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003205 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003206 private static class InputMethodFileManager {
3207 private static final String SYSTEM_PATH = "system";
3208 private static final String INPUT_METHOD_PATH = "inputmethod";
3209 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3210 private static final String NODE_SUBTYPES = "subtypes";
3211 private static final String NODE_SUBTYPE = "subtype";
3212 private static final String NODE_IMI = "imi";
3213 private static final String ATTR_ID = "id";
3214 private static final String ATTR_LABEL = "label";
3215 private static final String ATTR_ICON = "icon";
3216 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3217 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3218 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3219 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3220 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3221 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003222 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003223 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003224 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003225 if (methodMap == null) {
3226 throw new NullPointerException("methodMap is null");
3227 }
3228 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003229 final File systemDir = userId == UserHandle.USER_OWNER
3230 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3231 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003232 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3233 if (!inputMethodDir.mkdirs()) {
3234 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3235 }
3236 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3237 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3238 if (!subtypeFile.exists()) {
3239 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003240 writeAdditionalInputMethodSubtypes(
3241 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003242 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003243 readAdditionalInputMethodSubtypes(
3244 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003245 }
3246 }
3247
3248 private void deleteAllInputMethodSubtypes(String imiId) {
3249 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003250 mAdditionalSubtypesMap.remove(imiId);
3251 writeAdditionalInputMethodSubtypes(
3252 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003253 }
3254 }
3255
3256 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003257 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003258 synchronized (mMethodMap) {
3259 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3260 final int N = additionalSubtypes.length;
3261 for (int i = 0; i < N; ++i) {
3262 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003263 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003264 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003265 } else {
3266 Slog.w(TAG, "Duplicated subtype definition found: "
3267 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003268 }
3269 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003270 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3271 writeAdditionalInputMethodSubtypes(
3272 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003273 }
3274 }
3275
3276 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3277 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003278 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003279 }
3280 }
3281
3282 private static void writeAdditionalInputMethodSubtypes(
3283 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3284 HashMap<String, InputMethodInfo> methodMap) {
3285 // Safety net for the case that this function is called before methodMap is set.
3286 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3287 FileOutputStream fos = null;
3288 try {
3289 fos = subtypesFile.startWrite();
3290 final XmlSerializer out = new FastXmlSerializer();
3291 out.setOutput(fos, "utf-8");
3292 out.startDocument(null, true);
3293 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3294 out.startTag(null, NODE_SUBTYPES);
3295 for (String imiId : allSubtypes.keySet()) {
3296 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3297 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3298 continue;
3299 }
3300 out.startTag(null, NODE_IMI);
3301 out.attribute(null, ATTR_ID, imiId);
3302 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3303 final int N = subtypesList.size();
3304 for (int i = 0; i < N; ++i) {
3305 final InputMethodSubtype subtype = subtypesList.get(i);
3306 out.startTag(null, NODE_SUBTYPE);
3307 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3308 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3309 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3310 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3311 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3312 out.attribute(null, ATTR_IS_AUXILIARY,
3313 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3314 out.endTag(null, NODE_SUBTYPE);
3315 }
3316 out.endTag(null, NODE_IMI);
3317 }
3318 out.endTag(null, NODE_SUBTYPES);
3319 out.endDocument();
3320 subtypesFile.finishWrite(fos);
3321 } catch (java.io.IOException e) {
3322 Slog.w(TAG, "Error writing subtypes", e);
3323 if (fos != null) {
3324 subtypesFile.failWrite(fos);
3325 }
3326 }
3327 }
3328
3329 private static void readAdditionalInputMethodSubtypes(
3330 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3331 if (allSubtypes == null || subtypesFile == null) return;
3332 allSubtypes.clear();
3333 FileInputStream fis = null;
3334 try {
3335 fis = subtypesFile.openRead();
3336 final XmlPullParser parser = Xml.newPullParser();
3337 parser.setInput(fis, null);
3338 int type = parser.getEventType();
3339 // Skip parsing until START_TAG
3340 while ((type = parser.next()) != XmlPullParser.START_TAG
3341 && type != XmlPullParser.END_DOCUMENT) {}
3342 String firstNodeName = parser.getName();
3343 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3344 throw new XmlPullParserException("Xml doesn't start with subtypes");
3345 }
3346 final int depth =parser.getDepth();
3347 String currentImiId = null;
3348 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3349 while (((type = parser.next()) != XmlPullParser.END_TAG
3350 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3351 if (type != XmlPullParser.START_TAG)
3352 continue;
3353 final String nodeName = parser.getName();
3354 if (NODE_IMI.equals(nodeName)) {
3355 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3356 if (TextUtils.isEmpty(currentImiId)) {
3357 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3358 continue;
3359 }
3360 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3361 allSubtypes.put(currentImiId, tempSubtypesArray);
3362 } else if (NODE_SUBTYPE.equals(nodeName)) {
3363 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3364 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3365 continue;
3366 }
3367 final int icon = Integer.valueOf(
3368 parser.getAttributeValue(null, ATTR_ICON));
3369 final int label = Integer.valueOf(
3370 parser.getAttributeValue(null, ATTR_LABEL));
3371 final String imeSubtypeLocale =
3372 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3373 final String imeSubtypeMode =
3374 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3375 final String imeSubtypeExtraValue =
3376 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003377 final boolean isAuxiliary = "1".equals(String.valueOf(
3378 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003379 final InputMethodSubtype subtype =
3380 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3381 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3382 tempSubtypesArray.add(subtype);
3383 }
3384 }
3385 } catch (XmlPullParserException e) {
3386 Slog.w(TAG, "Error reading subtypes: " + e);
3387 return;
3388 } catch (java.io.IOException e) {
3389 Slog.w(TAG, "Error reading subtypes: " + e);
3390 return;
3391 } catch (NumberFormatException e) {
3392 Slog.w(TAG, "Error reading subtypes: " + e);
3393 return;
3394 } finally {
3395 if (fis != null) {
3396 try {
3397 fis.close();
3398 } catch (java.io.IOException e1) {
3399 Slog.w(TAG, "Failed to close.");
3400 }
3401 }
3402 }
3403 }
3404 }
3405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003407 // Utilities for debug
3408 private static String getStackTrace() {
3409 final StringBuilder sb = new StringBuilder();
3410 try {
3411 throw new RuntimeException();
3412 } catch (RuntimeException e) {
3413 final StackTraceElement[] frames = e.getStackTrace();
3414 // Start at 1 because the first frame is here and we don't care about it
3415 for (int j = 1; j < frames.length; ++j) {
3416 sb.append(frames[j].toString() + "\n");
3417 }
3418 }
3419 return sb.toString();
3420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 @Override
3423 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3424 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3425 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3428 + Binder.getCallingPid()
3429 + ", uid=" + Binder.getCallingUid());
3430 return;
3431 }
3432
3433 IInputMethod method;
3434 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 synchronized (mMethodMap) {
3439 p.println("Current Input Method Manager state:");
3440 int N = mMethodList.size();
3441 p.println(" Input Methods:");
3442 for (int i=0; i<N; i++) {
3443 InputMethodInfo info = mMethodList.get(i);
3444 p.println(" InputMethod #" + i + ":");
3445 info.dump(p, " ");
3446 }
3447 p.println(" Clients:");
3448 for (ClientState ci : mClients.values()) {
3449 p.println(" Client " + ci + ":");
3450 p.println(" client=" + ci.client);
3451 p.println(" inputContext=" + ci.inputContext);
3452 p.println(" sessionRequested=" + ci.sessionRequested);
3453 p.println(" curSession=" + ci.curSession);
3454 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003455 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003457 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3458 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3460 + " mBoundToMethod=" + mBoundToMethod);
3461 p.println(" mCurToken=" + mCurToken);
3462 p.println(" mCurIntent=" + mCurIntent);
3463 method = mCurMethod;
3464 p.println(" mCurMethod=" + mCurMethod);
3465 p.println(" mEnabledSession=" + mEnabledSession);
3466 p.println(" mShowRequested=" + mShowRequested
3467 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3468 + " mShowForced=" + mShowForced
3469 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003470 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003472
Jeff Brownb88102f2010-09-08 11:49:43 -07003473 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 pw.flush();
3476 try {
3477 client.client.asBinder().dump(fd, args);
3478 } catch (RemoteException e) {
3479 p.println("Input method client dead: " + e);
3480 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003481 } else {
3482 p.println("No input method client.");
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 (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 pw.flush();
3488 try {
3489 method.asBinder().dump(fd, args);
3490 } catch (RemoteException e) {
3491 p.println("Input method service dead: " + e);
3492 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003493 } else {
3494 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 }
3496 }
3497}