blob: 6952d723c622eb9679d0daa9a8e74a52c0476681 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070020import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090030import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090037import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090040import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090041import android.app.Notification;
42import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070043import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090044import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.ComponentName;
46import android.content.ContentResolver;
47import android.content.Context;
48import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.DialogInterface.OnCancelListener;
50import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090051import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070053import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090054import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.ResolveInfo;
58import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070059import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.res.Resources;
61import android.content.res.TypedArray;
62import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080063import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090065import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
68import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090069import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Parcel;
73import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080074import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.ServiceManager;
76import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090080import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090082import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070083import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090085import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090086import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.PrintWriterPrinter;
88import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090089import android.util.Slog;
90import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090092import android.view.LayoutInflater;
93import android.view.View;
94import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090096import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.inputmethod.InputBinding;
98import android.view.inputmethod.InputMethod;
99import android.view.inputmethod.InputMethodInfo;
100import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900101import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900102import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900103import android.widget.CompoundButton;
104import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900106import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
satoke7c6998e2011-06-03 17:57:59 +0900109import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900111import java.io.FileInputStream;
112import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.io.IOException;
114import java.io.PrintWriter;
115import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900116import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900117import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900119import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900121import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900122import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124/**
125 * This class provides a system service that manages input methods.
126 */
127public class InputMethodManagerService extends IInputMethodManager.Stub
128 implements ServiceConnection, Handler.Callback {
129 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700130 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900133 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900134 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900135 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_UNBIND_INPUT = 1000;
138 static final int MSG_BIND_INPUT = 1010;
139 static final int MSG_SHOW_SOFT_INPUT = 1020;
140 static final int MSG_HIDE_SOFT_INPUT = 1030;
141 static final int MSG_ATTACH_TOKEN = 1040;
142 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final int MSG_START_INPUT = 2000;
145 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final int MSG_UNBIND_METHOD = 3000;
148 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700149 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
satok01038492012-04-09 21:08:27 +0900151 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
satokf9f01002011-05-19 21:31:50 +0900155 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
156
satokab751aa2010-09-14 19:17:36 +0900157 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900158 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900159 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
160 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900161 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900162 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
163 "EnabledWhenDefaultIsNotAsciiCapable";
164 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900165 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800168 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900170 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final IWindowManager mIWindowManager;
173 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900174 private final InputMethodFileManager mFileManager;
satok688bd472012-02-09 20:09:17 +0900175 private final InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900176 private final HardKeyboardListener mHardKeyboardListener;
177 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // All known input methods. mMethodMap also serves as the global
182 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900183 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
184 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900185 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
186 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700188 // Used to bring IME service up to visible adjustment while it is being shown.
189 final ServiceConnection mVisibleConnection = new ServiceConnection() {
190 @Override public void onServiceConnected(ComponentName name, IBinder service) {
191 }
192
193 @Override public void onServiceDisconnected(ComponentName name) {
194 }
195 };
196 boolean mVisibleBound = false;
197
satok7cfc0ed2011-06-20 21:29:36 +0900198 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700199 private NotificationManager mNotificationManager;
200 private KeyguardManager mKeyguardManager;
201 private StatusBarManagerService mStatusBar;
202 private Notification mImeSwitcherNotification;
203 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900204 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900205 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900206 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 class SessionState {
209 final ClientState client;
210 final IInputMethod method;
211 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 @Override
214 public String toString() {
215 return "SessionState{uid " + client.uid + " pid " + client.pid
216 + " method " + Integer.toHexString(
217 System.identityHashCode(method))
218 + " session " + Integer.toHexString(
219 System.identityHashCode(session))
220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
224 IInputMethodSession _session) {
225 client = _client;
226 method = _method;
227 session = _session;
228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 class ClientState {
232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * Set to true if our ServiceConnection is currently actively bound to
315 * a service (whether or not we have gotten its IBinder back yet).
316 */
317 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * Set if the client has asked for the input method to be shown.
321 */
322 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set if we were explicitly told to show the input method.
326 */
327 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Set if we were forced to be shown.
331 */
332 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Set if we last told the input method to show itself.
336 */
337 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The Intent used to connect to the current input method.
341 */
342 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The token we have made for the currently active input method, to
346 * identify it in the future.
347 */
348 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * If non-null, this is the input method service we are currently connected
352 * to.
353 */
354 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Time that we last initiated a bind to the input method, to determine
358 * if we should try to disconnect and reconnect to it.
359 */
360 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * Have we called mCurMethod.bindInput()?
364 */
365 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Currently enabled session. Only touched by service thread, not
369 * protected by a lock.
370 */
371 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * True if the screen is on. The value is true initially.
375 */
376 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
Joe Onorato857fd9b2011-01-27 15:08:35 -0800378 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
379 int mImeWindowVis;
380
Ken Wakasa05dbb652011-08-22 15:22:43 +0900381 private AlertDialog.Builder mDialogBuilder;
382 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900383 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private InputMethodInfo[] mIms;
385 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900386 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900387 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
388 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 class SettingsObserver extends ContentObserver {
391 SettingsObserver(Handler handler) {
392 super(handler);
393 ContentResolver resolver = mContext.getContentResolver();
394 resolver.registerContentObserver(Settings.Secure.getUriFor(
395 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900396 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900397 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
398 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900399 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 @Override public void onChange(boolean selfChange) {
403 synchronized (mMethodMap) {
404 updateFromSettingsLocked();
405 }
406 }
407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900409 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
410 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700412 if (mCurClient != null && mCurClient.client != null) {
413 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
414 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900417
418 @Override
419 public void onReceive(Context context, Intent intent) {
420 final String action = intent.getAction();
421 if (Intent.ACTION_SCREEN_ON.equals(action)) {
422 mScreenOn = true;
423 refreshImeWindowVisibilityLocked();
424 updateActive();
425 return;
426 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
427 mScreenOn = false;
428 setImeWindowVisibilityStatusHiddenLocked();
429 updateActive();
430 return;
431 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
432 hideInputMethodMenu();
433 // No need to updateActive
434 return;
435 } else {
436 Slog.w(TAG, "Unexpected intent " + intent);
437 }
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900442 private boolean isChangingPackagesOfCurrentUser() {
443 final int userId = getChangingUserId();
444 final boolean retval = userId == mSettings.getCurrentUserId();
445 if (DEBUG) {
446 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
447 }
448 return retval;
449 }
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900453 if (!isChangingPackagesOfCurrentUser()) {
454 return false;
455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 final int N = mMethodList.size();
459 if (curInputMethodId != null) {
460 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 InputMethodInfo imi = mMethodList.get(i);
462 if (imi.getId().equals(curInputMethodId)) {
463 for (String pkg : packages) {
464 if (imi.getPackageName().equals(pkg)) {
465 if (!doit) {
466 return true;
467 }
satok723a27e2010-11-11 14:58:11 +0900468 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 chooseNewDefaultIMELocked();
470 return true;
471 }
472 }
473 }
474 }
475 }
476 }
477 return false;
478 }
479
480 @Override
481 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900482 if (!isChangingPackagesOfCurrentUser()) {
483 return;
484 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800485 synchronized (mMethodMap) {
486 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 final int N = mMethodList.size();
489 if (curInputMethodId != null) {
490 for (int i=0; i<N; i++) {
491 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900492 final String imiId = imi.getId();
493 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 curIm = imi;
495 }
satoke7c6998e2011-06-03 17:57:59 +0900496
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900498 if (isPackageModified(imi.getPackageName())) {
499 mFileManager.deleteAllInputMethodSubtypes(imiId);
500 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 if (change == PACKAGE_TEMPORARY_CHANGE
502 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800503 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800504 + imi.getComponent());
505 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507 }
508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 buildInputMethodListLocked(mMethodList, mMethodMap);
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800514 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 int change = isPackageDisappearing(curIm.getPackageName());
516 if (change == PACKAGE_TEMPORARY_CHANGE
517 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800518 ServiceInfo si = null;
519 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900520 si = mIPackageManager.getServiceInfo(
521 curIm.getComponent(), 0, mSettings.getCurrentUserId());
522 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 }
524 if (si == null) {
525 // Uh oh, current input method is no longer around!
526 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900528 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 changed = true;
531 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900533 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800534 }
535 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800537 }
satokab751aa2010-09-14 19:17:36 +0900538
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800539 if (curIm == null) {
540 // We currently don't have a default input method... is
541 // one now available?
542 changed = chooseNewDefaultIMELocked();
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545 if (changed) {
546 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 }
549 }
550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800551
Jean Chalarde0d32a62011-10-20 20:36:07 +0900552 private static class MethodCallback extends IInputMethodCallback.Stub {
553 private final IInputMethod mMethod;
554 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Jean Chalarde0d32a62011-10-20 20:36:07 +0900556 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900558 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
satoke7c6998e2011-06-03 17:57:59 +0900561 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public void finishedEvent(int seq, boolean handled) throws RemoteException {
563 }
564
satoke7c6998e2011-06-03 17:57:59 +0900565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900567 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
satok01038492012-04-09 21:08:27 +0900571 private class HardKeyboardListener
572 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
573 @Override
574 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
575 mHandler.sendMessage(mHandler.obtainMessage(
576 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
577 }
578
579 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
580 if (DEBUG) {
581 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
582 + enabled);
583 }
584 synchronized(mMethodMap) {
585 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
586 && mSwitchingDialog.isShowing()) {
587 mSwitchingDialogTitleView.findViewById(
588 com.android.internal.R.id.hard_keyboard_section).setVisibility(
589 available ? View.VISIBLE : View.GONE);
590 }
591 }
592 }
593 }
594
595 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900596 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800598 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 mHandler = new Handler(this);
600 mIWindowManager = IWindowManager.Stub.asInterface(
601 ServiceManager.getService(Context.WINDOW_SERVICE));
602 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900603 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public void executeMessage(Message msg) {
605 handleMessage(msg);
606 }
607 });
satok01038492012-04-09 21:08:27 +0900608 mWindowManagerService = windowManager;
609 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900610
satok7cfc0ed2011-06-20 21:29:36 +0900611 mImeSwitcherNotification = new Notification();
612 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
613 mImeSwitcherNotification.when = 0;
614 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
615 mImeSwitcherNotification.tickerText = null;
616 mImeSwitcherNotification.defaults = 0; // please be quiet
617 mImeSwitcherNotification.sound = null;
618 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400619
620 // Tag this notification specially so SystemUI knows it's important
621 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
622
satok7cfc0ed2011-06-20 21:29:36 +0900623 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900624 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900625
626 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900627
satoke7c6998e2011-06-03 17:57:59 +0900628 synchronized (mMethodMap) {
629 mFileManager = new InputMethodFileManager(mMethodMap);
630 }
satok688bd472012-02-09 20:09:17 +0900631 mImListManager = new InputMethodAndSubtypeListManager(context, this);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800632
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) {
646 switchUser(newUserId);
647 if (reply != null) {
648 try {
649 reply.sendResult(null);
650 } catch (RemoteException e) {
651 }
652 }
653 }
654
655 @Override
656 public void onUserSwitchComplete(int newUserId) throws RemoteException {
657 }
658 });
659 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
660 } catch (RemoteException e) {
661 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
662 }
663 mMyPackageMonitor.register(mContext, null, true);
satok913a8922010-08-26 21:53:41 +0900664
satokd87c2592010-09-29 11:52:06 +0900665 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900666 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
satok0a1bcf42012-05-16 19:26:31 +0900668
669 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900670 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900671 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900674 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
satok0a1bcf42012-05-16 19:26:31 +0900676 if (!mImeSelectedOnBoot) {
677 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900678 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 mSettingsObserver = new SettingsObserver(mHandler);
682 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900683
684 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
685 // according to the new system locale.
686 final IntentFilter filter = new IntentFilter();
687 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
688 mContext.registerReceiver(
689 new BroadcastReceiver() {
690 @Override
691 public void onReceive(Context context, Intent intent) {
692 synchronized(mMethodMap) {
693 checkCurrentLocaleChangedLocked();
694 }
695 }
696 }, filter);
697 }
698
satok5b927c432012-05-01 20:09:34 +0900699 private void resetDefaultImeLocked(Context context) {
700 // Do not reset the default (current) IME when it is a 3rd-party IME
701 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
702 return;
703 }
704
705 InputMethodInfo defIm = null;
706 for (InputMethodInfo imi : mMethodList) {
707 if (defIm == null) {
708 if (isValidSystemDefaultIme(imi, context)) {
709 defIm = imi;
710 Slog.i(TAG, "Selected default: " + imi.getId());
711 }
712 }
713 }
714 if (defIm == null && mMethodList.size() > 0) {
715 defIm = getMostApplicableDefaultIMELocked();
716 Slog.i(TAG, "No default found, using " + defIm.getId());
717 }
718 if (defIm != null) {
719 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
720 }
721 }
722
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900723 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
724 if (!mSystemReady) {
725 // not system ready
726 return;
727 }
728 final Locale newLocale = mRes.getConfiguration().locale;
729 if (!updateOnlyWhenLocaleChanged
730 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
731 if (!updateOnlyWhenLocaleChanged) {
732 hideCurrentInputLocked(0, null);
733 mCurMethodId = null;
734 unbindCurrentMethodLocked(true);
735 }
736 if (DEBUG) {
737 Slog.i(TAG, "Locale has been changed to " + newLocale);
738 }
739 buildInputMethodListLocked(mMethodList, mMethodMap);
740 if (!updateOnlyWhenLocaleChanged) {
741 final String selectedImiId = mSettings.getSelectedInputMethod();
742 if (TextUtils.isEmpty(selectedImiId)) {
743 // This is the first time of the user switch and
744 // set the current ime to the proper one.
745 resetDefaultImeLocked(mContext);
746 }
747 }
748 updateFromSettingsLocked();
749 mLastSystemLocale = newLocale;
750 if (!updateOnlyWhenLocaleChanged) {
751 try {
752 startInputInnerLocked();
753 } catch (RuntimeException e) {
754 Slog.w(TAG, "Unexpected exception", e);
755 }
756 }
757 }
758 }
759
760 private void checkCurrentLocaleChangedLocked() {
761 resetAllInternalStateLocked(true);
762 }
763
764 private void switchUser(int newUserId) {
765 mSettings.setCurrentUserId(newUserId);
766 resetAllInternalStateLocked(false);
767 }
768
satok0a1bcf42012-05-16 19:26:31 +0900769 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
770 if (!mSystemReady) {
771 return false;
772 }
satok5b927c432012-05-01 20:09:34 +0900773 if (!isSystemIme(imi)) {
774 return false;
775 }
776 if (imi.getIsDefaultResourceId() != 0) {
777 try {
778 Resources res = context.createPackageContext(
779 imi.getPackageName(), 0).getResources();
780 if (res.getBoolean(imi.getIsDefaultResourceId())
781 && containsSubtypeOf(imi, context.getResources().getConfiguration().
782 locale.getLanguage())) {
783 return true;
784 }
785 } catch (PackageManager.NameNotFoundException ex) {
786 } catch (Resources.NotFoundException ex) {
787 }
788 }
789 if (imi.getSubtypeCount() == 0) {
790 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
791 }
792 return false;
793 }
794
795 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
796 if (!isSystemIme(imi)) {
797 return false;
798 }
799 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
800 }
801
802 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
803 final int N = imi.getSubtypeCount();
804 for (int i = 0; i < N; ++i) {
805 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
806 return true;
807 }
808 }
809 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811
812 @Override
813 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
814 throws RemoteException {
815 try {
816 return super.onTransact(code, data, reply, flags);
817 } catch (RuntimeException e) {
818 // The input method manager only throws security exceptions, so let's
819 // log all others.
820 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800821 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823 throw e;
824 }
825 }
826
Dianne Hackborn661cd522011-08-22 00:26:20 -0700827 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700828 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900829 if (DEBUG) {
830 Slog.d(TAG, "--- systemReady");
831 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700832 if (!mSystemReady) {
833 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900834 mKeyguardManager =
835 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700836 mNotificationManager = (NotificationManager)
837 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
838 mStatusBar = statusBar;
839 statusBar.setIconVisibility("ime", false);
840 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900841 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
842 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900843 if (mShowOngoingImeSwitcherForPhones) {
844 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
845 mHardKeyboardListener);
846 }
satok0a1bcf42012-05-16 19:26:31 +0900847 buildInputMethodListLocked(mMethodList, mMethodMap);
848 if (!mImeSelectedOnBoot) {
849 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
850 checkCurrentLocaleChangedLocked();
851 }
852 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700853 try {
854 startInputInnerLocked();
855 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800856 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700857 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 }
859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
satok15452a42011-10-28 17:58:28 +0900862 private void setImeWindowVisibilityStatusHiddenLocked() {
863 mImeWindowVis = 0;
864 updateImeWindowStatusLocked();
865 }
866
satok3afd6c02011-11-18 08:38:19 +0900867 private void refreshImeWindowVisibilityLocked() {
868 final Configuration conf = mRes.getConfiguration();
869 final boolean haveHardKeyboard = conf.keyboard
870 != Configuration.KEYBOARD_NOKEYS;
871 final boolean hardKeyShown = haveHardKeyboard
872 && conf.hardKeyboardHidden
873 != Configuration.HARDKEYBOARDHIDDEN_YES;
874 final boolean isScreenLocked = mKeyguardManager != null
875 && mKeyguardManager.isKeyguardLocked()
876 && mKeyguardManager.isKeyguardSecure();
877 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
878 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
879 updateImeWindowStatusLocked();
880 }
881
satok15452a42011-10-28 17:58:28 +0900882 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900883 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700884 }
885
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900886 // ---------------------------------------------------------------------------------------
887 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
888 // 1) it comes from the system process
889 // 2) the calling process' user id is identical to the current user id IMMS thinks.
890 private boolean calledFromValidUser() {
891 final int uid = Binder.getCallingUid();
892 final int userId = UserHandle.getUserId(uid);
893 if (DEBUG) {
894 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
895 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
896 + " calling userId = " + userId + ", foreground user id = "
897 + mSettings.getCurrentUserId());
898 }
899 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
900 return true;
901 } else {
902 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
903 return false;
904 }
905 }
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 }
913 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
914 }
915
satoke7c6998e2011-06-03 17:57:59 +0900916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900918 // TODO: Make this work even for non-current users?
919 if (!calledFromValidUser()) {
920 return Collections.emptyList();
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 synchronized (mMethodMap) {
923 return new ArrayList<InputMethodInfo>(mMethodList);
924 }
925 }
926
satoke7c6998e2011-06-03 17:57:59 +0900927 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 // TODO: Make this work even for non-current users?
930 if (!calledFromValidUser()) {
931 return Collections.emptyList();
932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900934 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 }
937
satokbb4aa062011-01-19 21:40:27 +0900938 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
939 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
940 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
941 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900943 enabledInputMethodAndSubtypes.put(
944 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
945 }
946 return enabledInputMethodAndSubtypes;
947 }
948
949 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
950 boolean allowsImplicitlySelectedSubtypes) {
951 if (imi == null && mCurMethodId != null) {
952 imi = mMethodMap.get(mCurMethodId);
953 }
satok7265d9b2011-02-14 15:47:30 +0900954 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900955 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900956 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900957 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900958 }
satok7265d9b2011-02-14 15:47:30 +0900959 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900960 }
961
satoke7c6998e2011-06-03 17:57:59 +0900962 @Override
satok16331c82010-12-20 23:48:46 +0900963 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
964 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 // TODO: Make this work even for non-current users?
966 if (!calledFromValidUser()) {
967 return Collections.emptyList();
968 }
satok67ddf9c2010-11-17 09:45:54 +0900969 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900970 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900971 }
972 }
973
satoke7c6998e2011-06-03 17:57:59 +0900974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public void addClient(IInputMethodClient client,
976 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!calledFromValidUser()) {
978 return;
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
981 mClients.put(client.asBinder(), new ClientState(client,
982 inputContext, uid, pid));
983 }
984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800985
satoke7c6998e2011-06-03 17:57:59 +0900986 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 if (!calledFromValidUser()) {
989 return;
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 synchronized (mMethodMap) {
992 mClients.remove(client.asBinder());
993 }
994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 void executeOrSendMessage(IInterface target, Message msg) {
997 if (target.asBinder() instanceof Binder) {
998 mCaller.sendMessage(msg);
999 } else {
1000 handleMessage(msg);
1001 msg.recycle();
1002 }
1003 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001004
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001005 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001007 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 + mCurClient.client.asBinder());
1009 if (mBoundToMethod) {
1010 mBoundToMethod = false;
1011 if (mCurMethod != null) {
1012 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1013 MSG_UNBIND_INPUT, mCurMethod));
1014 }
1015 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001016
1017 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1018 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1020 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1021 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
The Android Open Source Project10592532009-03-18 17:39:46 -07001024 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 private int getImeShowFlags() {
1029 int flags = 0;
1030 if (mShowForced) {
1031 flags |= InputMethod.SHOW_FORCED
1032 | InputMethod.SHOW_EXPLICIT;
1033 } else if (mShowExplicitlyRequested) {
1034 flags |= InputMethod.SHOW_EXPLICIT;
1035 }
1036 return flags;
1037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 private int getAppShowFlags() {
1040 int flags = 0;
1041 if (mShowForced) {
1042 flags |= InputMethodManager.SHOW_FORCED;
1043 } else if (!mShowExplicitlyRequested) {
1044 flags |= InputMethodManager.SHOW_IMPLICIT;
1045 }
1046 return flags;
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
Dianne Hackborn7663d802012-02-24 13:08:49 -08001049 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (!mBoundToMethod) {
1051 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1052 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1053 mBoundToMethod = true;
1054 }
1055 final SessionState session = mCurClient.curSession;
1056 if (initial) {
1057 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1058 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1059 } else {
1060 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1061 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1062 }
1063 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001065 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001067 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001071 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 // If no method is currently selected, do nothing.
1073 if (mCurMethodId == null) {
1074 return mNoBinding;
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 ClientState cs = mClients.get(client.asBinder());
1078 if (cs == null) {
1079 throw new IllegalArgumentException("unknown client "
1080 + client.asBinder());
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 try {
1084 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1085 // Check with the window manager to make sure this client actually
1086 // has a window with focus. If not, reject. This is thread safe
1087 // because if the focus changes some time before or after, the
1088 // next client receiving focus that has any interest in input will
1089 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1092 return null;
1093 }
1094 } catch (RemoteException e) {
1095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
Dianne Hackborn7663d802012-02-24 13:08:49 -08001097 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1098 }
1099
1100 InputBindResult startInputUncheckedLocked(ClientState cs,
1101 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1102 // If no method is currently selected, do nothing.
1103 if (mCurMethodId == null) {
1104 return mNoBinding;
1105 }
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (mCurClient != cs) {
1108 // If the client is changing, we need to switch over to the new
1109 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001110 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 + cs.client.asBinder());
1113
1114 // If the screen is on, inform the new client it is active
1115 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001116 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1117 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 // Bump up the sequence for this client and attach it.
1122 mCurSeq++;
1123 if (mCurSeq <= 0) mCurSeq = 1;
1124 mCurClient = cs;
1125 mCurInputContext = inputContext;
1126 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 // Check if the input method is changing.
1129 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1130 if (cs.curSession != null) {
1131 // Fast case: if we are already connected to the input method,
1132 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001133 return attachNewInputLocked(
1134 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 if (mHaveConnection) {
1137 if (mCurMethod != null) {
1138 if (!cs.sessionRequested) {
1139 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001140 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1142 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001143 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 // Return to client, and we will get back with it when
1146 // we have had a session made for it.
1147 return new InputBindResult(null, mCurId, mCurSeq);
1148 } else if (SystemClock.uptimeMillis()
1149 < (mLastBindTime+TIME_TO_RECONNECT)) {
1150 // In this case we have connected to the service, but
1151 // don't yet have its interface. If it hasn't been too
1152 // long since we did the connection, we'll return to
1153 // the client and wait to get the service interface so
1154 // we can report back. If it has been too long, we want
1155 // to fall through so we can try a disconnect/reconnect
1156 // to see if we can get back in touch with the service.
1157 return new InputBindResult(null, mCurId, mCurSeq);
1158 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1160 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 }
1163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001164
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001165 return startInputInnerLocked();
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001168 InputBindResult startInputInnerLocked() {
1169 if (mCurMethodId == null) {
1170 return mNoBinding;
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001173 if (!mSystemReady) {
1174 // If the system is not yet ready, we shouldn't be running third
1175 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001176 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1180 if (info == null) {
1181 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001183
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001184 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1187 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001188 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1189 com.android.internal.R.string.input_method_binding_label);
1190 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1191 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001193 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 mLastBindTime = SystemClock.uptimeMillis();
1195 mHaveConnection = true;
1196 mCurId = info.getId();
1197 mCurToken = new Binder();
1198 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 mIWindowManager.addWindowToken(mCurToken,
1201 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1202 } catch (RemoteException e) {
1203 }
1204 return new InputBindResult(null, mCurId, mCurSeq);
1205 } else {
1206 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 + mCurIntent);
1209 }
1210 return null;
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
satoke7c6998e2011-06-03 17:57:59 +09001213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001215 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001216 if (!calledFromValidUser()) {
1217 return null;
1218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 synchronized (mMethodMap) {
1220 final long ident = Binder.clearCallingIdentity();
1221 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001222 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 } finally {
1224 Binder.restoreCallingIdentity(ident);
1225 }
1226 }
1227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001228
satoke7c6998e2011-06-03 17:57:59 +09001229 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 public void finishInput(IInputMethodClient client) {
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void onServiceConnected(ComponentName name, IBinder service) {
1235 synchronized (mMethodMap) {
1236 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1237 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001238 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001240 unbindCurrentMethodLocked(false);
1241 return;
1242 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001244 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1245 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001247 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001248 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001250 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001251 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253 }
1254 }
1255 }
1256
1257 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1258 synchronized (mMethodMap) {
1259 if (mCurMethod != null && method != null
1260 && mCurMethod.asBinder() == method.asBinder()) {
1261 if (mCurClient != null) {
1262 mCurClient.curSession = new SessionState(mCurClient,
1263 method, session);
1264 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001265 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 if (res.method != null) {
1267 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1268 MSG_BIND_METHOD, mCurClient.client, res));
1269 }
1270 }
1271 }
1272 }
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001275 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001276 if (mVisibleBound) {
1277 mContext.unbindService(mVisibleConnection);
1278 mVisibleBound = false;
1279 }
1280
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001281 if (mHaveConnection) {
1282 mContext.unbindService(this);
1283 mHaveConnection = false;
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001286 if (mCurToken != null) {
1287 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
satoke0a99412012-05-10 02:22:58 +09001289 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1290 // The current IME is shown. Hence an IME switch (transition) is happening.
1291 mWindowManagerService.saveLastInputMethodWindowForTransition();
1292 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001293 mIWindowManager.removeWindowToken(mCurToken);
1294 } catch (RemoteException e) {
1295 }
1296 mCurToken = null;
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
The Android Open Source Project10592532009-03-18 17:39:46 -07001299 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001300 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001302 if (reportToClient && mCurClient != null) {
1303 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1304 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1305 }
1306 }
1307
Devin Taylor0c33ed22010-02-23 13:26:46 -06001308 private void finishSession(SessionState sessionState) {
1309 if (sessionState != null && sessionState.session != null) {
1310 try {
1311 sessionState.session.finishSession();
1312 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001313 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001314 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001315 }
1316 }
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (mCurMethod != null) {
1321 for (ClientState cs : mClients.values()) {
1322 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001323 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 cs.curSession = null;
1325 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001326
1327 finishSession(mEnabledSession);
1328 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 mCurMethod = null;
1330 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001331 if (mStatusBar != null) {
1332 mStatusBar.setIconVisibility("ime", false);
1333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
satoke7c6998e2011-06-03 17:57:59 +09001336 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 public void onServiceDisconnected(ComponentName name) {
1338 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001339 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 + " mCurIntent=" + mCurIntent);
1341 if (mCurMethod != null && mCurIntent != null
1342 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001343 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 // We consider this to be a new bind attempt, since the system
1345 // should now try to restart the service for us.
1346 mLastBindTime = SystemClock.uptimeMillis();
1347 mShowRequested = mInputShown;
1348 mInputShown = false;
1349 if (mCurClient != null) {
1350 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1351 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1352 }
1353 }
1354 }
1355 }
1356
satokf9f01002011-05-19 21:31:50 +09001357 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001359 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 long ident = Binder.clearCallingIdentity();
1361 try {
1362 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001363 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 return;
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 synchronized (mMethodMap) {
1368 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001369 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001370 if (mStatusBar != null) {
1371 mStatusBar.setIconVisibility("ime", false);
1372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001374 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001375 CharSequence contentDescription = null;
1376 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001377 // Use PackageManager to load label
1378 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001379 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001380 mIPackageManager.getApplicationInfo(packageName, 0,
1381 mSettings.getCurrentUserId()));
1382 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001383 /* ignore */
1384 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001385 if (mStatusBar != null) {
1386 mStatusBar.setIcon("ime", packageName, iconId, 0,
1387 contentDescription != null
1388 ? contentDescription.toString() : null);
1389 mStatusBar.setIconVisibility("ime", true);
1390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392 }
1393 } finally {
1394 Binder.restoreCallingIdentity(ident);
1395 }
1396 }
1397
satok7cfc0ed2011-06-20 21:29:36 +09001398 private boolean needsToShowImeSwitchOngoingNotification() {
1399 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001400 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001401 synchronized (mMethodMap) {
1402 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1403 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001404 if (N > 2) return true;
1405 if (N < 1) return false;
1406 int nonAuxCount = 0;
1407 int auxCount = 0;
1408 InputMethodSubtype nonAuxSubtype = null;
1409 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001410 for(int i = 0; i < N; ++i) {
1411 final InputMethodInfo imi = imis.get(i);
1412 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1413 imi, true);
1414 final int subtypeCount = subtypes.size();
1415 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001416 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001417 } else {
1418 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001419 final InputMethodSubtype subtype = subtypes.get(j);
1420 if (!subtype.isAuxiliary()) {
1421 ++nonAuxCount;
1422 nonAuxSubtype = subtype;
1423 } else {
1424 ++auxCount;
1425 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001426 }
1427 }
1428 }
satok7cfc0ed2011-06-20 21:29:36 +09001429 }
satokb6359412011-06-28 17:47:41 +09001430 if (nonAuxCount > 1 || auxCount > 1) {
1431 return true;
1432 } else if (nonAuxCount == 1 && auxCount == 1) {
1433 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001434 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1435 || auxSubtype.overridesImplicitlyEnabledSubtype()
1436 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001437 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1438 return false;
1439 }
1440 return true;
1441 }
1442 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001443 }
satok7cfc0ed2011-06-20 21:29:36 +09001444 }
1445
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001446 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001447 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001448 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001449 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001450 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001451 try {
1452 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001453 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001454 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001455 return;
1456 }
1457
1458 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001459 mImeWindowVis = vis;
1460 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001461 if (mStatusBar != null) {
1462 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1463 }
satok7cfc0ed2011-06-20 21:29:36 +09001464 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001465 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1466 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001467 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001468 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001469 final CharSequence title = mRes.getText(
1470 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001471 final CharSequence imiLabel = imi.loadLabel(pm);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001472 if (DEBUG) {
1473 Slog.d(TAG, "--- imiLabel = " + imiLabel);
1474 }
satok5bc8e732011-07-22 21:07:23 +09001475 final CharSequence summary = mCurrentSubtype != null
1476 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1477 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1478 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001479 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001480 : imiLabel;
1481
satok7cfc0ed2011-06-20 21:29:36 +09001482 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001483 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001484 if (mNotificationManager != null) {
1485 mNotificationManager.notify(
1486 com.android.internal.R.string.select_input_method,
1487 mImeSwitcherNotification);
1488 mNotificationShown = true;
1489 }
satok7cfc0ed2011-06-20 21:29:36 +09001490 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001491 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001492 mNotificationManager.cancel(
1493 com.android.internal.R.string.select_input_method);
1494 mNotificationShown = false;
1495 }
1496 }
satok06487a52010-10-29 11:37:18 +09001497 }
1498 } finally {
1499 Binder.restoreCallingIdentity(ident);
1500 }
1501 }
1502
satoke7c6998e2011-06-03 17:57:59 +09001503 @Override
satokf9f01002011-05-19 21:31:50 +09001504 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 if (!calledFromValidUser()) {
1506 return;
1507 }
satokf9f01002011-05-19 21:31:50 +09001508 synchronized (mMethodMap) {
1509 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1510 for (int i = 0; i < spans.length; ++i) {
1511 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001512 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001513 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001514 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001515 }
1516 }
1517 }
1518 }
1519
satoke7c6998e2011-06-03 17:57:59 +09001520 @Override
satokf9f01002011-05-19 21:31:50 +09001521 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001522 if (!calledFromValidUser()) {
1523 return false;
1524 }
satokf9f01002011-05-19 21:31:50 +09001525 synchronized (mMethodMap) {
1526 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1527 // TODO: Do not send the intent if the process of the targetImi is already dead.
1528 if (targetImi != null) {
1529 final String[] suggestions = span.getSuggestions();
1530 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001531 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001532 final Intent intent = new Intent();
1533 // Ensures that only a class in the original IME package will receive the
1534 // notification.
satok42c5a162011-05-26 16:46:14 +09001535 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001536 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1537 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1538 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1539 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1540 mContext.sendBroadcast(intent);
1541 return true;
1542 }
1543 }
1544 return false;
1545 }
1546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001548 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1549 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1550 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1551 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001552 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001553 // There is no input method selected, try to choose new applicable input method.
1554 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001555 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001556 }
1557 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 try {
satokab751aa2010-09-14 19:17:36 +09001559 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001562 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001563 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
satokf3db1af2010-11-23 13:34:33 +09001565 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001566 } else {
1567 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001568 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001569 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
satokab751aa2010-09-14 19:17:36 +09001573 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 InputMethodInfo info = mMethodMap.get(id);
1575 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001576 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
satokd81e9502012-05-21 12:58:45 +09001579 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001581 final int subtypeCount = info.getSubtypeCount();
1582 if (subtypeCount <= 0) {
1583 return;
satokcd7cd292010-11-20 15:46:23 +09001584 }
satokd81e9502012-05-21 12:58:45 +09001585 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1586 final InputMethodSubtype newSubtype;
1587 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1588 newSubtype = info.getSubtypeAt(subtypeId);
1589 } else {
1590 // If subtype is null, try to find the most applicable one from
1591 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001592 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001593 }
1594 if (newSubtype == null || oldSubtype == null) {
1595 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1596 + ", new subtype = " + newSubtype);
1597 return;
1598 }
1599 if (newSubtype != oldSubtype) {
1600 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1601 if (mCurMethod != null) {
1602 try {
1603 refreshImeWindowVisibilityLocked();
1604 mCurMethod.changeInputMethodSubtype(newSubtype);
1605 } catch (RemoteException e) {
1606 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001607 }
1608 }
1609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 return;
1611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001612
satokd81e9502012-05-21 12:58:45 +09001613 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 final long ident = Binder.clearCallingIdentity();
1615 try {
satokab751aa2010-09-14 19:17:36 +09001616 // Set a subtype to this input method.
1617 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001618 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1619 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1620 // because mCurMethodId is stored as a history in
1621 // setSelectedInputMethodAndSubtypeLocked().
1622 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623
1624 if (ActivityManagerNative.isSystemReady()) {
1625 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001626 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 intent.putExtra("input_method_id", id);
1628 mContext.sendBroadcast(intent);
1629 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001630 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 } finally {
1632 Binder.restoreCallingIdentity(ident);
1633 }
1634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
satok42c5a162011-05-26 16:46:14 +09001636 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001637 public boolean showSoftInput(IInputMethodClient client, int flags,
1638 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001639 if (!calledFromValidUser()) {
1640 return false;
1641 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001642 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 long ident = Binder.clearCallingIdentity();
1644 try {
1645 synchronized (mMethodMap) {
1646 if (mCurClient == null || client == null
1647 || mCurClient.client.asBinder() != client.asBinder()) {
1648 try {
1649 // We need to check if this is the current client with
1650 // focus in the window manager, to allow this call to
1651 // be made before input is started in it.
1652 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001653 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001654 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001657 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660
Joe Onorato8a9b2202010-02-26 18:56:32 -08001661 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001662 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664 } finally {
1665 Binder.restoreCallingIdentity(ident);
1666 }
1667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
The Android Open Source Project4df24232009-03-05 14:34:35 -08001669 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 mShowRequested = true;
1671 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1672 mShowExplicitlyRequested = true;
1673 }
1674 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1675 mShowExplicitlyRequested = true;
1676 mShowForced = true;
1677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001678
Dianne Hackborncc278702009-09-02 23:07:23 -07001679 if (!mSystemReady) {
1680 return false;
1681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
The Android Open Source Project4df24232009-03-05 14:34:35 -08001683 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001685 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1686 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1687 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001689 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001690 bindCurrentInputMethodService(
1691 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001692 mVisibleBound = true;
1693 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001694 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001696 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 // The client has asked to have the input method shown, but
1698 // we have been sitting here too long with a connection to the
1699 // service and no interface received, so let's disconnect/connect
1700 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001703 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001705 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001706 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001707 } else {
1708 if (DEBUG) {
1709 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1710 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
The Android Open Source Project4df24232009-03-05 14:34:35 -08001714 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001716
satok42c5a162011-05-26 16:46:14 +09001717 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001718 public boolean hideSoftInput(IInputMethodClient client, int flags,
1719 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001720 if (!calledFromValidUser()) {
1721 return false;
1722 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001723 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 long ident = Binder.clearCallingIdentity();
1725 try {
1726 synchronized (mMethodMap) {
1727 if (mCurClient == null || client == null
1728 || mCurClient.client.asBinder() != client.asBinder()) {
1729 try {
1730 // We need to check if this is the current client with
1731 // focus in the window manager, to allow this call to
1732 // be made before input is started in it.
1733 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001734 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1735 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001736 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001737 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001740 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001741 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
Joe Onorato8a9b2202010-02-26 18:56:32 -08001745 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001746 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748 } finally {
1749 Binder.restoreCallingIdentity(ident);
1750 }
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
The Android Open Source Project4df24232009-03-05 14:34:35 -08001753 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1755 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001756 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001758 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001761 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001763 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001765 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001767 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1768 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1769 res = true;
1770 } else {
1771 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001773 if (mHaveConnection && mVisibleBound) {
1774 mContext.unbindService(mVisibleConnection);
1775 mVisibleBound = false;
1776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 mInputShown = false;
1778 mShowRequested = false;
1779 mShowExplicitlyRequested = false;
1780 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001781 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
satok42c5a162011-05-26 16:46:14 +09001784 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001785 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1786 int controlFlags, int softInputMode, int windowFlags,
1787 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001788 if (!calledFromValidUser()) {
1789 return null;
1790 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001791 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 long ident = Binder.clearCallingIdentity();
1793 try {
1794 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001795 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001796 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001798 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
Dianne Hackborn7663d802012-02-24 13:08:49 -08001800 ClientState cs = mClients.get(client.asBinder());
1801 if (cs == null) {
1802 throw new IllegalArgumentException("unknown client "
1803 + client.asBinder());
1804 }
1805
1806 try {
1807 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1808 // Check with the window manager to make sure this client actually
1809 // has a window with focus. If not, reject. This is thread safe
1810 // because if the focus changes some time before or after, the
1811 // next client receiving focus that has any interest in input will
1812 // be calling through here after that change happens.
1813 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1814 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1815 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001817 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001820 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001821 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
1822 + " attribute=" + attribute);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001823 if (attribute != null) {
1824 return startInputUncheckedLocked(cs, inputContext, attribute,
1825 controlFlags);
1826 }
1827 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001828 }
1829 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001830
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001831 // Should we auto-show the IME even if the caller has not
1832 // specified what should be done with it?
1833 // We only do this automatically if the window can resize
1834 // to accommodate the IME (so what the user sees will give
1835 // them good context without input information being obscured
1836 // by the IME) or if running on a large screen where there
1837 // is more room for the target window + IME.
1838 final boolean doAutoShow =
1839 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1840 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1841 || mRes.getConfiguration().isLayoutSizeAtLeast(
1842 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001843 final boolean isTextEditor =
1844 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1845
1846 // We want to start input before showing the IME, but after closing
1847 // it. We want to do this after closing it to help the IME disappear
1848 // more quickly (not get stuck behind it initializing itself for the
1849 // new focused input, even if its window wants to hide the IME).
1850 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1853 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001854 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1856 // There is no focus view, and this window will
1857 // be behind any soft input window, so hide the
1858 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001859 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001862 } else if (isTextEditor && doAutoShow && (softInputMode &
1863 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 // There is a focus view, and we are navigating forward
1865 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001866 // We only do this automatically if the window can resize
1867 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001868 // them good context without input information being obscured
1869 // by the IME) or if running on a large screen where there
1870 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001871 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001872 if (attribute != null) {
1873 res = startInputUncheckedLocked(cs, inputContext, attribute,
1874 controlFlags);
1875 didStart = true;
1876 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001877 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
1879 break;
1880 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1881 // Do nothing.
1882 break;
1883 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1884 if ((softInputMode &
1885 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001886 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001887 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889 break;
1890 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001891 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001892 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 break;
1894 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1895 if ((softInputMode &
1896 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001897 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001898 if (attribute != null) {
1899 res = startInputUncheckedLocked(cs, inputContext, attribute,
1900 controlFlags);
1901 didStart = true;
1902 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001903 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905 break;
1906 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001907 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001908 if (attribute != null) {
1909 res = startInputUncheckedLocked(cs, inputContext, attribute,
1910 controlFlags);
1911 didStart = true;
1912 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001913 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 break;
1915 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001916
1917 if (!didStart && attribute != null) {
1918 res = startInputUncheckedLocked(cs, inputContext, attribute,
1919 controlFlags);
1920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 }
1922 } finally {
1923 Binder.restoreCallingIdentity(ident);
1924 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001925
1926 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001928
satok42c5a162011-05-26 16:46:14 +09001929 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001931 if (!calledFromValidUser()) {
1932 return;
1933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 synchronized (mMethodMap) {
1935 if (mCurClient == null || client == null
1936 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001937 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001938 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
1940
satok440aab52010-11-25 09:43:11 +09001941 // Always call subtype picker, because subtype picker is a superset of input method
1942 // picker.
satokab751aa2010-09-14 19:17:36 +09001943 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1944 }
1945 }
1946
satok42c5a162011-05-26 16:46:14 +09001947 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001949 if (!calledFromValidUser()) {
1950 return;
1951 }
satok28203512010-11-24 11:06:49 +09001952 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1953 }
1954
satok42c5a162011-05-26 16:46:14 +09001955 @Override
satok28203512010-11-24 11:06:49 +09001956 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001957 if (!calledFromValidUser()) {
1958 return;
1959 }
satok28203512010-11-24 11:06:49 +09001960 synchronized (mMethodMap) {
1961 if (subtype != null) {
1962 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1963 mMethodMap.get(id), subtype.hashCode()));
1964 } else {
1965 setInputMethod(token, id);
1966 }
1967 }
satokab751aa2010-09-14 19:17:36 +09001968 }
1969
satok42c5a162011-05-26 16:46:14 +09001970 @Override
satokb416a712010-11-25 20:42:14 +09001971 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001972 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001973 if (!calledFromValidUser()) {
1974 return;
1975 }
satokb416a712010-11-25 20:42:14 +09001976 synchronized (mMethodMap) {
1977 if (mCurClient == null || client == null
1978 || mCurClient.client.asBinder() != client.asBinder()) {
1979 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1980 }
satok7fee71f2010-12-17 18:54:26 +09001981 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1982 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001983 }
1984 }
1985
satok4fc87d62011-05-20 16:13:43 +09001986 @Override
satok735cf382010-11-11 20:40:09 +09001987 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001988 if (!calledFromValidUser()) {
1989 return false;
1990 }
satok735cf382010-11-11 20:40:09 +09001991 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001992 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001993 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001994 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001995 lastImi = mMethodMap.get(lastIme.first);
1996 } else {
1997 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001998 }
satok4fc87d62011-05-20 16:13:43 +09001999 String targetLastImiId = null;
2000 int subtypeId = NOT_A_SUBTYPE_ID;
2001 if (lastIme != null && lastImi != null) {
2002 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2003 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2004 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2005 : mCurrentSubtype.hashCode();
2006 // If the last IME is the same as the current IME and the last subtype is not
2007 // defined, there is no need to switch to the last IME.
2008 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2009 targetLastImiId = lastIme.first;
2010 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2011 }
2012 }
2013
2014 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2015 // This is a safety net. If the currentSubtype can't be added to the history
2016 // and the framework couldn't find the last ime, we will make the last ime be
2017 // the most applicable enabled keyboard subtype of the system imes.
2018 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2019 if (enabled != null) {
2020 final int N = enabled.size();
2021 final String locale = mCurrentSubtype == null
2022 ? mRes.getConfiguration().locale.toString()
2023 : mCurrentSubtype.getLocale();
2024 for (int i = 0; i < N; ++i) {
2025 final InputMethodInfo imi = enabled.get(i);
2026 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2027 InputMethodSubtype keyboardSubtype =
2028 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2029 SUBTYPE_MODE_KEYBOARD, locale, true);
2030 if (keyboardSubtype != null) {
2031 targetLastImiId = imi.getId();
2032 subtypeId = getSubtypeIdFromHashCode(
2033 imi, keyboardSubtype.hashCode());
2034 if(keyboardSubtype.getLocale().equals(locale)) {
2035 break;
2036 }
2037 }
2038 }
2039 }
2040 }
2041 }
2042
2043 if (!TextUtils.isEmpty(targetLastImiId)) {
2044 if (DEBUG) {
2045 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2046 + ", from: " + mCurMethodId + ", " + subtypeId);
2047 }
2048 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2049 return true;
2050 } else {
2051 return false;
2052 }
satok735cf382010-11-11 20:40:09 +09002053 }
2054 }
2055
satoke7c6998e2011-06-03 17:57:59 +09002056 @Override
satok688bd472012-02-09 20:09:17 +09002057 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002058 if (!calledFromValidUser()) {
2059 return false;
2060 }
satok688bd472012-02-09 20:09:17 +09002061 synchronized (mMethodMap) {
2062 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2063 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2064 if (nextSubtype == null) {
2065 return false;
2066 }
2067 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2068 return true;
2069 }
2070 }
2071
2072 @Override
satok68f1b782011-04-11 14:26:04 +09002073 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002074 if (!calledFromValidUser()) {
2075 return null;
2076 }
satok68f1b782011-04-11 14:26:04 +09002077 synchronized (mMethodMap) {
2078 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2079 // TODO: Handle the case of the last IME with no subtypes
2080 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2081 || TextUtils.isEmpty(lastIme.second)) return null;
2082 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2083 if (lastImi == null) return null;
2084 try {
2085 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002086 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2087 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2088 return null;
2089 }
2090 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002091 } catch (NumberFormatException e) {
2092 return null;
2093 }
2094 }
2095 }
2096
satoke7c6998e2011-06-03 17:57:59 +09002097 @Override
satokee5e77c2011-09-02 18:50:15 +09002098 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002099 if (!calledFromValidUser()) {
2100 return;
2101 }
satok91e88122011-07-18 11:11:42 +09002102 // By this IPC call, only a process which shares the same uid with the IME can add
2103 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002104 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002105 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002106 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002107 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 final String[] packageInfos;
2109 try {
2110 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2111 } catch (RemoteException e) {
2112 Slog.e(TAG, "Failed to get package infos");
2113 return;
2114 }
satok91e88122011-07-18 11:11:42 +09002115 if (packageInfos != null) {
2116 final int packageNum = packageInfos.length;
2117 for (int i = 0; i < packageNum; ++i) {
2118 if (packageInfos[i].equals(imi.getPackageName())) {
2119 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002120 final long ident = Binder.clearCallingIdentity();
2121 try {
2122 buildInputMethodListLocked(mMethodList, mMethodMap);
2123 } finally {
2124 Binder.restoreCallingIdentity(ident);
2125 }
satokee5e77c2011-09-02 18:50:15 +09002126 return;
satok91e88122011-07-18 11:11:42 +09002127 }
2128 }
2129 }
satoke7c6998e2011-06-03 17:57:59 +09002130 }
satokee5e77c2011-09-02 18:50:15 +09002131 return;
satoke7c6998e2011-06-03 17:57:59 +09002132 }
2133
satok28203512010-11-24 11:06:49 +09002134 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 synchronized (mMethodMap) {
2136 if (token == null) {
2137 if (mContext.checkCallingOrSelfPermission(
2138 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2139 != PackageManager.PERMISSION_GRANTED) {
2140 throw new SecurityException(
2141 "Using null token requires permission "
2142 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2143 }
2144 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002145 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2146 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 return;
2148 }
2149
satokc5933802011-08-31 21:26:04 +09002150 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 try {
satokab751aa2010-09-14 19:17:36 +09002152 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 } finally {
2154 Binder.restoreCallingIdentity(ident);
2155 }
2156 }
2157 }
2158
satok42c5a162011-05-26 16:46:14 +09002159 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002161 if (!calledFromValidUser()) {
2162 return;
2163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 synchronized (mMethodMap) {
2165 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002166 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2167 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 return;
2169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 long ident = Binder.clearCallingIdentity();
2171 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002172 hideCurrentInputLocked(flags, null);
2173 } finally {
2174 Binder.restoreCallingIdentity(ident);
2175 }
2176 }
2177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
satok42c5a162011-05-26 16:46:14 +09002179 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002180 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002181 if (!calledFromValidUser()) {
2182 return;
2183 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 synchronized (mMethodMap) {
2185 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002186 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2187 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002188 return;
2189 }
2190 long ident = Binder.clearCallingIdentity();
2191 try {
2192 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 } finally {
2194 Binder.restoreCallingIdentity(ident);
2195 }
2196 }
2197 }
2198
2199 void setEnabledSessionInMainThread(SessionState session) {
2200 if (mEnabledSession != session) {
2201 if (mEnabledSession != null) {
2202 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002203 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mEnabledSession.method.setSessionEnabled(
2205 mEnabledSession.session, false);
2206 } catch (RemoteException e) {
2207 }
2208 }
2209 mEnabledSession = session;
2210 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002211 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 session.method.setSessionEnabled(
2213 session.session, true);
2214 } catch (RemoteException e) {
2215 }
2216 }
2217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002218
satok42c5a162011-05-26 16:46:14 +09002219 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002221 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 switch (msg.what) {
2223 case MSG_SHOW_IM_PICKER:
2224 showInputMethodMenu();
2225 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002226
satokab751aa2010-09-14 19:17:36 +09002227 case MSG_SHOW_IM_SUBTYPE_PICKER:
2228 showInputMethodSubtypeMenu();
2229 return true;
2230
satok47a44912010-10-06 16:03:58 +09002231 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002232 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002233 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002234 args.recycle();
satok217f5482010-12-15 05:19:19 +09002235 return true;
2236
2237 case MSG_SHOW_IM_CONFIG:
2238 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002239 return true;
2240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 case MSG_UNBIND_INPUT:
2244 try {
2245 ((IInputMethod)msg.obj).unbindInput();
2246 } catch (RemoteException e) {
2247 // There is nothing interesting about the method dying.
2248 }
2249 return true;
2250 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002251 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 try {
2253 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2254 } catch (RemoteException e) {
2255 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002256 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 return true;
2258 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002259 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2262 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 } catch (RemoteException e) {
2264 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002265 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 return true;
2267 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002268 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002270 ((IInputMethod)args.arg1).hideSoftInput(0,
2271 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 } catch (RemoteException e) {
2273 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002274 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 return true;
2276 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002277 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002279 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2281 } catch (RemoteException e) {
2282 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002283 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 return true;
2285 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002286 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 try {
2288 ((IInputMethod)args.arg1).createSession(
2289 (IInputMethodCallback)args.arg2);
2290 } catch (RemoteException e) {
2291 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002292 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002297 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 try {
2299 SessionState session = (SessionState)args.arg1;
2300 setEnabledSessionInMainThread(session);
2301 session.method.startInput((IInputContext)args.arg2,
2302 (EditorInfo)args.arg3);
2303 } catch (RemoteException e) {
2304 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 return true;
2307 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002308 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 try {
2310 SessionState session = (SessionState)args.arg1;
2311 setEnabledSessionInMainThread(session);
2312 session.method.restartInput((IInputContext)args.arg2,
2313 (EditorInfo)args.arg3);
2314 } catch (RemoteException e) {
2315 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002316 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 case MSG_UNBIND_METHOD:
2322 try {
2323 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2324 } catch (RemoteException e) {
2325 // There is nothing interesting about the last client dying.
2326 }
2327 return true;
2328 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002329 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 try {
2331 ((IInputMethodClient)args.arg1).onBindMethod(
2332 (InputBindResult)args.arg2);
2333 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002338 case MSG_SET_ACTIVE:
2339 try {
2340 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2341 } catch (RemoteException e) {
2342 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2343 + ((ClientState)msg.obj).pid + " uid "
2344 + ((ClientState)msg.obj).uid);
2345 }
2346 return true;
satok01038492012-04-09 21:08:27 +09002347
2348 // --------------------------------------------------------------
2349 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2350 mHardKeyboardListener.handleHardKeyboardStatusChange(
2351 msg.arg1 == 1, msg.arg2 == 1);
2352 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 return false;
2355 }
2356
satok5b927c432012-05-01 20:09:34 +09002357 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002358 return (inputMethod.getServiceInfo().applicationInfo.flags
2359 & ApplicationInfo.FLAG_SYSTEM) != 0;
2360 }
2361
Ken Wakasa586f0512011-01-20 22:31:01 +09002362 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2363 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2364 final int subtypeCount = imi.getSubtypeCount();
2365 for (int i = 0; i < subtypeCount; ++i) {
2366 subtypes.add(imi.getSubtypeAt(i));
2367 }
2368 return subtypes;
2369 }
2370
satoka86f5e42011-09-02 17:12:42 +09002371 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2372 InputMethodInfo imi, String mode) {
2373 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2374 final int subtypeCount = imi.getSubtypeCount();
2375 for (int i = 0; i < subtypeCount; ++i) {
2376 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2377 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2378 subtypes.add(subtype);
2379 }
2380 }
2381 return subtypes;
2382 }
2383
satokdc9ddae2011-10-06 12:22:36 +09002384 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002385 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002386 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002387 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002388 int i = enabled.size();
2389 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002390 while (i > 0) {
2391 i--;
satokdc9ddae2011-10-06 12:22:36 +09002392 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002393 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2394 return imi;
2395 }
2396 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2397 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002398 }
2399 }
satok0a1bcf42012-05-16 19:26:31 +09002400 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002401 }
2402 return null;
2403 }
2404
2405 private boolean chooseNewDefaultIMELocked() {
2406 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2407 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002408 if (DEBUG) {
2409 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2410 }
satok723a27e2010-11-11 14:58:11 +09002411 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002412 return true;
2413 }
2414
2415 return false;
2416 }
2417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2419 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002420 if (DEBUG) {
2421 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 list.clear();
2424 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002425
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 // Use for queryIntentServicesAsUser
2427 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002428 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002429 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002431 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002433 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002435 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002436
satoke7c6998e2011-06-03 17:57:59 +09002437 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2438 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 for (int i = 0; i < services.size(); ++i) {
2440 ResolveInfo ri = services.get(i);
2441 ServiceInfo si = ri.serviceInfo;
2442 ComponentName compName = new ComponentName(si.packageName, si.name);
2443 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2444 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002445 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 + ": it does not require the permission "
2447 + android.Manifest.permission.BIND_INPUT_METHOD);
2448 continue;
2449 }
2450
Joe Onorato8a9b2202010-02-26 18:56:32 -08002451 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452
2453 try {
satoke7c6998e2011-06-03 17:57:59 +09002454 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002456 final String id = p.getId();
2457 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458
satok5b927c432012-05-01 20:09:34 +09002459 // Valid system default IMEs and IMEs that have English subtypes are enabled
2460 // by default, unless there's a hard keyboard and the system IME was explicitly
2461 // disabled
2462 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2463 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002464 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002465 }
2466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002468 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002472 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002474 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002477
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002479 if (!TextUtils.isEmpty(defaultImiId)) {
2480 if (!map.containsKey(defaultImiId)) {
2481 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2482 if (chooseNewDefaultIMELocked()) {
2483 updateFromSettingsLocked();
2484 }
2485 } else {
2486 // Double check that the default IME is certainly enabled.
2487 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002488 }
2489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002493
satokab751aa2010-09-14 19:17:36 +09002494 private void showInputMethodMenu() {
2495 showInputMethodMenuInternal(false);
2496 }
2497
2498 private void showInputMethodSubtypeMenu() {
2499 showInputMethodMenuInternal(true);
2500 }
2501
satok217f5482010-12-15 05:19:19 +09002502 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002503 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002504 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002505 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2506 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002507 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002508 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002509 }
satok217f5482010-12-15 05:19:19 +09002510 mContext.startActivity(intent);
2511 }
2512
2513 private void showConfigureInputMethods() {
2514 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2515 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2516 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2517 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002518 mContext.startActivity(intent);
2519 }
2520
satok2c93efc2012-04-02 19:33:47 +09002521 private boolean isScreenLocked() {
2522 return mKeyguardManager != null
2523 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2524 }
satokab751aa2010-09-14 19:17:36 +09002525 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002526 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002529 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002530
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002531 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002532 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002533 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002534
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002535 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002536 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2537 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002538 if (immis == null || immis.size() == 0) {
2539 return;
2540 }
2541
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002542 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543
satok688bd472012-02-09 20:09:17 +09002544 final List<ImeSubtypeListItem> imList =
2545 mImListManager.getSortedInputMethodAndSubtypeList(
2546 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002547
satokc3690562012-01-10 20:14:43 +09002548 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002549 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002550 if (currentSubtype != null) {
2551 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2552 lastInputMethodSubtypeId =
2553 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2554 }
2555 }
2556
Ken Wakasa761eb372011-03-04 19:06:18 +09002557 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002558 mIms = new InputMethodInfo[N];
2559 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002560 int checkedItem = 0;
2561 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002562 final ImeSubtypeListItem item = imList.get(i);
2563 mIms[i] = item.mImi;
2564 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002565 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002566 int subtypeId = mSubtypeIds[i];
2567 if ((subtypeId == NOT_A_SUBTYPE_ID)
2568 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2569 || (subtypeId == lastInputMethodSubtypeId)) {
2570 checkedItem = i;
2571 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002574 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 com.android.internal.R.styleable.DialogPreference,
2576 com.android.internal.R.attr.alertDialogStyle, 0);
2577 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002578 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002579 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002582 }
2583 })
2584 .setIcon(a.getDrawable(
2585 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2586 a.recycle();
satok01038492012-04-09 21:08:27 +09002587 final LayoutInflater inflater =
2588 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2589 final View tv = inflater.inflate(
2590 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2591 mDialogBuilder.setCustomTitle(tv);
2592
2593 // Setup layout for a toggle switch of the hardware keyboard
2594 mSwitchingDialogTitleView = tv;
2595 mSwitchingDialogTitleView.findViewById(
2596 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2597 mWindowManagerService.isHardKeyboardAvailable() ?
2598 View.VISIBLE : View.GONE);
2599 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2600 com.android.internal.R.id.hard_keyboard_switch));
2601 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2602 hardKeySwitch.setOnCheckedChangeListener(
2603 new OnCheckedChangeListener() {
2604 @Override
2605 public void onCheckedChanged(
2606 CompoundButton buttonView, boolean isChecked) {
2607 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2608 }
2609 });
satokd87c2592010-09-29 11:52:06 +09002610
Ken Wakasa05dbb652011-08-22 15:22:43 +09002611 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2612 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2613 checkedItem);
2614
2615 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002616 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002617 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002618 public void onClick(DialogInterface dialog, int which) {
2619 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002620 if (mIms == null || mIms.length <= which
2621 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002622 return;
2623 }
2624 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002625 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002626 hideInputMethodMenu();
2627 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002628 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002629 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002630 subtypeId = NOT_A_SUBTYPE_ID;
2631 }
2632 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002633 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002636 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637
satokbc81b692011-08-26 16:22:22 +09002638 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002639 mDialogBuilder.setPositiveButton(
2640 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002641 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002642 @Override
satok7f35c8c2010-10-07 21:13:11 +09002643 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002644 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002645 }
2646 });
2647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002649 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 mSwitchingDialog.getWindow().setType(
2651 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002652 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 mSwitchingDialog.show();
2654 }
2655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002656
satok93d744d2012-05-09 17:14:08 +09002657 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002658 public final CharSequence mImeName;
2659 public final CharSequence mSubtypeName;
2660 public final InputMethodInfo mImi;
2661 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002662 private final boolean mIsSystemLocale;
2663 private final boolean mIsSystemLanguage;
2664
Ken Wakasa05dbb652011-08-22 15:22:43 +09002665 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002666 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002667 mImeName = imeName;
2668 mSubtypeName = subtypeName;
2669 mImi = imi;
2670 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002671 if (TextUtils.isEmpty(subtypeLocale)) {
2672 mIsSystemLocale = false;
2673 mIsSystemLanguage = false;
2674 } else {
2675 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2676 mIsSystemLanguage = mIsSystemLocale
2677 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2678 }
2679 }
2680
2681 @Override
2682 public int compareTo(ImeSubtypeListItem other) {
2683 if (TextUtils.isEmpty(mImeName)) {
2684 return 1;
2685 }
2686 if (TextUtils.isEmpty(other.mImeName)) {
2687 return -1;
2688 }
2689 if (!TextUtils.equals(mImeName, other.mImeName)) {
2690 return mImeName.toString().compareTo(other.mImeName.toString());
2691 }
2692 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2693 return 0;
2694 }
2695 if (mIsSystemLocale) {
2696 return -1;
2697 }
2698 if (other.mIsSystemLocale) {
2699 return 1;
2700 }
2701 if (mIsSystemLanguage) {
2702 return -1;
2703 }
2704 if (other.mIsSystemLanguage) {
2705 return 1;
2706 }
2707 if (TextUtils.isEmpty(mSubtypeName)) {
2708 return 1;
2709 }
2710 if (TextUtils.isEmpty(other.mSubtypeName)) {
2711 return -1;
2712 }
2713 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002714 }
2715 }
2716
2717 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2718 private final LayoutInflater mInflater;
2719 private final int mTextViewResourceId;
2720 private final List<ImeSubtypeListItem> mItemsList;
2721 private final int mCheckedItem;
2722 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2723 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2724 super(context, textViewResourceId, itemsList);
2725 mTextViewResourceId = textViewResourceId;
2726 mItemsList = itemsList;
2727 mCheckedItem = checkedItem;
2728 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2729 }
2730
2731 @Override
2732 public View getView(int position, View convertView, ViewGroup parent) {
2733 final View view = convertView != null ? convertView
2734 : mInflater.inflate(mTextViewResourceId, null);
2735 if (position < 0 || position >= mItemsList.size()) return view;
2736 final ImeSubtypeListItem item = mItemsList.get(position);
2737 final CharSequence imeName = item.mImeName;
2738 final CharSequence subtypeName = item.mSubtypeName;
2739 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2740 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2741 if (TextUtils.isEmpty(subtypeName)) {
2742 firstTextView.setText(imeName);
2743 secondTextView.setVisibility(View.GONE);
2744 } else {
2745 firstTextView.setText(subtypeName);
2746 secondTextView.setText(imeName);
2747 secondTextView.setVisibility(View.VISIBLE);
2748 }
2749 final RadioButton radioButton =
2750 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2751 radioButton.setChecked(position == mCheckedItem);
2752 return view;
2753 }
2754 }
2755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002757 synchronized (mMethodMap) {
2758 hideInputMethodMenuLocked();
2759 }
2760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002761
The Android Open Source Project10592532009-03-18 17:39:46 -07002762 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764
The Android Open Source Project10592532009-03-18 17:39:46 -07002765 if (mSwitchingDialog != null) {
2766 mSwitchingDialog.dismiss();
2767 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002769
The Android Open Source Project10592532009-03-18 17:39:46 -07002770 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002771 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002775
satok42c5a162011-05-26 16:46:14 +09002776 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002778 // TODO: Make this work even for non-current users?
2779 if (!calledFromValidUser()) {
2780 return false;
2781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 synchronized (mMethodMap) {
2783 if (mContext.checkCallingOrSelfPermission(
2784 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2785 != PackageManager.PERMISSION_GRANTED) {
2786 throw new SecurityException(
2787 "Requires permission "
2788 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2789 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 long ident = Binder.clearCallingIdentity();
2792 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002793 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 } finally {
2795 Binder.restoreCallingIdentity(ident);
2796 }
2797 }
2798 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002799
2800 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2801 // Make sure this is a valid input method.
2802 InputMethodInfo imm = mMethodMap.get(id);
2803 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002804 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002805 }
2806
satokd87c2592010-09-29 11:52:06 +09002807 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2808 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002809
satokd87c2592010-09-29 11:52:06 +09002810 if (enabled) {
2811 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2812 if (pair.first.equals(id)) {
2813 // We are enabling this input method, but it is already enabled.
2814 // Nothing to do. The previous state was enabled.
2815 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002816 }
2817 }
satokd87c2592010-09-29 11:52:06 +09002818 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2819 // Previous state was disabled.
2820 return false;
2821 } else {
2822 StringBuilder builder = new StringBuilder();
2823 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2824 builder, enabledInputMethodsList, id)) {
2825 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002826 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002827 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2828 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2829 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002830 }
2831 // Previous state was enabled.
2832 return true;
2833 } else {
2834 // We are disabling the input method but it is already disabled.
2835 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002836 return false;
2837 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002839 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002840
satok57ffc002011-01-25 00:11:47 +09002841 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2842 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002843 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002844 }
2845
satok723a27e2010-11-11 14:58:11 +09002846 private void saveCurrentInputMethodAndSubtypeToHistory() {
2847 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2848 if (mCurrentSubtype != null) {
2849 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2850 }
satok57ffc002011-01-25 00:11:47 +09002851 if (canAddToLastInputMethod(mCurrentSubtype)) {
2852 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2853 }
satokab751aa2010-09-14 19:17:36 +09002854 }
2855
satok723a27e2010-11-11 14:58:11 +09002856 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2857 boolean setSubtypeOnly) {
2858 // Update the history of InputMethod and Subtype
2859 saveCurrentInputMethodAndSubtypeToHistory();
2860
2861 // Set Subtype here
2862 if (imi == null || subtypeId < 0) {
2863 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002864 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002865 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002866 if (subtypeId < imi.getSubtypeCount()) {
2867 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2868 mSettings.putSelectedSubtype(subtype.hashCode());
2869 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002870 } else {
2871 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002872 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002873 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002874 }
satokab751aa2010-09-14 19:17:36 +09002875 }
satok723a27e2010-11-11 14:58:11 +09002876
satok4c0e7152012-06-20 20:08:44 +09002877 // Workaround.
2878 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2879 // IMEs are not recognized and considered uninstalled.
2880 // Actually, we can't move everything after SystemReady because
2881 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2882 // the default IME here and try cheking the default IME again in systemReady().
2883 // TODO: Do nothing before system ready and implement a separated logic for
2884 // the encryption lock screen.
2885 // TODO: ASEC should be ready before IMMS is instantiated.
2886 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002887 // Set InputMethod here
2888 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2889 }
2890 }
2891
2892 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2893 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2894 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2895 // newDefaultIme is empty when there is no candidate for the selected IME.
2896 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2897 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2898 if (subtypeHashCode != null) {
2899 try {
2900 lastSubtypeId = getSubtypeIdFromHashCode(
2901 imi, Integer.valueOf(subtypeHashCode));
2902 } catch (NumberFormatException e) {
2903 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2904 }
2905 }
2906 }
2907 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002908 }
2909
2910 private int getSelectedInputMethodSubtypeId(String id) {
2911 InputMethodInfo imi = mMethodMap.get(id);
2912 if (imi == null) {
2913 return NOT_A_SUBTYPE_ID;
2914 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002915 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2916 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002917 }
2918
satokfdf419e2012-05-08 16:52:08 +09002919 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2920 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2921 }
2922
2923 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002924 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002925 final int subtypeCount = imi.getSubtypeCount();
2926 for (int i = 0; i < subtypeCount; ++i) {
2927 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002928 if (subtypeHashCode == ims.hashCode()) {
2929 return i;
2930 }
satokab751aa2010-09-14 19:17:36 +09002931 }
2932 }
2933 return NOT_A_SUBTYPE_ID;
2934 }
2935
satoka86f5e42011-09-02 17:12:42 +09002936 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2937 Resources res, InputMethodInfo imi) {
2938 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002939 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002940 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002941 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002942 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002943 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002944 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002945 // scan overriding implicitly enabled subtypes.
2946 InputMethodSubtype subtype = subtypes.get(i);
2947 if (subtype.overridesImplicitlyEnabledSubtype()) {
2948 final String mode = subtype.getMode();
2949 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2950 applicableModeAndSubtypesMap.put(mode, subtype);
2951 }
2952 }
2953 }
2954 if (applicableModeAndSubtypesMap.size() > 0) {
2955 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2956 }
2957 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002958 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002959 final String locale = subtype.getLocale();
2960 final String mode = subtype.getMode();
2961 // When system locale starts with subtype's locale, that subtype will be applicable
2962 // for system locale
2963 // For instance, it's clearly applicable for cases like system locale = en_US and
2964 // subtype = en, but it is not necessarily considered applicable for cases like system
2965 // locale = en and subtype = en_US.
2966 // We just call systemLocale.startsWith(locale) in this function because there is no
2967 // need to find applicable subtypes aggressively unlike
2968 // findLastResortApplicableSubtypeLocked.
2969 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002970 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002971 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002972 if (applicableSubtype != null) {
2973 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2974 if (!systemLocale.equals(locale)) continue;
2975 }
satok3da92232011-01-11 22:46:30 +09002976 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002977 }
2978 }
2979 final InputMethodSubtype keyboardSubtype
2980 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2981 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2982 applicableModeAndSubtypesMap.values());
2983 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2984 for (int i = 0; i < N; ++i) {
2985 final InputMethodSubtype subtype = subtypes.get(i);
2986 final String mode = subtype.getMode();
2987 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2988 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2989 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002990 }
2991 }
2992 }
satokc3690562012-01-10 20:14:43 +09002993 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002994 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002995 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002996 if (lastResortKeyboardSubtype != null) {
2997 applicableSubtypes.add(lastResortKeyboardSubtype);
2998 }
2999 }
3000 return applicableSubtypes;
3001 }
3002
satok4e4569d2010-11-19 18:45:53 +09003003 /**
3004 * If there are no selected subtypes, tries finding the most applicable one according to the
3005 * given locale.
3006 * @param subtypes this function will search the most applicable subtype in subtypes
3007 * @param mode subtypes will be filtered by mode
3008 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003009 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3010 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003011 * @return the most applicable subtypeId
3012 */
satokdf31ae62011-01-15 06:19:44 +09003013 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3014 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003015 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003016 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003017 return null;
satok8fbb1e82010-11-02 23:15:58 +09003018 }
satok4e4569d2010-11-19 18:45:53 +09003019 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003020 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003021 }
satok8fbb1e82010-11-02 23:15:58 +09003022 final String language = locale.substring(0, 2);
3023 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003024 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003025 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003026 final int N = subtypes.size();
3027 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003028 InputMethodSubtype subtype = subtypes.get(i);
3029 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003030 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3031 // and all subtypes with all modes can be candidates.
3032 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003033 if (firstMatchedModeSubtype == null) {
3034 firstMatchedModeSubtype = subtype;
3035 }
satok9ef02832010-11-04 21:17:48 +09003036 if (locale.equals(subtypeLocale)) {
3037 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003038 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003039 break;
3040 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3041 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003042 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003043 partialMatchFound = true;
3044 }
satok8fbb1e82010-11-02 23:15:58 +09003045 }
3046 }
3047
satok7599a7f2010-12-22 13:45:23 +09003048 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3049 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003050 }
3051
satok8fbb1e82010-11-02 23:15:58 +09003052 // The first subtype applicable to the system locale will be defined as the most applicable
3053 // subtype.
3054 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003055 if (applicableSubtype != null) {
3056 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3057 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3058 }
satok8fbb1e82010-11-02 23:15:58 +09003059 }
satokcd7cd292010-11-20 15:46:23 +09003060 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003061 }
3062
satok4e4569d2010-11-19 18:45:53 +09003063 // If there are no selected shortcuts, tries finding the most applicable ones.
3064 private Pair<InputMethodInfo, InputMethodSubtype>
3065 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3066 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3067 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003068 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003069 boolean foundInSystemIME = false;
3070
3071 // Search applicable subtype for each InputMethodInfo
3072 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003073 final String imiId = imi.getId();
3074 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3075 continue;
3076 }
satokcd7cd292010-11-20 15:46:23 +09003077 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003078 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003079 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003080 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003081 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003082 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003083 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003084 }
satokdf31ae62011-01-15 06:19:44 +09003085 // 2. Search by the system locale from enabledSubtypes.
3086 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003087 if (subtype == null) {
3088 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003089 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003090 }
satoka86f5e42011-09-02 17:12:42 +09003091 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3092 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3093 final ArrayList<InputMethodSubtype> subtypesForSearch =
3094 overridingImplicitlyEnabledSubtypes.isEmpty()
3095 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003096 // 4. Search by the current subtype's locale from all subtypes.
3097 if (subtype == null && mCurrentSubtype != null) {
3098 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003099 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003100 }
3101 // 5. Search by the system locale from all subtypes.
3102 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003103 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003104 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003105 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003106 }
satokcd7cd292010-11-20 15:46:23 +09003107 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003108 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003109 // The current input method is the most applicable IME.
3110 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003111 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003112 break;
satok7599a7f2010-12-22 13:45:23 +09003113 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003114 // The system input method is 2nd applicable IME.
3115 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003116 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003117 if ((imi.getServiceInfo().applicationInfo.flags
3118 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3119 foundInSystemIME = true;
3120 }
satok4e4569d2010-11-19 18:45:53 +09003121 }
3122 }
3123 }
3124 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003125 if (mostApplicableIMI != null) {
3126 Slog.w(TAG, "Most applicable shortcut input method was:"
3127 + mostApplicableIMI.getId());
3128 if (mostApplicableSubtype != null) {
3129 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3130 + "," + mostApplicableSubtype.getMode() + ","
3131 + mostApplicableSubtype.getLocale());
3132 }
3133 }
satok4e4569d2010-11-19 18:45:53 +09003134 }
satokcd7cd292010-11-20 15:46:23 +09003135 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003136 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003137 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003138 } else {
3139 return null;
3140 }
3141 }
3142
satokab751aa2010-09-14 19:17:36 +09003143 /**
3144 * @return Return the current subtype of this input method.
3145 */
satok42c5a162011-05-26 16:46:14 +09003146 @Override
satokab751aa2010-09-14 19:17:36 +09003147 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003148 // TODO: Make this work even for non-current users?
3149 if (!calledFromValidUser()) {
3150 return null;
3151 }
3152 synchronized (mMethodMap) {
3153 return getCurrentInputMethodSubtypeLocked();
3154 }
3155 }
3156
3157 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003158 if (mCurMethodId == null) {
3159 return null;
3160 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003161 final boolean subtypeIsSelected =
3162 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3163 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3164 if (imi == null || imi.getSubtypeCount() == 0) {
3165 return null;
satok4e4569d2010-11-19 18:45:53 +09003166 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003167 if (!subtypeIsSelected || mCurrentSubtype == null
3168 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3169 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3170 if (subtypeId == NOT_A_SUBTYPE_ID) {
3171 // If there are no selected subtypes, the framework will try to find
3172 // the most applicable subtype from explicitly or implicitly enabled
3173 // subtypes.
3174 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3175 getEnabledInputMethodSubtypeListLocked(imi, true);
3176 // If there is only one explicitly or implicitly enabled subtype,
3177 // just returns it.
3178 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3179 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3180 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3181 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3182 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3183 SUBTYPE_MODE_KEYBOARD, null, true);
3184 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003185 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003186 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3187 true);
satok4e4569d2010-11-19 18:45:53 +09003188 }
satok3ef8b292010-11-23 06:06:29 +09003189 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003190 } else {
3191 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003192 }
3193 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003194 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003195 }
3196
satokf3db1af2010-11-23 13:34:33 +09003197 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3198 InputMethodSubtype subtype) {
3199 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3200 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3201 } else {
3202 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3203 subtypes.add(subtype);
3204 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3205 }
3206 }
3207
satok4e4569d2010-11-19 18:45:53 +09003208 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003209 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003210 @Override
satok4e4569d2010-11-19 18:45:53 +09003211 public List getShortcutInputMethodsAndSubtypes() {
3212 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003213 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003214 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003215 // If there are no selected shortcut subtypes, the framework will try to find
3216 // the most applicable subtype from all subtypes whose mode is
3217 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003218 Pair<InputMethodInfo, InputMethodSubtype> info =
3219 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3220 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003221 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003222 ret.add(info.first);
3223 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003224 }
satok3da92232011-01-11 22:46:30 +09003225 return ret;
satokf3db1af2010-11-23 13:34:33 +09003226 }
satokf3db1af2010-11-23 13:34:33 +09003227 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3228 ret.add(imi);
3229 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3230 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003231 }
3232 }
satokf3db1af2010-11-23 13:34:33 +09003233 return ret;
satok4e4569d2010-11-19 18:45:53 +09003234 }
3235 }
3236
satok42c5a162011-05-26 16:46:14 +09003237 @Override
satokb66d2872010-11-10 01:04:04 +09003238 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003239 // TODO: Make this work even for non-current users?
3240 if (!calledFromValidUser()) {
3241 return false;
3242 }
satokb66d2872010-11-10 01:04:04 +09003243 synchronized (mMethodMap) {
3244 if (subtype != null && mCurMethodId != null) {
3245 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3246 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3247 if (subtypeId != NOT_A_SUBTYPE_ID) {
3248 setInputMethodLocked(mCurMethodId, subtypeId);
3249 return true;
3250 }
3251 }
3252 return false;
3253 }
3254 }
3255
satok688bd472012-02-09 20:09:17 +09003256 private static class InputMethodAndSubtypeListManager {
3257 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003258 // Used to load label
satok688bd472012-02-09 20:09:17 +09003259 private final PackageManager mPm;
3260 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003261 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003262 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3263 mContext = context;
3264 mPm = context.getPackageManager();
3265 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003266 final Locale locale = context.getResources().getConfiguration().locale;
3267 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003268 }
3269
3270 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3271 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3272 new Comparator<InputMethodInfo>() {
3273 @Override
3274 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3275 if (imi2 == null) return 0;
3276 if (imi1 == null) return 1;
3277 if (mPm == null) {
3278 return imi1.getId().compareTo(imi2.getId());
3279 }
3280 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3281 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3282 return imiId1.toString().compareTo(imiId2.toString());
3283 }
3284 });
3285
3286 public ImeSubtypeListItem getNextInputMethod(
3287 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3288 if (imi == null) {
3289 return null;
3290 }
3291 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3292 if (imList.size() <= 1) {
3293 return null;
3294 }
3295 final int N = imList.size();
3296 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003297 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003298 : NOT_A_SUBTYPE_ID;
3299 for (int i = 0; i < N; ++i) {
3300 final ImeSubtypeListItem isli = imList.get(i);
3301 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3302 if (!onlyCurrentIme) {
3303 return imList.get((i + 1) % N);
3304 }
3305 for (int j = 0; j < N - 1; ++j) {
3306 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3307 if (candidate.mImi.equals(imi)) {
3308 return candidate;
3309 }
3310 }
3311 return null;
3312 }
3313 }
3314 return null;
3315 }
3316
3317 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3318 return getSortedInputMethodAndSubtypeList(true, false, false);
3319 }
3320
3321 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3322 boolean inputShown, boolean isScreenLocked) {
3323 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3324 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3325 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3326 if (immis == null || immis.size() == 0) {
3327 return Collections.emptyList();
3328 }
3329 mSortedImmis.clear();
3330 mSortedImmis.putAll(immis);
3331 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3332 if (imi == null) continue;
3333 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3334 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3335 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3336 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3337 }
3338 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3339 final CharSequence imeLabel = imi.loadLabel(mPm);
3340 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3341 final int subtypeCount = imi.getSubtypeCount();
3342 if (DEBUG) {
3343 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3344 }
3345 for (int j = 0; j < subtypeCount; ++j) {
3346 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3347 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3348 // We show all enabled IMEs and subtypes when an IME is shown.
3349 if (enabledSubtypeSet.contains(subtypeHashCode)
3350 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3351 final CharSequence subtypeLabel =
3352 subtype.overridesImplicitlyEnabledSubtype() ? null
3353 : subtype.getDisplayName(mContext, imi.getPackageName(),
3354 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003355 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3356 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003357
3358 // Removing this subtype from enabledSubtypeSet because we no longer
3359 // need to add an entry of this subtype to imList to avoid duplicated
3360 // entries.
3361 enabledSubtypeSet.remove(subtypeHashCode);
3362 }
3363 }
3364 } else {
satok93d744d2012-05-09 17:14:08 +09003365 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3366 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003367 }
3368 }
satok93d744d2012-05-09 17:14:08 +09003369 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003370 return imList;
3371 }
3372 }
3373
satokd87c2592010-09-29 11:52:06 +09003374 /**
3375 * Utility class for putting and getting settings for InputMethod
3376 * TODO: Move all putters and getters of settings to this class.
3377 */
3378 private static class InputMethodSettings {
3379 // The string for enabled input method is saved as follows:
3380 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3381 private static final char INPUT_METHOD_SEPARATER = ':';
3382 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003383 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003384 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3385
satok723a27e2010-11-11 14:58:11 +09003386 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003387 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3388
satokdf31ae62011-01-15 06:19:44 +09003389 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003390 private final ContentResolver mResolver;
3391 private final HashMap<String, InputMethodInfo> mMethodMap;
3392 private final ArrayList<InputMethodInfo> mMethodList;
3393
3394 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003395 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003396
3397 private static void buildEnabledInputMethodsSettingString(
3398 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3399 String id = pair.first;
3400 ArrayList<String> subtypes = pair.second;
3401 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003402 // Inputmethod and subtypes are saved in the settings as follows:
3403 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3404 for (String subtypeId: subtypes) {
3405 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003406 }
3407 }
3408
3409 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003410 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3412 int userId) {
3413 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003414 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003415 mResolver = resolver;
3416 mMethodMap = methodMap;
3417 mMethodList = methodList;
3418 }
3419
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 public void setCurrentUserId(int userId) {
3421 if (DEBUG) {
3422 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3423 + userId + ", new ime = " + getSelectedInputMethod());
3424 }
3425 // IMMS settings are kept per user, so keep track of current user
3426 mCurrentUserId = userId;
3427 }
3428
satokd87c2592010-09-29 11:52:06 +09003429 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3430 return createEnabledInputMethodListLocked(
3431 getEnabledInputMethodsAndSubtypeListLocked());
3432 }
3433
satok7f35c8c2010-10-07 21:13:11 +09003434 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003435 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3436 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003437 getEnabledInputMethodsAndSubtypeListLocked());
3438 }
3439
satok67ddf9c2010-11-17 09:45:54 +09003440 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3441 InputMethodInfo imi) {
3442 List<Pair<String, ArrayList<String>>> imsList =
3443 getEnabledInputMethodsAndSubtypeListLocked();
3444 ArrayList<InputMethodSubtype> enabledSubtypes =
3445 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003446 if (imi != null) {
3447 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3448 InputMethodInfo info = mMethodMap.get(imsPair.first);
3449 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003450 final int subtypeCount = info.getSubtypeCount();
3451 for (int i = 0; i < subtypeCount; ++i) {
3452 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003453 for (String s: imsPair.second) {
3454 if (String.valueOf(ims.hashCode()).equals(s)) {
3455 enabledSubtypes.add(ims);
3456 }
satok67ddf9c2010-11-17 09:45:54 +09003457 }
3458 }
satok884ef9a2010-11-18 10:39:46 +09003459 break;
satok67ddf9c2010-11-17 09:45:54 +09003460 }
satok67ddf9c2010-11-17 09:45:54 +09003461 }
3462 }
3463 return enabledSubtypes;
3464 }
3465
satokd87c2592010-09-29 11:52:06 +09003466 // At the initial boot, the settings for input methods are not set,
3467 // so we need to enable IME in that case.
3468 public void enableAllIMEsIfThereIsNoEnabledIME() {
3469 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3470 StringBuilder sb = new StringBuilder();
3471 final int N = mMethodList.size();
3472 for (int i = 0; i < N; i++) {
3473 InputMethodInfo imi = mMethodList.get(i);
3474 Slog.i(TAG, "Adding: " + imi.getId());
3475 if (i > 0) sb.append(':');
3476 sb.append(imi.getId());
3477 }
3478 putEnabledInputMethodsStr(sb.toString());
3479 }
3480 }
3481
satokbb4aa062011-01-19 21:40:27 +09003482 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003483 ArrayList<Pair<String, ArrayList<String>>> imsList
3484 = new ArrayList<Pair<String, ArrayList<String>>>();
3485 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3486 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3487 return imsList;
3488 }
satok723a27e2010-11-11 14:58:11 +09003489 mInputMethodSplitter.setString(enabledInputMethodsStr);
3490 while (mInputMethodSplitter.hasNext()) {
3491 String nextImsStr = mInputMethodSplitter.next();
3492 mSubtypeSplitter.setString(nextImsStr);
3493 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003494 ArrayList<String> subtypeHashes = new ArrayList<String>();
3495 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003496 String imeId = mSubtypeSplitter.next();
3497 while (mSubtypeSplitter.hasNext()) {
3498 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003499 }
3500 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3501 }
3502 }
3503 return imsList;
3504 }
3505
3506 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3507 if (reloadInputMethodStr) {
3508 getEnabledInputMethodsStr();
3509 }
3510 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3511 // Add in the newly enabled input method.
3512 putEnabledInputMethodsStr(id);
3513 } else {
3514 putEnabledInputMethodsStr(
3515 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3516 }
3517 }
3518
3519 /**
3520 * Build and put a string of EnabledInputMethods with removing specified Id.
3521 * @return the specified id was removed or not.
3522 */
3523 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3524 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3525 boolean isRemoved = false;
3526 boolean needsAppendSeparator = false;
3527 for (Pair<String, ArrayList<String>> ims: imsList) {
3528 String curId = ims.first;
3529 if (curId.equals(id)) {
3530 // We are disabling this input method, and it is
3531 // currently enabled. Skip it to remove from the
3532 // new list.
3533 isRemoved = true;
3534 } else {
3535 if (needsAppendSeparator) {
3536 builder.append(INPUT_METHOD_SEPARATER);
3537 } else {
3538 needsAppendSeparator = true;
3539 }
3540 buildEnabledInputMethodsSettingString(builder, ims);
3541 }
3542 }
3543 if (isRemoved) {
3544 // Update the setting with the new list of input methods.
3545 putEnabledInputMethodsStr(builder.toString());
3546 }
3547 return isRemoved;
3548 }
3549
3550 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3551 List<Pair<String, ArrayList<String>>> imsList) {
3552 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3553 for (Pair<String, ArrayList<String>> ims: imsList) {
3554 InputMethodInfo info = mMethodMap.get(ims.first);
3555 if (info != null) {
3556 res.add(info);
3557 }
3558 }
3559 return res;
3560 }
3561
satok7f35c8c2010-10-07 21:13:11 +09003562 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003563 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003564 List<Pair<String, ArrayList<String>>> imsList) {
3565 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3566 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3567 for (Pair<String, ArrayList<String>> ims : imsList) {
3568 InputMethodInfo info = mMethodMap.get(ims.first);
3569 if (info != null) {
3570 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3571 }
3572 }
3573 return res;
3574 }
3575
satokd87c2592010-09-29 11:52:06 +09003576 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003577 Settings.Secure.putStringForUser(
3578 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003579 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003580 if (DEBUG) {
3581 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3582 }
satokd87c2592010-09-29 11:52:06 +09003583 }
3584
3585 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003586 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3587 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003588 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003589 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3590 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003591 }
satokd87c2592010-09-29 11:52:06 +09003592 return mEnabledInputMethodsStrCache;
3593 }
satok723a27e2010-11-11 14:58:11 +09003594
3595 private void saveSubtypeHistory(
3596 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3597 StringBuilder builder = new StringBuilder();
3598 boolean isImeAdded = false;
3599 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3600 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3601 newSubtypeId);
3602 isImeAdded = true;
3603 }
3604 for (Pair<String, String> ime: savedImes) {
3605 String imeId = ime.first;
3606 String subtypeId = ime.second;
3607 if (TextUtils.isEmpty(subtypeId)) {
3608 subtypeId = NOT_A_SUBTYPE_ID_STR;
3609 }
3610 if (isImeAdded) {
3611 builder.append(INPUT_METHOD_SEPARATER);
3612 } else {
3613 isImeAdded = true;
3614 }
3615 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3616 subtypeId);
3617 }
3618 // Remove the last INPUT_METHOD_SEPARATER
3619 putSubtypeHistoryStr(builder.toString());
3620 }
3621
3622 public void addSubtypeToHistory(String imeId, String subtypeId) {
3623 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3624 for (Pair<String, String> ime: subtypeHistory) {
3625 if (ime.first.equals(imeId)) {
3626 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003627 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003628 + ime.second);
3629 }
3630 // We should break here
3631 subtypeHistory.remove(ime);
3632 break;
3633 }
3634 }
satokbb4aa062011-01-19 21:40:27 +09003635 if (DEBUG) {
3636 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3637 }
satok723a27e2010-11-11 14:58:11 +09003638 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3639 }
3640
3641 private void putSubtypeHistoryStr(String str) {
3642 if (DEBUG) {
3643 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003645 Settings.Secure.putStringForUser(
3646 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003647 }
3648
3649 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3650 // Gets the first one from the history
3651 return getLastSubtypeForInputMethodLockedInternal(null);
3652 }
3653
3654 public String getLastSubtypeForInputMethodLocked(String imeId) {
3655 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3656 if (ime != null) {
3657 return ime.second;
3658 } else {
3659 return null;
3660 }
3661 }
3662
3663 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3664 List<Pair<String, ArrayList<String>>> enabledImes =
3665 getEnabledInputMethodsAndSubtypeListLocked();
3666 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003667 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003668 final String imeInTheHistory = imeAndSubtype.first;
3669 // If imeId is empty, returns the first IME and subtype in the history
3670 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3671 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003672 final String subtypeHashCode =
3673 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3674 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003675 if (!TextUtils.isEmpty(subtypeHashCode)) {
3676 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003677 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003678 }
3679 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3680 }
3681 }
3682 }
3683 if (DEBUG) {
3684 Slog.d(TAG, "No enabled IME found in the history");
3685 }
3686 return null;
3687 }
3688
satokdf31ae62011-01-15 06:19:44 +09003689 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003690 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3691 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3692 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003693 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003694 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003695 if (explicitlyEnabledSubtypes.size() == 0) {
3696 // If there are no explicitly enabled subtypes, applicable subtypes are
3697 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003698 // If IME is enabled and no subtypes are enabled, applicable subtypes
3699 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003700 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003701 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003702 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003703 if (implicitlySelectedSubtypes != null) {
3704 final int N = implicitlySelectedSubtypes.size();
3705 for (int i = 0; i < N; ++i) {
3706 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3707 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3708 return subtypeHashCode;
3709 }
3710 }
3711 }
3712 }
3713 } else {
satokf6cafb62011-01-17 16:29:02 +09003714 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003715 if (s.equals(subtypeHashCode)) {
3716 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003717 try {
3718 final int hashCode = Integer.valueOf(subtypeHashCode);
3719 // Check whether the subtype id is valid or not
3720 if (isValidSubtypeId(imi, hashCode)) {
3721 return s;
3722 } else {
3723 return NOT_A_SUBTYPE_ID_STR;
3724 }
3725 } catch (NumberFormatException e) {
3726 return NOT_A_SUBTYPE_ID_STR;
3727 }
satokdf31ae62011-01-15 06:19:44 +09003728 }
satok723a27e2010-11-11 14:58:11 +09003729 }
3730 }
3731 // If imeId was enabled but subtypeId was disabled.
3732 return NOT_A_SUBTYPE_ID_STR;
3733 }
3734 }
3735 // If both imeId and subtypeId are disabled, return null
3736 return null;
3737 }
3738
3739 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3740 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3741 final String subtypeHistoryStr = getSubtypeHistoryStr();
3742 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3743 return imsList;
3744 }
3745 mInputMethodSplitter.setString(subtypeHistoryStr);
3746 while (mInputMethodSplitter.hasNext()) {
3747 String nextImsStr = mInputMethodSplitter.next();
3748 mSubtypeSplitter.setString(nextImsStr);
3749 if (mSubtypeSplitter.hasNext()) {
3750 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3751 // The first element is ime id.
3752 String imeId = mSubtypeSplitter.next();
3753 while (mSubtypeSplitter.hasNext()) {
3754 subtypeId = mSubtypeSplitter.next();
3755 break;
3756 }
3757 imsList.add(new Pair<String, String>(imeId, subtypeId));
3758 }
3759 }
3760 return imsList;
3761 }
3762
3763 private String getSubtypeHistoryStr() {
3764 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003765 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3766 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003767 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003768 return Settings.Secure.getStringForUser(
3769 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003770 }
3771
3772 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003773 if (DEBUG) {
3774 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3775 + mCurrentUserId);
3776 }
3777 Settings.Secure.putStringForUser(
3778 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003779 }
3780
3781 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003782 if (DEBUG) {
3783 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3784 + mCurrentUserId);
3785 }
3786 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3787 subtypeId, mCurrentUserId);
3788 }
3789
3790 public String getDisabledSystemInputMethods() {
3791 return Settings.Secure.getStringForUser(
3792 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3793 }
3794
3795 public String getSelectedInputMethod() {
3796 if (DEBUG) {
3797 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3798 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3799 + ", " + mCurrentUserId);
3800 }
3801 return Settings.Secure.getStringForUser(
3802 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3803 }
3804
3805 public int getSelectedInputMethodSubtypeHashCode() {
3806 try {
3807 return Settings.Secure.getIntForUser(
3808 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3809 } catch (SettingNotFoundException e) {
3810 return NOT_A_SUBTYPE_ID;
3811 }
3812 }
3813
3814 public int getCurrentUserId() {
3815 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003816 }
satokd87c2592010-09-29 11:52:06 +09003817 }
3818
satoke7c6998e2011-06-03 17:57:59 +09003819 private static class InputMethodFileManager {
3820 private static final String SYSTEM_PATH = "system";
3821 private static final String INPUT_METHOD_PATH = "inputmethod";
3822 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3823 private static final String NODE_SUBTYPES = "subtypes";
3824 private static final String NODE_SUBTYPE = "subtype";
3825 private static final String NODE_IMI = "imi";
3826 private static final String ATTR_ID = "id";
3827 private static final String ATTR_LABEL = "label";
3828 private static final String ATTR_ICON = "icon";
3829 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3830 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3831 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3832 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3833 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3834 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003835 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003836 new HashMap<String, List<InputMethodSubtype>>();
3837 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3838 if (methodMap == null) {
3839 throw new NullPointerException("methodMap is null");
3840 }
3841 mMethodMap = methodMap;
3842 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3843 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3844 if (!inputMethodDir.mkdirs()) {
3845 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3846 }
3847 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3848 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3849 if (!subtypeFile.exists()) {
3850 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003851 writeAdditionalInputMethodSubtypes(
3852 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003853 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003854 readAdditionalInputMethodSubtypes(
3855 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003856 }
3857 }
3858
3859 private void deleteAllInputMethodSubtypes(String imiId) {
3860 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003861 mAdditionalSubtypesMap.remove(imiId);
3862 writeAdditionalInputMethodSubtypes(
3863 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003864 }
3865 }
3866
3867 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003868 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003869 synchronized (mMethodMap) {
3870 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3871 final int N = additionalSubtypes.length;
3872 for (int i = 0; i < N; ++i) {
3873 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003874 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003875 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003876 } else {
3877 Slog.w(TAG, "Duplicated subtype definition found: "
3878 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003879 }
3880 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003881 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3882 writeAdditionalInputMethodSubtypes(
3883 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003884 }
3885 }
3886
3887 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3888 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003889 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003890 }
3891 }
3892
3893 private static void writeAdditionalInputMethodSubtypes(
3894 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3895 HashMap<String, InputMethodInfo> methodMap) {
3896 // Safety net for the case that this function is called before methodMap is set.
3897 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3898 FileOutputStream fos = null;
3899 try {
3900 fos = subtypesFile.startWrite();
3901 final XmlSerializer out = new FastXmlSerializer();
3902 out.setOutput(fos, "utf-8");
3903 out.startDocument(null, true);
3904 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3905 out.startTag(null, NODE_SUBTYPES);
3906 for (String imiId : allSubtypes.keySet()) {
3907 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3908 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3909 continue;
3910 }
3911 out.startTag(null, NODE_IMI);
3912 out.attribute(null, ATTR_ID, imiId);
3913 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3914 final int N = subtypesList.size();
3915 for (int i = 0; i < N; ++i) {
3916 final InputMethodSubtype subtype = subtypesList.get(i);
3917 out.startTag(null, NODE_SUBTYPE);
3918 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3919 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3920 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3921 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3922 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3923 out.attribute(null, ATTR_IS_AUXILIARY,
3924 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3925 out.endTag(null, NODE_SUBTYPE);
3926 }
3927 out.endTag(null, NODE_IMI);
3928 }
3929 out.endTag(null, NODE_SUBTYPES);
3930 out.endDocument();
3931 subtypesFile.finishWrite(fos);
3932 } catch (java.io.IOException e) {
3933 Slog.w(TAG, "Error writing subtypes", e);
3934 if (fos != null) {
3935 subtypesFile.failWrite(fos);
3936 }
3937 }
3938 }
3939
3940 private static void readAdditionalInputMethodSubtypes(
3941 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3942 if (allSubtypes == null || subtypesFile == null) return;
3943 allSubtypes.clear();
3944 FileInputStream fis = null;
3945 try {
3946 fis = subtypesFile.openRead();
3947 final XmlPullParser parser = Xml.newPullParser();
3948 parser.setInput(fis, null);
3949 int type = parser.getEventType();
3950 // Skip parsing until START_TAG
3951 while ((type = parser.next()) != XmlPullParser.START_TAG
3952 && type != XmlPullParser.END_DOCUMENT) {}
3953 String firstNodeName = parser.getName();
3954 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3955 throw new XmlPullParserException("Xml doesn't start with subtypes");
3956 }
3957 final int depth =parser.getDepth();
3958 String currentImiId = null;
3959 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3960 while (((type = parser.next()) != XmlPullParser.END_TAG
3961 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3962 if (type != XmlPullParser.START_TAG)
3963 continue;
3964 final String nodeName = parser.getName();
3965 if (NODE_IMI.equals(nodeName)) {
3966 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3967 if (TextUtils.isEmpty(currentImiId)) {
3968 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3969 continue;
3970 }
3971 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3972 allSubtypes.put(currentImiId, tempSubtypesArray);
3973 } else if (NODE_SUBTYPE.equals(nodeName)) {
3974 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3975 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3976 continue;
3977 }
3978 final int icon = Integer.valueOf(
3979 parser.getAttributeValue(null, ATTR_ICON));
3980 final int label = Integer.valueOf(
3981 parser.getAttributeValue(null, ATTR_LABEL));
3982 final String imeSubtypeLocale =
3983 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3984 final String imeSubtypeMode =
3985 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3986 final String imeSubtypeExtraValue =
3987 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003988 final boolean isAuxiliary = "1".equals(String.valueOf(
3989 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003990 final InputMethodSubtype subtype =
3991 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3992 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3993 tempSubtypesArray.add(subtype);
3994 }
3995 }
3996 } catch (XmlPullParserException e) {
3997 Slog.w(TAG, "Error reading subtypes: " + e);
3998 return;
3999 } catch (java.io.IOException e) {
4000 Slog.w(TAG, "Error reading subtypes: " + e);
4001 return;
4002 } catch (NumberFormatException e) {
4003 Slog.w(TAG, "Error reading subtypes: " + e);
4004 return;
4005 } finally {
4006 if (fis != null) {
4007 try {
4008 fis.close();
4009 } catch (java.io.IOException e1) {
4010 Slog.w(TAG, "Failed to close.");
4011 }
4012 }
4013 }
4014 }
4015 }
4016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004018 // Utilities for debug
4019 private static String getStackTrace() {
4020 final StringBuilder sb = new StringBuilder();
4021 try {
4022 throw new RuntimeException();
4023 } catch (RuntimeException e) {
4024 final StackTraceElement[] frames = e.getStackTrace();
4025 // Start at 1 because the first frame is here and we don't care about it
4026 for (int j = 1; j < frames.length; ++j) {
4027 sb.append(frames[j].toString() + "\n");
4028 }
4029 }
4030 return sb.toString();
4031 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 @Override
4034 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4035 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4036 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4039 + Binder.getCallingPid()
4040 + ", uid=" + Binder.getCallingUid());
4041 return;
4042 }
4043
4044 IInputMethod method;
4045 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 synchronized (mMethodMap) {
4050 p.println("Current Input Method Manager state:");
4051 int N = mMethodList.size();
4052 p.println(" Input Methods:");
4053 for (int i=0; i<N; i++) {
4054 InputMethodInfo info = mMethodList.get(i);
4055 p.println(" InputMethod #" + i + ":");
4056 info.dump(p, " ");
4057 }
4058 p.println(" Clients:");
4059 for (ClientState ci : mClients.values()) {
4060 p.println(" Client " + ci + ":");
4061 p.println(" client=" + ci.client);
4062 p.println(" inputContext=" + ci.inputContext);
4063 p.println(" sessionRequested=" + ci.sessionRequested);
4064 p.println(" curSession=" + ci.curSession);
4065 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004066 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004068 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4069 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4071 + " mBoundToMethod=" + mBoundToMethod);
4072 p.println(" mCurToken=" + mCurToken);
4073 p.println(" mCurIntent=" + mCurIntent);
4074 method = mCurMethod;
4075 p.println(" mCurMethod=" + mCurMethod);
4076 p.println(" mEnabledSession=" + mEnabledSession);
4077 p.println(" mShowRequested=" + mShowRequested
4078 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4079 + " mShowForced=" + mShowForced
4080 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004081 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004083
Jeff Brownb88102f2010-09-08 11:49:43 -07004084 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 pw.flush();
4087 try {
4088 client.client.asBinder().dump(fd, args);
4089 } catch (RemoteException e) {
4090 p.println("Input method client dead: " + e);
4091 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004092 } else {
4093 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004095
Jeff Brownb88102f2010-09-08 11:49:43 -07004096 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 pw.flush();
4099 try {
4100 method.asBinder().dump(fd, args);
4101 } catch (RemoteException e) {
4102 p.println("Input method service dead: " + e);
4103 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004104 } else {
4105 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 }
4108}