blob: 0f14265e1334660b8c75370f196bcdb686e0d1ab [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 buildInputMethodListLocked(mMethodList, mMethodMap);
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800517
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800518 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800519 int change = isPackageDisappearing(curIm.getPackageName());
520 if (change == PACKAGE_TEMPORARY_CHANGE
521 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800522 ServiceInfo si = null;
523 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900524 si = mIPackageManager.getServiceInfo(
525 curIm.getComponent(), 0, mSettings.getCurrentUserId());
526 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800527 }
528 if (si == null) {
529 // Uh oh, current input method is no longer around!
530 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900532 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800534 changed = true;
535 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900537 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800538 }
539 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800540 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800541 }
satokab751aa2010-09-14 19:17:36 +0900542
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800543 if (curIm == null) {
544 // We currently don't have a default input method... is
545 // one now available?
546 changed = chooseNewDefaultIMELocked();
547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800548
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800549 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800550 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
553 }
554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Jean Chalarde0d32a62011-10-20 20:36:07 +0900556 private static class MethodCallback extends IInputMethodCallback.Stub {
557 private final IInputMethod mMethod;
558 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800559
Jean Chalarde0d32a62011-10-20 20:36:07 +0900560 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900562 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800564
satoke7c6998e2011-06-03 17:57:59 +0900565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void finishedEvent(int seq, boolean handled) throws RemoteException {
567 }
568
satoke7c6998e2011-06-03 17:57:59 +0900569 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900571 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
satok01038492012-04-09 21:08:27 +0900575 private class HardKeyboardListener
576 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
577 @Override
578 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
579 mHandler.sendMessage(mHandler.obtainMessage(
580 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
581 }
582
583 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
584 if (DEBUG) {
585 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
586 + enabled);
587 }
588 synchronized(mMethodMap) {
589 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
590 && mSwitchingDialog.isShowing()) {
591 mSwitchingDialogTitleView.findViewById(
592 com.android.internal.R.id.hard_keyboard_section).setVisibility(
593 available ? View.VISIBLE : View.GONE);
594 }
595 }
596 }
597 }
598
599 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900600 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800602 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 mHandler = new Handler(this);
604 mIWindowManager = IWindowManager.Stub.asInterface(
605 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800606 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900607 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public void executeMessage(Message msg) {
609 handleMessage(msg);
610 }
Mita Yuned218c72012-12-06 17:18:25 -0800611 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900612 mWindowManagerService = windowManager;
613 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900614
satok7cfc0ed2011-06-20 21:29:36 +0900615 mImeSwitcherNotification = new Notification();
616 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
617 mImeSwitcherNotification.when = 0;
618 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
619 mImeSwitcherNotification.tickerText = null;
620 mImeSwitcherNotification.defaults = 0; // please be quiet
621 mImeSwitcherNotification.sound = null;
622 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400623
624 // Tag this notification specially so SystemUI knows it's important
625 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
626
satok7cfc0ed2011-06-20 21:29:36 +0900627 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900628 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900629
630 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900631
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900632 final IntentFilter broadcastFilter = new IntentFilter();
633 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
634 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
635 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
636 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
satok7cfc0ed2011-06-20 21:29:36 +0900638 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 int userId = 0;
640 try {
641 ActivityManagerNative.getDefault().registerUserSwitchObserver(
642 new IUserSwitchObserver.Stub() {
643 @Override
644 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900645 synchronized(mMethodMap) {
646 switchUserLocked(newUserId);
647 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900648 if (reply != null) {
649 try {
650 reply.sendResult(null);
651 } catch (RemoteException e) {
652 }
653 }
654 }
655
656 @Override
657 public void onUserSwitchComplete(int newUserId) throws RemoteException {
658 }
659 });
660 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
661 } catch (RemoteException e) {
662 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
663 }
satok81f8b7c2012-12-04 20:42:56 +0900664 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900665
satokd87c2592010-09-29 11:52:06 +0900666 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900667 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900668 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900669 mFileManager = new InputMethodFileManager(mMethodMap, userId);
670 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900671
672 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900673 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900674 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900677 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678
satok0a1bcf42012-05-16 19:26:31 +0900679 if (!mImeSelectedOnBoot) {
680 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900681 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800685 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900686
687 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
688 // according to the new system locale.
689 final IntentFilter filter = new IntentFilter();
690 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
691 mContext.registerReceiver(
692 new BroadcastReceiver() {
693 @Override
694 public void onReceive(Context context, Intent intent) {
695 synchronized(mMethodMap) {
696 checkCurrentLocaleChangedLocked();
697 }
698 }
699 }, filter);
700 }
701
satok5b927c432012-05-01 20:09:34 +0900702 private void resetDefaultImeLocked(Context context) {
703 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900704 if (mCurMethodId != null
705 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900706 return;
707 }
708
709 InputMethodInfo defIm = null;
710 for (InputMethodInfo imi : mMethodList) {
711 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900712 if (InputMethodUtils.isValidSystemDefaultIme(
713 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900714 defIm = imi;
715 Slog.i(TAG, "Selected default: " + imi.getId());
716 }
717 }
718 }
719 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900720 defIm = InputMethodUtils.getMostApplicableDefaultIME(
721 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900722 Slog.i(TAG, "No default found, using " + defIm.getId());
723 }
724 if (defIm != null) {
725 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
726 }
727 }
728
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900729 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
730 if (!mSystemReady) {
731 // not system ready
732 return;
733 }
734 final Locale newLocale = mRes.getConfiguration().locale;
735 if (!updateOnlyWhenLocaleChanged
736 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
737 if (!updateOnlyWhenLocaleChanged) {
738 hideCurrentInputLocked(0, null);
739 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700740 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900741 }
742 if (DEBUG) {
743 Slog.i(TAG, "Locale has been changed to " + newLocale);
744 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900745 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
746 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900747 buildInputMethodListLocked(mMethodList, mMethodMap);
748 if (!updateOnlyWhenLocaleChanged) {
749 final String selectedImiId = mSettings.getSelectedInputMethod();
750 if (TextUtils.isEmpty(selectedImiId)) {
751 // This is the first time of the user switch and
752 // set the current ime to the proper one.
753 resetDefaultImeLocked(mContext);
754 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900755 } else {
756 // If the locale is changed, needs to reset the default ime
757 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900758 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800759 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900760 mLastSystemLocale = newLocale;
761 if (!updateOnlyWhenLocaleChanged) {
762 try {
763 startInputInnerLocked();
764 } catch (RuntimeException e) {
765 Slog.w(TAG, "Unexpected exception", e);
766 }
767 }
768 }
769 }
770
771 private void checkCurrentLocaleChangedLocked() {
772 resetAllInternalStateLocked(true);
773 }
774
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900775 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900777 // InputMethodFileManager should be reset when the user is changed
778 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 resetAllInternalStateLocked(false);
780 }
781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 @Override
783 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
784 throws RemoteException {
785 try {
786 return super.onTransact(code, data, reply, flags);
787 } catch (RuntimeException e) {
788 // The input method manager only throws security exceptions, so let's
789 // log all others.
790 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800791 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793 throw e;
794 }
795 }
796
Dianne Hackborn661cd522011-08-22 00:26:20 -0700797 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700798 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 if (DEBUG) {
800 Slog.d(TAG, "--- systemReady");
801 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700802 if (!mSystemReady) {
803 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 mKeyguardManager =
805 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700806 mNotificationManager = (NotificationManager)
807 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
808 mStatusBar = statusBar;
809 statusBar.setIconVisibility("ime", false);
810 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900811 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
812 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900813 if (mShowOngoingImeSwitcherForPhones) {
814 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
815 mHardKeyboardListener);
816 }
satok0a1bcf42012-05-16 19:26:31 +0900817 buildInputMethodListLocked(mMethodList, mMethodMap);
818 if (!mImeSelectedOnBoot) {
819 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
820 checkCurrentLocaleChangedLocked();
821 }
822 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700823 try {
824 startInputInnerLocked();
825 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800826 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700827 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700828 }
829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800831
satok15452a42011-10-28 17:58:28 +0900832 private void setImeWindowVisibilityStatusHiddenLocked() {
833 mImeWindowVis = 0;
834 updateImeWindowStatusLocked();
835 }
836
satok3afd6c02011-11-18 08:38:19 +0900837 private void refreshImeWindowVisibilityLocked() {
838 final Configuration conf = mRes.getConfiguration();
839 final boolean haveHardKeyboard = conf.keyboard
840 != Configuration.KEYBOARD_NOKEYS;
841 final boolean hardKeyShown = haveHardKeyboard
842 && conf.hardKeyboardHidden
843 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500844 final boolean isScreenLocked =
845 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
846 final boolean isScreenSecurelyLocked =
847 isScreenLocked && mKeyguardManager.isKeyguardSecure();
848 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
849 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900850 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
851 updateImeWindowStatusLocked();
852 }
853
satok15452a42011-10-28 17:58:28 +0900854 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900855 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700856 }
857
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900858 // ---------------------------------------------------------------------------------------
859 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
860 // 1) it comes from the system process
861 // 2) the calling process' user id is identical to the current user id IMMS thinks.
862 private boolean calledFromValidUser() {
863 final int uid = Binder.getCallingUid();
864 final int userId = UserHandle.getUserId(uid);
865 if (DEBUG) {
866 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
867 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
868 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900869 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900870 }
871 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
872 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900873 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900874
875 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
876 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
877 // must not manage background users' states in any functions.
878 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
879 // by a token.
880 if (mContext.checkCallingOrSelfPermission(
881 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
882 == PackageManager.PERMISSION_GRANTED) {
883 if (DEBUG) {
884 Slog.d(TAG, "--- Access granted because the calling process has "
885 + "the INTERACT_ACROSS_USERS_FULL permission");
886 }
887 return true;
888 }
889 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
890 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900891 }
892
893 private boolean bindCurrentInputMethodService(
894 Intent service, ServiceConnection conn, int flags) {
895 if (service == null || conn == null) {
896 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
897 return false;
898 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800899 return mContext.bindServiceAsUser(service, conn, flags,
900 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 }
902
satoke7c6998e2011-06-03 17:57:59 +0900903 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900905 // TODO: Make this work even for non-current users?
906 if (!calledFromValidUser()) {
907 return Collections.emptyList();
908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 synchronized (mMethodMap) {
910 return new ArrayList<InputMethodInfo>(mMethodList);
911 }
912 }
913
satoke7c6998e2011-06-03 17:57:59 +0900914 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900916 // TODO: Make this work even for non-current users?
917 if (!calledFromValidUser()) {
918 return Collections.emptyList();
919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900921 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
923 }
924
satokbb4aa062011-01-19 21:40:27 +0900925 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
926 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
927 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
928 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900930 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900931 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900932 }
933 return enabledInputMethodAndSubtypes;
934 }
935
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900936 /**
937 * @param imi if null, returns enabled subtypes for the current imi
938 * @return enabled subtypes of the specified imi
939 */
satoke7c6998e2011-06-03 17:57:59 +0900940 @Override
satok16331c82010-12-20 23:48:46 +0900941 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
942 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 // TODO: Make this work even for non-current users?
944 if (!calledFromValidUser()) {
945 return Collections.emptyList();
946 }
satok67ddf9c2010-11-17 09:45:54 +0900947 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900948 if (imi == null && mCurMethodId != null) {
949 imi = mMethodMap.get(mCurMethodId);
950 }
951 return mSettings.getEnabledInputMethodSubtypeListLocked(
952 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900953 }
954 }
955
satoke7c6998e2011-06-03 17:57:59 +0900956 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 public void addClient(IInputMethodClient client,
958 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 if (!calledFromValidUser()) {
960 return;
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 synchronized (mMethodMap) {
963 mClients.put(client.asBinder(), new ClientState(client,
964 inputContext, uid, pid));
965 }
966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800967
satoke7c6998e2011-06-03 17:57:59 +0900968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 if (!calledFromValidUser()) {
971 return;
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 synchronized (mMethodMap) {
974 mClients.remove(client.asBinder());
975 }
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 void executeOrSendMessage(IInterface target, Message msg) {
979 if (target.asBinder() instanceof Binder) {
980 mCaller.sendMessage(msg);
981 } else {
982 handleMessage(msg);
983 msg.recycle();
984 }
985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700987 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800989 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 + mCurClient.client.asBinder());
991 if (mBoundToMethod) {
992 mBoundToMethod = false;
993 if (mCurMethod != null) {
994 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
995 MSG_UNBIND_INPUT, mCurMethod));
996 }
997 }
Dianne Hackborna6e41342012-05-22 16:30:34 -0700998
999 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1000 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1002 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1003 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Project10592532009-03-18 17:39:46 -07001006 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 private int getImeShowFlags() {
1011 int flags = 0;
1012 if (mShowForced) {
1013 flags |= InputMethod.SHOW_FORCED
1014 | InputMethod.SHOW_EXPLICIT;
1015 } else if (mShowExplicitlyRequested) {
1016 flags |= InputMethod.SHOW_EXPLICIT;
1017 }
1018 return flags;
1019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 private int getAppShowFlags() {
1022 int flags = 0;
1023 if (mShowForced) {
1024 flags |= InputMethodManager.SHOW_FORCED;
1025 } else if (!mShowExplicitlyRequested) {
1026 flags |= InputMethodManager.SHOW_IMPLICIT;
1027 }
1028 return flags;
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
Dianne Hackborn7663d802012-02-24 13:08:49 -08001031 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (!mBoundToMethod) {
1033 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1034 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1035 mBoundToMethod = true;
1036 }
1037 final SessionState session = mCurClient.curSession;
1038 if (initial) {
1039 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1040 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1041 } else {
1042 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1043 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1044 }
1045 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001046 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001047 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001049 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001053 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 // If no method is currently selected, do nothing.
1055 if (mCurMethodId == null) {
1056 return mNoBinding;
1057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 ClientState cs = mClients.get(client.asBinder());
1060 if (cs == null) {
1061 throw new IllegalArgumentException("unknown client "
1062 + client.asBinder());
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 try {
1066 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1067 // Check with the window manager to make sure this client actually
1068 // has a window with focus. If not, reject. This is thread safe
1069 // because if the focus changes some time before or after, the
1070 // next client receiving focus that has any interest in input will
1071 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001072 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1074 return null;
1075 }
1076 } catch (RemoteException e) {
1077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078
Dianne Hackborn7663d802012-02-24 13:08:49 -08001079 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1080 }
1081
1082 InputBindResult startInputUncheckedLocked(ClientState cs,
1083 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1084 // If no method is currently selected, do nothing.
1085 if (mCurMethodId == null) {
1086 return mNoBinding;
1087 }
1088
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001089 if (mCurClient == null) {
1090 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1091 if (DEBUG) {
1092 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1093 }
1094 }
1095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (mCurClient != cs) {
1097 // If the client is changing, we need to switch over to the new
1098 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001099 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001100 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 + cs.client.asBinder());
1102
1103 // If the screen is on, inform the new client it is active
1104 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001105 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1106 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 // Bump up the sequence for this client and attach it.
1111 mCurSeq++;
1112 if (mCurSeq <= 0) mCurSeq = 1;
1113 mCurClient = cs;
1114 mCurInputContext = inputContext;
1115 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 // Check if the input method is changing.
1118 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1119 if (cs.curSession != null) {
1120 // Fast case: if we are already connected to the input method,
1121 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001122 return attachNewInputLocked(
1123 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125 if (mHaveConnection) {
1126 if (mCurMethod != null) {
1127 if (!cs.sessionRequested) {
1128 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001129 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1131 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001132 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 // Return to client, and we will get back with it when
1135 // we have had a session made for it.
1136 return new InputBindResult(null, mCurId, mCurSeq);
1137 } else if (SystemClock.uptimeMillis()
1138 < (mLastBindTime+TIME_TO_RECONNECT)) {
1139 // In this case we have connected to the service, but
1140 // don't yet have its interface. If it hasn't been too
1141 // long since we did the connection, we'll return to
1142 // the client and wait to get the service interface so
1143 // we can report back. If it has been too long, we want
1144 // to fall through so we can try a disconnect/reconnect
1145 // to see if we can get back in touch with the service.
1146 return new InputBindResult(null, mCurId, mCurSeq);
1147 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1149 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 }
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001154 return startInputInnerLocked();
1155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001156
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001157 InputBindResult startInputInnerLocked() {
1158 if (mCurMethodId == null) {
1159 return mNoBinding;
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001162 if (!mSystemReady) {
1163 // If the system is not yet ready, we shouldn't be running third
1164 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001165 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1169 if (info == null) {
1170 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001173 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1176 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001177 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1178 com.android.internal.R.string.input_method_binding_label);
1179 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1180 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001182 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 mLastBindTime = SystemClock.uptimeMillis();
1184 mHaveConnection = true;
1185 mCurId = info.getId();
1186 mCurToken = new Binder();
1187 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 mIWindowManager.addWindowToken(mCurToken,
1190 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1191 } catch (RemoteException e) {
1192 }
1193 return new InputBindResult(null, mCurId, mCurSeq);
1194 } else {
1195 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 + mCurIntent);
1198 }
1199 return null;
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
satoke7c6998e2011-06-03 17:57:59 +09001202 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001204 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 if (!calledFromValidUser()) {
1206 return null;
1207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 synchronized (mMethodMap) {
1209 final long ident = Binder.clearCallingIdentity();
1210 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001211 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } finally {
1213 Binder.restoreCallingIdentity(ident);
1214 }
1215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
satoke7c6998e2011-06-03 17:57:59 +09001218 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 public void finishInput(IInputMethodClient client) {
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
satoke7c6998e2011-06-03 17:57:59 +09001222 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 public void onServiceConnected(ComponentName name, IBinder service) {
1224 synchronized (mMethodMap) {
1225 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1226 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001227 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001229 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001230 return;
1231 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001232 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001233 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1234 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001236 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001237 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001239 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001240 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 }
1243 }
1244 }
1245
1246 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1247 synchronized (mMethodMap) {
1248 if (mCurMethod != null && method != null
1249 && mCurMethod.asBinder() == method.asBinder()) {
1250 if (mCurClient != null) {
1251 mCurClient.curSession = new SessionState(mCurClient,
1252 method, session);
1253 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001254 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 if (res.method != null) {
1256 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1257 MSG_BIND_METHOD, mCurClient.client, res));
1258 }
1259 }
1260 }
1261 }
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001264 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001265 if (mVisibleBound) {
1266 mContext.unbindService(mVisibleConnection);
1267 mVisibleBound = false;
1268 }
1269
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001270 if (mHaveConnection) {
1271 mContext.unbindService(this);
1272 mHaveConnection = false;
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001275 if (mCurToken != null) {
1276 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001278 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001279 // The current IME is shown. Hence an IME switch (transition) is happening.
1280 mWindowManagerService.saveLastInputMethodWindowForTransition();
1281 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001282 mIWindowManager.removeWindowToken(mCurToken);
1283 } catch (RemoteException e) {
1284 }
1285 mCurToken = null;
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Project10592532009-03-18 17:39:46 -07001288 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001289 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001291 if (reportToClient && mCurClient != null) {
1292 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1293 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1294 }
1295 }
1296
Devin Taylor0c33ed22010-02-23 13:26:46 -06001297 private void finishSession(SessionState sessionState) {
1298 if (sessionState != null && sessionState.session != null) {
1299 try {
1300 sessionState.session.finishSession();
1301 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001302 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001303 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001304 }
1305 }
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001308 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 if (mCurMethod != null) {
1310 for (ClientState cs : mClients.values()) {
1311 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001312 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 cs.curSession = null;
1314 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001315
1316 finishSession(mEnabledSession);
1317 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 mCurMethod = null;
1319 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001320 if (mStatusBar != null) {
1321 mStatusBar.setIconVisibility("ime", false);
1322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324
satoke7c6998e2011-06-03 17:57:59 +09001325 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public void onServiceDisconnected(ComponentName name) {
1327 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 + " mCurIntent=" + mCurIntent);
1330 if (mCurMethod != null && mCurIntent != null
1331 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001332 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 // We consider this to be a new bind attempt, since the system
1334 // should now try to restart the service for us.
1335 mLastBindTime = SystemClock.uptimeMillis();
1336 mShowRequested = mInputShown;
1337 mInputShown = false;
1338 if (mCurClient != null) {
1339 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1340 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1341 }
1342 }
1343 }
1344 }
1345
satokf9f01002011-05-19 21:31:50 +09001346 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001348 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 long ident = Binder.clearCallingIdentity();
1350 try {
1351 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001352 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 return;
1354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 synchronized (mMethodMap) {
1357 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001358 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001359 if (mStatusBar != null) {
1360 mStatusBar.setIconVisibility("ime", false);
1361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001363 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001364 CharSequence contentDescription = null;
1365 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001366 // Use PackageManager to load label
1367 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001368 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001369 mIPackageManager.getApplicationInfo(packageName, 0,
1370 mSettings.getCurrentUserId()));
1371 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001372 /* ignore */
1373 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001374 if (mStatusBar != null) {
1375 mStatusBar.setIcon("ime", packageName, iconId, 0,
1376 contentDescription != null
1377 ? contentDescription.toString() : null);
1378 mStatusBar.setIconVisibility("ime", true);
1379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 }
1382 } finally {
1383 Binder.restoreCallingIdentity(ident);
1384 }
1385 }
1386
satok7cfc0ed2011-06-20 21:29:36 +09001387 private boolean needsToShowImeSwitchOngoingNotification() {
1388 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001389 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001390 synchronized (mMethodMap) {
1391 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1392 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001393 if (N > 2) return true;
1394 if (N < 1) return false;
1395 int nonAuxCount = 0;
1396 int auxCount = 0;
1397 InputMethodSubtype nonAuxSubtype = null;
1398 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001399 for(int i = 0; i < N; ++i) {
1400 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001401 final List<InputMethodSubtype> subtypes =
1402 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001403 final int subtypeCount = subtypes.size();
1404 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001405 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001406 } else {
1407 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001408 final InputMethodSubtype subtype = subtypes.get(j);
1409 if (!subtype.isAuxiliary()) {
1410 ++nonAuxCount;
1411 nonAuxSubtype = subtype;
1412 } else {
1413 ++auxCount;
1414 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001415 }
1416 }
1417 }
satok7cfc0ed2011-06-20 21:29:36 +09001418 }
satokb6359412011-06-28 17:47:41 +09001419 if (nonAuxCount > 1 || auxCount > 1) {
1420 return true;
1421 } else if (nonAuxCount == 1 && auxCount == 1) {
1422 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001423 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1424 || auxSubtype.overridesImplicitlyEnabledSubtype()
1425 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001426 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1427 return false;
1428 }
1429 return true;
1430 }
1431 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001432 }
satok7cfc0ed2011-06-20 21:29:36 +09001433 }
1434
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001435 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001436 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001437 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001438 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001439 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001440 try {
1441 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001442 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001443 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001444 return;
1445 }
1446
1447 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001448 mImeWindowVis = vis;
1449 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001450 if (mStatusBar != null) {
1451 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1452 }
satok7cfc0ed2011-06-20 21:29:36 +09001453 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001454 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1455 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001456 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001457 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001458 final CharSequence title = mRes.getText(
1459 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001460 final CharSequence imiLabel = imi.loadLabel(pm);
1461 final CharSequence summary = mCurrentSubtype != null
1462 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1463 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1464 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001465 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001466 : imiLabel;
1467
satok7cfc0ed2011-06-20 21:29:36 +09001468 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001469 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001470 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001471 if (DEBUG) {
1472 Slog.d(TAG, "--- show notification: label = " + imiLabel
1473 + ", summary = " + summary);
1474 }
1475 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001476 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001477 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001478 mNotificationShown = true;
1479 }
satok7cfc0ed2011-06-20 21:29:36 +09001480 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001481 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001482 if (DEBUG) {
1483 Slog.d(TAG, "--- hide notification");
1484 }
1485 mNotificationManager.cancelAsUser(null,
1486 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001487 mNotificationShown = false;
1488 }
1489 }
satok06487a52010-10-29 11:37:18 +09001490 }
1491 } finally {
1492 Binder.restoreCallingIdentity(ident);
1493 }
1494 }
1495
satoke7c6998e2011-06-03 17:57:59 +09001496 @Override
satokf9f01002011-05-19 21:31:50 +09001497 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001498 if (!calledFromValidUser()) {
1499 return;
1500 }
satokf9f01002011-05-19 21:31:50 +09001501 synchronized (mMethodMap) {
1502 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1503 for (int i = 0; i < spans.length; ++i) {
1504 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001505 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001506 mSecureSuggestionSpans.put(ss, currentImi);
1507 }
1508 }
1509 }
1510 }
1511
satoke7c6998e2011-06-03 17:57:59 +09001512 @Override
satokf9f01002011-05-19 21:31:50 +09001513 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001514 if (!calledFromValidUser()) {
1515 return false;
1516 }
satokf9f01002011-05-19 21:31:50 +09001517 synchronized (mMethodMap) {
1518 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1519 // TODO: Do not send the intent if the process of the targetImi is already dead.
1520 if (targetImi != null) {
1521 final String[] suggestions = span.getSuggestions();
1522 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001523 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001524 final Intent intent = new Intent();
1525 // Ensures that only a class in the original IME package will receive the
1526 // notification.
satok42c5a162011-05-26 16:46:14 +09001527 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001528 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1529 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1530 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1531 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001532 final long ident = Binder.clearCallingIdentity();
1533 try {
1534 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1535 } finally {
1536 Binder.restoreCallingIdentity(ident);
1537 }
satokf9f01002011-05-19 21:31:50 +09001538 return true;
1539 }
1540 }
1541 return false;
1542 }
1543
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001544 void updateFromSettingsLocked(boolean enabledMayChange) {
1545 if (enabledMayChange) {
1546 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1547 for (int i=0; i<enabled.size(); i++) {
1548 // We allow the user to select "disabled until used" apps, so if they
1549 // are enabling one of those here we now need to make it enabled.
1550 InputMethodInfo imm = enabled.get(i);
1551 try {
1552 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1553 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1554 mSettings.getCurrentUserId());
1555 if (ai.enabledSetting
1556 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1557 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1558 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1559 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1560 }
1561 } catch (RemoteException e) {
1562 }
1563 }
1564 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001565 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1566 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1567 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1568 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001569 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001570 // There is no input method selected, try to choose new applicable input method.
1571 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001572 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001573 }
1574 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001576 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001578 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001579 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001580 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
satokf3db1af2010-11-23 13:34:33 +09001582 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001583 } else {
1584 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001585 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001586 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001589
satokab751aa2010-09-14 19:17:36 +09001590 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 InputMethodInfo info = mMethodMap.get(id);
1592 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001593 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001595
satokd81e9502012-05-21 12:58:45 +09001596 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001598 final int subtypeCount = info.getSubtypeCount();
1599 if (subtypeCount <= 0) {
1600 return;
satokcd7cd292010-11-20 15:46:23 +09001601 }
satokd81e9502012-05-21 12:58:45 +09001602 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1603 final InputMethodSubtype newSubtype;
1604 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1605 newSubtype = info.getSubtypeAt(subtypeId);
1606 } else {
1607 // If subtype is null, try to find the most applicable one from
1608 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001609 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001610 }
1611 if (newSubtype == null || oldSubtype == null) {
1612 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1613 + ", new subtype = " + newSubtype);
1614 return;
1615 }
1616 if (newSubtype != oldSubtype) {
1617 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1618 if (mCurMethod != null) {
1619 try {
1620 refreshImeWindowVisibilityLocked();
1621 mCurMethod.changeInputMethodSubtype(newSubtype);
1622 } catch (RemoteException e) {
1623 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001624 }
1625 }
1626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 return;
1628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001629
satokd81e9502012-05-21 12:58:45 +09001630 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 final long ident = Binder.clearCallingIdentity();
1632 try {
satokab751aa2010-09-14 19:17:36 +09001633 // Set a subtype to this input method.
1634 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001635 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1636 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1637 // because mCurMethodId is stored as a history in
1638 // setSelectedInputMethodAndSubtypeLocked().
1639 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640
1641 if (ActivityManagerNative.isSystemReady()) {
1642 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001643 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001645 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001647 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 } finally {
1649 Binder.restoreCallingIdentity(ident);
1650 }
1651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
satok42c5a162011-05-26 16:46:14 +09001653 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001654 public boolean showSoftInput(IInputMethodClient client, int flags,
1655 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001656 if (!calledFromValidUser()) {
1657 return false;
1658 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001659 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 long ident = Binder.clearCallingIdentity();
1661 try {
1662 synchronized (mMethodMap) {
1663 if (mCurClient == null || client == null
1664 || mCurClient.client.asBinder() != client.asBinder()) {
1665 try {
1666 // We need to check if this is the current client with
1667 // focus in the window manager, to allow this call to
1668 // be made before input is started in it.
1669 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001670 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001671 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001674 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001677
Joe Onorato8a9b2202010-02-26 18:56:32 -08001678 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001679 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681 } finally {
1682 Binder.restoreCallingIdentity(ident);
1683 }
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 mShowRequested = true;
1688 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1689 mShowExplicitlyRequested = true;
1690 }
1691 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1692 mShowExplicitlyRequested = true;
1693 mShowForced = true;
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
Dianne Hackborncc278702009-09-02 23:07:23 -07001696 if (!mSystemReady) {
1697 return false;
1698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699
The Android Open Source Project4df24232009-03-05 14:34:35 -08001700 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001702 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1703 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1704 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001706 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001707 bindCurrentInputMethodService(
1708 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001709 mVisibleBound = true;
1710 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001713 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 // The client has asked to have the input method shown, but
1715 // we have been sitting here too long with a connection to the
1716 // service and no interface received, so let's disconnect/connect
1717 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001718 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001720 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001722 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001723 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001724 } else {
1725 if (DEBUG) {
1726 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1727 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
satok42c5a162011-05-26 16:46:14 +09001734 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 public boolean hideSoftInput(IInputMethodClient client, int flags,
1736 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001737 if (!calledFromValidUser()) {
1738 return false;
1739 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001740 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 long ident = Binder.clearCallingIdentity();
1742 try {
1743 synchronized (mMethodMap) {
1744 if (mCurClient == null || client == null
1745 || mCurClient.client.asBinder() != client.asBinder()) {
1746 try {
1747 // We need to check if this is the current client with
1748 // focus in the window manager, to allow this call to
1749 // be made before input is started in it.
1750 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001751 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1752 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001753 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
1756 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001757 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001758 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Joe Onorato8a9b2202010-02-26 18:56:32 -08001762 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001763 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765 } finally {
1766 Binder.restoreCallingIdentity(ident);
1767 }
1768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001769
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1772 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001773 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001775 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001778 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1785 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1786 res = true;
1787 } else {
1788 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001790 if (mHaveConnection && mVisibleBound) {
1791 mContext.unbindService(mVisibleConnection);
1792 mVisibleBound = false;
1793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 mInputShown = false;
1795 mShowRequested = false;
1796 mShowExplicitlyRequested = false;
1797 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001798 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
satok42c5a162011-05-26 16:46:14 +09001801 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001802 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1803 int controlFlags, int softInputMode, int windowFlags,
1804 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001805 // Needs to check the validity before clearing calling identity
1806 final boolean calledFromValidUser = calledFromValidUser();
1807
Dianne Hackborn7663d802012-02-24 13:08:49 -08001808 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 long ident = Binder.clearCallingIdentity();
1810 try {
1811 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001812 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001813 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001815 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001816
Dianne Hackborn7663d802012-02-24 13:08:49 -08001817 ClientState cs = mClients.get(client.asBinder());
1818 if (cs == null) {
1819 throw new IllegalArgumentException("unknown client "
1820 + client.asBinder());
1821 }
1822
1823 try {
1824 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1825 // Check with the window manager to make sure this client actually
1826 // has a window with focus. If not, reject. This is thread safe
1827 // because if the focus changes some time before or after, the
1828 // next client receiving focus that has any interest in input will
1829 // be calling through here after that change happens.
1830 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1831 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1832 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001834 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001836
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001837 if (!calledFromValidUser) {
1838 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1839 Slog.w(TAG, "If you want to interect with IME, you need "
1840 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1841 hideCurrentInputLocked(0, null);
1842 return null;
1843 }
1844
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001845 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001846 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001847 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001848 if (attribute != null) {
1849 return startInputUncheckedLocked(cs, inputContext, attribute,
1850 controlFlags);
1851 }
1852 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001853 }
1854 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001856 // Should we auto-show the IME even if the caller has not
1857 // specified what should be done with it?
1858 // We only do this automatically if the window can resize
1859 // to accommodate the IME (so what the user sees will give
1860 // them good context without input information being obscured
1861 // by the IME) or if running on a large screen where there
1862 // is more room for the target window + IME.
1863 final boolean doAutoShow =
1864 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1865 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1866 || mRes.getConfiguration().isLayoutSizeAtLeast(
1867 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001868 final boolean isTextEditor =
1869 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1870
1871 // We want to start input before showing the IME, but after closing
1872 // it. We want to do this after closing it to help the IME disappear
1873 // more quickly (not get stuck behind it initializing itself for the
1874 // new focused input, even if its window wants to hide the IME).
1875 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1878 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001879 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1881 // There is no focus view, and this window will
1882 // be behind any soft input window, so hide the
1883 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001884 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001885 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001887 } else if (isTextEditor && doAutoShow && (softInputMode &
1888 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 // There is a focus view, and we are navigating forward
1890 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001891 // We only do this automatically if the window can resize
1892 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001893 // them good context without input information being obscured
1894 // by the IME) or if running on a large screen where there
1895 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001896 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001897 if (attribute != null) {
1898 res = startInputUncheckedLocked(cs, inputContext, attribute,
1899 controlFlags);
1900 didStart = true;
1901 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001902 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
1904 break;
1905 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1906 // Do nothing.
1907 break;
1908 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1909 if ((softInputMode &
1910 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001911 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001912 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
1914 break;
1915 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001916 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001917 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 break;
1919 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1920 if ((softInputMode &
1921 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001922 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001923 if (attribute != null) {
1924 res = startInputUncheckedLocked(cs, inputContext, attribute,
1925 controlFlags);
1926 didStart = true;
1927 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001928 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
1930 break;
1931 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001933 if (attribute != null) {
1934 res = startInputUncheckedLocked(cs, inputContext, attribute,
1935 controlFlags);
1936 didStart = true;
1937 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001938 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 break;
1940 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001941
1942 if (!didStart && attribute != null) {
1943 res = startInputUncheckedLocked(cs, inputContext, attribute,
1944 controlFlags);
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
1947 } finally {
1948 Binder.restoreCallingIdentity(ident);
1949 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001950
1951 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001953
satok42c5a162011-05-26 16:46:14 +09001954 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001956 if (!calledFromValidUser()) {
1957 return;
1958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 synchronized (mMethodMap) {
1960 if (mCurClient == null || client == null
1961 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001962 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001963 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965
satok440aab52010-11-25 09:43:11 +09001966 // Always call subtype picker, because subtype picker is a superset of input method
1967 // picker.
satokab751aa2010-09-14 19:17:36 +09001968 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1969 }
1970 }
1971
satok42c5a162011-05-26 16:46:14 +09001972 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001974 if (!calledFromValidUser()) {
1975 return;
1976 }
satok28203512010-11-24 11:06:49 +09001977 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1978 }
1979
satok42c5a162011-05-26 16:46:14 +09001980 @Override
satok28203512010-11-24 11:06:49 +09001981 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001982 if (!calledFromValidUser()) {
1983 return;
1984 }
satok28203512010-11-24 11:06:49 +09001985 synchronized (mMethodMap) {
1986 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001987 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09001988 mMethodMap.get(id), subtype.hashCode()));
1989 } else {
1990 setInputMethod(token, id);
1991 }
1992 }
satokab751aa2010-09-14 19:17:36 +09001993 }
1994
satok42c5a162011-05-26 16:46:14 +09001995 @Override
satokb416a712010-11-25 20:42:14 +09001996 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001997 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001998 if (!calledFromValidUser()) {
1999 return;
2000 }
satokb416a712010-11-25 20:42:14 +09002001 synchronized (mMethodMap) {
2002 if (mCurClient == null || client == null
2003 || mCurClient.client.asBinder() != client.asBinder()) {
2004 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2005 }
satok7fee71f2010-12-17 18:54:26 +09002006 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2007 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002008 }
2009 }
2010
satok4fc87d62011-05-20 16:13:43 +09002011 @Override
satok735cf382010-11-11 20:40:09 +09002012 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002013 if (!calledFromValidUser()) {
2014 return false;
2015 }
satok735cf382010-11-11 20:40:09 +09002016 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002017 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002018 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002019 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002020 lastImi = mMethodMap.get(lastIme.first);
2021 } else {
2022 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002023 }
satok4fc87d62011-05-20 16:13:43 +09002024 String targetLastImiId = null;
2025 int subtypeId = NOT_A_SUBTYPE_ID;
2026 if (lastIme != null && lastImi != null) {
2027 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2028 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2029 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2030 : mCurrentSubtype.hashCode();
2031 // If the last IME is the same as the current IME and the last subtype is not
2032 // defined, there is no need to switch to the last IME.
2033 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2034 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002035 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002036 }
2037 }
2038
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002039 if (TextUtils.isEmpty(targetLastImiId)
2040 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002041 // This is a safety net. If the currentSubtype can't be added to the history
2042 // and the framework couldn't find the last ime, we will make the last ime be
2043 // the most applicable enabled keyboard subtype of the system imes.
2044 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2045 if (enabled != null) {
2046 final int N = enabled.size();
2047 final String locale = mCurrentSubtype == null
2048 ? mRes.getConfiguration().locale.toString()
2049 : mCurrentSubtype.getLocale();
2050 for (int i = 0; i < N; ++i) {
2051 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002052 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002053 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002054 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2055 InputMethodUtils.getSubtypes(imi),
2056 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002057 if (keyboardSubtype != null) {
2058 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002059 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002060 imi, keyboardSubtype.hashCode());
2061 if(keyboardSubtype.getLocale().equals(locale)) {
2062 break;
2063 }
2064 }
2065 }
2066 }
2067 }
2068 }
2069
2070 if (!TextUtils.isEmpty(targetLastImiId)) {
2071 if (DEBUG) {
2072 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2073 + ", from: " + mCurMethodId + ", " + subtypeId);
2074 }
2075 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2076 return true;
2077 } else {
2078 return false;
2079 }
satok735cf382010-11-11 20:40:09 +09002080 }
2081 }
2082
satoke7c6998e2011-06-03 17:57:59 +09002083 @Override
satok688bd472012-02-09 20:09:17 +09002084 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002085 if (!calledFromValidUser()) {
2086 return false;
2087 }
satok688bd472012-02-09 20:09:17 +09002088 synchronized (mMethodMap) {
2089 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2090 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2091 if (nextSubtype == null) {
2092 return false;
2093 }
2094 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2095 return true;
2096 }
2097 }
2098
2099 @Override
satok68f1b782011-04-11 14:26:04 +09002100 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002101 if (!calledFromValidUser()) {
2102 return null;
2103 }
satok68f1b782011-04-11 14:26:04 +09002104 synchronized (mMethodMap) {
2105 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2106 // TODO: Handle the case of the last IME with no subtypes
2107 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2108 || TextUtils.isEmpty(lastIme.second)) return null;
2109 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2110 if (lastImi == null) return null;
2111 try {
2112 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002113 final int lastSubtypeId =
2114 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002115 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2116 return null;
2117 }
2118 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002119 } catch (NumberFormatException e) {
2120 return null;
2121 }
2122 }
2123 }
2124
satoke7c6998e2011-06-03 17:57:59 +09002125 @Override
satokee5e77c2011-09-02 18:50:15 +09002126 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 if (!calledFromValidUser()) {
2128 return;
2129 }
satok91e88122011-07-18 11:11:42 +09002130 // By this IPC call, only a process which shares the same uid with the IME can add
2131 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002132 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002133 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002134 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002135 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002136 final String[] packageInfos;
2137 try {
2138 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2139 } catch (RemoteException e) {
2140 Slog.e(TAG, "Failed to get package infos");
2141 return;
2142 }
satok91e88122011-07-18 11:11:42 +09002143 if (packageInfos != null) {
2144 final int packageNum = packageInfos.length;
2145 for (int i = 0; i < packageNum; ++i) {
2146 if (packageInfos[i].equals(imi.getPackageName())) {
2147 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002148 final long ident = Binder.clearCallingIdentity();
2149 try {
2150 buildInputMethodListLocked(mMethodList, mMethodMap);
2151 } finally {
2152 Binder.restoreCallingIdentity(ident);
2153 }
satokee5e77c2011-09-02 18:50:15 +09002154 return;
satok91e88122011-07-18 11:11:42 +09002155 }
2156 }
2157 }
satoke7c6998e2011-06-03 17:57:59 +09002158 }
satokee5e77c2011-09-02 18:50:15 +09002159 return;
satoke7c6998e2011-06-03 17:57:59 +09002160 }
2161
satok28203512010-11-24 11:06:49 +09002162 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 synchronized (mMethodMap) {
2164 if (token == null) {
2165 if (mContext.checkCallingOrSelfPermission(
2166 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2167 != PackageManager.PERMISSION_GRANTED) {
2168 throw new SecurityException(
2169 "Using null token requires permission "
2170 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2171 }
2172 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002173 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2174 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 return;
2176 }
2177
satokc5933802011-08-31 21:26:04 +09002178 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 try {
satokab751aa2010-09-14 19:17:36 +09002180 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 } finally {
2182 Binder.restoreCallingIdentity(ident);
2183 }
2184 }
2185 }
2186
satok42c5a162011-05-26 16:46:14 +09002187 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002189 if (!calledFromValidUser()) {
2190 return;
2191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 synchronized (mMethodMap) {
2193 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002194 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2195 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 return;
2197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 long ident = Binder.clearCallingIdentity();
2199 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002200 hideCurrentInputLocked(flags, null);
2201 } finally {
2202 Binder.restoreCallingIdentity(ident);
2203 }
2204 }
2205 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002206
satok42c5a162011-05-26 16:46:14 +09002207 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002209 if (!calledFromValidUser()) {
2210 return;
2211 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002212 synchronized (mMethodMap) {
2213 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002214 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2215 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002216 return;
2217 }
2218 long ident = Binder.clearCallingIdentity();
2219 try {
2220 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 } finally {
2222 Binder.restoreCallingIdentity(ident);
2223 }
2224 }
2225 }
2226
2227 void setEnabledSessionInMainThread(SessionState session) {
2228 if (mEnabledSession != session) {
2229 if (mEnabledSession != null) {
2230 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002231 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 mEnabledSession.method.setSessionEnabled(
2233 mEnabledSession.session, false);
2234 } catch (RemoteException e) {
2235 }
2236 }
2237 mEnabledSession = session;
2238 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002239 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 session.method.setSessionEnabled(
2241 session.session, true);
2242 } catch (RemoteException e) {
2243 }
2244 }
2245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002246
satok42c5a162011-05-26 16:46:14 +09002247 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002249 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 switch (msg.what) {
2251 case MSG_SHOW_IM_PICKER:
2252 showInputMethodMenu();
2253 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
satokab751aa2010-09-14 19:17:36 +09002255 case MSG_SHOW_IM_SUBTYPE_PICKER:
2256 showInputMethodSubtypeMenu();
2257 return true;
2258
satok47a44912010-10-06 16:03:58 +09002259 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002260 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002261 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002262 args.recycle();
satok217f5482010-12-15 05:19:19 +09002263 return true;
2264
2265 case MSG_SHOW_IM_CONFIG:
2266 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002267 return true;
2268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 case MSG_UNBIND_INPUT:
2272 try {
2273 ((IInputMethod)msg.obj).unbindInput();
2274 } catch (RemoteException e) {
2275 // There is nothing interesting about the method dying.
2276 }
2277 return true;
2278 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002279 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 try {
2281 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2282 } catch (RemoteException e) {
2283 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002284 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 return true;
2286 case MSG_SHOW_SOFT_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 {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002289 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2290 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 } catch (RemoteException e) {
2292 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002293 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 return true;
2295 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002296 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002298 ((IInputMethod)args.arg1).hideSoftInput(0,
2299 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 } catch (RemoteException e) {
2301 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002302 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 return true;
2304 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002307 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2309 } catch (RemoteException e) {
2310 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002311 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 return true;
2313 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002314 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 try {
2316 ((IInputMethod)args.arg1).createSession(
2317 (IInputMethodCallback)args.arg2);
2318 } catch (RemoteException e) {
2319 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002320 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002325 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 try {
2327 SessionState session = (SessionState)args.arg1;
2328 setEnabledSessionInMainThread(session);
2329 session.method.startInput((IInputContext)args.arg2,
2330 (EditorInfo)args.arg3);
2331 } catch (RemoteException e) {
2332 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002333 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 return true;
2335 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 try {
2338 SessionState session = (SessionState)args.arg1;
2339 setEnabledSessionInMainThread(session);
2340 session.method.restartInput((IInputContext)args.arg2,
2341 (EditorInfo)args.arg3);
2342 } catch (RemoteException e) {
2343 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 case MSG_UNBIND_METHOD:
2350 try {
2351 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2352 } catch (RemoteException e) {
2353 // There is nothing interesting about the last client dying.
2354 }
2355 return true;
2356 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002357 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 try {
2359 ((IInputMethodClient)args.arg1).onBindMethod(
2360 (InputBindResult)args.arg2);
2361 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002362 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002364 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002366 case MSG_SET_ACTIVE:
2367 try {
2368 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2369 } catch (RemoteException e) {
2370 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2371 + ((ClientState)msg.obj).pid + " uid "
2372 + ((ClientState)msg.obj).uid);
2373 }
2374 return true;
satok01038492012-04-09 21:08:27 +09002375
2376 // --------------------------------------------------------------
2377 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2378 mHardKeyboardListener.handleHardKeyboardStatusChange(
2379 msg.arg1 == 1, msg.arg2 == 1);
2380 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 }
2382 return false;
2383 }
2384
satokdc9ddae2011-10-06 12:22:36 +09002385 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002386 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2387 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002388 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002389 if (DEBUG) {
2390 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2391 }
satok723a27e2010-11-11 14:58:11 +09002392 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002393 return true;
2394 }
2395
2396 return false;
2397 }
2398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2400 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002401 if (DEBUG) {
2402 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 list.clear();
2405 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002406
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002407 // Use for queryIntentServicesAsUser
2408 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002409 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002410 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002412 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002414 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2415 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002416
satoke7c6998e2011-06-03 17:57:59 +09002417 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2418 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 for (int i = 0; i < services.size(); ++i) {
2420 ResolveInfo ri = services.get(i);
2421 ServiceInfo si = ri.serviceInfo;
2422 ComponentName compName = new ComponentName(si.packageName, si.name);
2423 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2424 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002425 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 + ": it does not require the permission "
2427 + android.Manifest.permission.BIND_INPUT_METHOD);
2428 continue;
2429 }
2430
Joe Onorato8a9b2202010-02-26 18:56:32 -08002431 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
2433 try {
satoke7c6998e2011-06-03 17:57:59 +09002434 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002436 final String id = p.getId();
2437 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438
satok5b927c432012-05-01 20:09:34 +09002439 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002440 // by default
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002441 if (InputMethodUtils.isDefaultEnabledIme(mSystemReady, p, mContext)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002442 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002443 }
2444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002446 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002450 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002452 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
2454 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002455
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002456 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002457 if (!TextUtils.isEmpty(defaultImiId)) {
2458 if (!map.containsKey(defaultImiId)) {
2459 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2460 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002461 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002462 }
2463 } else {
2464 // Double check that the default IME is certainly enabled.
2465 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002466 }
2467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002471
satokab751aa2010-09-14 19:17:36 +09002472 private void showInputMethodMenu() {
2473 showInputMethodMenuInternal(false);
2474 }
2475
2476 private void showInputMethodSubtypeMenu() {
2477 showInputMethodMenuInternal(true);
2478 }
2479
satok217f5482010-12-15 05:19:19 +09002480 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002481 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002482 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002483 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2484 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002485 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002486 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002487 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002488 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002489 }
2490
2491 private void showConfigureInputMethods() {
2492 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2493 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2494 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2495 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002496 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002497 }
2498
satok2c93efc2012-04-02 19:33:47 +09002499 private boolean isScreenLocked() {
2500 return mKeyguardManager != null
2501 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2502 }
satokab751aa2010-09-14 19:17:36 +09002503 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002504 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002507 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002508
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002509 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002510 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002511 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002512
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002513 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002514 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2515 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002516 if (immis == null || immis.size() == 0) {
2517 return;
2518 }
2519
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002520 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521
satok688bd472012-02-09 20:09:17 +09002522 final List<ImeSubtypeListItem> imList =
2523 mImListManager.getSortedInputMethodAndSubtypeList(
2524 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002525
satokc3690562012-01-10 20:14:43 +09002526 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002527 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002528 if (currentSubtype != null) {
2529 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002530 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2531 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002532 }
2533 }
2534
Ken Wakasa761eb372011-03-04 19:06:18 +09002535 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002536 mIms = new InputMethodInfo[N];
2537 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002538 int checkedItem = 0;
2539 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002540 final ImeSubtypeListItem item = imList.get(i);
2541 mIms[i] = item.mImi;
2542 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002543 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002544 int subtypeId = mSubtypeIds[i];
2545 if ((subtypeId == NOT_A_SUBTYPE_ID)
2546 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2547 || (subtypeId == lastInputMethodSubtypeId)) {
2548 checkedItem = i;
2549 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002552 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002553 com.android.internal.R.styleable.DialogPreference,
2554 com.android.internal.R.attr.alertDialogStyle, 0);
2555 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002556 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002557 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002558 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002560 }
2561 })
2562 .setIcon(a.getDrawable(
2563 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2564 a.recycle();
satok01038492012-04-09 21:08:27 +09002565 final LayoutInflater inflater =
2566 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2567 final View tv = inflater.inflate(
2568 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2569 mDialogBuilder.setCustomTitle(tv);
2570
2571 // Setup layout for a toggle switch of the hardware keyboard
2572 mSwitchingDialogTitleView = tv;
2573 mSwitchingDialogTitleView.findViewById(
2574 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2575 mWindowManagerService.isHardKeyboardAvailable() ?
2576 View.VISIBLE : View.GONE);
2577 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2578 com.android.internal.R.id.hard_keyboard_switch));
2579 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2580 hardKeySwitch.setOnCheckedChangeListener(
2581 new OnCheckedChangeListener() {
2582 @Override
2583 public void onCheckedChanged(
2584 CompoundButton buttonView, boolean isChecked) {
2585 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002586 // Ensure that the input method dialog is dismissed when changing
2587 // the hardware keyboard state.
2588 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002589 }
2590 });
satokd87c2592010-09-29 11:52:06 +09002591
Ken Wakasa05dbb652011-08-22 15:22:43 +09002592 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2593 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2594 checkedItem);
2595
2596 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002597 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002598 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002599 public void onClick(DialogInterface dialog, int which) {
2600 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002601 if (mIms == null || mIms.length <= which
2602 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002603 return;
2604 }
2605 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002606 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002607 adapter.mCheckedItem = which;
2608 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002609 hideInputMethodMenu();
2610 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002611 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002612 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002613 subtypeId = NOT_A_SUBTYPE_ID;
2614 }
2615 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002616 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620
satokbc81b692011-08-26 16:22:22 +09002621 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002622 mDialogBuilder.setPositiveButton(
2623 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002624 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002625 @Override
satok7f35c8c2010-10-07 21:13:11 +09002626 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002627 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002628 }
2629 });
2630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002632 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 mSwitchingDialog.getWindow().setType(
2634 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002635 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2636 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002637 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 mSwitchingDialog.show();
2639 }
2640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002641
satok93d744d2012-05-09 17:14:08 +09002642 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002643 public final CharSequence mImeName;
2644 public final CharSequence mSubtypeName;
2645 public final InputMethodInfo mImi;
2646 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002647 private final boolean mIsSystemLocale;
2648 private final boolean mIsSystemLanguage;
2649
Ken Wakasa05dbb652011-08-22 15:22:43 +09002650 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002651 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002652 mImeName = imeName;
2653 mSubtypeName = subtypeName;
2654 mImi = imi;
2655 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002656 if (TextUtils.isEmpty(subtypeLocale)) {
2657 mIsSystemLocale = false;
2658 mIsSystemLanguage = false;
2659 } else {
2660 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2661 mIsSystemLanguage = mIsSystemLocale
2662 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2663 }
2664 }
2665
2666 @Override
2667 public int compareTo(ImeSubtypeListItem other) {
2668 if (TextUtils.isEmpty(mImeName)) {
2669 return 1;
2670 }
2671 if (TextUtils.isEmpty(other.mImeName)) {
2672 return -1;
2673 }
2674 if (!TextUtils.equals(mImeName, other.mImeName)) {
2675 return mImeName.toString().compareTo(other.mImeName.toString());
2676 }
2677 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2678 return 0;
2679 }
2680 if (mIsSystemLocale) {
2681 return -1;
2682 }
2683 if (other.mIsSystemLocale) {
2684 return 1;
2685 }
2686 if (mIsSystemLanguage) {
2687 return -1;
2688 }
2689 if (other.mIsSystemLanguage) {
2690 return 1;
2691 }
2692 if (TextUtils.isEmpty(mSubtypeName)) {
2693 return 1;
2694 }
2695 if (TextUtils.isEmpty(other.mSubtypeName)) {
2696 return -1;
2697 }
2698 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002699 }
2700 }
2701
2702 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2703 private final LayoutInflater mInflater;
2704 private final int mTextViewResourceId;
2705 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002706 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002707 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2708 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2709 super(context, textViewResourceId, itemsList);
2710 mTextViewResourceId = textViewResourceId;
2711 mItemsList = itemsList;
2712 mCheckedItem = checkedItem;
2713 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2714 }
2715
2716 @Override
2717 public View getView(int position, View convertView, ViewGroup parent) {
2718 final View view = convertView != null ? convertView
2719 : mInflater.inflate(mTextViewResourceId, null);
2720 if (position < 0 || position >= mItemsList.size()) return view;
2721 final ImeSubtypeListItem item = mItemsList.get(position);
2722 final CharSequence imeName = item.mImeName;
2723 final CharSequence subtypeName = item.mSubtypeName;
2724 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2725 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2726 if (TextUtils.isEmpty(subtypeName)) {
2727 firstTextView.setText(imeName);
2728 secondTextView.setVisibility(View.GONE);
2729 } else {
2730 firstTextView.setText(subtypeName);
2731 secondTextView.setText(imeName);
2732 secondTextView.setVisibility(View.VISIBLE);
2733 }
2734 final RadioButton radioButton =
2735 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2736 radioButton.setChecked(position == mCheckedItem);
2737 return view;
2738 }
2739 }
2740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002742 synchronized (mMethodMap) {
2743 hideInputMethodMenuLocked();
2744 }
2745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002746
The Android Open Source Project10592532009-03-18 17:39:46 -07002747 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002748 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749
The Android Open Source Project10592532009-03-18 17:39:46 -07002750 if (mSwitchingDialog != null) {
2751 mSwitchingDialog.dismiss();
2752 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
The Android Open Source Project10592532009-03-18 17:39:46 -07002755 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002756 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002760
satok42c5a162011-05-26 16:46:14 +09002761 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002763 // TODO: Make this work even for non-current users?
2764 if (!calledFromValidUser()) {
2765 return false;
2766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 synchronized (mMethodMap) {
2768 if (mContext.checkCallingOrSelfPermission(
2769 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2770 != PackageManager.PERMISSION_GRANTED) {
2771 throw new SecurityException(
2772 "Requires permission "
2773 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2774 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 long ident = Binder.clearCallingIdentity();
2777 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002778 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 } finally {
2780 Binder.restoreCallingIdentity(ident);
2781 }
2782 }
2783 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002784
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002785 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2786 // Make sure this is a valid input method.
2787 InputMethodInfo imm = mMethodMap.get(id);
2788 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002789 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002790 }
2791
satokd87c2592010-09-29 11:52:06 +09002792 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2793 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002794
satokd87c2592010-09-29 11:52:06 +09002795 if (enabled) {
2796 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2797 if (pair.first.equals(id)) {
2798 // We are enabling this input method, but it is already enabled.
2799 // Nothing to do. The previous state was enabled.
2800 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002801 }
2802 }
satokd87c2592010-09-29 11:52:06 +09002803 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2804 // Previous state was disabled.
2805 return false;
2806 } else {
2807 StringBuilder builder = new StringBuilder();
2808 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2809 builder, enabledInputMethodsList, id)) {
2810 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002811 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002812 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2813 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2814 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002815 }
2816 // Previous state was enabled.
2817 return true;
2818 } else {
2819 // We are disabling the input method but it is already disabled.
2820 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002821 return false;
2822 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002823 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002824 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002825
satok723a27e2010-11-11 14:58:11 +09002826 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2827 boolean setSubtypeOnly) {
2828 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002829 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002830
2831 // Set Subtype here
2832 if (imi == null || subtypeId < 0) {
2833 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002834 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002835 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002836 if (subtypeId < imi.getSubtypeCount()) {
2837 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2838 mSettings.putSelectedSubtype(subtype.hashCode());
2839 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002840 } else {
2841 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002842 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002843 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002844 }
satokab751aa2010-09-14 19:17:36 +09002845 }
satok723a27e2010-11-11 14:58:11 +09002846
satok4c0e7152012-06-20 20:08:44 +09002847 // Workaround.
2848 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2849 // IMEs are not recognized and considered uninstalled.
2850 // Actually, we can't move everything after SystemReady because
2851 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2852 // the default IME here and try cheking the default IME again in systemReady().
2853 // TODO: Do nothing before system ready and implement a separated logic for
2854 // the encryption lock screen.
2855 // TODO: ASEC should be ready before IMMS is instantiated.
2856 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002857 // Set InputMethod here
2858 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2859 }
2860 }
2861
2862 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2863 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2864 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2865 // newDefaultIme is empty when there is no candidate for the selected IME.
2866 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2867 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2868 if (subtypeHashCode != null) {
2869 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002870 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002871 imi, Integer.valueOf(subtypeHashCode));
2872 } catch (NumberFormatException e) {
2873 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2874 }
2875 }
2876 }
2877 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002878 }
2879
satok4e4569d2010-11-19 18:45:53 +09002880 // If there are no selected shortcuts, tries finding the most applicable ones.
2881 private Pair<InputMethodInfo, InputMethodSubtype>
2882 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2883 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2884 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002885 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002886 boolean foundInSystemIME = false;
2887
2888 // Search applicable subtype for each InputMethodInfo
2889 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002890 final String imiId = imi.getId();
2891 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2892 continue;
2893 }
satokcd7cd292010-11-20 15:46:23 +09002894 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002895 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002896 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002897 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002898 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002899 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002900 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002901 }
satokdf31ae62011-01-15 06:19:44 +09002902 // 2. Search by the system locale from enabledSubtypes.
2903 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002904 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002905 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002906 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002907 }
satoka86f5e42011-09-02 17:12:42 +09002908 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002909 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002910 final ArrayList<InputMethodSubtype> subtypesForSearch =
2911 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002912 ? InputMethodUtils.getSubtypes(imi)
2913 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002914 // 4. Search by the current subtype's locale from all subtypes.
2915 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002916 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002917 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002918 }
2919 // 5. Search by the system locale from all subtypes.
2920 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002921 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002922 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002923 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002924 }
satokcd7cd292010-11-20 15:46:23 +09002925 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002926 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002927 // The current input method is the most applicable IME.
2928 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002929 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002930 break;
satok7599a7f2010-12-22 13:45:23 +09002931 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002932 // The system input method is 2nd applicable IME.
2933 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002934 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002935 if ((imi.getServiceInfo().applicationInfo.flags
2936 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2937 foundInSystemIME = true;
2938 }
satok4e4569d2010-11-19 18:45:53 +09002939 }
2940 }
2941 }
2942 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002943 if (mostApplicableIMI != null) {
2944 Slog.w(TAG, "Most applicable shortcut input method was:"
2945 + mostApplicableIMI.getId());
2946 if (mostApplicableSubtype != null) {
2947 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2948 + "," + mostApplicableSubtype.getMode() + ","
2949 + mostApplicableSubtype.getLocale());
2950 }
2951 }
satok4e4569d2010-11-19 18:45:53 +09002952 }
satokcd7cd292010-11-20 15:46:23 +09002953 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002954 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002955 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002956 } else {
2957 return null;
2958 }
2959 }
2960
satokab751aa2010-09-14 19:17:36 +09002961 /**
2962 * @return Return the current subtype of this input method.
2963 */
satok42c5a162011-05-26 16:46:14 +09002964 @Override
satokab751aa2010-09-14 19:17:36 +09002965 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002966 // TODO: Make this work even for non-current users?
2967 if (!calledFromValidUser()) {
2968 return null;
2969 }
2970 synchronized (mMethodMap) {
2971 return getCurrentInputMethodSubtypeLocked();
2972 }
2973 }
2974
2975 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002976 if (mCurMethodId == null) {
2977 return null;
2978 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002979 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002980 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2981 if (imi == null || imi.getSubtypeCount() == 0) {
2982 return null;
satok4e4569d2010-11-19 18:45:53 +09002983 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002984 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002985 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2986 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002987 if (subtypeId == NOT_A_SUBTYPE_ID) {
2988 // If there are no selected subtypes, the framework will try to find
2989 // the most applicable subtype from explicitly or implicitly enabled
2990 // subtypes.
2991 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002992 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002993 // If there is only one explicitly or implicitly enabled subtype,
2994 // just returns it.
2995 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2996 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2997 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002998 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002999 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003000 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003001 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003002 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003003 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3004 true);
satok4e4569d2010-11-19 18:45:53 +09003005 }
satok3ef8b292010-11-23 06:06:29 +09003006 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003007 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003008 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003009 }
3010 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003011 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003012 }
3013
satokf3db1af2010-11-23 13:34:33 +09003014 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3015 InputMethodSubtype subtype) {
3016 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3017 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3018 } else {
3019 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3020 subtypes.add(subtype);
3021 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3022 }
3023 }
3024
satok4e4569d2010-11-19 18:45:53 +09003025 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003026 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003027 @Override
satok4e4569d2010-11-19 18:45:53 +09003028 public List getShortcutInputMethodsAndSubtypes() {
3029 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003030 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003031 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003032 // If there are no selected shortcut subtypes, the framework will try to find
3033 // the most applicable subtype from all subtypes whose mode is
3034 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003035 Pair<InputMethodInfo, InputMethodSubtype> info =
3036 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003037 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003038 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003039 ret.add(info.first);
3040 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003041 }
satok3da92232011-01-11 22:46:30 +09003042 return ret;
satokf3db1af2010-11-23 13:34:33 +09003043 }
satokf3db1af2010-11-23 13:34:33 +09003044 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3045 ret.add(imi);
3046 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3047 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003048 }
3049 }
satokf3db1af2010-11-23 13:34:33 +09003050 return ret;
satok4e4569d2010-11-19 18:45:53 +09003051 }
3052 }
3053
satok42c5a162011-05-26 16:46:14 +09003054 @Override
satokb66d2872010-11-10 01:04:04 +09003055 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003056 // TODO: Make this work even for non-current users?
3057 if (!calledFromValidUser()) {
3058 return false;
3059 }
satokb66d2872010-11-10 01:04:04 +09003060 synchronized (mMethodMap) {
3061 if (subtype != null && mCurMethodId != null) {
3062 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003063 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003064 if (subtypeId != NOT_A_SUBTYPE_ID) {
3065 setInputMethodLocked(mCurMethodId, subtypeId);
3066 return true;
3067 }
3068 }
3069 return false;
3070 }
3071 }
3072
satok688bd472012-02-09 20:09:17 +09003073 private static class InputMethodAndSubtypeListManager {
3074 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 // Used to load label
satok688bd472012-02-09 20:09:17 +09003076 private final PackageManager mPm;
3077 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003078 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003079 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3080 mContext = context;
3081 mPm = context.getPackageManager();
3082 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003083 final Locale locale = context.getResources().getConfiguration().locale;
3084 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003085 }
3086
3087 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3088 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3089 new Comparator<InputMethodInfo>() {
3090 @Override
3091 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3092 if (imi2 == null) return 0;
3093 if (imi1 == null) return 1;
3094 if (mPm == null) {
3095 return imi1.getId().compareTo(imi2.getId());
3096 }
3097 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3098 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3099 return imiId1.toString().compareTo(imiId2.toString());
3100 }
3101 });
3102
3103 public ImeSubtypeListItem getNextInputMethod(
3104 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3105 if (imi == null) {
3106 return null;
3107 }
3108 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3109 if (imList.size() <= 1) {
3110 return null;
3111 }
3112 final int N = imList.size();
3113 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003114 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003115 : NOT_A_SUBTYPE_ID;
3116 for (int i = 0; i < N; ++i) {
3117 final ImeSubtypeListItem isli = imList.get(i);
3118 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3119 if (!onlyCurrentIme) {
3120 return imList.get((i + 1) % N);
3121 }
3122 for (int j = 0; j < N - 1; ++j) {
3123 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3124 if (candidate.mImi.equals(imi)) {
3125 return candidate;
3126 }
3127 }
3128 return null;
3129 }
3130 }
3131 return null;
3132 }
3133
3134 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3135 return getSortedInputMethodAndSubtypeList(true, false, false);
3136 }
3137
3138 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3139 boolean inputShown, boolean isScreenLocked) {
3140 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3141 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3142 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3143 if (immis == null || immis.size() == 0) {
3144 return Collections.emptyList();
3145 }
3146 mSortedImmis.clear();
3147 mSortedImmis.putAll(immis);
3148 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3149 if (imi == null) continue;
3150 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3151 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3152 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3153 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3154 }
satok688bd472012-02-09 20:09:17 +09003155 final CharSequence imeLabel = imi.loadLabel(mPm);
3156 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3157 final int subtypeCount = imi.getSubtypeCount();
3158 if (DEBUG) {
3159 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3160 }
3161 for (int j = 0; j < subtypeCount; ++j) {
3162 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3163 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3164 // We show all enabled IMEs and subtypes when an IME is shown.
3165 if (enabledSubtypeSet.contains(subtypeHashCode)
3166 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3167 final CharSequence subtypeLabel =
3168 subtype.overridesImplicitlyEnabledSubtype() ? null
3169 : subtype.getDisplayName(mContext, imi.getPackageName(),
3170 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003171 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3172 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003173
3174 // Removing this subtype from enabledSubtypeSet because we no longer
3175 // need to add an entry of this subtype to imList to avoid duplicated
3176 // entries.
3177 enabledSubtypeSet.remove(subtypeHashCode);
3178 }
3179 }
3180 } else {
satok93d744d2012-05-09 17:14:08 +09003181 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3182 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003183 }
3184 }
satok93d744d2012-05-09 17:14:08 +09003185 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003186 return imList;
3187 }
3188 }
3189
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003190 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003191 private static class InputMethodFileManager {
3192 private static final String SYSTEM_PATH = "system";
3193 private static final String INPUT_METHOD_PATH = "inputmethod";
3194 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3195 private static final String NODE_SUBTYPES = "subtypes";
3196 private static final String NODE_SUBTYPE = "subtype";
3197 private static final String NODE_IMI = "imi";
3198 private static final String ATTR_ID = "id";
3199 private static final String ATTR_LABEL = "label";
3200 private static final String ATTR_ICON = "icon";
3201 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3202 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3203 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3204 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3205 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3206 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003207 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003208 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003209 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003210 if (methodMap == null) {
3211 throw new NullPointerException("methodMap is null");
3212 }
3213 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003214 final File systemDir = userId == UserHandle.USER_OWNER
3215 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3216 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003217 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3218 if (!inputMethodDir.mkdirs()) {
3219 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3220 }
3221 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3222 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3223 if (!subtypeFile.exists()) {
3224 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003225 writeAdditionalInputMethodSubtypes(
3226 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003227 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003228 readAdditionalInputMethodSubtypes(
3229 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003230 }
3231 }
3232
3233 private void deleteAllInputMethodSubtypes(String imiId) {
3234 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003235 mAdditionalSubtypesMap.remove(imiId);
3236 writeAdditionalInputMethodSubtypes(
3237 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003238 }
3239 }
3240
3241 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003242 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003243 synchronized (mMethodMap) {
3244 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3245 final int N = additionalSubtypes.length;
3246 for (int i = 0; i < N; ++i) {
3247 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003248 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003249 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003250 } else {
3251 Slog.w(TAG, "Duplicated subtype definition found: "
3252 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003253 }
3254 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003255 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3256 writeAdditionalInputMethodSubtypes(
3257 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003258 }
3259 }
3260
3261 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3262 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003263 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003264 }
3265 }
3266
3267 private static void writeAdditionalInputMethodSubtypes(
3268 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3269 HashMap<String, InputMethodInfo> methodMap) {
3270 // Safety net for the case that this function is called before methodMap is set.
3271 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3272 FileOutputStream fos = null;
3273 try {
3274 fos = subtypesFile.startWrite();
3275 final XmlSerializer out = new FastXmlSerializer();
3276 out.setOutput(fos, "utf-8");
3277 out.startDocument(null, true);
3278 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3279 out.startTag(null, NODE_SUBTYPES);
3280 for (String imiId : allSubtypes.keySet()) {
3281 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3282 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3283 continue;
3284 }
3285 out.startTag(null, NODE_IMI);
3286 out.attribute(null, ATTR_ID, imiId);
3287 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3288 final int N = subtypesList.size();
3289 for (int i = 0; i < N; ++i) {
3290 final InputMethodSubtype subtype = subtypesList.get(i);
3291 out.startTag(null, NODE_SUBTYPE);
3292 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3293 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3294 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3295 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3296 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3297 out.attribute(null, ATTR_IS_AUXILIARY,
3298 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3299 out.endTag(null, NODE_SUBTYPE);
3300 }
3301 out.endTag(null, NODE_IMI);
3302 }
3303 out.endTag(null, NODE_SUBTYPES);
3304 out.endDocument();
3305 subtypesFile.finishWrite(fos);
3306 } catch (java.io.IOException e) {
3307 Slog.w(TAG, "Error writing subtypes", e);
3308 if (fos != null) {
3309 subtypesFile.failWrite(fos);
3310 }
3311 }
3312 }
3313
3314 private static void readAdditionalInputMethodSubtypes(
3315 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3316 if (allSubtypes == null || subtypesFile == null) return;
3317 allSubtypes.clear();
3318 FileInputStream fis = null;
3319 try {
3320 fis = subtypesFile.openRead();
3321 final XmlPullParser parser = Xml.newPullParser();
3322 parser.setInput(fis, null);
3323 int type = parser.getEventType();
3324 // Skip parsing until START_TAG
3325 while ((type = parser.next()) != XmlPullParser.START_TAG
3326 && type != XmlPullParser.END_DOCUMENT) {}
3327 String firstNodeName = parser.getName();
3328 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3329 throw new XmlPullParserException("Xml doesn't start with subtypes");
3330 }
3331 final int depth =parser.getDepth();
3332 String currentImiId = null;
3333 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3334 while (((type = parser.next()) != XmlPullParser.END_TAG
3335 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3336 if (type != XmlPullParser.START_TAG)
3337 continue;
3338 final String nodeName = parser.getName();
3339 if (NODE_IMI.equals(nodeName)) {
3340 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3341 if (TextUtils.isEmpty(currentImiId)) {
3342 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3343 continue;
3344 }
3345 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3346 allSubtypes.put(currentImiId, tempSubtypesArray);
3347 } else if (NODE_SUBTYPE.equals(nodeName)) {
3348 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3349 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3350 continue;
3351 }
3352 final int icon = Integer.valueOf(
3353 parser.getAttributeValue(null, ATTR_ICON));
3354 final int label = Integer.valueOf(
3355 parser.getAttributeValue(null, ATTR_LABEL));
3356 final String imeSubtypeLocale =
3357 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3358 final String imeSubtypeMode =
3359 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3360 final String imeSubtypeExtraValue =
3361 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003362 final boolean isAuxiliary = "1".equals(String.valueOf(
3363 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003364 final InputMethodSubtype subtype =
3365 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3366 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3367 tempSubtypesArray.add(subtype);
3368 }
3369 }
3370 } catch (XmlPullParserException e) {
3371 Slog.w(TAG, "Error reading subtypes: " + e);
3372 return;
3373 } catch (java.io.IOException e) {
3374 Slog.w(TAG, "Error reading subtypes: " + e);
3375 return;
3376 } catch (NumberFormatException e) {
3377 Slog.w(TAG, "Error reading subtypes: " + e);
3378 return;
3379 } finally {
3380 if (fis != null) {
3381 try {
3382 fis.close();
3383 } catch (java.io.IOException e1) {
3384 Slog.w(TAG, "Failed to close.");
3385 }
3386 }
3387 }
3388 }
3389 }
3390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003392 // Utilities for debug
3393 private static String getStackTrace() {
3394 final StringBuilder sb = new StringBuilder();
3395 try {
3396 throw new RuntimeException();
3397 } catch (RuntimeException e) {
3398 final StackTraceElement[] frames = e.getStackTrace();
3399 // Start at 1 because the first frame is here and we don't care about it
3400 for (int j = 1; j < frames.length; ++j) {
3401 sb.append(frames[j].toString() + "\n");
3402 }
3403 }
3404 return sb.toString();
3405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 @Override
3408 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3409 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3410 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3413 + Binder.getCallingPid()
3414 + ", uid=" + Binder.getCallingUid());
3415 return;
3416 }
3417
3418 IInputMethod method;
3419 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 synchronized (mMethodMap) {
3424 p.println("Current Input Method Manager state:");
3425 int N = mMethodList.size();
3426 p.println(" Input Methods:");
3427 for (int i=0; i<N; i++) {
3428 InputMethodInfo info = mMethodList.get(i);
3429 p.println(" InputMethod #" + i + ":");
3430 info.dump(p, " ");
3431 }
3432 p.println(" Clients:");
3433 for (ClientState ci : mClients.values()) {
3434 p.println(" Client " + ci + ":");
3435 p.println(" client=" + ci.client);
3436 p.println(" inputContext=" + ci.inputContext);
3437 p.println(" sessionRequested=" + ci.sessionRequested);
3438 p.println(" curSession=" + ci.curSession);
3439 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003440 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003442 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3443 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3445 + " mBoundToMethod=" + mBoundToMethod);
3446 p.println(" mCurToken=" + mCurToken);
3447 p.println(" mCurIntent=" + mCurIntent);
3448 method = mCurMethod;
3449 p.println(" mCurMethod=" + mCurMethod);
3450 p.println(" mEnabledSession=" + mEnabledSession);
3451 p.println(" mShowRequested=" + mShowRequested
3452 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3453 + " mShowForced=" + mShowForced
3454 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003455 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003457
Jeff Brownb88102f2010-09-08 11:49:43 -07003458 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 pw.flush();
3461 try {
3462 client.client.asBinder().dump(fd, args);
3463 } catch (RemoteException e) {
3464 p.println("Input method client dead: " + e);
3465 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003466 } else {
3467 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003469
Jeff Brownb88102f2010-09-08 11:49:43 -07003470 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 pw.flush();
3473 try {
3474 method.asBinder().dump(fd, args);
3475 } catch (RemoteException e) {
3476 p.println("Input method service dead: " + e);
3477 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003478 } else {
3479 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 }
3481 }
3482}