blob: 14841af70f4367755b890b93e80e5a62a0bee52c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090019import com.android.internal.inputmethod.InputMethodUtils;
20import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070022import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090023import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.view.IInputContext;
25import com.android.internal.view.IInputMethod;
26import com.android.internal.view.IInputMethodCallback;
27import com.android.internal.view.IInputMethodClient;
28import com.android.internal.view.IInputMethodManager;
29import com.android.internal.view.IInputMethodSession;
30import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080031import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090032import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090042import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090043import android.app.Notification;
44import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070045import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090046import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.ComponentName;
48import android.content.ContentResolver;
49import android.content.Context;
50import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.DialogInterface.OnCancelListener;
52import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090053import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070055import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090056import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.PackageManager;
58import android.content.pm.ResolveInfo;
59import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070060import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Resources;
62import android.content.res.TypedArray;
63import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080064import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090066import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Handler;
68import android.os.IBinder;
69import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090070import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Parcel;
74import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080075import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.ServiceManager;
77import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.provider.Settings;
80import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090081import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070082import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090084import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090085import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.PrintWriterPrinter;
87import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090088import android.util.Slog;
89import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090091import android.view.LayoutInflater;
92import android.view.View;
93import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090095import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.inputmethod.InputBinding;
97import android.view.inputmethod.InputMethod;
98import android.view.inputmethod.InputMethodInfo;
99import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900101import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900102import android.widget.CompoundButton;
103import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900104import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900105import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
satoke7c6998e2011-06-03 17:57:59 +0900108import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900110import java.io.FileInputStream;
111import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.IOException;
113import java.io.PrintWriter;
114import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900115import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900116import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900118import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900120import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900121import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123/**
124 * This class provides a system service that manages input methods.
125 */
126public class InputMethodManagerService extends IInputMethodManager.Stub
127 implements ServiceConnection, Handler.Callback {
128 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700129 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
131 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900132 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900133 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900134 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final int MSG_UNBIND_INPUT = 1000;
137 static final int MSG_BIND_INPUT = 1010;
138 static final int MSG_SHOW_SOFT_INPUT = 1020;
139 static final int MSG_HIDE_SOFT_INPUT = 1030;
140 static final int MSG_ATTACH_TOKEN = 1040;
141 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final int MSG_START_INPUT = 2000;
144 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int MSG_UNBIND_METHOD = 3000;
147 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700148 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
satok01038492012-04-09 21:08:27 +0900150 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
satokf9f01002011-05-19 21:31:50 +0900154 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
155
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900156 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900157 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900167 private InputMethodFileManager mFileManager;
168 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900169 private final HardKeyboardListener mHardKeyboardListener;
170 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // All known input methods. mMethodMap also serves as the global
175 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900176 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
177 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900178 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
179 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700181 // Used to bring IME service up to visible adjustment while it is being shown.
182 final ServiceConnection mVisibleConnection = new ServiceConnection() {
183 @Override public void onServiceConnected(ComponentName name, IBinder service) {
184 }
185
186 @Override public void onServiceDisconnected(ComponentName name) {
187 }
188 };
189 boolean mVisibleBound = false;
190
satok7cfc0ed2011-06-20 21:29:36 +0900191 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700192 private NotificationManager mNotificationManager;
193 private KeyguardManager mKeyguardManager;
194 private StatusBarManagerService mStatusBar;
195 private Notification mImeSwitcherNotification;
196 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900197 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900198 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900199 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 class SessionState {
202 final ClientState client;
203 final IInputMethod method;
204 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "SessionState{uid " + client.uid + " pid " + client.pid
209 + " method " + Integer.toHexString(
210 System.identityHashCode(method))
211 + " session " + Integer.toHexString(
212 System.identityHashCode(session))
213 + "}";
214 }
215
216 SessionState(ClientState _client, IInputMethod _method,
217 IInputMethodSession _session) {
218 client = _client;
219 method = _method;
220 session = _session;
221 }
222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 class ClientState {
225 final IInputMethodClient client;
226 final IInputContext inputContext;
227 final int uid;
228 final int pid;
229 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 boolean sessionRequested;
232 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 @Override
235 public String toString() {
236 return "ClientState{" + Integer.toHexString(
237 System.identityHashCode(this)) + " uid " + uid
238 + " pid " + pid + "}";
239 }
240
241 ClientState(IInputMethodClient _client, IInputContext _inputContext,
242 int _uid, int _pid) {
243 client = _client;
244 inputContext = _inputContext;
245 uid = _uid;
246 pid = _pid;
247 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final HashMap<IBinder, ClientState> mClients
252 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700255 * Set once the system is ready to run third party code.
256 */
257 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Id of the currently selected input method.
261 */
262 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * The current binding sequence number, incremented every time there is
266 * a new bind performed.
267 */
268 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The client that is currently bound to an input method.
272 */
273 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700276 * The last window token that gained focus.
277 */
278 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * The input context last provided by the current client.
282 */
283 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The attributes last provided by the current client.
287 */
288 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The input method ID of the input method service that we are currently
292 * connected to or in the process of connecting to.
293 */
294 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
satokab751aa2010-09-14 19:17:36 +0900297 * The current subtype of the current input method.
298 */
299 private InputMethodSubtype mCurrentSubtype;
300
satok4e4569d2010-11-19 18:45:53 +0900301 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900302 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
303 mShortcutInputMethodsAndSubtypes =
304 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900305
306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * Set to true if our ServiceConnection is currently actively bound to
308 * a service (whether or not we have gotten its IBinder back yet).
309 */
310 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if the client has asked for the input method to be shown.
314 */
315 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if we were explicitly told to show the input method.
319 */
320 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we were forced to be shown.
324 */
325 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we last told the input method to show itself.
329 */
330 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The Intent used to connect to the current input method.
334 */
335 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The token we have made for the currently active input method, to
339 * identify it in the future.
340 */
341 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * If non-null, this is the input method service we are currently connected
345 * to.
346 */
347 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Time that we last initiated a bind to the input method, to determine
351 * if we should try to disconnect and reconnect to it.
352 */
353 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Have we called mCurMethod.bindInput()?
357 */
358 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Currently enabled session. Only touched by service thread, not
362 * protected by a lock.
363 */
364 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * True if the screen is on. The value is true initially.
368 */
369 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Joe Onorato857fd9b2011-01-27 15:08:35 -0800371 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
372 int mImeWindowVis;
373
Ken Wakasa05dbb652011-08-22 15:22:43 +0900374 private AlertDialog.Builder mDialogBuilder;
375 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900376 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900377 private InputMethodInfo[] mIms;
378 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900379 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900380 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
381 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500382 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800385 String mLastEnabled = "";
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 SettingsObserver(Handler handler) {
388 super(handler);
389 ContentResolver resolver = mContext.getContentResolver();
390 resolver.registerContentObserver(Settings.Secure.getUriFor(
391 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900392 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900393 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
394 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900395 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override public void onChange(boolean selfChange) {
399 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800400 boolean enabledChanged = false;
401 String newEnabled = mSettings.getEnabledInputMethodsStr();
402 if (!mLastEnabled.equals(newEnabled)) {
403 mLastEnabled = newEnabled;
404 enabledChanged = true;
405 }
406 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408 }
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900411 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
412 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700414 if (mCurClient != null && mCurClient.client != null) {
415 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
416 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900419
420 @Override
421 public void onReceive(Context context, Intent intent) {
422 final String action = intent.getAction();
423 if (Intent.ACTION_SCREEN_ON.equals(action)) {
424 mScreenOn = true;
425 refreshImeWindowVisibilityLocked();
426 updateActive();
427 return;
428 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
429 mScreenOn = false;
430 setImeWindowVisibilityStatusHiddenLocked();
431 updateActive();
432 return;
433 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
434 hideInputMethodMenu();
435 // No need to updateActive
436 return;
437 } else {
438 Slog.w(TAG, "Unexpected intent " + intent);
439 }
440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900444 private boolean isChangingPackagesOfCurrentUser() {
445 final int userId = getChangingUserId();
446 final boolean retval = userId == mSettings.getCurrentUserId();
447 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900448 if (!retval) {
449 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
450 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 }
452 return retval;
453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 if (!isChangingPackagesOfCurrentUser()) {
458 return false;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900461 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final int N = mMethodList.size();
463 if (curInputMethodId != null) {
464 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 InputMethodInfo imi = mMethodList.get(i);
466 if (imi.getId().equals(curInputMethodId)) {
467 for (String pkg : packages) {
468 if (imi.getPackageName().equals(pkg)) {
469 if (!doit) {
470 return true;
471 }
satok723a27e2010-11-11 14:58:11 +0900472 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 chooseNewDefaultIMELocked();
474 return true;
475 }
476 }
477 }
478 }
479 }
480 }
481 return false;
482 }
483
484 @Override
485 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900486 if (!isChangingPackagesOfCurrentUser()) {
487 return;
488 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 synchronized (mMethodMap) {
490 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800492 final int N = mMethodList.size();
493 if (curInputMethodId != null) {
494 for (int i=0; i<N; i++) {
495 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900496 final String imiId = imi.getId();
497 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 curIm = imi;
499 }
satoke7c6998e2011-06-03 17:57:59 +0900500
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900502 if (isPackageModified(imi.getPackageName())) {
503 mFileManager.deleteAllInputMethodSubtypes(imiId);
504 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 if (change == PACKAGE_TEMPORARY_CHANGE
506 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 + imi.getComponent());
509 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900514 buildInputMethodListLocked(
515 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800520 int change = isPackageDisappearing(curIm.getPackageName());
521 if (change == PACKAGE_TEMPORARY_CHANGE
522 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 ServiceInfo si = null;
524 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900525 si = mIPackageManager.getServiceInfo(
526 curIm.getComponent(), 0, mSettings.getCurrentUserId());
527 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 }
529 if (si == null) {
530 // Uh oh, current input method is no longer around!
531 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900533 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 changed = true;
536 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900538 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 }
540 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 }
satokab751aa2010-09-14 19:17:36 +0900543
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800544 if (curIm == null) {
545 // We currently don't have a default input method... is
546 // one now available?
547 changed = chooseNewDefaultIMELocked();
548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800551 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553 }
554 }
555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Jean Chalarde0d32a62011-10-20 20:36:07 +0900557 private static class MethodCallback extends IInputMethodCallback.Stub {
558 private final IInputMethod mMethod;
559 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
Jean Chalarde0d32a62011-10-20 20:36:07 +0900561 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900563 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
satoke7c6998e2011-06-03 17:57:59 +0900566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void finishedEvent(int seq, boolean handled) throws RemoteException {
568 }
569
satoke7c6998e2011-06-03 17:57:59 +0900570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900572 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800575
satok01038492012-04-09 21:08:27 +0900576 private class HardKeyboardListener
577 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
578 @Override
579 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
580 mHandler.sendMessage(mHandler.obtainMessage(
581 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
582 }
583
584 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
585 if (DEBUG) {
586 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
587 + enabled);
588 }
589 synchronized(mMethodMap) {
590 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
591 && mSwitchingDialog.isShowing()) {
592 mSwitchingDialogTitleView.findViewById(
593 com.android.internal.R.id.hard_keyboard_section).setVisibility(
594 available ? View.VISIBLE : View.GONE);
595 }
596 }
597 }
598 }
599
600 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900601 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800603 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 mHandler = new Handler(this);
605 mIWindowManager = IWindowManager.Stub.asInterface(
606 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800607 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900608 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public void executeMessage(Message msg) {
610 handleMessage(msg);
611 }
Mita Yuned218c72012-12-06 17:18:25 -0800612 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900613 mWindowManagerService = windowManager;
614 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900615
satok7cfc0ed2011-06-20 21:29:36 +0900616 mImeSwitcherNotification = new Notification();
617 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
618 mImeSwitcherNotification.when = 0;
619 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
620 mImeSwitcherNotification.tickerText = null;
621 mImeSwitcherNotification.defaults = 0; // please be quiet
622 mImeSwitcherNotification.sound = null;
623 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400624
625 // Tag this notification specially so SystemUI knows it's important
626 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
627
satok7cfc0ed2011-06-20 21:29:36 +0900628 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900629 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900630
631 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900632
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 final IntentFilter broadcastFilter = new IntentFilter();
634 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
635 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
636 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
637 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
satok7cfc0ed2011-06-20 21:29:36 +0900639 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 int userId = 0;
641 try {
642 ActivityManagerNative.getDefault().registerUserSwitchObserver(
643 new IUserSwitchObserver.Stub() {
644 @Override
645 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900646 synchronized(mMethodMap) {
647 switchUserLocked(newUserId);
648 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900649 if (reply != null) {
650 try {
651 reply.sendResult(null);
652 } catch (RemoteException e) {
653 }
654 }
655 }
656
657 @Override
658 public void onUserSwitchComplete(int newUserId) throws RemoteException {
659 }
660 });
661 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
662 } catch (RemoteException e) {
663 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
664 }
satok81f8b7c2012-12-04 20:42:56 +0900665 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900666
satokd87c2592010-09-29 11:52:06 +0900667 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900668 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900670 mFileManager = new InputMethodFileManager(mMethodMap, userId);
671 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900672
673 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900675 if (DEBUG) {
676 Slog.d(TAG, "Initial default ime = " + defaultImiId);
677 }
satok0a1bcf42012-05-16 19:26:31 +0900678 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
679
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900680 buildInputMethodListLocked(mMethodList, mMethodMap,
681 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900682 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
satok0a1bcf42012-05-16 19:26:31 +0900684 if (!mImeSelectedOnBoot) {
685 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900686 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800690 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900691
692 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
693 // according to the new system locale.
694 final IntentFilter filter = new IntentFilter();
695 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
696 mContext.registerReceiver(
697 new BroadcastReceiver() {
698 @Override
699 public void onReceive(Context context, Intent intent) {
700 synchronized(mMethodMap) {
701 checkCurrentLocaleChangedLocked();
702 }
703 }
704 }, filter);
705 }
706
satok5b927c432012-05-01 20:09:34 +0900707 private void resetDefaultImeLocked(Context context) {
708 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900709 if (mCurMethodId != null
710 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900711 return;
712 }
713
714 InputMethodInfo defIm = null;
715 for (InputMethodInfo imi : mMethodList) {
716 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900717 if (InputMethodUtils.isValidSystemDefaultIme(
718 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900719 defIm = imi;
720 Slog.i(TAG, "Selected default: " + imi.getId());
721 }
722 }
723 }
724 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900725 defIm = InputMethodUtils.getMostApplicableDefaultIME(
726 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900727 Slog.i(TAG, "No default found, using " + defIm.getId());
728 }
729 if (defIm != null) {
730 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
731 }
732 }
733
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900734 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
735 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 if (!mSystemReady) {
737 // not system ready
738 return;
739 }
740 final Locale newLocale = mRes.getConfiguration().locale;
741 if (!updateOnlyWhenLocaleChanged
742 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
743 if (!updateOnlyWhenLocaleChanged) {
744 hideCurrentInputLocked(0, null);
745 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700746 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900747 }
748 if (DEBUG) {
749 Slog.i(TAG, "Locale has been changed to " + newLocale);
750 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900751 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
752 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900753 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 if (!updateOnlyWhenLocaleChanged) {
755 final String selectedImiId = mSettings.getSelectedInputMethod();
756 if (TextUtils.isEmpty(selectedImiId)) {
757 // This is the first time of the user switch and
758 // set the current ime to the proper one.
759 resetDefaultImeLocked(mContext);
760 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900761 } else {
762 // If the locale is changed, needs to reset the default ime
763 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900764 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800765 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900766 mLastSystemLocale = newLocale;
767 if (!updateOnlyWhenLocaleChanged) {
768 try {
769 startInputInnerLocked();
770 } catch (RuntimeException e) {
771 Slog.w(TAG, "Unexpected exception", e);
772 }
773 }
774 }
775 }
776
777 private void checkCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900778 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
779 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 }
781
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900782 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900783 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900784 // InputMethodFileManager should be reset when the user is changed
785 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900786 final String defaultImiId = mSettings.getSelectedInputMethod();
787 final boolean needsToResetDefaultIme = TextUtils.isEmpty(defaultImiId);
788 if (DEBUG) {
789 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
790 }
791 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
792 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900793 }
794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 @Override
796 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
797 throws RemoteException {
798 try {
799 return super.onTransact(code, data, reply, flags);
800 } catch (RuntimeException e) {
801 // The input method manager only throws security exceptions, so let's
802 // log all others.
803 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800804 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806 throw e;
807 }
808 }
809
Dianne Hackborn661cd522011-08-22 00:26:20 -0700810 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700811 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900812 if (DEBUG) {
813 Slog.d(TAG, "--- systemReady");
814 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700815 if (!mSystemReady) {
816 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 mKeyguardManager =
818 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700819 mNotificationManager = (NotificationManager)
820 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
821 mStatusBar = statusBar;
822 statusBar.setIconVisibility("ime", false);
823 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900824 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
825 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900826 if (mShowOngoingImeSwitcherForPhones) {
827 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
828 mHardKeyboardListener);
829 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900830 buildInputMethodListLocked(mMethodList, mMethodMap,
831 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900832 if (!mImeSelectedOnBoot) {
833 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
834 checkCurrentLocaleChangedLocked();
835 }
836 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 try {
838 startInputInnerLocked();
839 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800840 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700841 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700842 }
843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800845
satok15452a42011-10-28 17:58:28 +0900846 private void setImeWindowVisibilityStatusHiddenLocked() {
847 mImeWindowVis = 0;
848 updateImeWindowStatusLocked();
849 }
850
satok3afd6c02011-11-18 08:38:19 +0900851 private void refreshImeWindowVisibilityLocked() {
852 final Configuration conf = mRes.getConfiguration();
853 final boolean haveHardKeyboard = conf.keyboard
854 != Configuration.KEYBOARD_NOKEYS;
855 final boolean hardKeyShown = haveHardKeyboard
856 && conf.hardKeyboardHidden
857 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500858 final boolean isScreenLocked =
859 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
860 final boolean isScreenSecurelyLocked =
861 isScreenLocked && mKeyguardManager.isKeyguardSecure();
862 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
863 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900864 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
865 updateImeWindowStatusLocked();
866 }
867
satok15452a42011-10-28 17:58:28 +0900868 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900869 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700870 }
871
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 // ---------------------------------------------------------------------------------------
873 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
874 // 1) it comes from the system process
875 // 2) the calling process' user id is identical to the current user id IMMS thinks.
876 private boolean calledFromValidUser() {
877 final int uid = Binder.getCallingUid();
878 final int userId = UserHandle.getUserId(uid);
879 if (DEBUG) {
880 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
881 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
882 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900883 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900884 }
885 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
886 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900887 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900888
889 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
890 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
891 // must not manage background users' states in any functions.
892 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
893 // by a token.
894 if (mContext.checkCallingOrSelfPermission(
895 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
896 == PackageManager.PERMISSION_GRANTED) {
897 if (DEBUG) {
898 Slog.d(TAG, "--- Access granted because the calling process has "
899 + "the INTERACT_ACROSS_USERS_FULL permission");
900 }
901 return true;
902 }
903 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
904 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900905 }
906
907 private boolean bindCurrentInputMethodService(
908 Intent service, ServiceConnection conn, int flags) {
909 if (service == null || conn == null) {
910 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
911 return false;
912 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800913 return mContext.bindServiceAsUser(service, conn, flags,
914 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900915 }
916
satoke7c6998e2011-06-03 17:57:59 +0900917 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900919 // TODO: Make this work even for non-current users?
920 if (!calledFromValidUser()) {
921 return Collections.emptyList();
922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 synchronized (mMethodMap) {
924 return new ArrayList<InputMethodInfo>(mMethodList);
925 }
926 }
927
satoke7c6998e2011-06-03 17:57:59 +0900928 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 // TODO: Make this work even for non-current users?
931 if (!calledFromValidUser()) {
932 return Collections.emptyList();
933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900935 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937 }
938
satokbb4aa062011-01-19 21:40:27 +0900939 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
940 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
941 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
942 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900944 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900945 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900946 }
947 return enabledInputMethodAndSubtypes;
948 }
949
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900950 /**
951 * @param imi if null, returns enabled subtypes for the current imi
952 * @return enabled subtypes of the specified imi
953 */
satoke7c6998e2011-06-03 17:57:59 +0900954 @Override
satok16331c82010-12-20 23:48:46 +0900955 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
956 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 // TODO: Make this work even for non-current users?
958 if (!calledFromValidUser()) {
959 return Collections.emptyList();
960 }
satok67ddf9c2010-11-17 09:45:54 +0900961 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900962 if (imi == null && mCurMethodId != null) {
963 imi = mMethodMap.get(mCurMethodId);
964 }
965 return mSettings.getEnabledInputMethodSubtypeListLocked(
966 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900967 }
968 }
969
satoke7c6998e2011-06-03 17:57:59 +0900970 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 public void addClient(IInputMethodClient client,
972 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 if (!calledFromValidUser()) {
974 return;
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 synchronized (mMethodMap) {
977 mClients.put(client.asBinder(), new ClientState(client,
978 inputContext, uid, pid));
979 }
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
satoke7c6998e2011-06-03 17:57:59 +0900982 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 if (!calledFromValidUser()) {
985 return;
986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 synchronized (mMethodMap) {
988 mClients.remove(client.asBinder());
989 }
990 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 void executeOrSendMessage(IInterface target, Message msg) {
993 if (target.asBinder() instanceof Binder) {
994 mCaller.sendMessage(msg);
995 } else {
996 handleMessage(msg);
997 msg.recycle();
998 }
999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001000
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001001 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001003 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 + mCurClient.client.asBinder());
1005 if (mBoundToMethod) {
1006 mBoundToMethod = false;
1007 if (mCurMethod != null) {
1008 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1009 MSG_UNBIND_INPUT, mCurMethod));
1010 }
1011 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001012
1013 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1014 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1016 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1017 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001019
The Android Open Source Project10592532009-03-18 17:39:46 -07001020 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 private int getImeShowFlags() {
1025 int flags = 0;
1026 if (mShowForced) {
1027 flags |= InputMethod.SHOW_FORCED
1028 | InputMethod.SHOW_EXPLICIT;
1029 } else if (mShowExplicitlyRequested) {
1030 flags |= InputMethod.SHOW_EXPLICIT;
1031 }
1032 return flags;
1033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 private int getAppShowFlags() {
1036 int flags = 0;
1037 if (mShowForced) {
1038 flags |= InputMethodManager.SHOW_FORCED;
1039 } else if (!mShowExplicitlyRequested) {
1040 flags |= InputMethodManager.SHOW_IMPLICIT;
1041 }
1042 return flags;
1043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
Dianne Hackborn7663d802012-02-24 13:08:49 -08001045 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (!mBoundToMethod) {
1047 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1048 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1049 mBoundToMethod = true;
1050 }
1051 final SessionState session = mCurClient.curSession;
1052 if (initial) {
1053 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1054 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1055 } else {
1056 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1057 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1058 }
1059 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001060 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001061 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001063 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001067 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 // If no method is currently selected, do nothing.
1069 if (mCurMethodId == null) {
1070 return mNoBinding;
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 ClientState cs = mClients.get(client.asBinder());
1074 if (cs == null) {
1075 throw new IllegalArgumentException("unknown client "
1076 + client.asBinder());
1077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 try {
1080 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1081 // Check with the window manager to make sure this client actually
1082 // has a window with focus. If not, reject. This is thread safe
1083 // because if the focus changes some time before or after, the
1084 // next client receiving focus that has any interest in input will
1085 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001086 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1088 return null;
1089 }
1090 } catch (RemoteException e) {
1091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
Dianne Hackborn7663d802012-02-24 13:08:49 -08001093 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1094 }
1095
1096 InputBindResult startInputUncheckedLocked(ClientState cs,
1097 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1098 // If no method is currently selected, do nothing.
1099 if (mCurMethodId == null) {
1100 return mNoBinding;
1101 }
1102
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001103 if (mCurClient == null) {
1104 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1105 if (DEBUG) {
1106 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1107 }
1108 }
1109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 if (mCurClient != cs) {
1111 // If the client is changing, we need to switch over to the new
1112 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001113 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001114 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 + cs.client.asBinder());
1116
1117 // If the screen is on, inform the new client it is active
1118 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001119 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1120 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // Bump up the sequence for this client and attach it.
1125 mCurSeq++;
1126 if (mCurSeq <= 0) mCurSeq = 1;
1127 mCurClient = cs;
1128 mCurInputContext = inputContext;
1129 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 // Check if the input method is changing.
1132 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1133 if (cs.curSession != null) {
1134 // Fast case: if we are already connected to the input method,
1135 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001136 return attachNewInputLocked(
1137 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 if (mHaveConnection) {
1140 if (mCurMethod != null) {
1141 if (!cs.sessionRequested) {
1142 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1145 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001146 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148 // Return to client, and we will get back with it when
1149 // we have had a session made for it.
1150 return new InputBindResult(null, mCurId, mCurSeq);
1151 } else if (SystemClock.uptimeMillis()
1152 < (mLastBindTime+TIME_TO_RECONNECT)) {
1153 // In this case we have connected to the service, but
1154 // don't yet have its interface. If it hasn't been too
1155 // long since we did the connection, we'll return to
1156 // the client and wait to get the service interface so
1157 // we can report back. If it has been too long, we want
1158 // to fall through so we can try a disconnect/reconnect
1159 // to see if we can get back in touch with the service.
1160 return new InputBindResult(null, mCurId, mCurSeq);
1161 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001162 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1163 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001168 return startInputInnerLocked();
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001171 InputBindResult startInputInnerLocked() {
1172 if (mCurMethodId == null) {
1173 return mNoBinding;
1174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001176 if (!mSystemReady) {
1177 // If the system is not yet ready, we shouldn't be running third
1178 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001179 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1183 if (info == null) {
1184 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001187 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1190 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001191 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1192 com.android.internal.R.string.input_method_binding_label);
1193 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1194 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001195 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001196 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 mLastBindTime = SystemClock.uptimeMillis();
1198 mHaveConnection = true;
1199 mCurId = info.getId();
1200 mCurToken = new Binder();
1201 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001202 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 mIWindowManager.addWindowToken(mCurToken,
1204 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1205 } catch (RemoteException e) {
1206 }
1207 return new InputBindResult(null, mCurId, mCurSeq);
1208 } else {
1209 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 + mCurIntent);
1212 }
1213 return null;
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
satoke7c6998e2011-06-03 17:57:59 +09001216 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001218 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001219 if (!calledFromValidUser()) {
1220 return null;
1221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 synchronized (mMethodMap) {
1223 final long ident = Binder.clearCallingIdentity();
1224 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001225 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } finally {
1227 Binder.restoreCallingIdentity(ident);
1228 }
1229 }
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
satoke7c6998e2011-06-03 17:57:59 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void finishInput(IInputMethodClient client) {
1234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
satoke7c6998e2011-06-03 17:57:59 +09001236 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 public void onServiceConnected(ComponentName name, IBinder service) {
1238 synchronized (mMethodMap) {
1239 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001240 IInputMethod prevMethod = mCurMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001242 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001244 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001245 return;
1246 }
Craig Mautnerca0ac712013-03-14 09:43:02 -07001247 // Remove messages relating to the previous service. Otherwise WindowManagerService
1248 // will throw a BadTokenException because the old token is being removed.
1249 if (prevMethod != null) {
1250 try {
1251 prevMethod.removeSoftInputMessages();
1252 } catch (RemoteException e) {
1253 }
1254 }
Craig Mautner274323c2013-03-12 14:12:39 -07001255 mCaller.removeMessages(MSG_SHOW_SOFT_INPUT);
1256 mCaller.removeMessages(MSG_HIDE_SOFT_INPUT);
Craig Mautner6efb4c72013-03-13 10:17:41 -07001257 if (true || DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001258 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1259 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001261 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001262 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001264 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001265 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267 }
1268 }
1269 }
1270
1271 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1272 synchronized (mMethodMap) {
1273 if (mCurMethod != null && method != null
1274 && mCurMethod.asBinder() == method.asBinder()) {
1275 if (mCurClient != null) {
1276 mCurClient.curSession = new SessionState(mCurClient,
1277 method, session);
1278 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001279 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 if (res.method != null) {
1281 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1282 MSG_BIND_METHOD, mCurClient.client, res));
1283 }
1284 }
1285 }
1286 }
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001289 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001290 if (mVisibleBound) {
1291 mContext.unbindService(mVisibleConnection);
1292 mVisibleBound = false;
1293 }
1294
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001295 if (mHaveConnection) {
1296 mContext.unbindService(this);
1297 mHaveConnection = false;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001300 if (mCurToken != null) {
1301 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001302 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001303 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001304 // The current IME is shown. Hence an IME switch (transition) is happening.
1305 mWindowManagerService.saveLastInputMethodWindowForTransition();
1306 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001307 mIWindowManager.removeWindowToken(mCurToken);
1308 } catch (RemoteException e) {
1309 }
1310 mCurToken = null;
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
The Android Open Source Project10592532009-03-18 17:39:46 -07001313 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001314 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 if (reportToClient && mCurClient != null) {
1317 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1318 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1319 }
1320 }
1321
Devin Taylor0c33ed22010-02-23 13:26:46 -06001322 private void finishSession(SessionState sessionState) {
1323 if (sessionState != null && sessionState.session != null) {
1324 try {
1325 sessionState.session.finishSession();
1326 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001327 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001328 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001329 }
1330 }
1331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001333 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 if (mCurMethod != null) {
1335 for (ClientState cs : mClients.values()) {
1336 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001337 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 cs.curSession = null;
1339 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001340
1341 finishSession(mEnabledSession);
1342 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 mCurMethod = null;
1344 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001345 if (mStatusBar != null) {
1346 mStatusBar.setIconVisibility("ime", false);
1347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
satoke7c6998e2011-06-03 17:57:59 +09001350 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 public void onServiceDisconnected(ComponentName name) {
1352 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001353 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 + " mCurIntent=" + mCurIntent);
1355 if (mCurMethod != null && mCurIntent != null
1356 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001357 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 // We consider this to be a new bind attempt, since the system
1359 // should now try to restart the service for us.
1360 mLastBindTime = SystemClock.uptimeMillis();
1361 mShowRequested = mInputShown;
1362 mInputShown = false;
1363 if (mCurClient != null) {
1364 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1365 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1366 }
1367 }
1368 }
1369 }
1370
satokf9f01002011-05-19 21:31:50 +09001371 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001373 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 long ident = Binder.clearCallingIdentity();
1375 try {
1376 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001377 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 return;
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 synchronized (mMethodMap) {
1382 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001384 if (mStatusBar != null) {
1385 mStatusBar.setIconVisibility("ime", false);
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001389 CharSequence contentDescription = null;
1390 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001391 // Use PackageManager to load label
1392 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001393 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001394 mIPackageManager.getApplicationInfo(packageName, 0,
1395 mSettings.getCurrentUserId()));
1396 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001397 /* ignore */
1398 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001399 if (mStatusBar != null) {
1400 mStatusBar.setIcon("ime", packageName, iconId, 0,
1401 contentDescription != null
1402 ? contentDescription.toString() : null);
1403 mStatusBar.setIconVisibility("ime", true);
1404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 }
1407 } finally {
1408 Binder.restoreCallingIdentity(ident);
1409 }
1410 }
1411
satok7cfc0ed2011-06-20 21:29:36 +09001412 private boolean needsToShowImeSwitchOngoingNotification() {
1413 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001414 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001415 synchronized (mMethodMap) {
1416 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1417 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001418 if (N > 2) return true;
1419 if (N < 1) return false;
1420 int nonAuxCount = 0;
1421 int auxCount = 0;
1422 InputMethodSubtype nonAuxSubtype = null;
1423 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001424 for(int i = 0; i < N; ++i) {
1425 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001426 final List<InputMethodSubtype> subtypes =
1427 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001428 final int subtypeCount = subtypes.size();
1429 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001430 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001431 } else {
1432 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001433 final InputMethodSubtype subtype = subtypes.get(j);
1434 if (!subtype.isAuxiliary()) {
1435 ++nonAuxCount;
1436 nonAuxSubtype = subtype;
1437 } else {
1438 ++auxCount;
1439 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001440 }
1441 }
1442 }
satok7cfc0ed2011-06-20 21:29:36 +09001443 }
satokb6359412011-06-28 17:47:41 +09001444 if (nonAuxCount > 1 || auxCount > 1) {
1445 return true;
1446 } else if (nonAuxCount == 1 && auxCount == 1) {
1447 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001448 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1449 || auxSubtype.overridesImplicitlyEnabledSubtype()
1450 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001451 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1452 return false;
1453 }
1454 return true;
1455 }
1456 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001457 }
satok7cfc0ed2011-06-20 21:29:36 +09001458 }
1459
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001460 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001461 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001462 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001463 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001464 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001465 try {
1466 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001467 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001468 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001469 return;
1470 }
1471
1472 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001473 mImeWindowVis = vis;
1474 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001475 if (mStatusBar != null) {
1476 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1477 }
satok7cfc0ed2011-06-20 21:29:36 +09001478 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001479 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1480 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001482 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001483 final CharSequence title = mRes.getText(
1484 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001485 final CharSequence imiLabel = imi.loadLabel(pm);
1486 final CharSequence summary = mCurrentSubtype != null
1487 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1488 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1489 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001490 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001491 : imiLabel;
1492
satok7cfc0ed2011-06-20 21:29:36 +09001493 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001494 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001495 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001496 if (DEBUG) {
1497 Slog.d(TAG, "--- show notification: label = " + imiLabel
1498 + ", summary = " + summary);
1499 }
1500 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001501 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001502 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001503 mNotificationShown = true;
1504 }
satok7cfc0ed2011-06-20 21:29:36 +09001505 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001506 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001507 if (DEBUG) {
1508 Slog.d(TAG, "--- hide notification");
1509 }
1510 mNotificationManager.cancelAsUser(null,
1511 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001512 mNotificationShown = false;
1513 }
1514 }
satok06487a52010-10-29 11:37:18 +09001515 }
1516 } finally {
1517 Binder.restoreCallingIdentity(ident);
1518 }
1519 }
1520
satoke7c6998e2011-06-03 17:57:59 +09001521 @Override
satokf9f01002011-05-19 21:31:50 +09001522 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001523 if (!calledFromValidUser()) {
1524 return;
1525 }
satokf9f01002011-05-19 21:31:50 +09001526 synchronized (mMethodMap) {
1527 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1528 for (int i = 0; i < spans.length; ++i) {
1529 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001530 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001531 mSecureSuggestionSpans.put(ss, currentImi);
1532 }
1533 }
1534 }
1535 }
1536
satoke7c6998e2011-06-03 17:57:59 +09001537 @Override
satokf9f01002011-05-19 21:31:50 +09001538 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001539 if (!calledFromValidUser()) {
1540 return false;
1541 }
satokf9f01002011-05-19 21:31:50 +09001542 synchronized (mMethodMap) {
1543 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1544 // TODO: Do not send the intent if the process of the targetImi is already dead.
1545 if (targetImi != null) {
1546 final String[] suggestions = span.getSuggestions();
1547 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001548 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001549 final Intent intent = new Intent();
1550 // Ensures that only a class in the original IME package will receive the
1551 // notification.
satok42c5a162011-05-26 16:46:14 +09001552 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001553 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1554 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1555 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1556 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001557 final long ident = Binder.clearCallingIdentity();
1558 try {
1559 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1560 } finally {
1561 Binder.restoreCallingIdentity(ident);
1562 }
satokf9f01002011-05-19 21:31:50 +09001563 return true;
1564 }
1565 }
1566 return false;
1567 }
1568
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001569 void updateFromSettingsLocked(boolean enabledMayChange) {
1570 if (enabledMayChange) {
1571 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1572 for (int i=0; i<enabled.size(); i++) {
1573 // We allow the user to select "disabled until used" apps, so if they
1574 // are enabling one of those here we now need to make it enabled.
1575 InputMethodInfo imm = enabled.get(i);
1576 try {
1577 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1578 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1579 mSettings.getCurrentUserId());
1580 if (ai.enabledSetting
1581 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1582 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1583 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1584 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1585 }
1586 } catch (RemoteException e) {
1587 }
1588 }
1589 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1591 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1592 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1593 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001595 // There is no input method selected, try to choose new applicable input method.
1596 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001597 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001598 }
1599 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001601 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001604 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001605 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
satokf3db1af2010-11-23 13:34:33 +09001607 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 } else {
1609 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001610 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001611 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
satokab751aa2010-09-14 19:17:36 +09001615 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 InputMethodInfo info = mMethodMap.get(id);
1617 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001618 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
satokd81e9502012-05-21 12:58:45 +09001621 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001623 final int subtypeCount = info.getSubtypeCount();
1624 if (subtypeCount <= 0) {
1625 return;
satokcd7cd292010-11-20 15:46:23 +09001626 }
satokd81e9502012-05-21 12:58:45 +09001627 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1628 final InputMethodSubtype newSubtype;
1629 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1630 newSubtype = info.getSubtypeAt(subtypeId);
1631 } else {
1632 // If subtype is null, try to find the most applicable one from
1633 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001634 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001635 }
1636 if (newSubtype == null || oldSubtype == null) {
1637 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1638 + ", new subtype = " + newSubtype);
1639 return;
1640 }
1641 if (newSubtype != oldSubtype) {
1642 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1643 if (mCurMethod != null) {
1644 try {
1645 refreshImeWindowVisibilityLocked();
1646 mCurMethod.changeInputMethodSubtype(newSubtype);
1647 } catch (RemoteException e) {
1648 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001649 }
1650 }
1651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 return;
1653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001654
satokd81e9502012-05-21 12:58:45 +09001655 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 final long ident = Binder.clearCallingIdentity();
1657 try {
satokab751aa2010-09-14 19:17:36 +09001658 // Set a subtype to this input method.
1659 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001660 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1661 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1662 // because mCurMethodId is stored as a history in
1663 // setSelectedInputMethodAndSubtypeLocked().
1664 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665
1666 if (ActivityManagerNative.isSystemReady()) {
1667 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001668 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001670 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001672 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 } finally {
1674 Binder.restoreCallingIdentity(ident);
1675 }
1676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001677
satok42c5a162011-05-26 16:46:14 +09001678 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001679 public boolean showSoftInput(IInputMethodClient client, int flags,
1680 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001681 if (!calledFromValidUser()) {
1682 return false;
1683 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001684 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 long ident = Binder.clearCallingIdentity();
1686 try {
1687 synchronized (mMethodMap) {
1688 if (mCurClient == null || client == null
1689 || mCurClient.client.asBinder() != client.asBinder()) {
1690 try {
1691 // We need to check if this is the current client with
1692 // focus in the window manager, to allow this call to
1693 // be made before input is started in it.
1694 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001695 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001699 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
Craig Mautner6efb4c72013-03-13 10:17:41 -07001703 if (true || DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001704 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 } finally {
1707 Binder.restoreCallingIdentity(ident);
1708 }
1709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 mShowRequested = true;
1713 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1714 mShowExplicitlyRequested = true;
1715 }
1716 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1717 mShowExplicitlyRequested = true;
1718 mShowForced = true;
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Dianne Hackborncc278702009-09-02 23:07:23 -07001721 if (!mSystemReady) {
1722 return false;
1723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001724
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 if (mCurMethod != null) {
Craig Mautner6efb4c72013-03-13 10:17:41 -07001727 if (true ||DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken,
1728 new RuntimeException("here").fillInStackTrace());
The Android Open Source Project4df24232009-03-05 14:34:35 -08001729 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1730 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1731 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001733 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001734 bindCurrentInputMethodService(
1735 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001736 mVisibleBound = true;
1737 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001738 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001740 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 // The client has asked to have the input method shown, but
1742 // we have been sitting here too long with a connection to the
1743 // service and no interface received, so let's disconnect/connect
1744 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001745 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001747 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001749 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001750 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001751 } else {
1752 if (DEBUG) {
1753 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1754 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
The Android Open Source Project4df24232009-03-05 14:34:35 -08001758 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001760
satok42c5a162011-05-26 16:46:14 +09001761 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001762 public boolean hideSoftInput(IInputMethodClient client, int flags,
1763 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001764 if (!calledFromValidUser()) {
1765 return false;
1766 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001767 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 long ident = Binder.clearCallingIdentity();
1769 try {
1770 synchronized (mMethodMap) {
1771 if (mCurClient == null || client == null
1772 || mCurClient.client.asBinder() != client.asBinder()) {
1773 try {
1774 // We need to check if this is the current client with
1775 // focus in the window manager, to allow this call to
1776 // be made before input is started in it.
1777 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001778 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1779 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001780 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001781 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001784 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001785 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
Joe Onorato8a9b2202010-02-26 18:56:32 -08001789 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001790 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 }
1792 } finally {
1793 Binder.restoreCallingIdentity(ident);
1794 }
1795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796
The Android Open Source Project4df24232009-03-05 14:34:35 -08001797 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1799 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautner6efb4c72013-03-13 10:17:41 -07001800 if (true ||DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautner6efb4c72013-03-13 10:17:41 -07001805 if (true ||DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001807 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001809 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001811 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1812 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1813 res = true;
1814 } else {
1815 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001817 if (mHaveConnection && mVisibleBound) {
1818 mContext.unbindService(mVisibleConnection);
1819 mVisibleBound = false;
1820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 mInputShown = false;
1822 mShowRequested = false;
1823 mShowExplicitlyRequested = false;
1824 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
satok42c5a162011-05-26 16:46:14 +09001828 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001829 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1830 int controlFlags, int softInputMode, int windowFlags,
1831 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001832 // Needs to check the validity before clearing calling identity
1833 final boolean calledFromValidUser = calledFromValidUser();
1834
Dianne Hackborn7663d802012-02-24 13:08:49 -08001835 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 long ident = Binder.clearCallingIdentity();
1837 try {
1838 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001839 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001840 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001842 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843
Dianne Hackborn7663d802012-02-24 13:08:49 -08001844 ClientState cs = mClients.get(client.asBinder());
1845 if (cs == null) {
1846 throw new IllegalArgumentException("unknown client "
1847 + client.asBinder());
1848 }
1849
1850 try {
1851 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1852 // Check with the window manager to make sure this client actually
1853 // has a window with focus. If not, reject. This is thread safe
1854 // because if the focus changes some time before or after, the
1855 // next client receiving focus that has any interest in input will
1856 // be calling through here after that change happens.
1857 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1858 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1859 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001861 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001863
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001864 if (!calledFromValidUser) {
1865 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1866 Slog.w(TAG, "If you want to interect with IME, you need "
1867 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1868 hideCurrentInputLocked(0, null);
1869 return null;
1870 }
1871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001872 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001873 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001874 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001875 if (attribute != null) {
1876 return startInputUncheckedLocked(cs, inputContext, attribute,
1877 controlFlags);
1878 }
1879 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001880 }
1881 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001883 // Should we auto-show the IME even if the caller has not
1884 // specified what should be done with it?
1885 // We only do this automatically if the window can resize
1886 // to accommodate the IME (so what the user sees will give
1887 // them good context without input information being obscured
1888 // by the IME) or if running on a large screen where there
1889 // is more room for the target window + IME.
1890 final boolean doAutoShow =
1891 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1892 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1893 || mRes.getConfiguration().isLayoutSizeAtLeast(
1894 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001895 final boolean isTextEditor =
1896 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1897
1898 // We want to start input before showing the IME, but after closing
1899 // it. We want to do this after closing it to help the IME disappear
1900 // more quickly (not get stuck behind it initializing itself for the
1901 // new focused input, even if its window wants to hide the IME).
1902 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1905 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001906 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1908 // There is no focus view, and this window will
1909 // be behind any soft input window, so hide the
1910 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001911 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001912 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001914 } else if (isTextEditor && doAutoShow && (softInputMode &
1915 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 // There is a focus view, and we are navigating forward
1917 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001918 // We only do this automatically if the window can resize
1919 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001920 // them good context without input information being obscured
1921 // by the IME) or if running on a large screen where there
1922 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001924 if (attribute != null) {
1925 res = startInputUncheckedLocked(cs, inputContext, attribute,
1926 controlFlags);
1927 didStart = true;
1928 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001929 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 break;
1932 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1933 // Do nothing.
1934 break;
1935 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1936 if ((softInputMode &
1937 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001938 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001939 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
1941 break;
1942 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001943 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001944 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 break;
1946 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1947 if ((softInputMode &
1948 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001949 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001950 if (attribute != null) {
1951 res = startInputUncheckedLocked(cs, inputContext, attribute,
1952 controlFlags);
1953 didStart = true;
1954 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001955 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957 break;
1958 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001959 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001960 if (attribute != null) {
1961 res = startInputUncheckedLocked(cs, inputContext, attribute,
1962 controlFlags);
1963 didStart = true;
1964 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001965 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 break;
1967 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001968
1969 if (!didStart && attribute != null) {
1970 res = startInputUncheckedLocked(cs, inputContext, attribute,
1971 controlFlags);
1972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 }
1974 } finally {
1975 Binder.restoreCallingIdentity(ident);
1976 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001977
1978 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
satok42c5a162011-05-26 16:46:14 +09001981 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001983 if (!calledFromValidUser()) {
1984 return;
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 synchronized (mMethodMap) {
1987 if (mCurClient == null || client == null
1988 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001989 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001990 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992
satok440aab52010-11-25 09:43:11 +09001993 // Always call subtype picker, because subtype picker is a superset of input method
1994 // picker.
satokab751aa2010-09-14 19:17:36 +09001995 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1996 }
1997 }
1998
satok42c5a162011-05-26 16:46:14 +09001999 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002001 if (!calledFromValidUser()) {
2002 return;
2003 }
satok28203512010-11-24 11:06:49 +09002004 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2005 }
2006
satok42c5a162011-05-26 16:46:14 +09002007 @Override
satok28203512010-11-24 11:06:49 +09002008 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002009 if (!calledFromValidUser()) {
2010 return;
2011 }
satok28203512010-11-24 11:06:49 +09002012 synchronized (mMethodMap) {
2013 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002014 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002015 mMethodMap.get(id), subtype.hashCode()));
2016 } else {
2017 setInputMethod(token, id);
2018 }
2019 }
satokab751aa2010-09-14 19:17:36 +09002020 }
2021
satok42c5a162011-05-26 16:46:14 +09002022 @Override
satokb416a712010-11-25 20:42:14 +09002023 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002024 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002025 if (!calledFromValidUser()) {
2026 return;
2027 }
satokb416a712010-11-25 20:42:14 +09002028 synchronized (mMethodMap) {
2029 if (mCurClient == null || client == null
2030 || mCurClient.client.asBinder() != client.asBinder()) {
2031 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2032 }
satok7fee71f2010-12-17 18:54:26 +09002033 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2034 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002035 }
2036 }
2037
satok4fc87d62011-05-20 16:13:43 +09002038 @Override
satok735cf382010-11-11 20:40:09 +09002039 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002040 if (!calledFromValidUser()) {
2041 return false;
2042 }
satok735cf382010-11-11 20:40:09 +09002043 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002044 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002045 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002046 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002047 lastImi = mMethodMap.get(lastIme.first);
2048 } else {
2049 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002050 }
satok4fc87d62011-05-20 16:13:43 +09002051 String targetLastImiId = null;
2052 int subtypeId = NOT_A_SUBTYPE_ID;
2053 if (lastIme != null && lastImi != null) {
2054 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2055 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2056 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2057 : mCurrentSubtype.hashCode();
2058 // If the last IME is the same as the current IME and the last subtype is not
2059 // defined, there is no need to switch to the last IME.
2060 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2061 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002062 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002063 }
2064 }
2065
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002066 if (TextUtils.isEmpty(targetLastImiId)
2067 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002068 // This is a safety net. If the currentSubtype can't be added to the history
2069 // and the framework couldn't find the last ime, we will make the last ime be
2070 // the most applicable enabled keyboard subtype of the system imes.
2071 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2072 if (enabled != null) {
2073 final int N = enabled.size();
2074 final String locale = mCurrentSubtype == null
2075 ? mRes.getConfiguration().locale.toString()
2076 : mCurrentSubtype.getLocale();
2077 for (int i = 0; i < N; ++i) {
2078 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002079 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002080 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002081 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2082 InputMethodUtils.getSubtypes(imi),
2083 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002084 if (keyboardSubtype != null) {
2085 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002086 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002087 imi, keyboardSubtype.hashCode());
2088 if(keyboardSubtype.getLocale().equals(locale)) {
2089 break;
2090 }
2091 }
2092 }
2093 }
2094 }
2095 }
2096
2097 if (!TextUtils.isEmpty(targetLastImiId)) {
2098 if (DEBUG) {
2099 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2100 + ", from: " + mCurMethodId + ", " + subtypeId);
2101 }
2102 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2103 return true;
2104 } else {
2105 return false;
2106 }
satok735cf382010-11-11 20:40:09 +09002107 }
2108 }
2109
satoke7c6998e2011-06-03 17:57:59 +09002110 @Override
satok688bd472012-02-09 20:09:17 +09002111 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002112 if (!calledFromValidUser()) {
2113 return false;
2114 }
satok688bd472012-02-09 20:09:17 +09002115 synchronized (mMethodMap) {
2116 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2117 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2118 if (nextSubtype == null) {
2119 return false;
2120 }
2121 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2122 return true;
2123 }
2124 }
2125
2126 @Override
satok68f1b782011-04-11 14:26:04 +09002127 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002128 if (!calledFromValidUser()) {
2129 return null;
2130 }
satok68f1b782011-04-11 14:26:04 +09002131 synchronized (mMethodMap) {
2132 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2133 // TODO: Handle the case of the last IME with no subtypes
2134 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2135 || TextUtils.isEmpty(lastIme.second)) return null;
2136 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2137 if (lastImi == null) return null;
2138 try {
2139 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002140 final int lastSubtypeId =
2141 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002142 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2143 return null;
2144 }
2145 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002146 } catch (NumberFormatException e) {
2147 return null;
2148 }
2149 }
2150 }
2151
satoke7c6998e2011-06-03 17:57:59 +09002152 @Override
satokee5e77c2011-09-02 18:50:15 +09002153 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002154 if (!calledFromValidUser()) {
2155 return;
2156 }
satok91e88122011-07-18 11:11:42 +09002157 // By this IPC call, only a process which shares the same uid with the IME can add
2158 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002159 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002160 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002161 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002162 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002163 final String[] packageInfos;
2164 try {
2165 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2166 } catch (RemoteException e) {
2167 Slog.e(TAG, "Failed to get package infos");
2168 return;
2169 }
satok91e88122011-07-18 11:11:42 +09002170 if (packageInfos != null) {
2171 final int packageNum = packageInfos.length;
2172 for (int i = 0; i < packageNum; ++i) {
2173 if (packageInfos[i].equals(imi.getPackageName())) {
2174 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002175 final long ident = Binder.clearCallingIdentity();
2176 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002177 buildInputMethodListLocked(mMethodList, mMethodMap,
2178 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002179 } finally {
2180 Binder.restoreCallingIdentity(ident);
2181 }
satokee5e77c2011-09-02 18:50:15 +09002182 return;
satok91e88122011-07-18 11:11:42 +09002183 }
2184 }
2185 }
satoke7c6998e2011-06-03 17:57:59 +09002186 }
satokee5e77c2011-09-02 18:50:15 +09002187 return;
satoke7c6998e2011-06-03 17:57:59 +09002188 }
2189
satok28203512010-11-24 11:06:49 +09002190 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 synchronized (mMethodMap) {
2192 if (token == null) {
2193 if (mContext.checkCallingOrSelfPermission(
2194 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2195 != PackageManager.PERMISSION_GRANTED) {
2196 throw new SecurityException(
2197 "Using null token requires permission "
2198 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2199 }
2200 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002201 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2202 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 return;
2204 }
2205
satokc5933802011-08-31 21:26:04 +09002206 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 try {
satokab751aa2010-09-14 19:17:36 +09002208 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 } finally {
2210 Binder.restoreCallingIdentity(ident);
2211 }
2212 }
2213 }
2214
satok42c5a162011-05-26 16:46:14 +09002215 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002217 if (!calledFromValidUser()) {
2218 return;
2219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 synchronized (mMethodMap) {
2221 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002222 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2223 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 return;
2225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 long ident = Binder.clearCallingIdentity();
2227 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002228 hideCurrentInputLocked(flags, null);
2229 } finally {
2230 Binder.restoreCallingIdentity(ident);
2231 }
2232 }
2233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002234
satok42c5a162011-05-26 16:46:14 +09002235 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002236 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002237 if (!calledFromValidUser()) {
2238 return;
2239 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002240 synchronized (mMethodMap) {
2241 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002242 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2243 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002244 return;
2245 }
2246 long ident = Binder.clearCallingIdentity();
2247 try {
2248 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 } finally {
2250 Binder.restoreCallingIdentity(ident);
2251 }
2252 }
2253 }
2254
2255 void setEnabledSessionInMainThread(SessionState session) {
2256 if (mEnabledSession != session) {
2257 if (mEnabledSession != null) {
2258 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002259 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 mEnabledSession.method.setSessionEnabled(
2261 mEnabledSession.session, false);
2262 } catch (RemoteException e) {
2263 }
2264 }
2265 mEnabledSession = session;
2266 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002267 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 session.method.setSessionEnabled(
2269 session.session, true);
2270 } catch (RemoteException e) {
2271 }
2272 }
2273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002274
satok42c5a162011-05-26 16:46:14 +09002275 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002277 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 switch (msg.what) {
2279 case MSG_SHOW_IM_PICKER:
2280 showInputMethodMenu();
2281 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002282
satokab751aa2010-09-14 19:17:36 +09002283 case MSG_SHOW_IM_SUBTYPE_PICKER:
2284 showInputMethodSubtypeMenu();
2285 return true;
2286
satok47a44912010-10-06 16:03:58 +09002287 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002288 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002289 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002290 args.recycle();
satok217f5482010-12-15 05:19:19 +09002291 return true;
2292
2293 case MSG_SHOW_IM_CONFIG:
2294 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002295 return true;
2296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 case MSG_UNBIND_INPUT:
2300 try {
2301 ((IInputMethod)msg.obj).unbindInput();
2302 } catch (RemoteException e) {
2303 // There is nothing interesting about the method dying.
2304 }
2305 return true;
2306 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002307 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 try {
2309 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2310 } catch (RemoteException e) {
2311 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002312 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 return true;
2314 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002315 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 try {
Craig Mautner6efb4c72013-03-13 10:17:41 -07002317 if (true || DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
2318 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002319 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 } catch (RemoteException e) {
2321 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002322 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 return true;
2324 case MSG_HIDE_SOFT_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 {
Craig Mautner6efb4c72013-03-13 10:17:41 -07002327 if (true || DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
2328 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002329 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 } catch (RemoteException e) {
2331 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002332 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 return true;
2334 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 try {
Craig Mautner6efb4c72013-03-13 10:17:41 -07002337 if (true || DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2339 } catch (RemoteException e) {
2340 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002341 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 return true;
2343 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 try {
2346 ((IInputMethod)args.arg1).createSession(
2347 (IInputMethodCallback)args.arg2);
2348 } catch (RemoteException e) {
2349 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002350 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002355 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 try {
2357 SessionState session = (SessionState)args.arg1;
2358 setEnabledSessionInMainThread(session);
2359 session.method.startInput((IInputContext)args.arg2,
2360 (EditorInfo)args.arg3);
2361 } catch (RemoteException e) {
2362 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002363 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 return true;
2365 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002366 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 try {
2368 SessionState session = (SessionState)args.arg1;
2369 setEnabledSessionInMainThread(session);
2370 session.method.restartInput((IInputContext)args.arg2,
2371 (EditorInfo)args.arg3);
2372 } catch (RemoteException e) {
2373 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002374 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 case MSG_UNBIND_METHOD:
2380 try {
2381 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2382 } catch (RemoteException e) {
2383 // There is nothing interesting about the last client dying.
2384 }
2385 return true;
2386 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002387 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 try {
2389 ((IInputMethodClient)args.arg1).onBindMethod(
2390 (InputBindResult)args.arg2);
2391 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002392 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002394 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002396 case MSG_SET_ACTIVE:
2397 try {
2398 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2399 } catch (RemoteException e) {
2400 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2401 + ((ClientState)msg.obj).pid + " uid "
2402 + ((ClientState)msg.obj).uid);
2403 }
2404 return true;
satok01038492012-04-09 21:08:27 +09002405
2406 // --------------------------------------------------------------
2407 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2408 mHardKeyboardListener.handleHardKeyboardStatusChange(
2409 msg.arg1 == 1, msg.arg2 == 1);
2410 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412 return false;
2413 }
2414
satokdc9ddae2011-10-06 12:22:36 +09002415 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002416 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2417 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002418 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002419 if (DEBUG) {
2420 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2421 }
satok723a27e2010-11-11 14:58:11 +09002422 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002423 return true;
2424 }
2425
2426 return false;
2427 }
2428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002430 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002431 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002432 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2433 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 list.clear();
2436 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002437
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002438 // Use for queryIntentServicesAsUser
2439 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002440 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002441 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002443 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002445 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2446 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002447
satoke7c6998e2011-06-03 17:57:59 +09002448 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2449 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 for (int i = 0; i < services.size(); ++i) {
2451 ResolveInfo ri = services.get(i);
2452 ServiceInfo si = ri.serviceInfo;
2453 ComponentName compName = new ComponentName(si.packageName, si.name);
2454 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2455 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002456 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 + ": it does not require the permission "
2458 + android.Manifest.permission.BIND_INPUT_METHOD);
2459 continue;
2460 }
2461
Joe Onorato8a9b2202010-02-26 18:56:32 -08002462 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463
2464 try {
satoke7c6998e2011-06-03 17:57:59 +09002465 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002467 final String id = p.getId();
2468 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469
2470 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002471 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002475 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002477 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 }
2479 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002480
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002481 if (resetDefaultEnabledIme) {
2482 final ArrayList<InputMethodInfo> defaultEnabledIme =
2483 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2484 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2485 final InputMethodInfo imi = defaultEnabledIme.get(i);
2486 if (DEBUG) {
2487 Slog.d(TAG, "--- enable ime = " + imi);
2488 }
2489 setInputMethodEnabledLocked(imi.getId(), true);
2490 }
2491 }
2492
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002493 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002494 if (!TextUtils.isEmpty(defaultImiId)) {
2495 if (!map.containsKey(defaultImiId)) {
2496 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2497 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002498 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002499 }
2500 } else {
2501 // Double check that the default IME is certainly enabled.
2502 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002503 }
2504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002508
satokab751aa2010-09-14 19:17:36 +09002509 private void showInputMethodMenu() {
2510 showInputMethodMenuInternal(false);
2511 }
2512
2513 private void showInputMethodSubtypeMenu() {
2514 showInputMethodMenuInternal(true);
2515 }
2516
satok217f5482010-12-15 05:19:19 +09002517 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002518 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002519 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002520 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2521 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002522 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002523 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002524 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002525 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002526 }
2527
2528 private void showConfigureInputMethods() {
2529 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2530 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2531 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2532 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002533 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002534 }
2535
satok2c93efc2012-04-02 19:33:47 +09002536 private boolean isScreenLocked() {
2537 return mKeyguardManager != null
2538 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2539 }
satokab751aa2010-09-14 19:17:36 +09002540 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002541 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002544 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002545
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002546 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002547 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002548 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002549
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002550 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002551 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2552 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002553 if (immis == null || immis.size() == 0) {
2554 return;
2555 }
2556
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002557 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558
satok688bd472012-02-09 20:09:17 +09002559 final List<ImeSubtypeListItem> imList =
2560 mImListManager.getSortedInputMethodAndSubtypeList(
2561 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002562
satokc3690562012-01-10 20:14:43 +09002563 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002564 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002565 if (currentSubtype != null) {
2566 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002567 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2568 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002569 }
2570 }
2571
Ken Wakasa761eb372011-03-04 19:06:18 +09002572 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002573 mIms = new InputMethodInfo[N];
2574 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 int checkedItem = 0;
2576 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002577 final ImeSubtypeListItem item = imList.get(i);
2578 mIms[i] = item.mImi;
2579 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002581 int subtypeId = mSubtypeIds[i];
2582 if ((subtypeId == NOT_A_SUBTYPE_ID)
2583 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2584 || (subtypeId == lastInputMethodSubtypeId)) {
2585 checkedItem = i;
2586 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002589 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002590 com.android.internal.R.styleable.DialogPreference,
2591 com.android.internal.R.attr.alertDialogStyle, 0);
2592 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002593 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002594 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002595 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002597 }
2598 })
2599 .setIcon(a.getDrawable(
2600 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2601 a.recycle();
satok01038492012-04-09 21:08:27 +09002602 final LayoutInflater inflater =
2603 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2604 final View tv = inflater.inflate(
2605 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2606 mDialogBuilder.setCustomTitle(tv);
2607
2608 // Setup layout for a toggle switch of the hardware keyboard
2609 mSwitchingDialogTitleView = tv;
2610 mSwitchingDialogTitleView.findViewById(
2611 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2612 mWindowManagerService.isHardKeyboardAvailable() ?
2613 View.VISIBLE : View.GONE);
2614 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2615 com.android.internal.R.id.hard_keyboard_switch));
2616 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2617 hardKeySwitch.setOnCheckedChangeListener(
2618 new OnCheckedChangeListener() {
2619 @Override
2620 public void onCheckedChanged(
2621 CompoundButton buttonView, boolean isChecked) {
2622 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002623 // Ensure that the input method dialog is dismissed when changing
2624 // the hardware keyboard state.
2625 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002626 }
2627 });
satokd87c2592010-09-29 11:52:06 +09002628
Ken Wakasa05dbb652011-08-22 15:22:43 +09002629 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2630 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2631 checkedItem);
2632
2633 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002634 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002635 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002636 public void onClick(DialogInterface dialog, int which) {
2637 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002638 if (mIms == null || mIms.length <= which
2639 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002640 return;
2641 }
2642 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002643 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002644 adapter.mCheckedItem = which;
2645 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002646 hideInputMethodMenu();
2647 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002648 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002649 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002650 subtypeId = NOT_A_SUBTYPE_ID;
2651 }
2652 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002653 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002656 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657
satokbc81b692011-08-26 16:22:22 +09002658 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002659 mDialogBuilder.setPositiveButton(
2660 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002661 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002662 @Override
satok7f35c8c2010-10-07 21:13:11 +09002663 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002664 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002665 }
2666 });
2667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002669 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 mSwitchingDialog.getWindow().setType(
2671 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002672 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2673 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002674 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 mSwitchingDialog.show();
2676 }
2677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002678
satok93d744d2012-05-09 17:14:08 +09002679 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002680 public final CharSequence mImeName;
2681 public final CharSequence mSubtypeName;
2682 public final InputMethodInfo mImi;
2683 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002684 private final boolean mIsSystemLocale;
2685 private final boolean mIsSystemLanguage;
2686
Ken Wakasa05dbb652011-08-22 15:22:43 +09002687 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002688 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002689 mImeName = imeName;
2690 mSubtypeName = subtypeName;
2691 mImi = imi;
2692 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002693 if (TextUtils.isEmpty(subtypeLocale)) {
2694 mIsSystemLocale = false;
2695 mIsSystemLanguage = false;
2696 } else {
2697 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2698 mIsSystemLanguage = mIsSystemLocale
2699 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2700 }
2701 }
2702
2703 @Override
2704 public int compareTo(ImeSubtypeListItem other) {
2705 if (TextUtils.isEmpty(mImeName)) {
2706 return 1;
2707 }
2708 if (TextUtils.isEmpty(other.mImeName)) {
2709 return -1;
2710 }
2711 if (!TextUtils.equals(mImeName, other.mImeName)) {
2712 return mImeName.toString().compareTo(other.mImeName.toString());
2713 }
2714 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2715 return 0;
2716 }
2717 if (mIsSystemLocale) {
2718 return -1;
2719 }
2720 if (other.mIsSystemLocale) {
2721 return 1;
2722 }
2723 if (mIsSystemLanguage) {
2724 return -1;
2725 }
2726 if (other.mIsSystemLanguage) {
2727 return 1;
2728 }
2729 if (TextUtils.isEmpty(mSubtypeName)) {
2730 return 1;
2731 }
2732 if (TextUtils.isEmpty(other.mSubtypeName)) {
2733 return -1;
2734 }
2735 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002736 }
2737 }
2738
2739 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2740 private final LayoutInflater mInflater;
2741 private final int mTextViewResourceId;
2742 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002743 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002744 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2745 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2746 super(context, textViewResourceId, itemsList);
2747 mTextViewResourceId = textViewResourceId;
2748 mItemsList = itemsList;
2749 mCheckedItem = checkedItem;
2750 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2751 }
2752
2753 @Override
2754 public View getView(int position, View convertView, ViewGroup parent) {
2755 final View view = convertView != null ? convertView
2756 : mInflater.inflate(mTextViewResourceId, null);
2757 if (position < 0 || position >= mItemsList.size()) return view;
2758 final ImeSubtypeListItem item = mItemsList.get(position);
2759 final CharSequence imeName = item.mImeName;
2760 final CharSequence subtypeName = item.mSubtypeName;
2761 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2762 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2763 if (TextUtils.isEmpty(subtypeName)) {
2764 firstTextView.setText(imeName);
2765 secondTextView.setVisibility(View.GONE);
2766 } else {
2767 firstTextView.setText(subtypeName);
2768 secondTextView.setText(imeName);
2769 secondTextView.setVisibility(View.VISIBLE);
2770 }
2771 final RadioButton radioButton =
2772 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2773 radioButton.setChecked(position == mCheckedItem);
2774 return view;
2775 }
2776 }
2777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002779 synchronized (mMethodMap) {
2780 hideInputMethodMenuLocked();
2781 }
2782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002783
The Android Open Source Project10592532009-03-18 17:39:46 -07002784 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002785 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786
The Android Open Source Project10592532009-03-18 17:39:46 -07002787 if (mSwitchingDialog != null) {
2788 mSwitchingDialog.dismiss();
2789 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002791
The Android Open Source Project10592532009-03-18 17:39:46 -07002792 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002793 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
satok42c5a162011-05-26 16:46:14 +09002798 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002800 // TODO: Make this work even for non-current users?
2801 if (!calledFromValidUser()) {
2802 return false;
2803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 synchronized (mMethodMap) {
2805 if (mContext.checkCallingOrSelfPermission(
2806 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2807 != PackageManager.PERMISSION_GRANTED) {
2808 throw new SecurityException(
2809 "Requires permission "
2810 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2811 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 long ident = Binder.clearCallingIdentity();
2814 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002815 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 } finally {
2817 Binder.restoreCallingIdentity(ident);
2818 }
2819 }
2820 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002821
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002822 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2823 // Make sure this is a valid input method.
2824 InputMethodInfo imm = mMethodMap.get(id);
2825 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002826 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002827 }
2828
satokd87c2592010-09-29 11:52:06 +09002829 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2830 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002831
satokd87c2592010-09-29 11:52:06 +09002832 if (enabled) {
2833 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2834 if (pair.first.equals(id)) {
2835 // We are enabling this input method, but it is already enabled.
2836 // Nothing to do. The previous state was enabled.
2837 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838 }
2839 }
satokd87c2592010-09-29 11:52:06 +09002840 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2841 // Previous state was disabled.
2842 return false;
2843 } else {
2844 StringBuilder builder = new StringBuilder();
2845 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2846 builder, enabledInputMethodsList, id)) {
2847 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002848 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002849 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2850 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2851 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002852 }
2853 // Previous state was enabled.
2854 return true;
2855 } else {
2856 // We are disabling the input method but it is already disabled.
2857 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002858 return false;
2859 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002860 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002861 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002862
satok723a27e2010-11-11 14:58:11 +09002863 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2864 boolean setSubtypeOnly) {
2865 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002866 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002867
2868 // Set Subtype here
2869 if (imi == null || subtypeId < 0) {
2870 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002871 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002872 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002873 if (subtypeId < imi.getSubtypeCount()) {
2874 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2875 mSettings.putSelectedSubtype(subtype.hashCode());
2876 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002877 } else {
2878 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002879 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002880 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002881 }
satokab751aa2010-09-14 19:17:36 +09002882 }
satok723a27e2010-11-11 14:58:11 +09002883
satok4c0e7152012-06-20 20:08:44 +09002884 // Workaround.
2885 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2886 // IMEs are not recognized and considered uninstalled.
2887 // Actually, we can't move everything after SystemReady because
2888 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2889 // the default IME here and try cheking the default IME again in systemReady().
2890 // TODO: Do nothing before system ready and implement a separated logic for
2891 // the encryption lock screen.
2892 // TODO: ASEC should be ready before IMMS is instantiated.
2893 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002894 // Set InputMethod here
2895 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2896 }
2897 }
2898
2899 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2900 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2901 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2902 // newDefaultIme is empty when there is no candidate for the selected IME.
2903 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2904 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2905 if (subtypeHashCode != null) {
2906 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002907 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002908 imi, Integer.valueOf(subtypeHashCode));
2909 } catch (NumberFormatException e) {
2910 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2911 }
2912 }
2913 }
2914 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002915 }
2916
satok4e4569d2010-11-19 18:45:53 +09002917 // If there are no selected shortcuts, tries finding the most applicable ones.
2918 private Pair<InputMethodInfo, InputMethodSubtype>
2919 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2920 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2921 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002922 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002923 boolean foundInSystemIME = false;
2924
2925 // Search applicable subtype for each InputMethodInfo
2926 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002927 final String imiId = imi.getId();
2928 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2929 continue;
2930 }
satokcd7cd292010-11-20 15:46:23 +09002931 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002932 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002933 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002934 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002935 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002936 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002937 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002938 }
satokdf31ae62011-01-15 06:19:44 +09002939 // 2. Search by the system locale from enabledSubtypes.
2940 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002941 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002942 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002943 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002944 }
satoka86f5e42011-09-02 17:12:42 +09002945 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002946 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002947 final ArrayList<InputMethodSubtype> subtypesForSearch =
2948 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002949 ? InputMethodUtils.getSubtypes(imi)
2950 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002951 // 4. Search by the current subtype's locale from all subtypes.
2952 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002953 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002954 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002955 }
2956 // 5. Search by the system locale from all subtypes.
2957 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002958 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002959 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002960 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002961 }
satokcd7cd292010-11-20 15:46:23 +09002962 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002963 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002964 // The current input method is the most applicable IME.
2965 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002966 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002967 break;
satok7599a7f2010-12-22 13:45:23 +09002968 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002969 // The system input method is 2nd applicable IME.
2970 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002971 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002972 if ((imi.getServiceInfo().applicationInfo.flags
2973 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2974 foundInSystemIME = true;
2975 }
satok4e4569d2010-11-19 18:45:53 +09002976 }
2977 }
2978 }
2979 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002980 if (mostApplicableIMI != null) {
2981 Slog.w(TAG, "Most applicable shortcut input method was:"
2982 + mostApplicableIMI.getId());
2983 if (mostApplicableSubtype != null) {
2984 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2985 + "," + mostApplicableSubtype.getMode() + ","
2986 + mostApplicableSubtype.getLocale());
2987 }
2988 }
satok4e4569d2010-11-19 18:45:53 +09002989 }
satokcd7cd292010-11-20 15:46:23 +09002990 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002991 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002992 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002993 } else {
2994 return null;
2995 }
2996 }
2997
satokab751aa2010-09-14 19:17:36 +09002998 /**
2999 * @return Return the current subtype of this input method.
3000 */
satok42c5a162011-05-26 16:46:14 +09003001 @Override
satokab751aa2010-09-14 19:17:36 +09003002 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003003 // TODO: Make this work even for non-current users?
3004 if (!calledFromValidUser()) {
3005 return null;
3006 }
3007 synchronized (mMethodMap) {
3008 return getCurrentInputMethodSubtypeLocked();
3009 }
3010 }
3011
3012 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003013 if (mCurMethodId == null) {
3014 return null;
3015 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003016 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003017 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3018 if (imi == null || imi.getSubtypeCount() == 0) {
3019 return null;
satok4e4569d2010-11-19 18:45:53 +09003020 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003021 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003022 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3023 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003024 if (subtypeId == NOT_A_SUBTYPE_ID) {
3025 // If there are no selected subtypes, the framework will try to find
3026 // the most applicable subtype from explicitly or implicitly enabled
3027 // subtypes.
3028 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003029 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003030 // If there is only one explicitly or implicitly enabled subtype,
3031 // just returns it.
3032 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3033 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3034 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003035 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003036 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003037 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003039 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3041 true);
satok4e4569d2010-11-19 18:45:53 +09003042 }
satok3ef8b292010-11-23 06:06:29 +09003043 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003044 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003045 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003046 }
3047 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003048 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003049 }
3050
satokf3db1af2010-11-23 13:34:33 +09003051 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3052 InputMethodSubtype subtype) {
3053 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3054 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3055 } else {
3056 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3057 subtypes.add(subtype);
3058 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3059 }
3060 }
3061
satok4e4569d2010-11-19 18:45:53 +09003062 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003063 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003064 @Override
satok4e4569d2010-11-19 18:45:53 +09003065 public List getShortcutInputMethodsAndSubtypes() {
3066 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003067 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003068 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003069 // If there are no selected shortcut subtypes, the framework will try to find
3070 // the most applicable subtype from all subtypes whose mode is
3071 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003072 Pair<InputMethodInfo, InputMethodSubtype> info =
3073 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003074 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003075 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003076 ret.add(info.first);
3077 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003078 }
satok3da92232011-01-11 22:46:30 +09003079 return ret;
satokf3db1af2010-11-23 13:34:33 +09003080 }
satokf3db1af2010-11-23 13:34:33 +09003081 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3082 ret.add(imi);
3083 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3084 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003085 }
3086 }
satokf3db1af2010-11-23 13:34:33 +09003087 return ret;
satok4e4569d2010-11-19 18:45:53 +09003088 }
3089 }
3090
satok42c5a162011-05-26 16:46:14 +09003091 @Override
satokb66d2872010-11-10 01:04:04 +09003092 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 // TODO: Make this work even for non-current users?
3094 if (!calledFromValidUser()) {
3095 return false;
3096 }
satokb66d2872010-11-10 01:04:04 +09003097 synchronized (mMethodMap) {
3098 if (subtype != null && mCurMethodId != null) {
3099 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003100 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003101 if (subtypeId != NOT_A_SUBTYPE_ID) {
3102 setInputMethodLocked(mCurMethodId, subtypeId);
3103 return true;
3104 }
3105 }
3106 return false;
3107 }
3108 }
3109
satok688bd472012-02-09 20:09:17 +09003110 private static class InputMethodAndSubtypeListManager {
3111 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003112 // Used to load label
satok688bd472012-02-09 20:09:17 +09003113 private final PackageManager mPm;
3114 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003115 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003116 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3117 mContext = context;
3118 mPm = context.getPackageManager();
3119 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003120 final Locale locale = context.getResources().getConfiguration().locale;
3121 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003122 }
3123
3124 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3125 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3126 new Comparator<InputMethodInfo>() {
3127 @Override
3128 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3129 if (imi2 == null) return 0;
3130 if (imi1 == null) return 1;
3131 if (mPm == null) {
3132 return imi1.getId().compareTo(imi2.getId());
3133 }
3134 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3135 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3136 return imiId1.toString().compareTo(imiId2.toString());
3137 }
3138 });
3139
3140 public ImeSubtypeListItem getNextInputMethod(
3141 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3142 if (imi == null) {
3143 return null;
3144 }
3145 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3146 if (imList.size() <= 1) {
3147 return null;
3148 }
3149 final int N = imList.size();
3150 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003151 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003152 : NOT_A_SUBTYPE_ID;
3153 for (int i = 0; i < N; ++i) {
3154 final ImeSubtypeListItem isli = imList.get(i);
3155 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3156 if (!onlyCurrentIme) {
3157 return imList.get((i + 1) % N);
3158 }
3159 for (int j = 0; j < N - 1; ++j) {
3160 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3161 if (candidate.mImi.equals(imi)) {
3162 return candidate;
3163 }
3164 }
3165 return null;
3166 }
3167 }
3168 return null;
3169 }
3170
3171 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3172 return getSortedInputMethodAndSubtypeList(true, false, false);
3173 }
3174
3175 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3176 boolean inputShown, boolean isScreenLocked) {
3177 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3178 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3179 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3180 if (immis == null || immis.size() == 0) {
3181 return Collections.emptyList();
3182 }
3183 mSortedImmis.clear();
3184 mSortedImmis.putAll(immis);
3185 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3186 if (imi == null) continue;
3187 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3188 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3189 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3190 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3191 }
satok688bd472012-02-09 20:09:17 +09003192 final CharSequence imeLabel = imi.loadLabel(mPm);
3193 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3194 final int subtypeCount = imi.getSubtypeCount();
3195 if (DEBUG) {
3196 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3197 }
3198 for (int j = 0; j < subtypeCount; ++j) {
3199 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3200 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3201 // We show all enabled IMEs and subtypes when an IME is shown.
3202 if (enabledSubtypeSet.contains(subtypeHashCode)
3203 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3204 final CharSequence subtypeLabel =
3205 subtype.overridesImplicitlyEnabledSubtype() ? null
3206 : subtype.getDisplayName(mContext, imi.getPackageName(),
3207 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003208 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3209 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003210
3211 // Removing this subtype from enabledSubtypeSet because we no longer
3212 // need to add an entry of this subtype to imList to avoid duplicated
3213 // entries.
3214 enabledSubtypeSet.remove(subtypeHashCode);
3215 }
3216 }
3217 } else {
satok93d744d2012-05-09 17:14:08 +09003218 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3219 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003220 }
3221 }
satok93d744d2012-05-09 17:14:08 +09003222 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003223 return imList;
3224 }
3225 }
3226
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003227 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003228 private static class InputMethodFileManager {
3229 private static final String SYSTEM_PATH = "system";
3230 private static final String INPUT_METHOD_PATH = "inputmethod";
3231 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3232 private static final String NODE_SUBTYPES = "subtypes";
3233 private static final String NODE_SUBTYPE = "subtype";
3234 private static final String NODE_IMI = "imi";
3235 private static final String ATTR_ID = "id";
3236 private static final String ATTR_LABEL = "label";
3237 private static final String ATTR_ICON = "icon";
3238 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3239 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3240 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3241 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3242 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3243 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003244 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003245 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003246 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003247 if (methodMap == null) {
3248 throw new NullPointerException("methodMap is null");
3249 }
3250 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003251 final File systemDir = userId == UserHandle.USER_OWNER
3252 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3253 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003254 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3255 if (!inputMethodDir.mkdirs()) {
3256 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3257 }
3258 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3259 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3260 if (!subtypeFile.exists()) {
3261 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003262 writeAdditionalInputMethodSubtypes(
3263 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003264 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003265 readAdditionalInputMethodSubtypes(
3266 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003267 }
3268 }
3269
3270 private void deleteAllInputMethodSubtypes(String imiId) {
3271 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003272 mAdditionalSubtypesMap.remove(imiId);
3273 writeAdditionalInputMethodSubtypes(
3274 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003275 }
3276 }
3277
3278 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003279 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003280 synchronized (mMethodMap) {
3281 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3282 final int N = additionalSubtypes.length;
3283 for (int i = 0; i < N; ++i) {
3284 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003285 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003286 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003287 } else {
3288 Slog.w(TAG, "Duplicated subtype definition found: "
3289 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003290 }
3291 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003292 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3293 writeAdditionalInputMethodSubtypes(
3294 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003295 }
3296 }
3297
3298 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3299 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003300 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003301 }
3302 }
3303
3304 private static void writeAdditionalInputMethodSubtypes(
3305 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3306 HashMap<String, InputMethodInfo> methodMap) {
3307 // Safety net for the case that this function is called before methodMap is set.
3308 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3309 FileOutputStream fos = null;
3310 try {
3311 fos = subtypesFile.startWrite();
3312 final XmlSerializer out = new FastXmlSerializer();
3313 out.setOutput(fos, "utf-8");
3314 out.startDocument(null, true);
3315 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3316 out.startTag(null, NODE_SUBTYPES);
3317 for (String imiId : allSubtypes.keySet()) {
3318 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3319 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3320 continue;
3321 }
3322 out.startTag(null, NODE_IMI);
3323 out.attribute(null, ATTR_ID, imiId);
3324 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3325 final int N = subtypesList.size();
3326 for (int i = 0; i < N; ++i) {
3327 final InputMethodSubtype subtype = subtypesList.get(i);
3328 out.startTag(null, NODE_SUBTYPE);
3329 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3330 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3331 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3332 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3333 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3334 out.attribute(null, ATTR_IS_AUXILIARY,
3335 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3336 out.endTag(null, NODE_SUBTYPE);
3337 }
3338 out.endTag(null, NODE_IMI);
3339 }
3340 out.endTag(null, NODE_SUBTYPES);
3341 out.endDocument();
3342 subtypesFile.finishWrite(fos);
3343 } catch (java.io.IOException e) {
3344 Slog.w(TAG, "Error writing subtypes", e);
3345 if (fos != null) {
3346 subtypesFile.failWrite(fos);
3347 }
3348 }
3349 }
3350
3351 private static void readAdditionalInputMethodSubtypes(
3352 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3353 if (allSubtypes == null || subtypesFile == null) return;
3354 allSubtypes.clear();
3355 FileInputStream fis = null;
3356 try {
3357 fis = subtypesFile.openRead();
3358 final XmlPullParser parser = Xml.newPullParser();
3359 parser.setInput(fis, null);
3360 int type = parser.getEventType();
3361 // Skip parsing until START_TAG
3362 while ((type = parser.next()) != XmlPullParser.START_TAG
3363 && type != XmlPullParser.END_DOCUMENT) {}
3364 String firstNodeName = parser.getName();
3365 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3366 throw new XmlPullParserException("Xml doesn't start with subtypes");
3367 }
3368 final int depth =parser.getDepth();
3369 String currentImiId = null;
3370 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3371 while (((type = parser.next()) != XmlPullParser.END_TAG
3372 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3373 if (type != XmlPullParser.START_TAG)
3374 continue;
3375 final String nodeName = parser.getName();
3376 if (NODE_IMI.equals(nodeName)) {
3377 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3378 if (TextUtils.isEmpty(currentImiId)) {
3379 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3380 continue;
3381 }
3382 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3383 allSubtypes.put(currentImiId, tempSubtypesArray);
3384 } else if (NODE_SUBTYPE.equals(nodeName)) {
3385 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3386 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3387 continue;
3388 }
3389 final int icon = Integer.valueOf(
3390 parser.getAttributeValue(null, ATTR_ICON));
3391 final int label = Integer.valueOf(
3392 parser.getAttributeValue(null, ATTR_LABEL));
3393 final String imeSubtypeLocale =
3394 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3395 final String imeSubtypeMode =
3396 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3397 final String imeSubtypeExtraValue =
3398 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003399 final boolean isAuxiliary = "1".equals(String.valueOf(
3400 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003401 final InputMethodSubtype subtype =
3402 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3403 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3404 tempSubtypesArray.add(subtype);
3405 }
3406 }
3407 } catch (XmlPullParserException e) {
3408 Slog.w(TAG, "Error reading subtypes: " + e);
3409 return;
3410 } catch (java.io.IOException e) {
3411 Slog.w(TAG, "Error reading subtypes: " + e);
3412 return;
3413 } catch (NumberFormatException e) {
3414 Slog.w(TAG, "Error reading subtypes: " + e);
3415 return;
3416 } finally {
3417 if (fis != null) {
3418 try {
3419 fis.close();
3420 } catch (java.io.IOException e1) {
3421 Slog.w(TAG, "Failed to close.");
3422 }
3423 }
3424 }
3425 }
3426 }
3427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 // Utilities for debug
3430 private static String getStackTrace() {
3431 final StringBuilder sb = new StringBuilder();
3432 try {
3433 throw new RuntimeException();
3434 } catch (RuntimeException e) {
3435 final StackTraceElement[] frames = e.getStackTrace();
3436 // Start at 1 because the first frame is here and we don't care about it
3437 for (int j = 1; j < frames.length; ++j) {
3438 sb.append(frames[j].toString() + "\n");
3439 }
3440 }
3441 return sb.toString();
3442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 @Override
3445 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3446 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3447 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3450 + Binder.getCallingPid()
3451 + ", uid=" + Binder.getCallingUid());
3452 return;
3453 }
3454
3455 IInputMethod method;
3456 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 synchronized (mMethodMap) {
3461 p.println("Current Input Method Manager state:");
3462 int N = mMethodList.size();
3463 p.println(" Input Methods:");
3464 for (int i=0; i<N; i++) {
3465 InputMethodInfo info = mMethodList.get(i);
3466 p.println(" InputMethod #" + i + ":");
3467 info.dump(p, " ");
3468 }
3469 p.println(" Clients:");
3470 for (ClientState ci : mClients.values()) {
3471 p.println(" Client " + ci + ":");
3472 p.println(" client=" + ci.client);
3473 p.println(" inputContext=" + ci.inputContext);
3474 p.println(" sessionRequested=" + ci.sessionRequested);
3475 p.println(" curSession=" + ci.curSession);
3476 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003477 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003479 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3480 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3482 + " mBoundToMethod=" + mBoundToMethod);
3483 p.println(" mCurToken=" + mCurToken);
3484 p.println(" mCurIntent=" + mCurIntent);
3485 method = mCurMethod;
3486 p.println(" mCurMethod=" + mCurMethod);
3487 p.println(" mEnabledSession=" + mEnabledSession);
3488 p.println(" mShowRequested=" + mShowRequested
3489 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3490 + " mShowForced=" + mShowForced
3491 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003492 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003494
Jeff Brownb88102f2010-09-08 11:49:43 -07003495 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 pw.flush();
3498 try {
3499 client.client.asBinder().dump(fd, args);
3500 } catch (RemoteException e) {
3501 p.println("Input method client dead: " + e);
3502 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003503 } else {
3504 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003506
Jeff Brownb88102f2010-09-08 11:49:43 -07003507 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 pw.flush();
3510 try {
3511 method.asBinder().dump(fd, args);
3512 } catch (RemoteException e) {
3513 p.println("Input method service dead: " + e);
3514 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003515 } else {
3516 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518 }
3519}