blob: 0f2bb206520f75b677dfdd5ec6eca1913fe5bc1c [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;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900174 private InputMethodFileManager mFileManager;
175 private 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
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900628 final IntentFilter broadcastFilter = new IntentFilter();
629 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
630 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
631 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
632 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800633
satok7cfc0ed2011-06-20 21:29:36 +0900634 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 int userId = 0;
636 try {
637 ActivityManagerNative.getDefault().registerUserSwitchObserver(
638 new IUserSwitchObserver.Stub() {
639 @Override
640 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900641 synchronized(mMethodMap) {
642 switchUserLocked(newUserId);
643 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900644 if (reply != null) {
645 try {
646 reply.sendResult(null);
647 } catch (RemoteException e) {
648 }
649 }
650 }
651
652 @Override
653 public void onUserSwitchComplete(int newUserId) throws RemoteException {
654 }
655 });
656 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
657 } catch (RemoteException e) {
658 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
659 }
660 mMyPackageMonitor.register(mContext, null, true);
satok913a8922010-08-26 21:53:41 +0900661
satokd87c2592010-09-29 11:52:06 +0900662 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900663 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900664 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900665 mFileManager = new InputMethodFileManager(mMethodMap, userId);
666 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900667
668 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900670 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900673 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
satok0a1bcf42012-05-16 19:26:31 +0900675 if (!mImeSelectedOnBoot) {
676 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900677 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 mSettingsObserver = new SettingsObserver(mHandler);
681 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900682
683 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
684 // according to the new system locale.
685 final IntentFilter filter = new IntentFilter();
686 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
687 mContext.registerReceiver(
688 new BroadcastReceiver() {
689 @Override
690 public void onReceive(Context context, Intent intent) {
691 synchronized(mMethodMap) {
692 checkCurrentLocaleChangedLocked();
693 }
694 }
695 }, filter);
696 }
697
satok5b927c432012-05-01 20:09:34 +0900698 private void resetDefaultImeLocked(Context context) {
699 // Do not reset the default (current) IME when it is a 3rd-party IME
700 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
701 return;
702 }
703
704 InputMethodInfo defIm = null;
705 for (InputMethodInfo imi : mMethodList) {
706 if (defIm == null) {
707 if (isValidSystemDefaultIme(imi, context)) {
708 defIm = imi;
709 Slog.i(TAG, "Selected default: " + imi.getId());
710 }
711 }
712 }
713 if (defIm == null && mMethodList.size() > 0) {
714 defIm = getMostApplicableDefaultIMELocked();
715 Slog.i(TAG, "No default found, using " + defIm.getId());
716 }
717 if (defIm != null) {
718 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
719 }
720 }
721
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900722 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
723 if (!mSystemReady) {
724 // not system ready
725 return;
726 }
727 final Locale newLocale = mRes.getConfiguration().locale;
728 if (!updateOnlyWhenLocaleChanged
729 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
730 if (!updateOnlyWhenLocaleChanged) {
731 hideCurrentInputLocked(0, null);
732 mCurMethodId = null;
733 unbindCurrentMethodLocked(true);
734 }
735 if (DEBUG) {
736 Slog.i(TAG, "Locale has been changed to " + newLocale);
737 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900738 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
739 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900740 buildInputMethodListLocked(mMethodList, mMethodMap);
741 if (!updateOnlyWhenLocaleChanged) {
742 final String selectedImiId = mSettings.getSelectedInputMethod();
743 if (TextUtils.isEmpty(selectedImiId)) {
744 // This is the first time of the user switch and
745 // set the current ime to the proper one.
746 resetDefaultImeLocked(mContext);
747 }
748 }
749 updateFromSettingsLocked();
750 mLastSystemLocale = newLocale;
751 if (!updateOnlyWhenLocaleChanged) {
752 try {
753 startInputInnerLocked();
754 } catch (RuntimeException e) {
755 Slog.w(TAG, "Unexpected exception", e);
756 }
757 }
758 }
759 }
760
761 private void checkCurrentLocaleChangedLocked() {
762 resetAllInternalStateLocked(true);
763 }
764
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900765 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900766 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900767 // InputMethodFileManager should be reset when the user is changed
768 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900769 resetAllInternalStateLocked(false);
770 }
771
satok0a1bcf42012-05-16 19:26:31 +0900772 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
773 if (!mSystemReady) {
774 return false;
775 }
satok5b927c432012-05-01 20:09:34 +0900776 if (!isSystemIme(imi)) {
777 return false;
778 }
779 if (imi.getIsDefaultResourceId() != 0) {
780 try {
781 Resources res = context.createPackageContext(
782 imi.getPackageName(), 0).getResources();
783 if (res.getBoolean(imi.getIsDefaultResourceId())
784 && containsSubtypeOf(imi, context.getResources().getConfiguration().
785 locale.getLanguage())) {
786 return true;
787 }
788 } catch (PackageManager.NameNotFoundException ex) {
789 } catch (Resources.NotFoundException ex) {
790 }
791 }
792 if (imi.getSubtypeCount() == 0) {
793 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
794 }
795 return false;
796 }
797
798 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
799 if (!isSystemIme(imi)) {
800 return false;
801 }
802 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
803 }
804
805 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
806 final int N = imi.getSubtypeCount();
807 for (int i = 0; i < N; ++i) {
808 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
809 return true;
810 }
811 }
812 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814
815 @Override
816 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
817 throws RemoteException {
818 try {
819 return super.onTransact(code, data, reply, flags);
820 } catch (RuntimeException e) {
821 // The input method manager only throws security exceptions, so let's
822 // log all others.
823 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800824 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826 throw e;
827 }
828 }
829
Dianne Hackborn661cd522011-08-22 00:26:20 -0700830 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700831 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900832 if (DEBUG) {
833 Slog.d(TAG, "--- systemReady");
834 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700835 if (!mSystemReady) {
836 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900837 mKeyguardManager =
838 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700839 mNotificationManager = (NotificationManager)
840 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
841 mStatusBar = statusBar;
842 statusBar.setIconVisibility("ime", false);
843 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900844 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
845 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900846 if (mShowOngoingImeSwitcherForPhones) {
847 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
848 mHardKeyboardListener);
849 }
satok0a1bcf42012-05-16 19:26:31 +0900850 buildInputMethodListLocked(mMethodList, mMethodMap);
851 if (!mImeSelectedOnBoot) {
852 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
853 checkCurrentLocaleChangedLocked();
854 }
855 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700856 try {
857 startInputInnerLocked();
858 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800859 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700860 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 }
862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
satok15452a42011-10-28 17:58:28 +0900865 private void setImeWindowVisibilityStatusHiddenLocked() {
866 mImeWindowVis = 0;
867 updateImeWindowStatusLocked();
868 }
869
satok3afd6c02011-11-18 08:38:19 +0900870 private void refreshImeWindowVisibilityLocked() {
871 final Configuration conf = mRes.getConfiguration();
872 final boolean haveHardKeyboard = conf.keyboard
873 != Configuration.KEYBOARD_NOKEYS;
874 final boolean hardKeyShown = haveHardKeyboard
875 && conf.hardKeyboardHidden
876 != Configuration.HARDKEYBOARDHIDDEN_YES;
877 final boolean isScreenLocked = mKeyguardManager != null
878 && mKeyguardManager.isKeyguardLocked()
879 && mKeyguardManager.isKeyguardSecure();
880 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
881 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
882 updateImeWindowStatusLocked();
883 }
884
satok15452a42011-10-28 17:58:28 +0900885 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900886 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700887 }
888
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900889 // ---------------------------------------------------------------------------------------
890 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
891 // 1) it comes from the system process
892 // 2) the calling process' user id is identical to the current user id IMMS thinks.
893 private boolean calledFromValidUser() {
894 final int uid = Binder.getCallingUid();
895 final int userId = UserHandle.getUserId(uid);
896 if (DEBUG) {
897 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
898 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
899 + " calling userId = " + userId + ", foreground user id = "
900 + mSettings.getCurrentUserId());
901 }
902 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
903 return true;
904 } else {
905 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
906 return false;
907 }
908 }
909
910 private boolean bindCurrentInputMethodService(
911 Intent service, ServiceConnection conn, int flags) {
912 if (service == null || conn == null) {
913 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
914 return false;
915 }
916 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
917 }
918
satoke7c6998e2011-06-03 17:57:59 +0900919 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900921 // TODO: Make this work even for non-current users?
922 if (!calledFromValidUser()) {
923 return Collections.emptyList();
924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 synchronized (mMethodMap) {
926 return new ArrayList<InputMethodInfo>(mMethodList);
927 }
928 }
929
satoke7c6998e2011-06-03 17:57:59 +0900930 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900932 // TODO: Make this work even for non-current users?
933 if (!calledFromValidUser()) {
934 return Collections.emptyList();
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900937 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939 }
940
satokbb4aa062011-01-19 21:40:27 +0900941 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
942 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
943 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
944 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900946 enabledInputMethodAndSubtypes.put(
947 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
948 }
949 return enabledInputMethodAndSubtypes;
950 }
951
952 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
953 boolean allowsImplicitlySelectedSubtypes) {
954 if (imi == null && mCurMethodId != null) {
955 imi = mMethodMap.get(mCurMethodId);
956 }
satok7265d9b2011-02-14 15:47:30 +0900957 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900958 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900959 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900960 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900961 }
satok7265d9b2011-02-14 15:47:30 +0900962 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900963 }
964
satoke7c6998e2011-06-03 17:57:59 +0900965 @Override
satok16331c82010-12-20 23:48:46 +0900966 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
967 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 // TODO: Make this work even for non-current users?
969 if (!calledFromValidUser()) {
970 return Collections.emptyList();
971 }
satok67ddf9c2010-11-17 09:45:54 +0900972 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900973 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900974 }
975 }
976
satoke7c6998e2011-06-03 17:57:59 +0900977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 public void addClient(IInputMethodClient client,
979 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 if (!calledFromValidUser()) {
981 return;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 synchronized (mMethodMap) {
984 mClients.put(client.asBinder(), new ClientState(client,
985 inputContext, uid, pid));
986 }
987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800988
satoke7c6998e2011-06-03 17:57:59 +0900989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (!calledFromValidUser()) {
992 return;
993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 synchronized (mMethodMap) {
995 mClients.remove(client.asBinder());
996 }
997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 void executeOrSendMessage(IInterface target, Message msg) {
1000 if (target.asBinder() instanceof Binder) {
1001 mCaller.sendMessage(msg);
1002 } else {
1003 handleMessage(msg);
1004 msg.recycle();
1005 }
1006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001008 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001010 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 + mCurClient.client.asBinder());
1012 if (mBoundToMethod) {
1013 mBoundToMethod = false;
1014 if (mCurMethod != null) {
1015 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1016 MSG_UNBIND_INPUT, mCurMethod));
1017 }
1018 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001019
1020 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1021 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1023 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1024 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
The Android Open Source Project10592532009-03-18 17:39:46 -07001027 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 private int getImeShowFlags() {
1032 int flags = 0;
1033 if (mShowForced) {
1034 flags |= InputMethod.SHOW_FORCED
1035 | InputMethod.SHOW_EXPLICIT;
1036 } else if (mShowExplicitlyRequested) {
1037 flags |= InputMethod.SHOW_EXPLICIT;
1038 }
1039 return flags;
1040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 private int getAppShowFlags() {
1043 int flags = 0;
1044 if (mShowForced) {
1045 flags |= InputMethodManager.SHOW_FORCED;
1046 } else if (!mShowExplicitlyRequested) {
1047 flags |= InputMethodManager.SHOW_IMPLICIT;
1048 }
1049 return flags;
1050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001051
Dianne Hackborn7663d802012-02-24 13:08:49 -08001052 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 if (!mBoundToMethod) {
1054 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1055 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1056 mBoundToMethod = true;
1057 }
1058 final SessionState session = mCurClient.curSession;
1059 if (initial) {
1060 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1061 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1062 } else {
1063 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1064 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1065 }
1066 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001067 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001068 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001070 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001074 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 // If no method is currently selected, do nothing.
1076 if (mCurMethodId == null) {
1077 return mNoBinding;
1078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 ClientState cs = mClients.get(client.asBinder());
1081 if (cs == null) {
1082 throw new IllegalArgumentException("unknown client "
1083 + client.asBinder());
1084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 try {
1087 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1088 // Check with the window manager to make sure this client actually
1089 // has a window with focus. If not, reject. This is thread safe
1090 // because if the focus changes some time before or after, the
1091 // next client receiving focus that has any interest in input will
1092 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001093 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1095 return null;
1096 }
1097 } catch (RemoteException e) {
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
Dianne Hackborn7663d802012-02-24 13:08:49 -08001100 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1101 }
1102
1103 InputBindResult startInputUncheckedLocked(ClientState cs,
1104 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1105 // If no method is currently selected, do nothing.
1106 if (mCurMethodId == null) {
1107 return mNoBinding;
1108 }
1109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 if (mCurClient != cs) {
1111 // If the client is changing, we need to switch over to the new
1112 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001113 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001114 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 + cs.client.asBinder());
1116
1117 // If the screen is on, inform the new client it is active
1118 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001119 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1120 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // Bump up the sequence for this client and attach it.
1125 mCurSeq++;
1126 if (mCurSeq <= 0) mCurSeq = 1;
1127 mCurClient = cs;
1128 mCurInputContext = inputContext;
1129 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 // Check if the input method is changing.
1132 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1133 if (cs.curSession != null) {
1134 // Fast case: if we are already connected to the input method,
1135 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001136 return attachNewInputLocked(
1137 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 if (mHaveConnection) {
1140 if (mCurMethod != null) {
1141 if (!cs.sessionRequested) {
1142 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1145 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001146 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148 // Return to client, and we will get back with it when
1149 // we have had a session made for it.
1150 return new InputBindResult(null, mCurId, mCurSeq);
1151 } else if (SystemClock.uptimeMillis()
1152 < (mLastBindTime+TIME_TO_RECONNECT)) {
1153 // In this case we have connected to the service, but
1154 // don't yet have its interface. If it hasn't been too
1155 // long since we did the connection, we'll return to
1156 // the client and wait to get the service interface so
1157 // we can report back. If it has been too long, we want
1158 // to fall through so we can try a disconnect/reconnect
1159 // to see if we can get back in touch with the service.
1160 return new InputBindResult(null, mCurId, mCurSeq);
1161 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001162 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1163 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001168 return startInputInnerLocked();
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001171 InputBindResult startInputInnerLocked() {
1172 if (mCurMethodId == null) {
1173 return mNoBinding;
1174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001176 if (!mSystemReady) {
1177 // If the system is not yet ready, we shouldn't be running third
1178 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001179 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1183 if (info == null) {
1184 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001187 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1190 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001191 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1192 com.android.internal.R.string.input_method_binding_label);
1193 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1194 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001195 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001196 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 mLastBindTime = SystemClock.uptimeMillis();
1198 mHaveConnection = true;
1199 mCurId = info.getId();
1200 mCurToken = new Binder();
1201 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001202 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 mIWindowManager.addWindowToken(mCurToken,
1204 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1205 } catch (RemoteException e) {
1206 }
1207 return new InputBindResult(null, mCurId, mCurSeq);
1208 } else {
1209 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 + mCurIntent);
1212 }
1213 return null;
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
satoke7c6998e2011-06-03 17:57:59 +09001216 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001218 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001219 if (!calledFromValidUser()) {
1220 return null;
1221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 synchronized (mMethodMap) {
1223 final long ident = Binder.clearCallingIdentity();
1224 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001225 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } finally {
1227 Binder.restoreCallingIdentity(ident);
1228 }
1229 }
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
satoke7c6998e2011-06-03 17:57:59 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void finishInput(IInputMethodClient client) {
1234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
satoke7c6998e2011-06-03 17:57:59 +09001236 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 public void onServiceConnected(ComponentName name, IBinder service) {
1238 synchronized (mMethodMap) {
1239 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1240 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001241 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001243 unbindCurrentMethodLocked(false);
1244 return;
1245 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001247 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1248 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001251 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001253 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001254 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 }
1257 }
1258 }
1259
1260 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1261 synchronized (mMethodMap) {
1262 if (mCurMethod != null && method != null
1263 && mCurMethod.asBinder() == method.asBinder()) {
1264 if (mCurClient != null) {
1265 mCurClient.curSession = new SessionState(mCurClient,
1266 method, session);
1267 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001268 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 if (res.method != null) {
1270 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1271 MSG_BIND_METHOD, mCurClient.client, res));
1272 }
1273 }
1274 }
1275 }
1276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001278 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001279 if (mVisibleBound) {
1280 mContext.unbindService(mVisibleConnection);
1281 mVisibleBound = false;
1282 }
1283
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001284 if (mHaveConnection) {
1285 mContext.unbindService(this);
1286 mHaveConnection = false;
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001289 if (mCurToken != null) {
1290 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001291 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
satoke0a99412012-05-10 02:22:58 +09001292 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1293 // The current IME is shown. Hence an IME switch (transition) is happening.
1294 mWindowManagerService.saveLastInputMethodWindowForTransition();
1295 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001296 mIWindowManager.removeWindowToken(mCurToken);
1297 } catch (RemoteException e) {
1298 }
1299 mCurToken = null;
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Project10592532009-03-18 17:39:46 -07001302 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001303 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001305 if (reportToClient && mCurClient != null) {
1306 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1307 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1308 }
1309 }
1310
Devin Taylor0c33ed22010-02-23 13:26:46 -06001311 private void finishSession(SessionState sessionState) {
1312 if (sessionState != null && sessionState.session != null) {
1313 try {
1314 sessionState.session.finishSession();
1315 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001316 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001317 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001318 }
1319 }
1320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001322 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (mCurMethod != null) {
1324 for (ClientState cs : mClients.values()) {
1325 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001326 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 cs.curSession = null;
1328 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001329
1330 finishSession(mEnabledSession);
1331 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 mCurMethod = null;
1333 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001334 if (mStatusBar != null) {
1335 mStatusBar.setIconVisibility("ime", false);
1336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
satoke7c6998e2011-06-03 17:57:59 +09001339 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 public void onServiceDisconnected(ComponentName name) {
1341 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001342 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 + " mCurIntent=" + mCurIntent);
1344 if (mCurMethod != null && mCurIntent != null
1345 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001346 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 // We consider this to be a new bind attempt, since the system
1348 // should now try to restart the service for us.
1349 mLastBindTime = SystemClock.uptimeMillis();
1350 mShowRequested = mInputShown;
1351 mInputShown = false;
1352 if (mCurClient != null) {
1353 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1354 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1355 }
1356 }
1357 }
1358 }
1359
satokf9f01002011-05-19 21:31:50 +09001360 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001362 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 long ident = Binder.clearCallingIdentity();
1364 try {
1365 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001366 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 return;
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 synchronized (mMethodMap) {
1371 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001373 if (mStatusBar != null) {
1374 mStatusBar.setIconVisibility("ime", false);
1375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001378 CharSequence contentDescription = null;
1379 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001380 // Use PackageManager to load label
1381 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001382 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001383 mIPackageManager.getApplicationInfo(packageName, 0,
1384 mSettings.getCurrentUserId()));
1385 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001386 /* ignore */
1387 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001388 if (mStatusBar != null) {
1389 mStatusBar.setIcon("ime", packageName, iconId, 0,
1390 contentDescription != null
1391 ? contentDescription.toString() : null);
1392 mStatusBar.setIconVisibility("ime", true);
1393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395 }
1396 } finally {
1397 Binder.restoreCallingIdentity(ident);
1398 }
1399 }
1400
satok7cfc0ed2011-06-20 21:29:36 +09001401 private boolean needsToShowImeSwitchOngoingNotification() {
1402 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001403 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001404 synchronized (mMethodMap) {
1405 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1406 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001407 if (N > 2) return true;
1408 if (N < 1) return false;
1409 int nonAuxCount = 0;
1410 int auxCount = 0;
1411 InputMethodSubtype nonAuxSubtype = null;
1412 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001413 for(int i = 0; i < N; ++i) {
1414 final InputMethodInfo imi = imis.get(i);
1415 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1416 imi, true);
1417 final int subtypeCount = subtypes.size();
1418 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001419 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001420 } else {
1421 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001422 final InputMethodSubtype subtype = subtypes.get(j);
1423 if (!subtype.isAuxiliary()) {
1424 ++nonAuxCount;
1425 nonAuxSubtype = subtype;
1426 } else {
1427 ++auxCount;
1428 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001429 }
1430 }
1431 }
satok7cfc0ed2011-06-20 21:29:36 +09001432 }
satokb6359412011-06-28 17:47:41 +09001433 if (nonAuxCount > 1 || auxCount > 1) {
1434 return true;
1435 } else if (nonAuxCount == 1 && auxCount == 1) {
1436 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001437 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1438 || auxSubtype.overridesImplicitlyEnabledSubtype()
1439 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001440 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1441 return false;
1442 }
1443 return true;
1444 }
1445 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001446 }
satok7cfc0ed2011-06-20 21:29:36 +09001447 }
1448
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001449 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001450 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001451 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001452 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001453 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001454 try {
1455 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001456 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001457 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001458 return;
1459 }
1460
1461 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001462 mImeWindowVis = vis;
1463 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001464 if (mStatusBar != null) {
1465 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1466 }
satok7cfc0ed2011-06-20 21:29:36 +09001467 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001468 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1469 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001470 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001471 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001472 final CharSequence title = mRes.getText(
1473 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001474 final CharSequence imiLabel = imi.loadLabel(pm);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 if (DEBUG) {
1476 Slog.d(TAG, "--- imiLabel = " + imiLabel);
1477 }
satok5bc8e732011-07-22 21:07:23 +09001478 final CharSequence summary = mCurrentSubtype != null
1479 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1480 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1481 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001482 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001483 : imiLabel;
1484
satok7cfc0ed2011-06-20 21:29:36 +09001485 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001486 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001487 if (mNotificationManager != null) {
1488 mNotificationManager.notify(
1489 com.android.internal.R.string.select_input_method,
1490 mImeSwitcherNotification);
1491 mNotificationShown = true;
1492 }
satok7cfc0ed2011-06-20 21:29:36 +09001493 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001494 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001495 mNotificationManager.cancel(
1496 com.android.internal.R.string.select_input_method);
1497 mNotificationShown = false;
1498 }
1499 }
satok06487a52010-10-29 11:37:18 +09001500 }
1501 } finally {
1502 Binder.restoreCallingIdentity(ident);
1503 }
1504 }
1505
satoke7c6998e2011-06-03 17:57:59 +09001506 @Override
satokf9f01002011-05-19 21:31:50 +09001507 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 if (!calledFromValidUser()) {
1509 return;
1510 }
satokf9f01002011-05-19 21:31:50 +09001511 synchronized (mMethodMap) {
1512 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1513 for (int i = 0; i < spans.length; ++i) {
1514 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001515 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001516 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001517 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001518 }
1519 }
1520 }
1521 }
1522
satoke7c6998e2011-06-03 17:57:59 +09001523 @Override
satokf9f01002011-05-19 21:31:50 +09001524 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001525 if (!calledFromValidUser()) {
1526 return false;
1527 }
satokf9f01002011-05-19 21:31:50 +09001528 synchronized (mMethodMap) {
1529 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1530 // TODO: Do not send the intent if the process of the targetImi is already dead.
1531 if (targetImi != null) {
1532 final String[] suggestions = span.getSuggestions();
1533 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001534 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001535 final Intent intent = new Intent();
1536 // Ensures that only a class in the original IME package will receive the
1537 // notification.
satok42c5a162011-05-26 16:46:14 +09001538 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001539 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1540 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1541 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1542 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1543 mContext.sendBroadcast(intent);
1544 return true;
1545 }
1546 }
1547 return false;
1548 }
1549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001551 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1552 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1553 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1554 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001555 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001556 // There is no input method selected, try to choose new applicable input method.
1557 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001558 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001559 }
1560 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 try {
satokab751aa2010-09-14 19:17:36 +09001562 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001564 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001565 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001566 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
satokf3db1af2010-11-23 13:34:33 +09001568 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001569 } else {
1570 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001571 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001572 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
satokab751aa2010-09-14 19:17:36 +09001576 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 InputMethodInfo info = mMethodMap.get(id);
1578 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001579 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
satokd81e9502012-05-21 12:58:45 +09001582 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001584 final int subtypeCount = info.getSubtypeCount();
1585 if (subtypeCount <= 0) {
1586 return;
satokcd7cd292010-11-20 15:46:23 +09001587 }
satokd81e9502012-05-21 12:58:45 +09001588 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1589 final InputMethodSubtype newSubtype;
1590 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1591 newSubtype = info.getSubtypeAt(subtypeId);
1592 } else {
1593 // If subtype is null, try to find the most applicable one from
1594 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001595 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001596 }
1597 if (newSubtype == null || oldSubtype == null) {
1598 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1599 + ", new subtype = " + newSubtype);
1600 return;
1601 }
1602 if (newSubtype != oldSubtype) {
1603 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1604 if (mCurMethod != null) {
1605 try {
1606 refreshImeWindowVisibilityLocked();
1607 mCurMethod.changeInputMethodSubtype(newSubtype);
1608 } catch (RemoteException e) {
1609 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001610 }
1611 }
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 return;
1614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001615
satokd81e9502012-05-21 12:58:45 +09001616 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 final long ident = Binder.clearCallingIdentity();
1618 try {
satokab751aa2010-09-14 19:17:36 +09001619 // Set a subtype to this input method.
1620 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001621 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1622 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1623 // because mCurMethodId is stored as a history in
1624 // setSelectedInputMethodAndSubtypeLocked().
1625 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626
1627 if (ActivityManagerNative.isSystemReady()) {
1628 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001629 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 intent.putExtra("input_method_id", id);
1631 mContext.sendBroadcast(intent);
1632 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001633 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 } finally {
1635 Binder.restoreCallingIdentity(ident);
1636 }
1637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
satok42c5a162011-05-26 16:46:14 +09001639 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001640 public boolean showSoftInput(IInputMethodClient client, int flags,
1641 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001642 if (!calledFromValidUser()) {
1643 return false;
1644 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001645 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 long ident = Binder.clearCallingIdentity();
1647 try {
1648 synchronized (mMethodMap) {
1649 if (mCurClient == null || client == null
1650 || mCurClient.client.asBinder() != client.asBinder()) {
1651 try {
1652 // We need to check if this is the current client with
1653 // focus in the window manager, to allow this call to
1654 // be made before input is started in it.
1655 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001656 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001660 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001663
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001665 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667 } finally {
1668 Binder.restoreCallingIdentity(ident);
1669 }
1670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
The Android Open Source Project4df24232009-03-05 14:34:35 -08001672 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 mShowRequested = true;
1674 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1675 mShowExplicitlyRequested = true;
1676 }
1677 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1678 mShowExplicitlyRequested = true;
1679 mShowForced = true;
1680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001681
Dianne Hackborncc278702009-09-02 23:07:23 -07001682 if (!mSystemReady) {
1683 return false;
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001688 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1689 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1690 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001692 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001693 bindCurrentInputMethodService(
1694 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001695 mVisibleBound = true;
1696 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001697 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001699 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 // The client has asked to have the input method shown, but
1701 // we have been sitting here too long with a connection to the
1702 // service and no interface received, so let's disconnect/connect
1703 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001706 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001708 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001709 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001710 } else {
1711 if (DEBUG) {
1712 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1713 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001716
The Android Open Source Project4df24232009-03-05 14:34:35 -08001717 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
satok42c5a162011-05-26 16:46:14 +09001720 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001721 public boolean hideSoftInput(IInputMethodClient client, int flags,
1722 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001723 if (!calledFromValidUser()) {
1724 return false;
1725 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001726 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 long ident = Binder.clearCallingIdentity();
1728 try {
1729 synchronized (mMethodMap) {
1730 if (mCurClient == null || client == null
1731 || mCurClient.client.asBinder() != client.asBinder()) {
1732 try {
1733 // We need to check if this is the current client with
1734 // focus in the window manager, to allow this call to
1735 // be made before input is started in it.
1736 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001737 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1738 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001739 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001740 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001743 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001744 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
1746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
Joe Onorato8a9b2202010-02-26 18:56:32 -08001748 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001749 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751 } finally {
1752 Binder.restoreCallingIdentity(ident);
1753 }
1754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1758 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001759 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001761 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001764 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001766 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001768 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1771 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1772 res = true;
1773 } else {
1774 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001776 if (mHaveConnection && mVisibleBound) {
1777 mContext.unbindService(mVisibleConnection);
1778 mVisibleBound = false;
1779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 mInputShown = false;
1781 mShowRequested = false;
1782 mShowExplicitlyRequested = false;
1783 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001786
satok42c5a162011-05-26 16:46:14 +09001787 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001788 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1789 int controlFlags, int softInputMode, int windowFlags,
1790 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001791 if (!calledFromValidUser()) {
1792 return null;
1793 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001794 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 long ident = Binder.clearCallingIdentity();
1796 try {
1797 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001798 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001799 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001801 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
Dianne Hackborn7663d802012-02-24 13:08:49 -08001803 ClientState cs = mClients.get(client.asBinder());
1804 if (cs == null) {
1805 throw new IllegalArgumentException("unknown client "
1806 + client.asBinder());
1807 }
1808
1809 try {
1810 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1811 // Check with the window manager to make sure this client actually
1812 // has a window with focus. If not, reject. This is thread safe
1813 // because if the focus changes some time before or after, the
1814 // next client receiving focus that has any interest in input will
1815 // be calling through here after that change happens.
1816 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1817 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1818 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001820 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001823 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001824 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
1825 + " attribute=" + attribute);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001826 if (attribute != null) {
1827 return startInputUncheckedLocked(cs, inputContext, attribute,
1828 controlFlags);
1829 }
1830 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001831 }
1832 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001834 // Should we auto-show the IME even if the caller has not
1835 // specified what should be done with it?
1836 // We only do this automatically if the window can resize
1837 // to accommodate the IME (so what the user sees will give
1838 // them good context without input information being obscured
1839 // by the IME) or if running on a large screen where there
1840 // is more room for the target window + IME.
1841 final boolean doAutoShow =
1842 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1843 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1844 || mRes.getConfiguration().isLayoutSizeAtLeast(
1845 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001846 final boolean isTextEditor =
1847 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1848
1849 // We want to start input before showing the IME, but after closing
1850 // it. We want to do this after closing it to help the IME disappear
1851 // more quickly (not get stuck behind it initializing itself for the
1852 // new focused input, even if its window wants to hide the IME).
1853 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1856 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001857 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1859 // There is no focus view, and this window will
1860 // be behind any soft input window, so hide the
1861 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001862 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001865 } else if (isTextEditor && doAutoShow && (softInputMode &
1866 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 // There is a focus view, and we are navigating forward
1868 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001869 // We only do this automatically if the window can resize
1870 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001871 // them good context without input information being obscured
1872 // by the IME) or if running on a large screen where there
1873 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001874 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001875 if (attribute != null) {
1876 res = startInputUncheckedLocked(cs, inputContext, attribute,
1877 controlFlags);
1878 didStart = true;
1879 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001880 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882 break;
1883 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1884 // Do nothing.
1885 break;
1886 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1887 if ((softInputMode &
1888 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001889 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001890 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
1892 break;
1893 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001894 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001895 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 break;
1897 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1898 if ((softInputMode &
1899 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001900 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001901 if (attribute != null) {
1902 res = startInputUncheckedLocked(cs, inputContext, attribute,
1903 controlFlags);
1904 didStart = true;
1905 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001906 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 break;
1909 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001911 if (attribute != null) {
1912 res = startInputUncheckedLocked(cs, inputContext, attribute,
1913 controlFlags);
1914 didStart = true;
1915 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001916 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 break;
1918 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001919
1920 if (!didStart && attribute != null) {
1921 res = startInputUncheckedLocked(cs, inputContext, attribute,
1922 controlFlags);
1923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
1925 } finally {
1926 Binder.restoreCallingIdentity(ident);
1927 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001928
1929 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001931
satok42c5a162011-05-26 16:46:14 +09001932 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001934 if (!calledFromValidUser()) {
1935 return;
1936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 synchronized (mMethodMap) {
1938 if (mCurClient == null || client == null
1939 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001940 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001941 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
1943
satok440aab52010-11-25 09:43:11 +09001944 // Always call subtype picker, because subtype picker is a superset of input method
1945 // picker.
satokab751aa2010-09-14 19:17:36 +09001946 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1947 }
1948 }
1949
satok42c5a162011-05-26 16:46:14 +09001950 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001952 if (!calledFromValidUser()) {
1953 return;
1954 }
satok28203512010-11-24 11:06:49 +09001955 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1956 }
1957
satok42c5a162011-05-26 16:46:14 +09001958 @Override
satok28203512010-11-24 11:06:49 +09001959 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001960 if (!calledFromValidUser()) {
1961 return;
1962 }
satok28203512010-11-24 11:06:49 +09001963 synchronized (mMethodMap) {
1964 if (subtype != null) {
1965 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1966 mMethodMap.get(id), subtype.hashCode()));
1967 } else {
1968 setInputMethod(token, id);
1969 }
1970 }
satokab751aa2010-09-14 19:17:36 +09001971 }
1972
satok42c5a162011-05-26 16:46:14 +09001973 @Override
satokb416a712010-11-25 20:42:14 +09001974 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001975 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001976 if (!calledFromValidUser()) {
1977 return;
1978 }
satokb416a712010-11-25 20:42:14 +09001979 synchronized (mMethodMap) {
1980 if (mCurClient == null || client == null
1981 || mCurClient.client.asBinder() != client.asBinder()) {
1982 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1983 }
satok7fee71f2010-12-17 18:54:26 +09001984 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1985 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001986 }
1987 }
1988
satok4fc87d62011-05-20 16:13:43 +09001989 @Override
satok735cf382010-11-11 20:40:09 +09001990 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001991 if (!calledFromValidUser()) {
1992 return false;
1993 }
satok735cf382010-11-11 20:40:09 +09001994 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001995 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001996 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001997 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001998 lastImi = mMethodMap.get(lastIme.first);
1999 } else {
2000 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002001 }
satok4fc87d62011-05-20 16:13:43 +09002002 String targetLastImiId = null;
2003 int subtypeId = NOT_A_SUBTYPE_ID;
2004 if (lastIme != null && lastImi != null) {
2005 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2006 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2007 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2008 : mCurrentSubtype.hashCode();
2009 // If the last IME is the same as the current IME and the last subtype is not
2010 // defined, there is no need to switch to the last IME.
2011 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2012 targetLastImiId = lastIme.first;
2013 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2014 }
2015 }
2016
2017 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2018 // This is a safety net. If the currentSubtype can't be added to the history
2019 // and the framework couldn't find the last ime, we will make the last ime be
2020 // the most applicable enabled keyboard subtype of the system imes.
2021 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2022 if (enabled != null) {
2023 final int N = enabled.size();
2024 final String locale = mCurrentSubtype == null
2025 ? mRes.getConfiguration().locale.toString()
2026 : mCurrentSubtype.getLocale();
2027 for (int i = 0; i < N; ++i) {
2028 final InputMethodInfo imi = enabled.get(i);
2029 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2030 InputMethodSubtype keyboardSubtype =
2031 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2032 SUBTYPE_MODE_KEYBOARD, locale, true);
2033 if (keyboardSubtype != null) {
2034 targetLastImiId = imi.getId();
2035 subtypeId = getSubtypeIdFromHashCode(
2036 imi, keyboardSubtype.hashCode());
2037 if(keyboardSubtype.getLocale().equals(locale)) {
2038 break;
2039 }
2040 }
2041 }
2042 }
2043 }
2044 }
2045
2046 if (!TextUtils.isEmpty(targetLastImiId)) {
2047 if (DEBUG) {
2048 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2049 + ", from: " + mCurMethodId + ", " + subtypeId);
2050 }
2051 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2052 return true;
2053 } else {
2054 return false;
2055 }
satok735cf382010-11-11 20:40:09 +09002056 }
2057 }
2058
satoke7c6998e2011-06-03 17:57:59 +09002059 @Override
satok688bd472012-02-09 20:09:17 +09002060 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002061 if (!calledFromValidUser()) {
2062 return false;
2063 }
satok688bd472012-02-09 20:09:17 +09002064 synchronized (mMethodMap) {
2065 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2066 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2067 if (nextSubtype == null) {
2068 return false;
2069 }
2070 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2071 return true;
2072 }
2073 }
2074
2075 @Override
satok68f1b782011-04-11 14:26:04 +09002076 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002077 if (!calledFromValidUser()) {
2078 return null;
2079 }
satok68f1b782011-04-11 14:26:04 +09002080 synchronized (mMethodMap) {
2081 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2082 // TODO: Handle the case of the last IME with no subtypes
2083 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2084 || TextUtils.isEmpty(lastIme.second)) return null;
2085 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2086 if (lastImi == null) return null;
2087 try {
2088 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002089 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2090 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2091 return null;
2092 }
2093 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002094 } catch (NumberFormatException e) {
2095 return null;
2096 }
2097 }
2098 }
2099
satoke7c6998e2011-06-03 17:57:59 +09002100 @Override
satokee5e77c2011-09-02 18:50:15 +09002101 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002102 if (!calledFromValidUser()) {
2103 return;
2104 }
satok91e88122011-07-18 11:11:42 +09002105 // By this IPC call, only a process which shares the same uid with the IME can add
2106 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002107 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002108 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002109 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002110 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002111 final String[] packageInfos;
2112 try {
2113 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2114 } catch (RemoteException e) {
2115 Slog.e(TAG, "Failed to get package infos");
2116 return;
2117 }
satok91e88122011-07-18 11:11:42 +09002118 if (packageInfos != null) {
2119 final int packageNum = packageInfos.length;
2120 for (int i = 0; i < packageNum; ++i) {
2121 if (packageInfos[i].equals(imi.getPackageName())) {
2122 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002123 final long ident = Binder.clearCallingIdentity();
2124 try {
2125 buildInputMethodListLocked(mMethodList, mMethodMap);
2126 } finally {
2127 Binder.restoreCallingIdentity(ident);
2128 }
satokee5e77c2011-09-02 18:50:15 +09002129 return;
satok91e88122011-07-18 11:11:42 +09002130 }
2131 }
2132 }
satoke7c6998e2011-06-03 17:57:59 +09002133 }
satokee5e77c2011-09-02 18:50:15 +09002134 return;
satoke7c6998e2011-06-03 17:57:59 +09002135 }
2136
satok28203512010-11-24 11:06:49 +09002137 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 synchronized (mMethodMap) {
2139 if (token == null) {
2140 if (mContext.checkCallingOrSelfPermission(
2141 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2142 != PackageManager.PERMISSION_GRANTED) {
2143 throw new SecurityException(
2144 "Using null token requires permission "
2145 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2146 }
2147 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002148 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2149 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 return;
2151 }
2152
satokc5933802011-08-31 21:26:04 +09002153 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 try {
satokab751aa2010-09-14 19:17:36 +09002155 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 } finally {
2157 Binder.restoreCallingIdentity(ident);
2158 }
2159 }
2160 }
2161
satok42c5a162011-05-26 16:46:14 +09002162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002164 if (!calledFromValidUser()) {
2165 return;
2166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 synchronized (mMethodMap) {
2168 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002169 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2170 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 return;
2172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 long ident = Binder.clearCallingIdentity();
2174 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 hideCurrentInputLocked(flags, null);
2176 } finally {
2177 Binder.restoreCallingIdentity(ident);
2178 }
2179 }
2180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002181
satok42c5a162011-05-26 16:46:14 +09002182 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002184 if (!calledFromValidUser()) {
2185 return;
2186 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 synchronized (mMethodMap) {
2188 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002189 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2190 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002191 return;
2192 }
2193 long ident = Binder.clearCallingIdentity();
2194 try {
2195 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 } finally {
2197 Binder.restoreCallingIdentity(ident);
2198 }
2199 }
2200 }
2201
2202 void setEnabledSessionInMainThread(SessionState session) {
2203 if (mEnabledSession != session) {
2204 if (mEnabledSession != null) {
2205 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002206 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 mEnabledSession.method.setSessionEnabled(
2208 mEnabledSession.session, false);
2209 } catch (RemoteException e) {
2210 }
2211 }
2212 mEnabledSession = session;
2213 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002214 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 session.method.setSessionEnabled(
2216 session.session, true);
2217 } catch (RemoteException e) {
2218 }
2219 }
2220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002221
satok42c5a162011-05-26 16:46:14 +09002222 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002224 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 switch (msg.what) {
2226 case MSG_SHOW_IM_PICKER:
2227 showInputMethodMenu();
2228 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002229
satokab751aa2010-09-14 19:17:36 +09002230 case MSG_SHOW_IM_SUBTYPE_PICKER:
2231 showInputMethodSubtypeMenu();
2232 return true;
2233
satok47a44912010-10-06 16:03:58 +09002234 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002235 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002236 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002237 args.recycle();
satok217f5482010-12-15 05:19:19 +09002238 return true;
2239
2240 case MSG_SHOW_IM_CONFIG:
2241 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002242 return true;
2243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 case MSG_UNBIND_INPUT:
2247 try {
2248 ((IInputMethod)msg.obj).unbindInput();
2249 } catch (RemoteException e) {
2250 // There is nothing interesting about the method dying.
2251 }
2252 return true;
2253 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002254 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 try {
2256 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2257 } catch (RemoteException e) {
2258 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002259 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 return true;
2261 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002262 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002264 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2265 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 } catch (RemoteException e) {
2267 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002268 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 return true;
2270 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002271 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002273 ((IInputMethod)args.arg1).hideSoftInput(0,
2274 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 } catch (RemoteException e) {
2276 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002277 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 return true;
2279 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002280 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2284 } catch (RemoteException e) {
2285 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002286 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 return true;
2288 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002289 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 try {
2291 ((IInputMethod)args.arg1).createSession(
2292 (IInputMethodCallback)args.arg2);
2293 } catch (RemoteException e) {
2294 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002295 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002300 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 try {
2302 SessionState session = (SessionState)args.arg1;
2303 setEnabledSessionInMainThread(session);
2304 session.method.startInput((IInputContext)args.arg2,
2305 (EditorInfo)args.arg3);
2306 } catch (RemoteException e) {
2307 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002308 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 return true;
2310 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002311 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 try {
2313 SessionState session = (SessionState)args.arg1;
2314 setEnabledSessionInMainThread(session);
2315 session.method.restartInput((IInputContext)args.arg2,
2316 (EditorInfo)args.arg3);
2317 } catch (RemoteException e) {
2318 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002319 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 case MSG_UNBIND_METHOD:
2325 try {
2326 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2327 } catch (RemoteException e) {
2328 // There is nothing interesting about the last client dying.
2329 }
2330 return true;
2331 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002332 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 try {
2334 ((IInputMethodClient)args.arg1).onBindMethod(
2335 (InputBindResult)args.arg2);
2336 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002337 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002339 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002341 case MSG_SET_ACTIVE:
2342 try {
2343 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2344 } catch (RemoteException e) {
2345 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2346 + ((ClientState)msg.obj).pid + " uid "
2347 + ((ClientState)msg.obj).uid);
2348 }
2349 return true;
satok01038492012-04-09 21:08:27 +09002350
2351 // --------------------------------------------------------------
2352 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2353 mHardKeyboardListener.handleHardKeyboardStatusChange(
2354 msg.arg1 == 1, msg.arg2 == 1);
2355 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357 return false;
2358 }
2359
satok5b927c432012-05-01 20:09:34 +09002360 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002361 return (inputMethod.getServiceInfo().applicationInfo.flags
2362 & ApplicationInfo.FLAG_SYSTEM) != 0;
2363 }
2364
Ken Wakasa586f0512011-01-20 22:31:01 +09002365 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2366 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2367 final int subtypeCount = imi.getSubtypeCount();
2368 for (int i = 0; i < subtypeCount; ++i) {
2369 subtypes.add(imi.getSubtypeAt(i));
2370 }
2371 return subtypes;
2372 }
2373
satoka86f5e42011-09-02 17:12:42 +09002374 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2375 InputMethodInfo imi, String mode) {
2376 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2377 final int subtypeCount = imi.getSubtypeCount();
2378 for (int i = 0; i < subtypeCount; ++i) {
2379 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2380 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2381 subtypes.add(subtype);
2382 }
2383 }
2384 return subtypes;
2385 }
2386
satokdc9ddae2011-10-06 12:22:36 +09002387 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002388 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002389 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002390 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002391 int i = enabled.size();
2392 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002393 while (i > 0) {
2394 i--;
satokdc9ddae2011-10-06 12:22:36 +09002395 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002396 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2397 return imi;
2398 }
2399 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2400 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002401 }
2402 }
satok0a1bcf42012-05-16 19:26:31 +09002403 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002404 }
2405 return null;
2406 }
2407
2408 private boolean chooseNewDefaultIMELocked() {
2409 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2410 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002411 if (DEBUG) {
2412 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2413 }
satok723a27e2010-11-11 14:58:11 +09002414 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002415 return true;
2416 }
2417
2418 return false;
2419 }
2420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2422 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002423 if (DEBUG) {
2424 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 list.clear();
2427 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002428
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002429 // Use for queryIntentServicesAsUser
2430 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002431 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002432 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002433 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002434 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002436 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002438 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002439
satoke7c6998e2011-06-03 17:57:59 +09002440 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2441 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 for (int i = 0; i < services.size(); ++i) {
2443 ResolveInfo ri = services.get(i);
2444 ServiceInfo si = ri.serviceInfo;
2445 ComponentName compName = new ComponentName(si.packageName, si.name);
2446 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2447 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002448 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 + ": it does not require the permission "
2450 + android.Manifest.permission.BIND_INPUT_METHOD);
2451 continue;
2452 }
2453
Joe Onorato8a9b2202010-02-26 18:56:32 -08002454 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455
2456 try {
satoke7c6998e2011-06-03 17:57:59 +09002457 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002459 final String id = p.getId();
2460 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461
satok5b927c432012-05-01 20:09:34 +09002462 // Valid system default IMEs and IMEs that have English subtypes are enabled
2463 // by default, unless there's a hard keyboard and the system IME was explicitly
2464 // disabled
2465 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2466 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002467 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002468 }
2469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002471 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002475 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002477 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 }
2479 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002480
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002481 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002482 if (!TextUtils.isEmpty(defaultImiId)) {
2483 if (!map.containsKey(defaultImiId)) {
2484 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2485 if (chooseNewDefaultIMELocked()) {
2486 updateFromSettingsLocked();
2487 }
2488 } else {
2489 // Double check that the default IME is certainly enabled.
2490 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002491 }
2492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
satokab751aa2010-09-14 19:17:36 +09002497 private void showInputMethodMenu() {
2498 showInputMethodMenuInternal(false);
2499 }
2500
2501 private void showInputMethodSubtypeMenu() {
2502 showInputMethodMenuInternal(true);
2503 }
2504
satok217f5482010-12-15 05:19:19 +09002505 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002506 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002507 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002508 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2509 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002510 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002511 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002512 }
satok217f5482010-12-15 05:19:19 +09002513 mContext.startActivity(intent);
2514 }
2515
2516 private void showConfigureInputMethods() {
2517 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2518 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2519 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2520 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002521 mContext.startActivity(intent);
2522 }
2523
satok2c93efc2012-04-02 19:33:47 +09002524 private boolean isScreenLocked() {
2525 return mKeyguardManager != null
2526 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2527 }
satokab751aa2010-09-14 19:17:36 +09002528 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002529 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002532 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002533
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002535 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002536 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002537
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002538 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002539 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2540 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002541 if (immis == null || immis.size() == 0) {
2542 return;
2543 }
2544
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002545 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546
satok688bd472012-02-09 20:09:17 +09002547 final List<ImeSubtypeListItem> imList =
2548 mImListManager.getSortedInputMethodAndSubtypeList(
2549 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002550
satokc3690562012-01-10 20:14:43 +09002551 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002552 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002553 if (currentSubtype != null) {
2554 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2555 lastInputMethodSubtypeId =
2556 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2557 }
2558 }
2559
Ken Wakasa761eb372011-03-04 19:06:18 +09002560 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002561 mIms = new InputMethodInfo[N];
2562 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002563 int checkedItem = 0;
2564 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002565 final ImeSubtypeListItem item = imList.get(i);
2566 mIms[i] = item.mImi;
2567 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002568 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002569 int subtypeId = mSubtypeIds[i];
2570 if ((subtypeId == NOT_A_SUBTYPE_ID)
2571 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2572 || (subtypeId == lastInputMethodSubtypeId)) {
2573 checkedItem = i;
2574 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002577 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002578 com.android.internal.R.styleable.DialogPreference,
2579 com.android.internal.R.attr.alertDialogStyle, 0);
2580 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002581 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002582 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002583 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002585 }
2586 })
2587 .setIcon(a.getDrawable(
2588 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2589 a.recycle();
satok01038492012-04-09 21:08:27 +09002590 final LayoutInflater inflater =
2591 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2592 final View tv = inflater.inflate(
2593 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2594 mDialogBuilder.setCustomTitle(tv);
2595
2596 // Setup layout for a toggle switch of the hardware keyboard
2597 mSwitchingDialogTitleView = tv;
2598 mSwitchingDialogTitleView.findViewById(
2599 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2600 mWindowManagerService.isHardKeyboardAvailable() ?
2601 View.VISIBLE : View.GONE);
2602 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2603 com.android.internal.R.id.hard_keyboard_switch));
2604 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2605 hardKeySwitch.setOnCheckedChangeListener(
2606 new OnCheckedChangeListener() {
2607 @Override
2608 public void onCheckedChanged(
2609 CompoundButton buttonView, boolean isChecked) {
2610 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2611 }
2612 });
satokd87c2592010-09-29 11:52:06 +09002613
Ken Wakasa05dbb652011-08-22 15:22:43 +09002614 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2615 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2616 checkedItem);
2617
2618 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002620 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002621 public void onClick(DialogInterface dialog, int which) {
2622 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002623 if (mIms == null || mIms.length <= which
2624 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 return;
2626 }
2627 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002628 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002629 hideInputMethodMenu();
2630 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002631 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002632 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002633 subtypeId = NOT_A_SUBTYPE_ID;
2634 }
2635 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002636 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002639 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640
satokbc81b692011-08-26 16:22:22 +09002641 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002642 mDialogBuilder.setPositiveButton(
2643 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002644 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002645 @Override
satok7f35c8c2010-10-07 21:13:11 +09002646 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002647 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002648 }
2649 });
2650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002652 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 mSwitchingDialog.getWindow().setType(
2654 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002655 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 mSwitchingDialog.show();
2657 }
2658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002659
satok93d744d2012-05-09 17:14:08 +09002660 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002661 public final CharSequence mImeName;
2662 public final CharSequence mSubtypeName;
2663 public final InputMethodInfo mImi;
2664 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002665 private final boolean mIsSystemLocale;
2666 private final boolean mIsSystemLanguage;
2667
Ken Wakasa05dbb652011-08-22 15:22:43 +09002668 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002669 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002670 mImeName = imeName;
2671 mSubtypeName = subtypeName;
2672 mImi = imi;
2673 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002674 if (TextUtils.isEmpty(subtypeLocale)) {
2675 mIsSystemLocale = false;
2676 mIsSystemLanguage = false;
2677 } else {
2678 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2679 mIsSystemLanguage = mIsSystemLocale
2680 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2681 }
2682 }
2683
2684 @Override
2685 public int compareTo(ImeSubtypeListItem other) {
2686 if (TextUtils.isEmpty(mImeName)) {
2687 return 1;
2688 }
2689 if (TextUtils.isEmpty(other.mImeName)) {
2690 return -1;
2691 }
2692 if (!TextUtils.equals(mImeName, other.mImeName)) {
2693 return mImeName.toString().compareTo(other.mImeName.toString());
2694 }
2695 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2696 return 0;
2697 }
2698 if (mIsSystemLocale) {
2699 return -1;
2700 }
2701 if (other.mIsSystemLocale) {
2702 return 1;
2703 }
2704 if (mIsSystemLanguage) {
2705 return -1;
2706 }
2707 if (other.mIsSystemLanguage) {
2708 return 1;
2709 }
2710 if (TextUtils.isEmpty(mSubtypeName)) {
2711 return 1;
2712 }
2713 if (TextUtils.isEmpty(other.mSubtypeName)) {
2714 return -1;
2715 }
2716 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002717 }
2718 }
2719
2720 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2721 private final LayoutInflater mInflater;
2722 private final int mTextViewResourceId;
2723 private final List<ImeSubtypeListItem> mItemsList;
2724 private final int mCheckedItem;
2725 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2726 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2727 super(context, textViewResourceId, itemsList);
2728 mTextViewResourceId = textViewResourceId;
2729 mItemsList = itemsList;
2730 mCheckedItem = checkedItem;
2731 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2732 }
2733
2734 @Override
2735 public View getView(int position, View convertView, ViewGroup parent) {
2736 final View view = convertView != null ? convertView
2737 : mInflater.inflate(mTextViewResourceId, null);
2738 if (position < 0 || position >= mItemsList.size()) return view;
2739 final ImeSubtypeListItem item = mItemsList.get(position);
2740 final CharSequence imeName = item.mImeName;
2741 final CharSequence subtypeName = item.mSubtypeName;
2742 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2743 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2744 if (TextUtils.isEmpty(subtypeName)) {
2745 firstTextView.setText(imeName);
2746 secondTextView.setVisibility(View.GONE);
2747 } else {
2748 firstTextView.setText(subtypeName);
2749 secondTextView.setText(imeName);
2750 secondTextView.setVisibility(View.VISIBLE);
2751 }
2752 final RadioButton radioButton =
2753 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2754 radioButton.setChecked(position == mCheckedItem);
2755 return view;
2756 }
2757 }
2758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002760 synchronized (mMethodMap) {
2761 hideInputMethodMenuLocked();
2762 }
2763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002764
The Android Open Source Project10592532009-03-18 17:39:46 -07002765 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002766 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
The Android Open Source Project10592532009-03-18 17:39:46 -07002768 if (mSwitchingDialog != null) {
2769 mSwitchingDialog.dismiss();
2770 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002772
The Android Open Source Project10592532009-03-18 17:39:46 -07002773 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002774 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002778
satok42c5a162011-05-26 16:46:14 +09002779 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002781 // TODO: Make this work even for non-current users?
2782 if (!calledFromValidUser()) {
2783 return false;
2784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 synchronized (mMethodMap) {
2786 if (mContext.checkCallingOrSelfPermission(
2787 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2788 != PackageManager.PERMISSION_GRANTED) {
2789 throw new SecurityException(
2790 "Requires permission "
2791 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2792 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 long ident = Binder.clearCallingIdentity();
2795 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002796 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 } finally {
2798 Binder.restoreCallingIdentity(ident);
2799 }
2800 }
2801 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002802
2803 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2804 // Make sure this is a valid input method.
2805 InputMethodInfo imm = mMethodMap.get(id);
2806 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002807 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002808 }
2809
satokd87c2592010-09-29 11:52:06 +09002810 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2811 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002812
satokd87c2592010-09-29 11:52:06 +09002813 if (enabled) {
2814 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2815 if (pair.first.equals(id)) {
2816 // We are enabling this input method, but it is already enabled.
2817 // Nothing to do. The previous state was enabled.
2818 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002819 }
2820 }
satokd87c2592010-09-29 11:52:06 +09002821 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2822 // Previous state was disabled.
2823 return false;
2824 } else {
2825 StringBuilder builder = new StringBuilder();
2826 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2827 builder, enabledInputMethodsList, id)) {
2828 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002829 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002830 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2831 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2832 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002833 }
2834 // Previous state was enabled.
2835 return true;
2836 } else {
2837 // We are disabling the input method but it is already disabled.
2838 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002839 return false;
2840 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002841 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002843
satok57ffc002011-01-25 00:11:47 +09002844 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2845 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002846 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002847 }
2848
satok723a27e2010-11-11 14:58:11 +09002849 private void saveCurrentInputMethodAndSubtypeToHistory() {
2850 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2851 if (mCurrentSubtype != null) {
2852 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2853 }
satok57ffc002011-01-25 00:11:47 +09002854 if (canAddToLastInputMethod(mCurrentSubtype)) {
2855 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2856 }
satokab751aa2010-09-14 19:17:36 +09002857 }
2858
satok723a27e2010-11-11 14:58:11 +09002859 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2860 boolean setSubtypeOnly) {
2861 // Update the history of InputMethod and Subtype
2862 saveCurrentInputMethodAndSubtypeToHistory();
2863
2864 // Set Subtype here
2865 if (imi == null || subtypeId < 0) {
2866 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002867 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002868 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002869 if (subtypeId < imi.getSubtypeCount()) {
2870 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2871 mSettings.putSelectedSubtype(subtype.hashCode());
2872 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002873 } else {
2874 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002875 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002876 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002877 }
satokab751aa2010-09-14 19:17:36 +09002878 }
satok723a27e2010-11-11 14:58:11 +09002879
satok4c0e7152012-06-20 20:08:44 +09002880 // Workaround.
2881 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2882 // IMEs are not recognized and considered uninstalled.
2883 // Actually, we can't move everything after SystemReady because
2884 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2885 // the default IME here and try cheking the default IME again in systemReady().
2886 // TODO: Do nothing before system ready and implement a separated logic for
2887 // the encryption lock screen.
2888 // TODO: ASEC should be ready before IMMS is instantiated.
2889 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002890 // Set InputMethod here
2891 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2892 }
2893 }
2894
2895 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2896 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2897 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2898 // newDefaultIme is empty when there is no candidate for the selected IME.
2899 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2900 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2901 if (subtypeHashCode != null) {
2902 try {
2903 lastSubtypeId = getSubtypeIdFromHashCode(
2904 imi, Integer.valueOf(subtypeHashCode));
2905 } catch (NumberFormatException e) {
2906 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2907 }
2908 }
2909 }
2910 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002911 }
2912
2913 private int getSelectedInputMethodSubtypeId(String id) {
2914 InputMethodInfo imi = mMethodMap.get(id);
2915 if (imi == null) {
2916 return NOT_A_SUBTYPE_ID;
2917 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002918 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2919 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002920 }
2921
satokfdf419e2012-05-08 16:52:08 +09002922 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2923 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2924 }
2925
2926 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002927 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002928 final int subtypeCount = imi.getSubtypeCount();
2929 for (int i = 0; i < subtypeCount; ++i) {
2930 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002931 if (subtypeHashCode == ims.hashCode()) {
2932 return i;
2933 }
satokab751aa2010-09-14 19:17:36 +09002934 }
2935 }
2936 return NOT_A_SUBTYPE_ID;
2937 }
2938
satoka86f5e42011-09-02 17:12:42 +09002939 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2940 Resources res, InputMethodInfo imi) {
2941 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002942 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002943 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002944 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002945 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002946 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002947 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002948 // scan overriding implicitly enabled subtypes.
2949 InputMethodSubtype subtype = subtypes.get(i);
2950 if (subtype.overridesImplicitlyEnabledSubtype()) {
2951 final String mode = subtype.getMode();
2952 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2953 applicableModeAndSubtypesMap.put(mode, subtype);
2954 }
2955 }
2956 }
2957 if (applicableModeAndSubtypesMap.size() > 0) {
2958 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2959 }
2960 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002961 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002962 final String locale = subtype.getLocale();
2963 final String mode = subtype.getMode();
2964 // When system locale starts with subtype's locale, that subtype will be applicable
2965 // for system locale
2966 // For instance, it's clearly applicable for cases like system locale = en_US and
2967 // subtype = en, but it is not necessarily considered applicable for cases like system
2968 // locale = en and subtype = en_US.
2969 // We just call systemLocale.startsWith(locale) in this function because there is no
2970 // need to find applicable subtypes aggressively unlike
2971 // findLastResortApplicableSubtypeLocked.
2972 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002973 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002974 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002975 if (applicableSubtype != null) {
2976 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2977 if (!systemLocale.equals(locale)) continue;
2978 }
satok3da92232011-01-11 22:46:30 +09002979 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002980 }
2981 }
2982 final InputMethodSubtype keyboardSubtype
2983 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2984 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2985 applicableModeAndSubtypesMap.values());
2986 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2987 for (int i = 0; i < N; ++i) {
2988 final InputMethodSubtype subtype = subtypes.get(i);
2989 final String mode = subtype.getMode();
2990 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2991 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2992 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002993 }
2994 }
2995 }
satokc3690562012-01-10 20:14:43 +09002996 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002997 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002998 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002999 if (lastResortKeyboardSubtype != null) {
3000 applicableSubtypes.add(lastResortKeyboardSubtype);
3001 }
3002 }
3003 return applicableSubtypes;
3004 }
3005
satok4e4569d2010-11-19 18:45:53 +09003006 /**
3007 * If there are no selected subtypes, tries finding the most applicable one according to the
3008 * given locale.
3009 * @param subtypes this function will search the most applicable subtype in subtypes
3010 * @param mode subtypes will be filtered by mode
3011 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003012 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3013 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003014 * @return the most applicable subtypeId
3015 */
satokdf31ae62011-01-15 06:19:44 +09003016 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3017 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003018 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003019 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003020 return null;
satok8fbb1e82010-11-02 23:15:58 +09003021 }
satok4e4569d2010-11-19 18:45:53 +09003022 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003023 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003024 }
satok8fbb1e82010-11-02 23:15:58 +09003025 final String language = locale.substring(0, 2);
3026 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003027 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003028 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003029 final int N = subtypes.size();
3030 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003031 InputMethodSubtype subtype = subtypes.get(i);
3032 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003033 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3034 // and all subtypes with all modes can be candidates.
3035 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003036 if (firstMatchedModeSubtype == null) {
3037 firstMatchedModeSubtype = subtype;
3038 }
satok9ef02832010-11-04 21:17:48 +09003039 if (locale.equals(subtypeLocale)) {
3040 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003041 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003042 break;
3043 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3044 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003045 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003046 partialMatchFound = true;
3047 }
satok8fbb1e82010-11-02 23:15:58 +09003048 }
3049 }
3050
satok7599a7f2010-12-22 13:45:23 +09003051 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3052 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003053 }
3054
satok8fbb1e82010-11-02 23:15:58 +09003055 // The first subtype applicable to the system locale will be defined as the most applicable
3056 // subtype.
3057 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003058 if (applicableSubtype != null) {
3059 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3060 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3061 }
satok8fbb1e82010-11-02 23:15:58 +09003062 }
satokcd7cd292010-11-20 15:46:23 +09003063 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003064 }
3065
satok4e4569d2010-11-19 18:45:53 +09003066 // If there are no selected shortcuts, tries finding the most applicable ones.
3067 private Pair<InputMethodInfo, InputMethodSubtype>
3068 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3069 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3070 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003071 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003072 boolean foundInSystemIME = false;
3073
3074 // Search applicable subtype for each InputMethodInfo
3075 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003076 final String imiId = imi.getId();
3077 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3078 continue;
3079 }
satokcd7cd292010-11-20 15:46:23 +09003080 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003081 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003082 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003083 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003084 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003085 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003086 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003087 }
satokdf31ae62011-01-15 06:19:44 +09003088 // 2. Search by the system locale from enabledSubtypes.
3089 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003090 if (subtype == null) {
3091 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003092 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003093 }
satoka86f5e42011-09-02 17:12:42 +09003094 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3095 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3096 final ArrayList<InputMethodSubtype> subtypesForSearch =
3097 overridingImplicitlyEnabledSubtypes.isEmpty()
3098 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003099 // 4. Search by the current subtype's locale from all subtypes.
3100 if (subtype == null && mCurrentSubtype != null) {
3101 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003102 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003103 }
3104 // 5. Search by the system locale from all subtypes.
3105 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003106 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003107 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003108 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003109 }
satokcd7cd292010-11-20 15:46:23 +09003110 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003111 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003112 // The current input method is the most applicable IME.
3113 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003114 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003115 break;
satok7599a7f2010-12-22 13:45:23 +09003116 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003117 // The system input method is 2nd applicable IME.
3118 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003119 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003120 if ((imi.getServiceInfo().applicationInfo.flags
3121 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3122 foundInSystemIME = true;
3123 }
satok4e4569d2010-11-19 18:45:53 +09003124 }
3125 }
3126 }
3127 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003128 if (mostApplicableIMI != null) {
3129 Slog.w(TAG, "Most applicable shortcut input method was:"
3130 + mostApplicableIMI.getId());
3131 if (mostApplicableSubtype != null) {
3132 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3133 + "," + mostApplicableSubtype.getMode() + ","
3134 + mostApplicableSubtype.getLocale());
3135 }
3136 }
satok4e4569d2010-11-19 18:45:53 +09003137 }
satokcd7cd292010-11-20 15:46:23 +09003138 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003139 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003140 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003141 } else {
3142 return null;
3143 }
3144 }
3145
satokab751aa2010-09-14 19:17:36 +09003146 /**
3147 * @return Return the current subtype of this input method.
3148 */
satok42c5a162011-05-26 16:46:14 +09003149 @Override
satokab751aa2010-09-14 19:17:36 +09003150 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003151 // TODO: Make this work even for non-current users?
3152 if (!calledFromValidUser()) {
3153 return null;
3154 }
3155 synchronized (mMethodMap) {
3156 return getCurrentInputMethodSubtypeLocked();
3157 }
3158 }
3159
3160 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003161 if (mCurMethodId == null) {
3162 return null;
3163 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003164 final boolean subtypeIsSelected =
3165 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3166 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3167 if (imi == null || imi.getSubtypeCount() == 0) {
3168 return null;
satok4e4569d2010-11-19 18:45:53 +09003169 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003170 if (!subtypeIsSelected || mCurrentSubtype == null
3171 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3172 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3173 if (subtypeId == NOT_A_SUBTYPE_ID) {
3174 // If there are no selected subtypes, the framework will try to find
3175 // the most applicable subtype from explicitly or implicitly enabled
3176 // subtypes.
3177 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3178 getEnabledInputMethodSubtypeListLocked(imi, true);
3179 // If there is only one explicitly or implicitly enabled subtype,
3180 // just returns it.
3181 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3182 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3183 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3184 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3185 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3186 SUBTYPE_MODE_KEYBOARD, null, true);
3187 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003188 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003189 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3190 true);
satok4e4569d2010-11-19 18:45:53 +09003191 }
satok3ef8b292010-11-23 06:06:29 +09003192 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003193 } else {
3194 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003195 }
3196 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003197 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003198 }
3199
satokf3db1af2010-11-23 13:34:33 +09003200 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3201 InputMethodSubtype subtype) {
3202 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3203 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3204 } else {
3205 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3206 subtypes.add(subtype);
3207 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3208 }
3209 }
3210
satok4e4569d2010-11-19 18:45:53 +09003211 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003212 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003213 @Override
satok4e4569d2010-11-19 18:45:53 +09003214 public List getShortcutInputMethodsAndSubtypes() {
3215 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003216 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003217 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003218 // If there are no selected shortcut subtypes, the framework will try to find
3219 // the most applicable subtype from all subtypes whose mode is
3220 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003221 Pair<InputMethodInfo, InputMethodSubtype> info =
3222 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3223 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003224 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003225 ret.add(info.first);
3226 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003227 }
satok3da92232011-01-11 22:46:30 +09003228 return ret;
satokf3db1af2010-11-23 13:34:33 +09003229 }
satokf3db1af2010-11-23 13:34:33 +09003230 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3231 ret.add(imi);
3232 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3233 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003234 }
3235 }
satokf3db1af2010-11-23 13:34:33 +09003236 return ret;
satok4e4569d2010-11-19 18:45:53 +09003237 }
3238 }
3239
satok42c5a162011-05-26 16:46:14 +09003240 @Override
satokb66d2872010-11-10 01:04:04 +09003241 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003242 // TODO: Make this work even for non-current users?
3243 if (!calledFromValidUser()) {
3244 return false;
3245 }
satokb66d2872010-11-10 01:04:04 +09003246 synchronized (mMethodMap) {
3247 if (subtype != null && mCurMethodId != null) {
3248 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3249 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3250 if (subtypeId != NOT_A_SUBTYPE_ID) {
3251 setInputMethodLocked(mCurMethodId, subtypeId);
3252 return true;
3253 }
3254 }
3255 return false;
3256 }
3257 }
3258
satok688bd472012-02-09 20:09:17 +09003259 private static class InputMethodAndSubtypeListManager {
3260 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003261 // Used to load label
satok688bd472012-02-09 20:09:17 +09003262 private final PackageManager mPm;
3263 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003264 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003265 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3266 mContext = context;
3267 mPm = context.getPackageManager();
3268 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003269 final Locale locale = context.getResources().getConfiguration().locale;
3270 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003271 }
3272
3273 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3274 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3275 new Comparator<InputMethodInfo>() {
3276 @Override
3277 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3278 if (imi2 == null) return 0;
3279 if (imi1 == null) return 1;
3280 if (mPm == null) {
3281 return imi1.getId().compareTo(imi2.getId());
3282 }
3283 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3284 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3285 return imiId1.toString().compareTo(imiId2.toString());
3286 }
3287 });
3288
3289 public ImeSubtypeListItem getNextInputMethod(
3290 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3291 if (imi == null) {
3292 return null;
3293 }
3294 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3295 if (imList.size() <= 1) {
3296 return null;
3297 }
3298 final int N = imList.size();
3299 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003300 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003301 : NOT_A_SUBTYPE_ID;
3302 for (int i = 0; i < N; ++i) {
3303 final ImeSubtypeListItem isli = imList.get(i);
3304 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3305 if (!onlyCurrentIme) {
3306 return imList.get((i + 1) % N);
3307 }
3308 for (int j = 0; j < N - 1; ++j) {
3309 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3310 if (candidate.mImi.equals(imi)) {
3311 return candidate;
3312 }
3313 }
3314 return null;
3315 }
3316 }
3317 return null;
3318 }
3319
3320 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3321 return getSortedInputMethodAndSubtypeList(true, false, false);
3322 }
3323
3324 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3325 boolean inputShown, boolean isScreenLocked) {
3326 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3327 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3328 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3329 if (immis == null || immis.size() == 0) {
3330 return Collections.emptyList();
3331 }
3332 mSortedImmis.clear();
3333 mSortedImmis.putAll(immis);
3334 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3335 if (imi == null) continue;
3336 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3337 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3338 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3339 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3340 }
3341 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3342 final CharSequence imeLabel = imi.loadLabel(mPm);
3343 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3344 final int subtypeCount = imi.getSubtypeCount();
3345 if (DEBUG) {
3346 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3347 }
3348 for (int j = 0; j < subtypeCount; ++j) {
3349 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3350 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3351 // We show all enabled IMEs and subtypes when an IME is shown.
3352 if (enabledSubtypeSet.contains(subtypeHashCode)
3353 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3354 final CharSequence subtypeLabel =
3355 subtype.overridesImplicitlyEnabledSubtype() ? null
3356 : subtype.getDisplayName(mContext, imi.getPackageName(),
3357 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003358 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3359 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003360
3361 // Removing this subtype from enabledSubtypeSet because we no longer
3362 // need to add an entry of this subtype to imList to avoid duplicated
3363 // entries.
3364 enabledSubtypeSet.remove(subtypeHashCode);
3365 }
3366 }
3367 } else {
satok93d744d2012-05-09 17:14:08 +09003368 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3369 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003370 }
3371 }
satok93d744d2012-05-09 17:14:08 +09003372 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003373 return imList;
3374 }
3375 }
3376
satokd87c2592010-09-29 11:52:06 +09003377 /**
3378 * Utility class for putting and getting settings for InputMethod
3379 * TODO: Move all putters and getters of settings to this class.
3380 */
3381 private static class InputMethodSettings {
3382 // The string for enabled input method is saved as follows:
3383 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3384 private static final char INPUT_METHOD_SEPARATER = ':';
3385 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003386 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003387 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3388
satok723a27e2010-11-11 14:58:11 +09003389 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003390 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3391
satokdf31ae62011-01-15 06:19:44 +09003392 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003393 private final ContentResolver mResolver;
3394 private final HashMap<String, InputMethodInfo> mMethodMap;
3395 private final ArrayList<InputMethodInfo> mMethodList;
3396
3397 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003398 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003399
3400 private static void buildEnabledInputMethodsSettingString(
3401 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3402 String id = pair.first;
3403 ArrayList<String> subtypes = pair.second;
3404 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003405 // Inputmethod and subtypes are saved in the settings as follows:
3406 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3407 for (String subtypeId: subtypes) {
3408 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003409 }
3410 }
3411
3412 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003413 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003414 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3415 int userId) {
3416 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003417 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003418 mResolver = resolver;
3419 mMethodMap = methodMap;
3420 mMethodList = methodList;
3421 }
3422
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003423 public void setCurrentUserId(int userId) {
3424 if (DEBUG) {
3425 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3426 + userId + ", new ime = " + getSelectedInputMethod());
3427 }
3428 // IMMS settings are kept per user, so keep track of current user
3429 mCurrentUserId = userId;
3430 }
3431
satokd87c2592010-09-29 11:52:06 +09003432 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3433 return createEnabledInputMethodListLocked(
3434 getEnabledInputMethodsAndSubtypeListLocked());
3435 }
3436
satok7f35c8c2010-10-07 21:13:11 +09003437 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003438 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3439 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003440 getEnabledInputMethodsAndSubtypeListLocked());
3441 }
3442
satok67ddf9c2010-11-17 09:45:54 +09003443 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3444 InputMethodInfo imi) {
3445 List<Pair<String, ArrayList<String>>> imsList =
3446 getEnabledInputMethodsAndSubtypeListLocked();
3447 ArrayList<InputMethodSubtype> enabledSubtypes =
3448 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003449 if (imi != null) {
3450 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3451 InputMethodInfo info = mMethodMap.get(imsPair.first);
3452 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003453 final int subtypeCount = info.getSubtypeCount();
3454 for (int i = 0; i < subtypeCount; ++i) {
3455 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003456 for (String s: imsPair.second) {
3457 if (String.valueOf(ims.hashCode()).equals(s)) {
3458 enabledSubtypes.add(ims);
3459 }
satok67ddf9c2010-11-17 09:45:54 +09003460 }
3461 }
satok884ef9a2010-11-18 10:39:46 +09003462 break;
satok67ddf9c2010-11-17 09:45:54 +09003463 }
satok67ddf9c2010-11-17 09:45:54 +09003464 }
3465 }
3466 return enabledSubtypes;
3467 }
3468
satokd87c2592010-09-29 11:52:06 +09003469 // At the initial boot, the settings for input methods are not set,
3470 // so we need to enable IME in that case.
3471 public void enableAllIMEsIfThereIsNoEnabledIME() {
3472 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3473 StringBuilder sb = new StringBuilder();
3474 final int N = mMethodList.size();
3475 for (int i = 0; i < N; i++) {
3476 InputMethodInfo imi = mMethodList.get(i);
3477 Slog.i(TAG, "Adding: " + imi.getId());
3478 if (i > 0) sb.append(':');
3479 sb.append(imi.getId());
3480 }
3481 putEnabledInputMethodsStr(sb.toString());
3482 }
3483 }
3484
satokbb4aa062011-01-19 21:40:27 +09003485 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003486 ArrayList<Pair<String, ArrayList<String>>> imsList
3487 = new ArrayList<Pair<String, ArrayList<String>>>();
3488 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3489 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3490 return imsList;
3491 }
satok723a27e2010-11-11 14:58:11 +09003492 mInputMethodSplitter.setString(enabledInputMethodsStr);
3493 while (mInputMethodSplitter.hasNext()) {
3494 String nextImsStr = mInputMethodSplitter.next();
3495 mSubtypeSplitter.setString(nextImsStr);
3496 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003497 ArrayList<String> subtypeHashes = new ArrayList<String>();
3498 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003499 String imeId = mSubtypeSplitter.next();
3500 while (mSubtypeSplitter.hasNext()) {
3501 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003502 }
3503 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3504 }
3505 }
3506 return imsList;
3507 }
3508
3509 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3510 if (reloadInputMethodStr) {
3511 getEnabledInputMethodsStr();
3512 }
3513 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3514 // Add in the newly enabled input method.
3515 putEnabledInputMethodsStr(id);
3516 } else {
3517 putEnabledInputMethodsStr(
3518 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3519 }
3520 }
3521
3522 /**
3523 * Build and put a string of EnabledInputMethods with removing specified Id.
3524 * @return the specified id was removed or not.
3525 */
3526 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3527 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3528 boolean isRemoved = false;
3529 boolean needsAppendSeparator = false;
3530 for (Pair<String, ArrayList<String>> ims: imsList) {
3531 String curId = ims.first;
3532 if (curId.equals(id)) {
3533 // We are disabling this input method, and it is
3534 // currently enabled. Skip it to remove from the
3535 // new list.
3536 isRemoved = true;
3537 } else {
3538 if (needsAppendSeparator) {
3539 builder.append(INPUT_METHOD_SEPARATER);
3540 } else {
3541 needsAppendSeparator = true;
3542 }
3543 buildEnabledInputMethodsSettingString(builder, ims);
3544 }
3545 }
3546 if (isRemoved) {
3547 // Update the setting with the new list of input methods.
3548 putEnabledInputMethodsStr(builder.toString());
3549 }
3550 return isRemoved;
3551 }
3552
3553 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3554 List<Pair<String, ArrayList<String>>> imsList) {
3555 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3556 for (Pair<String, ArrayList<String>> ims: imsList) {
3557 InputMethodInfo info = mMethodMap.get(ims.first);
3558 if (info != null) {
3559 res.add(info);
3560 }
3561 }
3562 return res;
3563 }
3564
satok7f35c8c2010-10-07 21:13:11 +09003565 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003566 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003567 List<Pair<String, ArrayList<String>>> imsList) {
3568 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3569 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3570 for (Pair<String, ArrayList<String>> ims : imsList) {
3571 InputMethodInfo info = mMethodMap.get(ims.first);
3572 if (info != null) {
3573 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3574 }
3575 }
3576 return res;
3577 }
3578
satokd87c2592010-09-29 11:52:06 +09003579 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003580 Settings.Secure.putStringForUser(
3581 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003582 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003583 if (DEBUG) {
3584 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3585 }
satokd87c2592010-09-29 11:52:06 +09003586 }
3587
3588 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003589 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3590 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003591 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003592 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3593 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003594 }
satokd87c2592010-09-29 11:52:06 +09003595 return mEnabledInputMethodsStrCache;
3596 }
satok723a27e2010-11-11 14:58:11 +09003597
3598 private void saveSubtypeHistory(
3599 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3600 StringBuilder builder = new StringBuilder();
3601 boolean isImeAdded = false;
3602 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3603 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3604 newSubtypeId);
3605 isImeAdded = true;
3606 }
3607 for (Pair<String, String> ime: savedImes) {
3608 String imeId = ime.first;
3609 String subtypeId = ime.second;
3610 if (TextUtils.isEmpty(subtypeId)) {
3611 subtypeId = NOT_A_SUBTYPE_ID_STR;
3612 }
3613 if (isImeAdded) {
3614 builder.append(INPUT_METHOD_SEPARATER);
3615 } else {
3616 isImeAdded = true;
3617 }
3618 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3619 subtypeId);
3620 }
3621 // Remove the last INPUT_METHOD_SEPARATER
3622 putSubtypeHistoryStr(builder.toString());
3623 }
3624
3625 public void addSubtypeToHistory(String imeId, String subtypeId) {
3626 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3627 for (Pair<String, String> ime: subtypeHistory) {
3628 if (ime.first.equals(imeId)) {
3629 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003630 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003631 + ime.second);
3632 }
3633 // We should break here
3634 subtypeHistory.remove(ime);
3635 break;
3636 }
3637 }
satokbb4aa062011-01-19 21:40:27 +09003638 if (DEBUG) {
3639 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3640 }
satok723a27e2010-11-11 14:58:11 +09003641 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3642 }
3643
3644 private void putSubtypeHistoryStr(String str) {
3645 if (DEBUG) {
3646 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3647 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003648 Settings.Secure.putStringForUser(
3649 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003650 }
3651
3652 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3653 // Gets the first one from the history
3654 return getLastSubtypeForInputMethodLockedInternal(null);
3655 }
3656
3657 public String getLastSubtypeForInputMethodLocked(String imeId) {
3658 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3659 if (ime != null) {
3660 return ime.second;
3661 } else {
3662 return null;
3663 }
3664 }
3665
3666 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3667 List<Pair<String, ArrayList<String>>> enabledImes =
3668 getEnabledInputMethodsAndSubtypeListLocked();
3669 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003670 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003671 final String imeInTheHistory = imeAndSubtype.first;
3672 // If imeId is empty, returns the first IME and subtype in the history
3673 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3674 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003675 final String subtypeHashCode =
3676 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3677 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003678 if (!TextUtils.isEmpty(subtypeHashCode)) {
3679 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003680 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003681 }
3682 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3683 }
3684 }
3685 }
3686 if (DEBUG) {
3687 Slog.d(TAG, "No enabled IME found in the history");
3688 }
3689 return null;
3690 }
3691
satokdf31ae62011-01-15 06:19:44 +09003692 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003693 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3694 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3695 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003696 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003697 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003698 if (explicitlyEnabledSubtypes.size() == 0) {
3699 // If there are no explicitly enabled subtypes, applicable subtypes are
3700 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003701 // If IME is enabled and no subtypes are enabled, applicable subtypes
3702 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003703 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003704 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003705 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003706 if (implicitlySelectedSubtypes != null) {
3707 final int N = implicitlySelectedSubtypes.size();
3708 for (int i = 0; i < N; ++i) {
3709 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3710 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3711 return subtypeHashCode;
3712 }
3713 }
3714 }
3715 }
3716 } else {
satokf6cafb62011-01-17 16:29:02 +09003717 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003718 if (s.equals(subtypeHashCode)) {
3719 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003720 try {
3721 final int hashCode = Integer.valueOf(subtypeHashCode);
3722 // Check whether the subtype id is valid or not
3723 if (isValidSubtypeId(imi, hashCode)) {
3724 return s;
3725 } else {
3726 return NOT_A_SUBTYPE_ID_STR;
3727 }
3728 } catch (NumberFormatException e) {
3729 return NOT_A_SUBTYPE_ID_STR;
3730 }
satokdf31ae62011-01-15 06:19:44 +09003731 }
satok723a27e2010-11-11 14:58:11 +09003732 }
3733 }
3734 // If imeId was enabled but subtypeId was disabled.
3735 return NOT_A_SUBTYPE_ID_STR;
3736 }
3737 }
3738 // If both imeId and subtypeId are disabled, return null
3739 return null;
3740 }
3741
3742 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3743 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3744 final String subtypeHistoryStr = getSubtypeHistoryStr();
3745 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3746 return imsList;
3747 }
3748 mInputMethodSplitter.setString(subtypeHistoryStr);
3749 while (mInputMethodSplitter.hasNext()) {
3750 String nextImsStr = mInputMethodSplitter.next();
3751 mSubtypeSplitter.setString(nextImsStr);
3752 if (mSubtypeSplitter.hasNext()) {
3753 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3754 // The first element is ime id.
3755 String imeId = mSubtypeSplitter.next();
3756 while (mSubtypeSplitter.hasNext()) {
3757 subtypeId = mSubtypeSplitter.next();
3758 break;
3759 }
3760 imsList.add(new Pair<String, String>(imeId, subtypeId));
3761 }
3762 }
3763 return imsList;
3764 }
3765
3766 private String getSubtypeHistoryStr() {
3767 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003768 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3769 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003770 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003771 return Settings.Secure.getStringForUser(
3772 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003773 }
3774
3775 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003776 if (DEBUG) {
3777 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3778 + mCurrentUserId);
3779 }
3780 Settings.Secure.putStringForUser(
3781 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003782 }
3783
3784 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003785 if (DEBUG) {
3786 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3787 + mCurrentUserId);
3788 }
3789 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3790 subtypeId, mCurrentUserId);
3791 }
3792
3793 public String getDisabledSystemInputMethods() {
3794 return Settings.Secure.getStringForUser(
3795 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3796 }
3797
3798 public String getSelectedInputMethod() {
3799 if (DEBUG) {
3800 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3801 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3802 + ", " + mCurrentUserId);
3803 }
3804 return Settings.Secure.getStringForUser(
3805 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3806 }
3807
3808 public int getSelectedInputMethodSubtypeHashCode() {
3809 try {
3810 return Settings.Secure.getIntForUser(
3811 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3812 } catch (SettingNotFoundException e) {
3813 return NOT_A_SUBTYPE_ID;
3814 }
3815 }
3816
3817 public int getCurrentUserId() {
3818 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003819 }
satokd87c2592010-09-29 11:52:06 +09003820 }
3821
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003822 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003823 private static class InputMethodFileManager {
3824 private static final String SYSTEM_PATH = "system";
3825 private static final String INPUT_METHOD_PATH = "inputmethod";
3826 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3827 private static final String NODE_SUBTYPES = "subtypes";
3828 private static final String NODE_SUBTYPE = "subtype";
3829 private static final String NODE_IMI = "imi";
3830 private static final String ATTR_ID = "id";
3831 private static final String ATTR_LABEL = "label";
3832 private static final String ATTR_ICON = "icon";
3833 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3834 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3835 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3836 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3837 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3838 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003839 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003840 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003841 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003842 if (methodMap == null) {
3843 throw new NullPointerException("methodMap is null");
3844 }
3845 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003846 final File systemDir = userId == UserHandle.USER_OWNER
3847 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3848 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003849 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3850 if (!inputMethodDir.mkdirs()) {
3851 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3852 }
3853 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3854 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3855 if (!subtypeFile.exists()) {
3856 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003857 writeAdditionalInputMethodSubtypes(
3858 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003859 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003860 readAdditionalInputMethodSubtypes(
3861 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003862 }
3863 }
3864
3865 private void deleteAllInputMethodSubtypes(String imiId) {
3866 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003867 mAdditionalSubtypesMap.remove(imiId);
3868 writeAdditionalInputMethodSubtypes(
3869 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003870 }
3871 }
3872
3873 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003874 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003875 synchronized (mMethodMap) {
3876 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3877 final int N = additionalSubtypes.length;
3878 for (int i = 0; i < N; ++i) {
3879 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003880 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003881 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003882 } else {
3883 Slog.w(TAG, "Duplicated subtype definition found: "
3884 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003885 }
3886 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003887 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3888 writeAdditionalInputMethodSubtypes(
3889 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003890 }
3891 }
3892
3893 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3894 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003895 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003896 }
3897 }
3898
3899 private static void writeAdditionalInputMethodSubtypes(
3900 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3901 HashMap<String, InputMethodInfo> methodMap) {
3902 // Safety net for the case that this function is called before methodMap is set.
3903 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3904 FileOutputStream fos = null;
3905 try {
3906 fos = subtypesFile.startWrite();
3907 final XmlSerializer out = new FastXmlSerializer();
3908 out.setOutput(fos, "utf-8");
3909 out.startDocument(null, true);
3910 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3911 out.startTag(null, NODE_SUBTYPES);
3912 for (String imiId : allSubtypes.keySet()) {
3913 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3914 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3915 continue;
3916 }
3917 out.startTag(null, NODE_IMI);
3918 out.attribute(null, ATTR_ID, imiId);
3919 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3920 final int N = subtypesList.size();
3921 for (int i = 0; i < N; ++i) {
3922 final InputMethodSubtype subtype = subtypesList.get(i);
3923 out.startTag(null, NODE_SUBTYPE);
3924 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3925 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3926 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3927 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3928 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3929 out.attribute(null, ATTR_IS_AUXILIARY,
3930 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3931 out.endTag(null, NODE_SUBTYPE);
3932 }
3933 out.endTag(null, NODE_IMI);
3934 }
3935 out.endTag(null, NODE_SUBTYPES);
3936 out.endDocument();
3937 subtypesFile.finishWrite(fos);
3938 } catch (java.io.IOException e) {
3939 Slog.w(TAG, "Error writing subtypes", e);
3940 if (fos != null) {
3941 subtypesFile.failWrite(fos);
3942 }
3943 }
3944 }
3945
3946 private static void readAdditionalInputMethodSubtypes(
3947 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3948 if (allSubtypes == null || subtypesFile == null) return;
3949 allSubtypes.clear();
3950 FileInputStream fis = null;
3951 try {
3952 fis = subtypesFile.openRead();
3953 final XmlPullParser parser = Xml.newPullParser();
3954 parser.setInput(fis, null);
3955 int type = parser.getEventType();
3956 // Skip parsing until START_TAG
3957 while ((type = parser.next()) != XmlPullParser.START_TAG
3958 && type != XmlPullParser.END_DOCUMENT) {}
3959 String firstNodeName = parser.getName();
3960 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3961 throw new XmlPullParserException("Xml doesn't start with subtypes");
3962 }
3963 final int depth =parser.getDepth();
3964 String currentImiId = null;
3965 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3966 while (((type = parser.next()) != XmlPullParser.END_TAG
3967 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3968 if (type != XmlPullParser.START_TAG)
3969 continue;
3970 final String nodeName = parser.getName();
3971 if (NODE_IMI.equals(nodeName)) {
3972 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3973 if (TextUtils.isEmpty(currentImiId)) {
3974 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3975 continue;
3976 }
3977 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3978 allSubtypes.put(currentImiId, tempSubtypesArray);
3979 } else if (NODE_SUBTYPE.equals(nodeName)) {
3980 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3981 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3982 continue;
3983 }
3984 final int icon = Integer.valueOf(
3985 parser.getAttributeValue(null, ATTR_ICON));
3986 final int label = Integer.valueOf(
3987 parser.getAttributeValue(null, ATTR_LABEL));
3988 final String imeSubtypeLocale =
3989 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3990 final String imeSubtypeMode =
3991 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3992 final String imeSubtypeExtraValue =
3993 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003994 final boolean isAuxiliary = "1".equals(String.valueOf(
3995 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003996 final InputMethodSubtype subtype =
3997 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3998 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3999 tempSubtypesArray.add(subtype);
4000 }
4001 }
4002 } catch (XmlPullParserException e) {
4003 Slog.w(TAG, "Error reading subtypes: " + e);
4004 return;
4005 } catch (java.io.IOException e) {
4006 Slog.w(TAG, "Error reading subtypes: " + e);
4007 return;
4008 } catch (NumberFormatException e) {
4009 Slog.w(TAG, "Error reading subtypes: " + e);
4010 return;
4011 } finally {
4012 if (fis != null) {
4013 try {
4014 fis.close();
4015 } catch (java.io.IOException e1) {
4016 Slog.w(TAG, "Failed to close.");
4017 }
4018 }
4019 }
4020 }
4021 }
4022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004024 // Utilities for debug
4025 private static String getStackTrace() {
4026 final StringBuilder sb = new StringBuilder();
4027 try {
4028 throw new RuntimeException();
4029 } catch (RuntimeException e) {
4030 final StackTraceElement[] frames = e.getStackTrace();
4031 // Start at 1 because the first frame is here and we don't care about it
4032 for (int j = 1; j < frames.length; ++j) {
4033 sb.append(frames[j].toString() + "\n");
4034 }
4035 }
4036 return sb.toString();
4037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 @Override
4040 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4041 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4042 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4045 + Binder.getCallingPid()
4046 + ", uid=" + Binder.getCallingUid());
4047 return;
4048 }
4049
4050 IInputMethod method;
4051 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 synchronized (mMethodMap) {
4056 p.println("Current Input Method Manager state:");
4057 int N = mMethodList.size();
4058 p.println(" Input Methods:");
4059 for (int i=0; i<N; i++) {
4060 InputMethodInfo info = mMethodList.get(i);
4061 p.println(" InputMethod #" + i + ":");
4062 info.dump(p, " ");
4063 }
4064 p.println(" Clients:");
4065 for (ClientState ci : mClients.values()) {
4066 p.println(" Client " + ci + ":");
4067 p.println(" client=" + ci.client);
4068 p.println(" inputContext=" + ci.inputContext);
4069 p.println(" sessionRequested=" + ci.sessionRequested);
4070 p.println(" curSession=" + ci.curSession);
4071 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004072 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004074 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4075 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4077 + " mBoundToMethod=" + mBoundToMethod);
4078 p.println(" mCurToken=" + mCurToken);
4079 p.println(" mCurIntent=" + mCurIntent);
4080 method = mCurMethod;
4081 p.println(" mCurMethod=" + mCurMethod);
4082 p.println(" mEnabledSession=" + mEnabledSession);
4083 p.println(" mShowRequested=" + mShowRequested
4084 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4085 + " mShowForced=" + mShowForced
4086 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004087 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004089
Jeff Brownb88102f2010-09-08 11:49:43 -07004090 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 pw.flush();
4093 try {
4094 client.client.asBinder().dump(fd, args);
4095 } catch (RemoteException e) {
4096 p.println("Input method client dead: " + e);
4097 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004098 } else {
4099 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004101
Jeff Brownb88102f2010-09-08 11:49:43 -07004102 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 pw.flush();
4105 try {
4106 method.asBinder().dump(fd, args);
4107 } catch (RemoteException e) {
4108 p.println("Input method service dead: " + e);
4109 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004110 } else {
4111 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113 }
4114}