blob: 4935d796177e93d1e5ef99777dd55b4c4aa869b9 [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;
satoke7c6998e2011-06-03 17:57:59 +090019import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
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;
37import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090038import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090039import android.app.Notification;
40import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070041import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090042import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.ComponentName;
44import android.content.ContentResolver;
45import android.content.Context;
46import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.DialogInterface.OnCancelListener;
48import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090049import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070051import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070056import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.res.Resources;
58import android.content.res.TypedArray;
59import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080060import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.IInterface;
66import android.os.Message;
67import android.os.Parcel;
68import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080069import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.ServiceManager;
71import android.os.SystemClock;
72import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070073import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090074import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090076import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090078import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090079import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.PrintWriterPrinter;
81import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090082import android.util.Slog;
83import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090085import android.view.LayoutInflater;
86import android.view.View;
87import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090089import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.inputmethod.InputBinding;
91import android.view.inputmethod.InputMethod;
92import android.view.inputmethod.InputMethodInfo;
93import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090094import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090095import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +090096import android.widget.CompoundButton;
97import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +090098import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +090099import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900100import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
satoke7c6998e2011-06-03 17:57:59 +0900102import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900104import java.io.FileInputStream;
105import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import java.io.IOException;
107import java.io.PrintWriter;
108import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900109import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900110import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900112import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900114import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900115import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117/**
118 * This class provides a system service that manages input methods.
119 */
120public class InputMethodManagerService extends IInputMethodManager.Stub
121 implements ServiceConnection, Handler.Callback {
122 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700123 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900126 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900127 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900128 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_UNBIND_INPUT = 1000;
131 static final int MSG_BIND_INPUT = 1010;
132 static final int MSG_SHOW_SOFT_INPUT = 1020;
133 static final int MSG_HIDE_SOFT_INPUT = 1030;
134 static final int MSG_ATTACH_TOKEN = 1040;
135 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_START_INPUT = 2000;
138 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final int MSG_UNBIND_METHOD = 3000;
141 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
satok01038492012-04-09 21:08:27 +0900143 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
satokf9f01002011-05-19 21:31:50 +0900147 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
148
satokab751aa2010-09-14 19:17:36 +0900149 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900150 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900151 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
152 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900153 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900154 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
155 "EnabledWhenDefaultIsNotAsciiCapable";
156 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900157 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800160 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900162 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final IWindowManager mIWindowManager;
165 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900166 private final InputMethodFileManager mFileManager;
satok688bd472012-02-09 20:09:17 +0900167 private final InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900168 private final HardKeyboardListener mHardKeyboardListener;
169 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 // All known input methods. mMethodMap also serves as the global
174 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900175 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
176 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900177 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
178 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700180 // Used to bring IME service up to visible adjustment while it is being shown.
181 final ServiceConnection mVisibleConnection = new ServiceConnection() {
182 @Override public void onServiceConnected(ComponentName name, IBinder service) {
183 }
184
185 @Override public void onServiceDisconnected(ComponentName name) {
186 }
187 };
188 boolean mVisibleBound = false;
189
satok7cfc0ed2011-06-20 21:29:36 +0900190 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700191 private NotificationManager mNotificationManager;
192 private KeyguardManager mKeyguardManager;
193 private StatusBarManagerService mStatusBar;
194 private Notification mImeSwitcherNotification;
195 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900196 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900197 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900198 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 class SessionState {
201 final ClientState client;
202 final IInputMethod method;
203 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 @Override
206 public String toString() {
207 return "SessionState{uid " + client.uid + " pid " + client.pid
208 + " method " + Integer.toHexString(
209 System.identityHashCode(method))
210 + " session " + Integer.toHexString(
211 System.identityHashCode(session))
212 + "}";
213 }
214
215 SessionState(ClientState _client, IInputMethod _method,
216 IInputMethodSession _session) {
217 client = _client;
218 method = _method;
219 session = _session;
220 }
221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 class ClientState {
224 final IInputMethodClient client;
225 final IInputContext inputContext;
226 final int uid;
227 final int pid;
228 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 boolean sessionRequested;
231 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
234 public String toString() {
235 return "ClientState{" + Integer.toHexString(
236 System.identityHashCode(this)) + " uid " + uid
237 + " pid " + pid + "}";
238 }
239
240 ClientState(IInputMethodClient _client, IInputContext _inputContext,
241 int _uid, int _pid) {
242 client = _client;
243 inputContext = _inputContext;
244 uid = _uid;
245 pid = _pid;
246 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
247 }
248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final HashMap<IBinder, ClientState> mClients
251 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700254 * Set once the system is ready to run third party code.
255 */
256 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * Id of the currently selected input method.
260 */
261 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * The current binding sequence number, incremented every time there is
265 * a new bind performed.
266 */
267 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * The client that is currently bound to an input method.
271 */
272 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700275 * The last window token that gained focus.
276 */
277 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 * The input context last provided by the current client.
281 */
282 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * The attributes last provided by the current client.
286 */
287 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * The input method ID of the input method service that we are currently
291 * connected to or in the process of connecting to.
292 */
293 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
satokab751aa2010-09-14 19:17:36 +0900296 * The current subtype of the current input method.
297 */
298 private InputMethodSubtype mCurrentSubtype;
299
satok4e4569d2010-11-19 18:45:53 +0900300 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900301 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
302 mShortcutInputMethodsAndSubtypes =
303 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900304
305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * Set to true if our ServiceConnection is currently actively bound to
307 * a service (whether or not we have gotten its IBinder back yet).
308 */
309 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
312 * Set if the client has asked for the input method to be shown.
313 */
314 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * Set if we were explicitly told to show the input method.
318 */
319 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Set if we were forced to be shown.
323 */
324 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Set if we last told the input method to show itself.
328 */
329 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * The Intent used to connect to the current input method.
333 */
334 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
337 * The token we have made for the currently active input method, to
338 * identify it in the future.
339 */
340 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * If non-null, this is the input method service we are currently connected
344 * to.
345 */
346 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * Time that we last initiated a bind to the input method, to determine
350 * if we should try to disconnect and reconnect to it.
351 */
352 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Have we called mCurMethod.bindInput()?
356 */
357 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * Currently enabled session. Only touched by service thread, not
361 * protected by a lock.
362 */
363 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * True if the screen is on. The value is true initially.
367 */
368 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
Joe Onorato857fd9b2011-01-27 15:08:35 -0800370 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
371 int mImeWindowVis;
372
Ken Wakasa05dbb652011-08-22 15:22:43 +0900373 private AlertDialog.Builder mDialogBuilder;
374 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900375 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900376 private InputMethodInfo[] mIms;
377 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900378 private Locale mLastSystemLocale;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 class SettingsObserver extends ContentObserver {
381 SettingsObserver(Handler handler) {
382 super(handler);
383 ContentResolver resolver = mContext.getContentResolver();
384 resolver.registerContentObserver(Settings.Secure.getUriFor(
385 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900386 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900387 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
388 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900389 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 @Override public void onChange(boolean selfChange) {
393 synchronized (mMethodMap) {
394 updateFromSettingsLocked();
395 }
396 }
397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
400 @Override
401 public void onReceive(Context context, Intent intent) {
402 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
403 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900404 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
406 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900407 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700408 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
409 hideInputMethodMenu();
410 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800412 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414
415 // Inform the current client of the change in active status
416 try {
417 if (mCurClient != null && mCurClient.client != null) {
418 mCurClient.client.setActive(mScreenOn);
419 }
420 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800421 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 + mCurClient.pid + " uid " + mCurClient.uid);
423 }
424 }
425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800427 class MyPackageMonitor extends PackageMonitor {
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800430 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
434 final int N = mMethodList.size();
435 if (curInputMethodId != null) {
436 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 InputMethodInfo imi = mMethodList.get(i);
438 if (imi.getId().equals(curInputMethodId)) {
439 for (String pkg : packages) {
440 if (imi.getPackageName().equals(pkg)) {
441 if (!doit) {
442 return true;
443 }
satok723a27e2010-11-11 14:58:11 +0900444 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800445 chooseNewDefaultIMELocked();
446 return true;
447 }
448 }
449 }
450 }
451 }
452 }
453 return false;
454 }
455
456 @Override
457 public void onSomePackagesChanged() {
458 synchronized (mMethodMap) {
459 InputMethodInfo curIm = null;
460 String curInputMethodId = Settings.Secure.getString(mContext
461 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
462 final int N = mMethodList.size();
463 if (curInputMethodId != null) {
464 for (int i=0; i<N; i++) {
465 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900466 final String imiId = imi.getId();
467 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800468 curIm = imi;
469 }
satoke7c6998e2011-06-03 17:57:59 +0900470
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800471 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900472 if (isPackageModified(imi.getPackageName())) {
473 mFileManager.deleteAllInputMethodSubtypes(imiId);
474 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 if (change == PACKAGE_TEMPORARY_CHANGE
476 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 + imi.getComponent());
479 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481 }
482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 buildInputMethodListLocked(mMethodList, mMethodMap);
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800488 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 int change = isPackageDisappearing(curIm.getPackageName());
490 if (change == PACKAGE_TEMPORARY_CHANGE
491 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800492 ServiceInfo si = null;
493 try {
494 si = mContext.getPackageManager().getServiceInfo(
495 curIm.getComponent(), 0);
496 } catch (PackageManager.NameNotFoundException ex) {
497 }
498 if (si == null) {
499 // Uh oh, current input method is no longer around!
500 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800501 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900502 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800503 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800504 changed = true;
505 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800506 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900507 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800508 }
509 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800510 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 }
satokab751aa2010-09-14 19:17:36 +0900512
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800513 if (curIm == null) {
514 // We currently don't have a default input method... is
515 // one now available?
516 changed = chooseNewDefaultIMELocked();
517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800519 if (changed) {
520 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522 }
523 }
524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800525
Jean Chalarde0d32a62011-10-20 20:36:07 +0900526 private static class MethodCallback extends IInputMethodCallback.Stub {
527 private final IInputMethod mMethod;
528 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
Jean Chalarde0d32a62011-10-20 20:36:07 +0900530 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900532 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
satoke7c6998e2011-06-03 17:57:59 +0900535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public void finishedEvent(int seq, boolean handled) throws RemoteException {
537 }
538
satoke7c6998e2011-06-03 17:57:59 +0900539 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900541 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
satok01038492012-04-09 21:08:27 +0900545 private class HardKeyboardListener
546 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
547 @Override
548 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
549 mHandler.sendMessage(mHandler.obtainMessage(
550 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
551 }
552
553 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
554 if (DEBUG) {
555 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
556 + enabled);
557 }
558 synchronized(mMethodMap) {
559 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
560 && mSwitchingDialog.isShowing()) {
561 mSwitchingDialogTitleView.findViewById(
562 com.android.internal.R.id.hard_keyboard_section).setVisibility(
563 available ? View.VISIBLE : View.GONE);
564 }
565 }
566 }
567 }
568
569 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800571 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 mHandler = new Handler(this);
573 mIWindowManager = IWindowManager.Stub.asInterface(
574 ServiceManager.getService(Context.WINDOW_SERVICE));
575 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900576 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public void executeMessage(Message msg) {
578 handleMessage(msg);
579 }
580 });
satok01038492012-04-09 21:08:27 +0900581 mWindowManagerService = windowManager;
582 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900583
satok7cfc0ed2011-06-20 21:29:36 +0900584 mImeSwitcherNotification = new Notification();
585 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
586 mImeSwitcherNotification.when = 0;
587 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
588 mImeSwitcherNotification.tickerText = null;
589 mImeSwitcherNotification.defaults = 0; // please be quiet
590 mImeSwitcherNotification.sound = null;
591 mImeSwitcherNotification.vibrate = null;
592 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900593 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900594
595 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900596
satoke7c6998e2011-06-03 17:57:59 +0900597 synchronized (mMethodMap) {
598 mFileManager = new InputMethodFileManager(mMethodMap);
599 }
satok688bd472012-02-09 20:09:17 +0900600 mImListManager = new InputMethodAndSubtypeListManager(context, this);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800601
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700602 (new MyPackageMonitor()).register(mContext, null, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 IntentFilter screenOnOffFilt = new IntentFilter();
605 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
606 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700607 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609
satok7cfc0ed2011-06-20 21:29:36 +0900610 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900611
satokd87c2592010-09-29 11:52:06 +0900612 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900613 mSettings = new InputMethodSettings(
614 mRes, context.getContentResolver(), mMethodMap, mMethodList);
satok0a1bcf42012-05-16 19:26:31 +0900615
616 // Just checking if defaultImiId is empty or not
617 final String defaultImiId = Settings.Secure.getString(
618 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
619 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900622 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
satok0a1bcf42012-05-16 19:26:31 +0900624 if (!mImeSelectedOnBoot) {
625 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900626 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 mSettingsObserver = new SettingsObserver(mHandler);
630 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900631
632 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
633 // according to the new system locale.
634 final IntentFilter filter = new IntentFilter();
635 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
636 mContext.registerReceiver(
637 new BroadcastReceiver() {
638 @Override
639 public void onReceive(Context context, Intent intent) {
640 synchronized(mMethodMap) {
641 checkCurrentLocaleChangedLocked();
642 }
643 }
644 }, filter);
645 }
646
647 private void checkCurrentLocaleChangedLocked() {
satok0a1bcf42012-05-16 19:26:31 +0900648 if (!mSystemReady) {
649 // not system ready
650 return;
651 }
satok5b927c432012-05-01 20:09:34 +0900652 final Locale newLocale = mRes.getConfiguration().locale;
653 if (newLocale != null && !newLocale.equals(mLastSystemLocale)) {
654 if (DEBUG) {
655 Slog.i(TAG, "Locale has been changed to " + newLocale);
656 }
657 buildInputMethodListLocked(mMethodList, mMethodMap);
658 // Reset the current ime to the proper one
659 resetDefaultImeLocked(mContext);
660 mLastSystemLocale = newLocale;
661 }
662 }
663
664 private void resetDefaultImeLocked(Context context) {
665 // Do not reset the default (current) IME when it is a 3rd-party IME
666 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
667 return;
668 }
669
670 InputMethodInfo defIm = null;
671 for (InputMethodInfo imi : mMethodList) {
672 if (defIm == null) {
673 if (isValidSystemDefaultIme(imi, context)) {
674 defIm = imi;
675 Slog.i(TAG, "Selected default: " + imi.getId());
676 }
677 }
678 }
679 if (defIm == null && mMethodList.size() > 0) {
680 defIm = getMostApplicableDefaultIMELocked();
681 Slog.i(TAG, "No default found, using " + defIm.getId());
682 }
683 if (defIm != null) {
684 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
685 }
686 }
687
satok0a1bcf42012-05-16 19:26:31 +0900688 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
689 if (!mSystemReady) {
690 return false;
691 }
satok5b927c432012-05-01 20:09:34 +0900692 if (!isSystemIme(imi)) {
693 return false;
694 }
695 if (imi.getIsDefaultResourceId() != 0) {
696 try {
697 Resources res = context.createPackageContext(
698 imi.getPackageName(), 0).getResources();
699 if (res.getBoolean(imi.getIsDefaultResourceId())
700 && containsSubtypeOf(imi, context.getResources().getConfiguration().
701 locale.getLanguage())) {
702 return true;
703 }
704 } catch (PackageManager.NameNotFoundException ex) {
705 } catch (Resources.NotFoundException ex) {
706 }
707 }
708 if (imi.getSubtypeCount() == 0) {
709 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
710 }
711 return false;
712 }
713
714 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
715 if (!isSystemIme(imi)) {
716 return false;
717 }
718 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
719 }
720
721 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
722 final int N = imi.getSubtypeCount();
723 for (int i = 0; i < N; ++i) {
724 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
725 return true;
726 }
727 }
728 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 @Override
732 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
733 throws RemoteException {
734 try {
735 return super.onTransact(code, data, reply, flags);
736 } catch (RuntimeException e) {
737 // The input method manager only throws security exceptions, so let's
738 // log all others.
739 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800740 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 throw e;
743 }
744 }
745
Dianne Hackborn661cd522011-08-22 00:26:20 -0700746 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700747 synchronized (mMethodMap) {
748 if (!mSystemReady) {
749 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700750 mKeyguardManager = (KeyguardManager)
751 mContext.getSystemService(Context.KEYGUARD_SERVICE);
752 mNotificationManager = (NotificationManager)
753 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
754 mStatusBar = statusBar;
755 statusBar.setIconVisibility("ime", false);
756 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900757 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
758 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900759 if (mShowOngoingImeSwitcherForPhones) {
760 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
761 mHardKeyboardListener);
762 }
satok0a1bcf42012-05-16 19:26:31 +0900763 buildInputMethodListLocked(mMethodList, mMethodMap);
764 if (!mImeSelectedOnBoot) {
765 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
766 checkCurrentLocaleChangedLocked();
767 }
768 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700769 try {
770 startInputInnerLocked();
771 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700773 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700774 }
775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
satok15452a42011-10-28 17:58:28 +0900778 private void setImeWindowVisibilityStatusHiddenLocked() {
779 mImeWindowVis = 0;
780 updateImeWindowStatusLocked();
781 }
782
satok3afd6c02011-11-18 08:38:19 +0900783 private void refreshImeWindowVisibilityLocked() {
784 final Configuration conf = mRes.getConfiguration();
785 final boolean haveHardKeyboard = conf.keyboard
786 != Configuration.KEYBOARD_NOKEYS;
787 final boolean hardKeyShown = haveHardKeyboard
788 && conf.hardKeyboardHidden
789 != Configuration.HARDKEYBOARDHIDDEN_YES;
790 final boolean isScreenLocked = mKeyguardManager != null
791 && mKeyguardManager.isKeyguardLocked()
792 && mKeyguardManager.isKeyguardSecure();
793 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
794 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
795 updateImeWindowStatusLocked();
796 }
797
satok15452a42011-10-28 17:58:28 +0900798 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900799 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700800 }
801
satoke7c6998e2011-06-03 17:57:59 +0900802 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 public List<InputMethodInfo> getInputMethodList() {
804 synchronized (mMethodMap) {
805 return new ArrayList<InputMethodInfo>(mMethodList);
806 }
807 }
808
satoke7c6998e2011-06-03 17:57:59 +0900809 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 public List<InputMethodInfo> getEnabledInputMethodList() {
811 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900812 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814 }
815
satokbb4aa062011-01-19 21:40:27 +0900816 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
817 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
818 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
819 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
820 for (InputMethodInfo imi: getEnabledInputMethodList()) {
821 enabledInputMethodAndSubtypes.put(
822 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
823 }
824 return enabledInputMethodAndSubtypes;
825 }
826
827 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
828 boolean allowsImplicitlySelectedSubtypes) {
829 if (imi == null && mCurMethodId != null) {
830 imi = mMethodMap.get(mCurMethodId);
831 }
satok7265d9b2011-02-14 15:47:30 +0900832 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900833 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900834 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900835 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900836 }
satok7265d9b2011-02-14 15:47:30 +0900837 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900838 }
839
satoke7c6998e2011-06-03 17:57:59 +0900840 @Override
satok16331c82010-12-20 23:48:46 +0900841 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
842 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900843 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900844 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900845 }
846 }
847
satoke7c6998e2011-06-03 17:57:59 +0900848 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 public void addClient(IInputMethodClient client,
850 IInputContext inputContext, int uid, int pid) {
851 synchronized (mMethodMap) {
852 mClients.put(client.asBinder(), new ClientState(client,
853 inputContext, uid, pid));
854 }
855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856
satoke7c6998e2011-06-03 17:57:59 +0900857 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public void removeClient(IInputMethodClient client) {
859 synchronized (mMethodMap) {
860 mClients.remove(client.asBinder());
861 }
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 void executeOrSendMessage(IInterface target, Message msg) {
865 if (target.asBinder() instanceof Binder) {
866 mCaller.sendMessage(msg);
867 } else {
868 handleMessage(msg);
869 msg.recycle();
870 }
871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800875 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 + mCurClient.client.asBinder());
877 if (mBoundToMethod) {
878 mBoundToMethod = false;
879 if (mCurMethod != null) {
880 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
881 MSG_UNBIND_INPUT, mCurMethod));
882 }
883 }
884 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
885 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
886 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 // Call setActive(false) on the old client
889 try {
890 mCurClient.client.setActive(false);
891 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800892 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 + mCurClient.pid + " uid " + mCurClient.uid);
894 }
895 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800896
The Android Open Source Project10592532009-03-18 17:39:46 -0700897 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 private int getImeShowFlags() {
902 int flags = 0;
903 if (mShowForced) {
904 flags |= InputMethod.SHOW_FORCED
905 | InputMethod.SHOW_EXPLICIT;
906 } else if (mShowExplicitlyRequested) {
907 flags |= InputMethod.SHOW_EXPLICIT;
908 }
909 return flags;
910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 private int getAppShowFlags() {
913 int flags = 0;
914 if (mShowForced) {
915 flags |= InputMethodManager.SHOW_FORCED;
916 } else if (!mShowExplicitlyRequested) {
917 flags |= InputMethodManager.SHOW_IMPLICIT;
918 }
919 return flags;
920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
Dianne Hackborn7663d802012-02-24 13:08:49 -0800922 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 if (!mBoundToMethod) {
924 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
925 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
926 mBoundToMethod = true;
927 }
928 final SessionState session = mCurClient.curSession;
929 if (initial) {
930 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
931 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
932 } else {
933 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
934 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
935 }
936 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800937 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800938 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800940 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800944 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 // If no method is currently selected, do nothing.
946 if (mCurMethodId == null) {
947 return mNoBinding;
948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 ClientState cs = mClients.get(client.asBinder());
951 if (cs == null) {
952 throw new IllegalArgumentException("unknown client "
953 + client.asBinder());
954 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 try {
957 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
958 // Check with the window manager to make sure this client actually
959 // has a window with focus. If not, reject. This is thread safe
960 // because if the focus changes some time before or after, the
961 // next client receiving focus that has any interest in input will
962 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800963 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
965 return null;
966 }
967 } catch (RemoteException e) {
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
Dianne Hackborn7663d802012-02-24 13:08:49 -0800970 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
971 }
972
973 InputBindResult startInputUncheckedLocked(ClientState cs,
974 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
975 // If no method is currently selected, do nothing.
976 if (mCurMethodId == null) {
977 return mNoBinding;
978 }
979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (mCurClient != cs) {
981 // If the client is changing, we need to switch over to the new
982 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800984 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 + cs.client.asBinder());
986
987 // If the screen is on, inform the new client it is active
988 if (mScreenOn) {
989 try {
990 cs.client.setActive(mScreenOn);
991 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800992 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 + cs.pid + " uid " + cs.uid);
994 }
995 }
996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 // Bump up the sequence for this client and attach it.
999 mCurSeq++;
1000 if (mCurSeq <= 0) mCurSeq = 1;
1001 mCurClient = cs;
1002 mCurInputContext = inputContext;
1003 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 // Check if the input method is changing.
1006 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1007 if (cs.curSession != null) {
1008 // Fast case: if we are already connected to the input method,
1009 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001010 return attachNewInputLocked(
1011 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 if (mHaveConnection) {
1014 if (mCurMethod != null) {
1015 if (!cs.sessionRequested) {
1016 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001017 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1019 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001020 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022 // Return to client, and we will get back with it when
1023 // we have had a session made for it.
1024 return new InputBindResult(null, mCurId, mCurSeq);
1025 } else if (SystemClock.uptimeMillis()
1026 < (mLastBindTime+TIME_TO_RECONNECT)) {
1027 // In this case we have connected to the service, but
1028 // don't yet have its interface. If it hasn't been too
1029 // long since we did the connection, we'll return to
1030 // the client and wait to get the service interface so
1031 // we can report back. If it has been too long, we want
1032 // to fall through so we can try a disconnect/reconnect
1033 // to see if we can get back in touch with the service.
1034 return new InputBindResult(null, mCurId, mCurSeq);
1035 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001036 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1037 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039 }
1040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001041
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001042 return startInputInnerLocked();
1043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001045 InputBindResult startInputInnerLocked() {
1046 if (mCurMethodId == null) {
1047 return mNoBinding;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001050 if (!mSystemReady) {
1051 // If the system is not yet ready, we shouldn't be running third
1052 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001053 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1057 if (info == null) {
1058 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001060
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001061 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1064 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001065 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1066 com.android.internal.R.string.input_method_binding_label);
1067 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1068 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001069 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1070 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 mLastBindTime = SystemClock.uptimeMillis();
1072 mHaveConnection = true;
1073 mCurId = info.getId();
1074 mCurToken = new Binder();
1075 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001076 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 mIWindowManager.addWindowToken(mCurToken,
1078 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1079 } catch (RemoteException e) {
1080 }
1081 return new InputBindResult(null, mCurId, mCurSeq);
1082 } else {
1083 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 + mCurIntent);
1086 }
1087 return null;
1088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
satoke7c6998e2011-06-03 17:57:59 +09001090 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001092 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 synchronized (mMethodMap) {
1094 final long ident = Binder.clearCallingIdentity();
1095 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001096 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 } finally {
1098 Binder.restoreCallingIdentity(ident);
1099 }
1100 }
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
satoke7c6998e2011-06-03 17:57:59 +09001103 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 public void finishInput(IInputMethodClient client) {
1105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
satoke7c6998e2011-06-03 17:57:59 +09001107 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 public void onServiceConnected(ComponentName name, IBinder service) {
1109 synchronized (mMethodMap) {
1110 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1111 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001112 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001114 unbindCurrentMethodLocked(false);
1115 return;
1116 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001118 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1119 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001122 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001124 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001125 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128 }
1129 }
1130
1131 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1132 synchronized (mMethodMap) {
1133 if (mCurMethod != null && method != null
1134 && mCurMethod.asBinder() == method.asBinder()) {
1135 if (mCurClient != null) {
1136 mCurClient.curSession = new SessionState(mCurClient,
1137 method, session);
1138 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001139 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (res.method != null) {
1141 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1142 MSG_BIND_METHOD, mCurClient.client, res));
1143 }
1144 }
1145 }
1146 }
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001149 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001150 if (mVisibleBound) {
1151 mContext.unbindService(mVisibleConnection);
1152 mVisibleBound = false;
1153 }
1154
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001155 if (mHaveConnection) {
1156 mContext.unbindService(this);
1157 mHaveConnection = false;
1158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001160 if (mCurToken != null) {
1161 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001162 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
satoke0a99412012-05-10 02:22:58 +09001163 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1164 // The current IME is shown. Hence an IME switch (transition) is happening.
1165 mWindowManagerService.saveLastInputMethodWindowForTransition();
1166 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001167 mIWindowManager.removeWindowToken(mCurToken);
1168 } catch (RemoteException e) {
1169 }
1170 mCurToken = null;
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
The Android Open Source Project10592532009-03-18 17:39:46 -07001173 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001174 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001176 if (reportToClient && mCurClient != null) {
1177 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1178 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1179 }
1180 }
1181
Devin Taylor0c33ed22010-02-23 13:26:46 -06001182 private void finishSession(SessionState sessionState) {
1183 if (sessionState != null && sessionState.session != null) {
1184 try {
1185 sessionState.session.finishSession();
1186 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001187 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001188 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001189 }
1190 }
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001193 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (mCurMethod != null) {
1195 for (ClientState cs : mClients.values()) {
1196 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001197 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 cs.curSession = null;
1199 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001200
1201 finishSession(mEnabledSession);
1202 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 mCurMethod = null;
1204 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001205 if (mStatusBar != null) {
1206 mStatusBar.setIconVisibility("ime", false);
1207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
satoke7c6998e2011-06-03 17:57:59 +09001210 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 public void onServiceDisconnected(ComponentName name) {
1212 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 + " mCurIntent=" + mCurIntent);
1215 if (mCurMethod != null && mCurIntent != null
1216 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001217 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 // We consider this to be a new bind attempt, since the system
1219 // should now try to restart the service for us.
1220 mLastBindTime = SystemClock.uptimeMillis();
1221 mShowRequested = mInputShown;
1222 mInputShown = false;
1223 if (mCurClient != null) {
1224 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1225 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1226 }
1227 }
1228 }
1229 }
1230
satokf9f01002011-05-19 21:31:50 +09001231 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001233 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 long ident = Binder.clearCallingIdentity();
1235 try {
1236 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001237 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 return;
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 synchronized (mMethodMap) {
1242 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001244 if (mStatusBar != null) {
1245 mStatusBar.setIconVisibility("ime", false);
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001248 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001249 CharSequence contentDescription = null;
1250 try {
1251 PackageManager packageManager = mContext.getPackageManager();
1252 contentDescription = packageManager.getApplicationLabel(
1253 packageManager.getApplicationInfo(packageName, 0));
1254 } catch (NameNotFoundException nnfe) {
1255 /* ignore */
1256 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001257 if (mStatusBar != null) {
1258 mStatusBar.setIcon("ime", packageName, iconId, 0,
1259 contentDescription != null
1260 ? contentDescription.toString() : null);
1261 mStatusBar.setIconVisibility("ime", true);
1262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264 }
1265 } finally {
1266 Binder.restoreCallingIdentity(ident);
1267 }
1268 }
1269
satok7cfc0ed2011-06-20 21:29:36 +09001270 private boolean needsToShowImeSwitchOngoingNotification() {
1271 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001272 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001273 synchronized (mMethodMap) {
1274 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1275 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001276 if (N > 2) return true;
1277 if (N < 1) return false;
1278 int nonAuxCount = 0;
1279 int auxCount = 0;
1280 InputMethodSubtype nonAuxSubtype = null;
1281 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001282 for(int i = 0; i < N; ++i) {
1283 final InputMethodInfo imi = imis.get(i);
1284 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1285 imi, true);
1286 final int subtypeCount = subtypes.size();
1287 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001288 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001289 } else {
1290 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001291 final InputMethodSubtype subtype = subtypes.get(j);
1292 if (!subtype.isAuxiliary()) {
1293 ++nonAuxCount;
1294 nonAuxSubtype = subtype;
1295 } else {
1296 ++auxCount;
1297 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001298 }
1299 }
1300 }
satok7cfc0ed2011-06-20 21:29:36 +09001301 }
satokb6359412011-06-28 17:47:41 +09001302 if (nonAuxCount > 1 || auxCount > 1) {
1303 return true;
1304 } else if (nonAuxCount == 1 && auxCount == 1) {
1305 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001306 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1307 || auxSubtype.overridesImplicitlyEnabledSubtype()
1308 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001309 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1310 return false;
1311 }
1312 return true;
1313 }
1314 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001315 }
satok7cfc0ed2011-06-20 21:29:36 +09001316 }
1317
satokdbf29502011-08-25 15:28:23 +09001318 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001319 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001320 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001321 int uid = Binder.getCallingUid();
1322 long ident = Binder.clearCallingIdentity();
1323 try {
1324 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001325 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001326 return;
1327 }
1328
1329 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001330 mImeWindowVis = vis;
1331 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001332 if (mStatusBar != null) {
1333 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1334 }
satok7cfc0ed2011-06-20 21:29:36 +09001335 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001336 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1337 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001338 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001339 final CharSequence title = mRes.getText(
1340 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001341 final CharSequence imiLabel = imi.loadLabel(pm);
1342 final CharSequence summary = mCurrentSubtype != null
1343 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1344 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1345 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001346 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001347 : imiLabel;
1348
satok7cfc0ed2011-06-20 21:29:36 +09001349 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001350 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001351 if (mNotificationManager != null) {
1352 mNotificationManager.notify(
1353 com.android.internal.R.string.select_input_method,
1354 mImeSwitcherNotification);
1355 mNotificationShown = true;
1356 }
satok7cfc0ed2011-06-20 21:29:36 +09001357 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001358 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001359 mNotificationManager.cancel(
1360 com.android.internal.R.string.select_input_method);
1361 mNotificationShown = false;
1362 }
1363 }
satok06487a52010-10-29 11:37:18 +09001364 }
1365 } finally {
1366 Binder.restoreCallingIdentity(ident);
1367 }
1368 }
1369
satoke7c6998e2011-06-03 17:57:59 +09001370 @Override
satokf9f01002011-05-19 21:31:50 +09001371 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1372 synchronized (mMethodMap) {
1373 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1374 for (int i = 0; i < spans.length; ++i) {
1375 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001376 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001377 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001378 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001379 }
1380 }
1381 }
1382 }
1383
satoke7c6998e2011-06-03 17:57:59 +09001384 @Override
satokf9f01002011-05-19 21:31:50 +09001385 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1386 synchronized (mMethodMap) {
1387 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1388 // TODO: Do not send the intent if the process of the targetImi is already dead.
1389 if (targetImi != null) {
1390 final String[] suggestions = span.getSuggestions();
1391 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001392 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001393 final Intent intent = new Intent();
1394 // Ensures that only a class in the original IME package will receive the
1395 // notification.
satok42c5a162011-05-26 16:46:14 +09001396 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001397 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1398 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1399 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1400 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1401 mContext.sendBroadcast(intent);
1402 return true;
1403 }
1404 }
1405 return false;
1406 }
1407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001409 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1410 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1411 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1412 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001414 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001415 // There is no input method selected, try to choose new applicable input method.
1416 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1417 id = Settings.Secure.getString(mContext.getContentResolver(),
1418 Settings.Secure.DEFAULT_INPUT_METHOD);
1419 }
1420 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 try {
satokab751aa2010-09-14 19:17:36 +09001422 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001425 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001426 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
satokf3db1af2010-11-23 13:34:33 +09001428 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001429 } else {
1430 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001431 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001432 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
satokab751aa2010-09-14 19:17:36 +09001436 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 InputMethodInfo info = mMethodMap.get(id);
1438 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001439 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001443 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001444 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1445 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001446 }
1447 if (subtype != mCurrentSubtype) {
1448 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001449 if (subtype != null) {
1450 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1451 }
satokcd7cd292010-11-20 15:46:23 +09001452 if (mCurMethod != null) {
1453 try {
satok3afd6c02011-11-18 08:38:19 +09001454 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001455 // If subtype is null, try to find the most applicable one from
1456 // getCurrentInputMethodSubtype.
1457 if (subtype == null) {
1458 subtype = getCurrentInputMethodSubtype();
1459 }
1460 mCurMethod.changeInputMethodSubtype(subtype);
1461 } catch (RemoteException e) {
1462 return;
satokab751aa2010-09-14 19:17:36 +09001463 }
1464 }
1465 }
1466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 return;
1468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 final long ident = Binder.clearCallingIdentity();
1471 try {
satokab751aa2010-09-14 19:17:36 +09001472 // Set a subtype to this input method.
1473 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001474 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1475 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1476 // because mCurMethodId is stored as a history in
1477 // setSelectedInputMethodAndSubtypeLocked().
1478 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479
1480 if (ActivityManagerNative.isSystemReady()) {
1481 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001482 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 intent.putExtra("input_method_id", id);
1484 mContext.sendBroadcast(intent);
1485 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001486 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 } finally {
1488 Binder.restoreCallingIdentity(ident);
1489 }
1490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
satok42c5a162011-05-26 16:46:14 +09001492 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001493 public boolean showSoftInput(IInputMethodClient client, int flags,
1494 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001495 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 long ident = Binder.clearCallingIdentity();
1497 try {
1498 synchronized (mMethodMap) {
1499 if (mCurClient == null || client == null
1500 || mCurClient.client.asBinder() != client.asBinder()) {
1501 try {
1502 // We need to check if this is the current client with
1503 // focus in the window manager, to allow this call to
1504 // be made before input is started in it.
1505 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001506 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001507 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001510 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Joe Onorato8a9b2202010-02-26 18:56:32 -08001514 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001515 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517 } finally {
1518 Binder.restoreCallingIdentity(ident);
1519 }
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project4df24232009-03-05 14:34:35 -08001522 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 mShowRequested = true;
1524 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1525 mShowExplicitlyRequested = true;
1526 }
1527 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1528 mShowExplicitlyRequested = true;
1529 mShowForced = true;
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
Dianne Hackborncc278702009-09-02 23:07:23 -07001532 if (!mSystemReady) {
1533 return false;
1534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
The Android Open Source Project4df24232009-03-05 14:34:35 -08001536 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001538 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1539 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1540 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001542 if (mHaveConnection && !mVisibleBound) {
1543 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1544 mVisibleBound = true;
1545 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001546 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001548 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 // The client has asked to have the input method shown, but
1550 // we have been sitting here too long with a connection to the
1551 // service and no interface received, so let's disconnect/connect
1552 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001553 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001555 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001557 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1558 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project4df24232009-03-05 14:34:35 -08001561 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001563
satok42c5a162011-05-26 16:46:14 +09001564 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001565 public boolean hideSoftInput(IInputMethodClient client, int flags,
1566 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001567 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 long ident = Binder.clearCallingIdentity();
1569 try {
1570 synchronized (mMethodMap) {
1571 if (mCurClient == null || client == null
1572 || mCurClient.client.asBinder() != client.asBinder()) {
1573 try {
1574 // We need to check if this is the current client with
1575 // focus in the window manager, to allow this call to
1576 // be made before input is started in it.
1577 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001578 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1579 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001580 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001581 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001584 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001585 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001590 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592 } finally {
1593 Binder.restoreCallingIdentity(ident);
1594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
The Android Open Source Project4df24232009-03-05 14:34:35 -08001597 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1599 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001602 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001605 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001609 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001611 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1612 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1613 res = true;
1614 } else {
1615 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001617 if (mHaveConnection && mVisibleBound) {
1618 mContext.unbindService(mVisibleConnection);
1619 mVisibleBound = false;
1620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 mInputShown = false;
1622 mShowRequested = false;
1623 mShowExplicitlyRequested = false;
1624 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001625 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
satok42c5a162011-05-26 16:46:14 +09001628 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001629 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1630 int controlFlags, int softInputMode, int windowFlags,
1631 EditorInfo attribute, IInputContext inputContext) {
1632 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 long ident = Binder.clearCallingIdentity();
1634 try {
1635 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001637 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001639 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
Dianne Hackborn7663d802012-02-24 13:08:49 -08001641 ClientState cs = mClients.get(client.asBinder());
1642 if (cs == null) {
1643 throw new IllegalArgumentException("unknown client "
1644 + client.asBinder());
1645 }
1646
1647 try {
1648 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1649 // Check with the window manager to make sure this client actually
1650 // has a window with focus. If not, reject. This is thread safe
1651 // because if the focus changes some time before or after, the
1652 // next client receiving focus that has any interest in input will
1653 // be calling through here after that change happens.
1654 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1655 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1656 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001658 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001661 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001662 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001663 if (attribute != null) {
1664 return startInputUncheckedLocked(cs, inputContext, attribute,
1665 controlFlags);
1666 }
1667 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001668 }
1669 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001671 // Should we auto-show the IME even if the caller has not
1672 // specified what should be done with it?
1673 // We only do this automatically if the window can resize
1674 // to accommodate the IME (so what the user sees will give
1675 // them good context without input information being obscured
1676 // by the IME) or if running on a large screen where there
1677 // is more room for the target window + IME.
1678 final boolean doAutoShow =
1679 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1680 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1681 || mRes.getConfiguration().isLayoutSizeAtLeast(
1682 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001683 final boolean isTextEditor =
1684 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1685
1686 // We want to start input before showing the IME, but after closing
1687 // it. We want to do this after closing it to help the IME disappear
1688 // more quickly (not get stuck behind it initializing itself for the
1689 // new focused input, even if its window wants to hide the IME).
1690 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1693 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001694 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1696 // There is no focus view, and this window will
1697 // be behind any soft input window, so hide the
1698 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001699 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001700 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001702 } else if (isTextEditor && doAutoShow && (softInputMode &
1703 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 // There is a focus view, and we are navigating forward
1705 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001706 // We only do this automatically if the window can resize
1707 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001708 // them good context without input information being obscured
1709 // by the IME) or if running on a large screen where there
1710 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001711 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001712 if (attribute != null) {
1713 res = startInputUncheckedLocked(cs, inputContext, attribute,
1714 controlFlags);
1715 didStart = true;
1716 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001717 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 break;
1720 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1721 // Do nothing.
1722 break;
1723 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1724 if ((softInputMode &
1725 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001726 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001727 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 break;
1730 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001731 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001732 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 break;
1734 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1735 if ((softInputMode &
1736 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001737 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001738 if (attribute != null) {
1739 res = startInputUncheckedLocked(cs, inputContext, attribute,
1740 controlFlags);
1741 didStart = true;
1742 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001743 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745 break;
1746 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001747 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001748 if (attribute != null) {
1749 res = startInputUncheckedLocked(cs, inputContext, attribute,
1750 controlFlags);
1751 didStart = true;
1752 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001753 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 break;
1755 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001756
1757 if (!didStart && attribute != null) {
1758 res = startInputUncheckedLocked(cs, inputContext, attribute,
1759 controlFlags);
1760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
1762 } finally {
1763 Binder.restoreCallingIdentity(ident);
1764 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001765
1766 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
satok42c5a162011-05-26 16:46:14 +09001769 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1771 synchronized (mMethodMap) {
1772 if (mCurClient == null || client == null
1773 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001774 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001775 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777
satok440aab52010-11-25 09:43:11 +09001778 // Always call subtype picker, because subtype picker is a superset of input method
1779 // picker.
satokab751aa2010-09-14 19:17:36 +09001780 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1781 }
1782 }
1783
satok42c5a162011-05-26 16:46:14 +09001784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001786 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1787 }
1788
satok42c5a162011-05-26 16:46:14 +09001789 @Override
satok28203512010-11-24 11:06:49 +09001790 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1791 synchronized (mMethodMap) {
1792 if (subtype != null) {
1793 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1794 mMethodMap.get(id), subtype.hashCode()));
1795 } else {
1796 setInputMethod(token, id);
1797 }
1798 }
satokab751aa2010-09-14 19:17:36 +09001799 }
1800
satok42c5a162011-05-26 16:46:14 +09001801 @Override
satokb416a712010-11-25 20:42:14 +09001802 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001803 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001804 synchronized (mMethodMap) {
1805 if (mCurClient == null || client == null
1806 || mCurClient.client.asBinder() != client.asBinder()) {
1807 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1808 }
satok7fee71f2010-12-17 18:54:26 +09001809 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1810 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001811 }
1812 }
1813
satok4fc87d62011-05-20 16:13:43 +09001814 @Override
satok735cf382010-11-11 20:40:09 +09001815 public boolean switchToLastInputMethod(IBinder token) {
1816 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001817 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001818 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001819 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001820 lastImi = mMethodMap.get(lastIme.first);
1821 } else {
1822 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001823 }
satok4fc87d62011-05-20 16:13:43 +09001824 String targetLastImiId = null;
1825 int subtypeId = NOT_A_SUBTYPE_ID;
1826 if (lastIme != null && lastImi != null) {
1827 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1828 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1829 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1830 : mCurrentSubtype.hashCode();
1831 // If the last IME is the same as the current IME and the last subtype is not
1832 // defined, there is no need to switch to the last IME.
1833 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1834 targetLastImiId = lastIme.first;
1835 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1836 }
1837 }
1838
1839 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1840 // This is a safety net. If the currentSubtype can't be added to the history
1841 // and the framework couldn't find the last ime, we will make the last ime be
1842 // the most applicable enabled keyboard subtype of the system imes.
1843 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1844 if (enabled != null) {
1845 final int N = enabled.size();
1846 final String locale = mCurrentSubtype == null
1847 ? mRes.getConfiguration().locale.toString()
1848 : mCurrentSubtype.getLocale();
1849 for (int i = 0; i < N; ++i) {
1850 final InputMethodInfo imi = enabled.get(i);
1851 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1852 InputMethodSubtype keyboardSubtype =
1853 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1854 SUBTYPE_MODE_KEYBOARD, locale, true);
1855 if (keyboardSubtype != null) {
1856 targetLastImiId = imi.getId();
1857 subtypeId = getSubtypeIdFromHashCode(
1858 imi, keyboardSubtype.hashCode());
1859 if(keyboardSubtype.getLocale().equals(locale)) {
1860 break;
1861 }
1862 }
1863 }
1864 }
1865 }
1866 }
1867
1868 if (!TextUtils.isEmpty(targetLastImiId)) {
1869 if (DEBUG) {
1870 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1871 + ", from: " + mCurMethodId + ", " + subtypeId);
1872 }
1873 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1874 return true;
1875 } else {
1876 return false;
1877 }
satok735cf382010-11-11 20:40:09 +09001878 }
1879 }
1880
satoke7c6998e2011-06-03 17:57:59 +09001881 @Override
satok688bd472012-02-09 20:09:17 +09001882 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1883 synchronized (mMethodMap) {
1884 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1885 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1886 if (nextSubtype == null) {
1887 return false;
1888 }
1889 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1890 return true;
1891 }
1892 }
1893
1894 @Override
satok68f1b782011-04-11 14:26:04 +09001895 public InputMethodSubtype getLastInputMethodSubtype() {
1896 synchronized (mMethodMap) {
1897 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1898 // TODO: Handle the case of the last IME with no subtypes
1899 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1900 || TextUtils.isEmpty(lastIme.second)) return null;
1901 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1902 if (lastImi == null) return null;
1903 try {
1904 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001905 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1906 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1907 return null;
1908 }
1909 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001910 } catch (NumberFormatException e) {
1911 return null;
1912 }
1913 }
1914 }
1915
satoke7c6998e2011-06-03 17:57:59 +09001916 @Override
satokee5e77c2011-09-02 18:50:15 +09001917 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001918 // By this IPC call, only a process which shares the same uid with the IME can add
1919 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001920 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001921 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001922 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001923 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001924 final PackageManager pm = mContext.getPackageManager();
1925 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1926 if (packageInfos != null) {
1927 final int packageNum = packageInfos.length;
1928 for (int i = 0; i < packageNum; ++i) {
1929 if (packageInfos[i].equals(imi.getPackageName())) {
1930 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001931 final long ident = Binder.clearCallingIdentity();
1932 try {
1933 buildInputMethodListLocked(mMethodList, mMethodMap);
1934 } finally {
1935 Binder.restoreCallingIdentity(ident);
1936 }
satokee5e77c2011-09-02 18:50:15 +09001937 return;
satok91e88122011-07-18 11:11:42 +09001938 }
1939 }
1940 }
satoke7c6998e2011-06-03 17:57:59 +09001941 }
satokee5e77c2011-09-02 18:50:15 +09001942 return;
satoke7c6998e2011-06-03 17:57:59 +09001943 }
1944
satok28203512010-11-24 11:06:49 +09001945 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 synchronized (mMethodMap) {
1947 if (token == null) {
1948 if (mContext.checkCallingOrSelfPermission(
1949 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1950 != PackageManager.PERMISSION_GRANTED) {
1951 throw new SecurityException(
1952 "Using null token requires permission "
1953 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1954 }
1955 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001956 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1957 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 return;
1959 }
1960
satokc5933802011-08-31 21:26:04 +09001961 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 try {
satokab751aa2010-09-14 19:17:36 +09001963 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 } finally {
1965 Binder.restoreCallingIdentity(ident);
1966 }
1967 }
1968 }
1969
satok42c5a162011-05-26 16:46:14 +09001970 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 public void hideMySoftInput(IBinder token, int flags) {
1972 synchronized (mMethodMap) {
1973 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001974 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1975 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 return;
1977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 long ident = Binder.clearCallingIdentity();
1979 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001980 hideCurrentInputLocked(flags, null);
1981 } finally {
1982 Binder.restoreCallingIdentity(ident);
1983 }
1984 }
1985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001986
satok42c5a162011-05-26 16:46:14 +09001987 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 public void showMySoftInput(IBinder token, int flags) {
1989 synchronized (mMethodMap) {
1990 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001991 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1992 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 return;
1994 }
1995 long ident = Binder.clearCallingIdentity();
1996 try {
1997 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 } finally {
1999 Binder.restoreCallingIdentity(ident);
2000 }
2001 }
2002 }
2003
2004 void setEnabledSessionInMainThread(SessionState session) {
2005 if (mEnabledSession != session) {
2006 if (mEnabledSession != null) {
2007 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002008 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 mEnabledSession.method.setSessionEnabled(
2010 mEnabledSession.session, false);
2011 } catch (RemoteException e) {
2012 }
2013 }
2014 mEnabledSession = session;
2015 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002016 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 session.method.setSessionEnabled(
2018 session.session, true);
2019 } catch (RemoteException e) {
2020 }
2021 }
2022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
satok42c5a162011-05-26 16:46:14 +09002024 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 public boolean handleMessage(Message msg) {
2026 HandlerCaller.SomeArgs args;
2027 switch (msg.what) {
2028 case MSG_SHOW_IM_PICKER:
2029 showInputMethodMenu();
2030 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002031
satokab751aa2010-09-14 19:17:36 +09002032 case MSG_SHOW_IM_SUBTYPE_PICKER:
2033 showInputMethodSubtypeMenu();
2034 return true;
2035
satok47a44912010-10-06 16:03:58 +09002036 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09002037 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002038 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09002039 return true;
2040
2041 case MSG_SHOW_IM_CONFIG:
2042 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002043 return true;
2044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 case MSG_UNBIND_INPUT:
2048 try {
2049 ((IInputMethod)msg.obj).unbindInput();
2050 } catch (RemoteException e) {
2051 // There is nothing interesting about the method dying.
2052 }
2053 return true;
2054 case MSG_BIND_INPUT:
2055 args = (HandlerCaller.SomeArgs)msg.obj;
2056 try {
2057 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2058 } catch (RemoteException e) {
2059 }
2060 return true;
2061 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002064 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2065 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 } catch (RemoteException e) {
2067 }
2068 return true;
2069 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002072 ((IInputMethod)args.arg1).hideSoftInput(0,
2073 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 } catch (RemoteException e) {
2075 }
2076 return true;
2077 case MSG_ATTACH_TOKEN:
2078 args = (HandlerCaller.SomeArgs)msg.obj;
2079 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002080 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2082 } catch (RemoteException e) {
2083 }
2084 return true;
2085 case MSG_CREATE_SESSION:
2086 args = (HandlerCaller.SomeArgs)msg.obj;
2087 try {
2088 ((IInputMethod)args.arg1).createSession(
2089 (IInputMethodCallback)args.arg2);
2090 } catch (RemoteException e) {
2091 }
2092 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 case MSG_START_INPUT:
2096 args = (HandlerCaller.SomeArgs)msg.obj;
2097 try {
2098 SessionState session = (SessionState)args.arg1;
2099 setEnabledSessionInMainThread(session);
2100 session.method.startInput((IInputContext)args.arg2,
2101 (EditorInfo)args.arg3);
2102 } catch (RemoteException e) {
2103 }
2104 return true;
2105 case MSG_RESTART_INPUT:
2106 args = (HandlerCaller.SomeArgs)msg.obj;
2107 try {
2108 SessionState session = (SessionState)args.arg1;
2109 setEnabledSessionInMainThread(session);
2110 session.method.restartInput((IInputContext)args.arg2,
2111 (EditorInfo)args.arg3);
2112 } catch (RemoteException e) {
2113 }
2114 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 case MSG_UNBIND_METHOD:
2119 try {
2120 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2121 } catch (RemoteException e) {
2122 // There is nothing interesting about the last client dying.
2123 }
2124 return true;
2125 case MSG_BIND_METHOD:
2126 args = (HandlerCaller.SomeArgs)msg.obj;
2127 try {
2128 ((IInputMethodClient)args.arg1).onBindMethod(
2129 (InputBindResult)args.arg2);
2130 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002131 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
2133 return true;
satok01038492012-04-09 21:08:27 +09002134
2135 // --------------------------------------------------------------
2136 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2137 mHardKeyboardListener.handleHardKeyboardStatusChange(
2138 msg.arg1 == 1, msg.arg2 == 1);
2139 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141 return false;
2142 }
2143
satok5b927c432012-05-01 20:09:34 +09002144 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002145 return (inputMethod.getServiceInfo().applicationInfo.flags
2146 & ApplicationInfo.FLAG_SYSTEM) != 0;
2147 }
2148
Ken Wakasa586f0512011-01-20 22:31:01 +09002149 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2150 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2151 final int subtypeCount = imi.getSubtypeCount();
2152 for (int i = 0; i < subtypeCount; ++i) {
2153 subtypes.add(imi.getSubtypeAt(i));
2154 }
2155 return subtypes;
2156 }
2157
satoka86f5e42011-09-02 17:12:42 +09002158 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2159 InputMethodInfo imi, String mode) {
2160 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2161 final int subtypeCount = imi.getSubtypeCount();
2162 for (int i = 0; i < subtypeCount; ++i) {
2163 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2164 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2165 subtypes.add(subtype);
2166 }
2167 }
2168 return subtypes;
2169 }
2170
satokdc9ddae2011-10-06 12:22:36 +09002171 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002172 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002173 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002174 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002175 int i = enabled.size();
2176 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002177 while (i > 0) {
2178 i--;
satokdc9ddae2011-10-06 12:22:36 +09002179 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002180 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2181 return imi;
2182 }
2183 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2184 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002185 }
2186 }
satok0a1bcf42012-05-16 19:26:31 +09002187 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002188 }
2189 return null;
2190 }
2191
2192 private boolean chooseNewDefaultIMELocked() {
2193 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2194 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002195 if (DEBUG) {
2196 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2197 }
satok723a27e2010-11-11 14:58:11 +09002198 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002199 return true;
2200 }
2201
2202 return false;
2203 }
2204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2206 HashMap<String, InputMethodInfo> map) {
2207 list.clear();
2208 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002211 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002212 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2213 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2214 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2215 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216
2217 List<ResolveInfo> services = pm.queryIntentServices(
2218 new Intent(InputMethod.SERVICE_INTERFACE),
2219 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002220
satoke7c6998e2011-06-03 17:57:59 +09002221 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2222 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 for (int i = 0; i < services.size(); ++i) {
2224 ResolveInfo ri = services.get(i);
2225 ServiceInfo si = ri.serviceInfo;
2226 ComponentName compName = new ComponentName(si.packageName, si.name);
2227 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2228 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002229 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 + ": it does not require the permission "
2231 + android.Manifest.permission.BIND_INPUT_METHOD);
2232 continue;
2233 }
2234
Joe Onorato8a9b2202010-02-26 18:56:32 -08002235 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236
2237 try {
satoke7c6998e2011-06-03 17:57:59 +09002238 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002240 final String id = p.getId();
2241 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242
satok5b927c432012-05-01 20:09:34 +09002243 // Valid system default IMEs and IMEs that have English subtypes are enabled
2244 // by default, unless there's a hard keyboard and the system IME was explicitly
2245 // disabled
2246 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2247 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002248 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002249 }
2250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002256 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 }
2260 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002261
satok0a1bcf42012-05-16 19:26:31 +09002262 final String defaultImiId = Settings.Secure.getString(mContext
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002263 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok0a1bcf42012-05-16 19:26:31 +09002264 if (!TextUtils.isEmpty(defaultImiId)) {
2265 if (!map.containsKey(defaultImiId)) {
2266 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2267 if (chooseNewDefaultIMELocked()) {
2268 updateFromSettingsLocked();
2269 }
2270 } else {
2271 // Double check that the default IME is certainly enabled.
2272 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002273 }
2274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002278
satokab751aa2010-09-14 19:17:36 +09002279 private void showInputMethodMenu() {
2280 showInputMethodMenuInternal(false);
2281 }
2282
2283 private void showInputMethodSubtypeMenu() {
2284 showInputMethodMenuInternal(true);
2285 }
2286
satok217f5482010-12-15 05:19:19 +09002287 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002288 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002290 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2291 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002292 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002293 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002294 }
satok217f5482010-12-15 05:19:19 +09002295 mContext.startActivity(intent);
2296 }
2297
2298 private void showConfigureInputMethods() {
2299 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2300 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2301 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2302 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002303 mContext.startActivity(intent);
2304 }
2305
satok2c93efc2012-04-02 19:33:47 +09002306 private boolean isScreenLocked() {
2307 return mKeyguardManager != null
2308 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2309 }
satokab751aa2010-09-14 19:17:36 +09002310 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002311 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002315 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002316
satok2c93efc2012-04-02 19:33:47 +09002317 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002319 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002320 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002321
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002322 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002323 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2324 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002325 if (immis == null || immis.size() == 0) {
2326 return;
2327 }
2328
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002329 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330
satok688bd472012-02-09 20:09:17 +09002331 final List<ImeSubtypeListItem> imList =
2332 mImListManager.getSortedInputMethodAndSubtypeList(
2333 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002334
satokc3690562012-01-10 20:14:43 +09002335 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2336 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2337 if (currentSubtype != null) {
2338 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2339 lastInputMethodSubtypeId =
2340 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2341 }
2342 }
2343
Ken Wakasa761eb372011-03-04 19:06:18 +09002344 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002345 mIms = new InputMethodInfo[N];
2346 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002347 int checkedItem = 0;
2348 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002349 final ImeSubtypeListItem item = imList.get(i);
2350 mIms[i] = item.mImi;
2351 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002352 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002353 int subtypeId = mSubtypeIds[i];
2354 if ((subtypeId == NOT_A_SUBTYPE_ID)
2355 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2356 || (subtypeId == lastInputMethodSubtypeId)) {
2357 checkedItem = i;
2358 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002361 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002362 com.android.internal.R.styleable.DialogPreference,
2363 com.android.internal.R.attr.alertDialogStyle, 0);
2364 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002365 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002366 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002367 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002369 }
2370 })
2371 .setIcon(a.getDrawable(
2372 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2373 a.recycle();
satok01038492012-04-09 21:08:27 +09002374 final LayoutInflater inflater =
2375 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2376 final View tv = inflater.inflate(
2377 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2378 mDialogBuilder.setCustomTitle(tv);
2379
2380 // Setup layout for a toggle switch of the hardware keyboard
2381 mSwitchingDialogTitleView = tv;
2382 mSwitchingDialogTitleView.findViewById(
2383 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2384 mWindowManagerService.isHardKeyboardAvailable() ?
2385 View.VISIBLE : View.GONE);
2386 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2387 com.android.internal.R.id.hard_keyboard_switch));
2388 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2389 hardKeySwitch.setOnCheckedChangeListener(
2390 new OnCheckedChangeListener() {
2391 @Override
2392 public void onCheckedChanged(
2393 CompoundButton buttonView, boolean isChecked) {
2394 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2395 }
2396 });
satokd87c2592010-09-29 11:52:06 +09002397
Ken Wakasa05dbb652011-08-22 15:22:43 +09002398 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2399 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2400 checkedItem);
2401
2402 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002403 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002404 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002405 public void onClick(DialogInterface dialog, int which) {
2406 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002407 if (mIms == null || mIms.length <= which
2408 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002409 return;
2410 }
2411 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002412 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002413 hideInputMethodMenu();
2414 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002415 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002416 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002417 subtypeId = NOT_A_SUBTYPE_ID;
2418 }
2419 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002420 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002423 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424
satokbc81b692011-08-26 16:22:22 +09002425 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002426 mDialogBuilder.setPositiveButton(
2427 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002428 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002429 @Override
satok7f35c8c2010-10-07 21:13:11 +09002430 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002431 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002432 }
2433 });
2434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002436 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 mSwitchingDialog.getWindow().setType(
2438 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002439 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 mSwitchingDialog.show();
2441 }
2442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002443
satok93d744d2012-05-09 17:14:08 +09002444 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002445 public final CharSequence mImeName;
2446 public final CharSequence mSubtypeName;
2447 public final InputMethodInfo mImi;
2448 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002449 private final boolean mIsSystemLocale;
2450 private final boolean mIsSystemLanguage;
2451
Ken Wakasa05dbb652011-08-22 15:22:43 +09002452 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002453 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002454 mImeName = imeName;
2455 mSubtypeName = subtypeName;
2456 mImi = imi;
2457 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002458 if (TextUtils.isEmpty(subtypeLocale)) {
2459 mIsSystemLocale = false;
2460 mIsSystemLanguage = false;
2461 } else {
2462 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2463 mIsSystemLanguage = mIsSystemLocale
2464 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2465 }
2466 }
2467
2468 @Override
2469 public int compareTo(ImeSubtypeListItem other) {
2470 if (TextUtils.isEmpty(mImeName)) {
2471 return 1;
2472 }
2473 if (TextUtils.isEmpty(other.mImeName)) {
2474 return -1;
2475 }
2476 if (!TextUtils.equals(mImeName, other.mImeName)) {
2477 return mImeName.toString().compareTo(other.mImeName.toString());
2478 }
2479 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2480 return 0;
2481 }
2482 if (mIsSystemLocale) {
2483 return -1;
2484 }
2485 if (other.mIsSystemLocale) {
2486 return 1;
2487 }
2488 if (mIsSystemLanguage) {
2489 return -1;
2490 }
2491 if (other.mIsSystemLanguage) {
2492 return 1;
2493 }
2494 if (TextUtils.isEmpty(mSubtypeName)) {
2495 return 1;
2496 }
2497 if (TextUtils.isEmpty(other.mSubtypeName)) {
2498 return -1;
2499 }
2500 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002501 }
2502 }
2503
2504 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2505 private final LayoutInflater mInflater;
2506 private final int mTextViewResourceId;
2507 private final List<ImeSubtypeListItem> mItemsList;
2508 private final int mCheckedItem;
2509 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2510 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2511 super(context, textViewResourceId, itemsList);
2512 mTextViewResourceId = textViewResourceId;
2513 mItemsList = itemsList;
2514 mCheckedItem = checkedItem;
2515 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2516 }
2517
2518 @Override
2519 public View getView(int position, View convertView, ViewGroup parent) {
2520 final View view = convertView != null ? convertView
2521 : mInflater.inflate(mTextViewResourceId, null);
2522 if (position < 0 || position >= mItemsList.size()) return view;
2523 final ImeSubtypeListItem item = mItemsList.get(position);
2524 final CharSequence imeName = item.mImeName;
2525 final CharSequence subtypeName = item.mSubtypeName;
2526 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2527 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2528 if (TextUtils.isEmpty(subtypeName)) {
2529 firstTextView.setText(imeName);
2530 secondTextView.setVisibility(View.GONE);
2531 } else {
2532 firstTextView.setText(subtypeName);
2533 secondTextView.setText(imeName);
2534 secondTextView.setVisibility(View.VISIBLE);
2535 }
2536 final RadioButton radioButton =
2537 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2538 radioButton.setChecked(position == mCheckedItem);
2539 return view;
2540 }
2541 }
2542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002544 synchronized (mMethodMap) {
2545 hideInputMethodMenuLocked();
2546 }
2547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002548
The Android Open Source Project10592532009-03-18 17:39:46 -07002549 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002550 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551
The Android Open Source Project10592532009-03-18 17:39:46 -07002552 if (mSwitchingDialog != null) {
2553 mSwitchingDialog.dismiss();
2554 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002556
The Android Open Source Project10592532009-03-18 17:39:46 -07002557 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002558 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002562
satok42c5a162011-05-26 16:46:14 +09002563 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 public boolean setInputMethodEnabled(String id, boolean enabled) {
2565 synchronized (mMethodMap) {
2566 if (mContext.checkCallingOrSelfPermission(
2567 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2568 != PackageManager.PERMISSION_GRANTED) {
2569 throw new SecurityException(
2570 "Requires permission "
2571 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2572 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 long ident = Binder.clearCallingIdentity();
2575 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002576 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 } finally {
2578 Binder.restoreCallingIdentity(ident);
2579 }
2580 }
2581 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002582
2583 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2584 // Make sure this is a valid input method.
2585 InputMethodInfo imm = mMethodMap.get(id);
2586 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002587 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002588 }
2589
satokd87c2592010-09-29 11:52:06 +09002590 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2591 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002592
satokd87c2592010-09-29 11:52:06 +09002593 if (enabled) {
2594 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2595 if (pair.first.equals(id)) {
2596 // We are enabling this input method, but it is already enabled.
2597 // Nothing to do. The previous state was enabled.
2598 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002599 }
2600 }
satokd87c2592010-09-29 11:52:06 +09002601 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2602 // Previous state was disabled.
2603 return false;
2604 } else {
2605 StringBuilder builder = new StringBuilder();
2606 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2607 builder, enabledInputMethodsList, id)) {
2608 // Disabled input method is currently selected, switch to another one.
2609 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2610 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002611 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2612 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2613 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002614 }
2615 // Previous state was enabled.
2616 return true;
2617 } else {
2618 // We are disabling the input method but it is already disabled.
2619 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002620 return false;
2621 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002622 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002623 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002624
satok57ffc002011-01-25 00:11:47 +09002625 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2626 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002627 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002628 }
2629
satok723a27e2010-11-11 14:58:11 +09002630 private void saveCurrentInputMethodAndSubtypeToHistory() {
2631 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2632 if (mCurrentSubtype != null) {
2633 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2634 }
satok57ffc002011-01-25 00:11:47 +09002635 if (canAddToLastInputMethod(mCurrentSubtype)) {
2636 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2637 }
satokab751aa2010-09-14 19:17:36 +09002638 }
2639
satok723a27e2010-11-11 14:58:11 +09002640 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2641 boolean setSubtypeOnly) {
2642 // Update the history of InputMethod and Subtype
2643 saveCurrentInputMethodAndSubtypeToHistory();
2644
2645 // Set Subtype here
2646 if (imi == null || subtypeId < 0) {
2647 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002648 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002649 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002650 if (subtypeId < imi.getSubtypeCount()) {
2651 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2652 mSettings.putSelectedSubtype(subtype.hashCode());
2653 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002654 } else {
2655 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2656 mCurrentSubtype = null;
2657 }
satokab751aa2010-09-14 19:17:36 +09002658 }
satok723a27e2010-11-11 14:58:11 +09002659
2660 if (!setSubtypeOnly) {
2661 // Set InputMethod here
2662 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2663 }
2664 }
2665
2666 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2667 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2668 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2669 // newDefaultIme is empty when there is no candidate for the selected IME.
2670 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2671 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2672 if (subtypeHashCode != null) {
2673 try {
2674 lastSubtypeId = getSubtypeIdFromHashCode(
2675 imi, Integer.valueOf(subtypeHashCode));
2676 } catch (NumberFormatException e) {
2677 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2678 }
2679 }
2680 }
2681 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002682 }
2683
2684 private int getSelectedInputMethodSubtypeId(String id) {
2685 InputMethodInfo imi = mMethodMap.get(id);
2686 if (imi == null) {
2687 return NOT_A_SUBTYPE_ID;
2688 }
satokab751aa2010-09-14 19:17:36 +09002689 int subtypeId;
2690 try {
2691 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2692 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2693 } catch (SettingNotFoundException e) {
2694 return NOT_A_SUBTYPE_ID;
2695 }
satok723a27e2010-11-11 14:58:11 +09002696 return getSubtypeIdFromHashCode(imi, subtypeId);
2697 }
2698
satokfdf419e2012-05-08 16:52:08 +09002699 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2700 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2701 }
2702
2703 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002704 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002705 final int subtypeCount = imi.getSubtypeCount();
2706 for (int i = 0; i < subtypeCount; ++i) {
2707 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002708 if (subtypeHashCode == ims.hashCode()) {
2709 return i;
2710 }
satokab751aa2010-09-14 19:17:36 +09002711 }
2712 }
2713 return NOT_A_SUBTYPE_ID;
2714 }
2715
satoka86f5e42011-09-02 17:12:42 +09002716 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2717 Resources res, InputMethodInfo imi) {
2718 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002719 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002720 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002721 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002722 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002723 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002724 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002725 // scan overriding implicitly enabled subtypes.
2726 InputMethodSubtype subtype = subtypes.get(i);
2727 if (subtype.overridesImplicitlyEnabledSubtype()) {
2728 final String mode = subtype.getMode();
2729 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2730 applicableModeAndSubtypesMap.put(mode, subtype);
2731 }
2732 }
2733 }
2734 if (applicableModeAndSubtypesMap.size() > 0) {
2735 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2736 }
2737 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002738 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002739 final String locale = subtype.getLocale();
2740 final String mode = subtype.getMode();
2741 // When system locale starts with subtype's locale, that subtype will be applicable
2742 // for system locale
2743 // For instance, it's clearly applicable for cases like system locale = en_US and
2744 // subtype = en, but it is not necessarily considered applicable for cases like system
2745 // locale = en and subtype = en_US.
2746 // We just call systemLocale.startsWith(locale) in this function because there is no
2747 // need to find applicable subtypes aggressively unlike
2748 // findLastResortApplicableSubtypeLocked.
2749 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002750 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002751 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002752 if (applicableSubtype != null) {
2753 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2754 if (!systemLocale.equals(locale)) continue;
2755 }
satok3da92232011-01-11 22:46:30 +09002756 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002757 }
2758 }
2759 final InputMethodSubtype keyboardSubtype
2760 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2761 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2762 applicableModeAndSubtypesMap.values());
2763 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2764 for (int i = 0; i < N; ++i) {
2765 final InputMethodSubtype subtype = subtypes.get(i);
2766 final String mode = subtype.getMode();
2767 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2768 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2769 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002770 }
2771 }
2772 }
satokc3690562012-01-10 20:14:43 +09002773 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002774 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002775 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002776 if (lastResortKeyboardSubtype != null) {
2777 applicableSubtypes.add(lastResortKeyboardSubtype);
2778 }
2779 }
2780 return applicableSubtypes;
2781 }
2782
satok4e4569d2010-11-19 18:45:53 +09002783 /**
2784 * If there are no selected subtypes, tries finding the most applicable one according to the
2785 * given locale.
2786 * @param subtypes this function will search the most applicable subtype in subtypes
2787 * @param mode subtypes will be filtered by mode
2788 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002789 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2790 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002791 * @return the most applicable subtypeId
2792 */
satokdf31ae62011-01-15 06:19:44 +09002793 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2794 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002795 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002796 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002797 return null;
satok8fbb1e82010-11-02 23:15:58 +09002798 }
satok4e4569d2010-11-19 18:45:53 +09002799 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002800 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002801 }
satok8fbb1e82010-11-02 23:15:58 +09002802 final String language = locale.substring(0, 2);
2803 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002804 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002805 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002806 final int N = subtypes.size();
2807 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002808 InputMethodSubtype subtype = subtypes.get(i);
2809 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002810 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2811 // and all subtypes with all modes can be candidates.
2812 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002813 if (firstMatchedModeSubtype == null) {
2814 firstMatchedModeSubtype = subtype;
2815 }
satok9ef02832010-11-04 21:17:48 +09002816 if (locale.equals(subtypeLocale)) {
2817 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002818 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002819 break;
2820 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2821 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002822 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002823 partialMatchFound = true;
2824 }
satok8fbb1e82010-11-02 23:15:58 +09002825 }
2826 }
2827
satok7599a7f2010-12-22 13:45:23 +09002828 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2829 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002830 }
2831
satok8fbb1e82010-11-02 23:15:58 +09002832 // The first subtype applicable to the system locale will be defined as the most applicable
2833 // subtype.
2834 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002835 if (applicableSubtype != null) {
2836 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2837 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2838 }
satok8fbb1e82010-11-02 23:15:58 +09002839 }
satokcd7cd292010-11-20 15:46:23 +09002840 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002841 }
2842
satok4e4569d2010-11-19 18:45:53 +09002843 // If there are no selected shortcuts, tries finding the most applicable ones.
2844 private Pair<InputMethodInfo, InputMethodSubtype>
2845 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2846 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2847 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002848 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002849 boolean foundInSystemIME = false;
2850
2851 // Search applicable subtype for each InputMethodInfo
2852 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002853 final String imiId = imi.getId();
2854 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2855 continue;
2856 }
satokcd7cd292010-11-20 15:46:23 +09002857 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002858 final List<InputMethodSubtype> enabledSubtypes =
2859 getEnabledInputMethodSubtypeList(imi, true);
2860 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002861 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002862 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002863 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002864 }
satokdf31ae62011-01-15 06:19:44 +09002865 // 2. Search by the system locale from enabledSubtypes.
2866 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002867 if (subtype == null) {
2868 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002869 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002870 }
satoka86f5e42011-09-02 17:12:42 +09002871 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2872 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2873 final ArrayList<InputMethodSubtype> subtypesForSearch =
2874 overridingImplicitlyEnabledSubtypes.isEmpty()
2875 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002876 // 4. Search by the current subtype's locale from all subtypes.
2877 if (subtype == null && mCurrentSubtype != null) {
2878 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002879 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002880 }
2881 // 5. Search by the system locale from all subtypes.
2882 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002883 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002884 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002885 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002886 }
satokcd7cd292010-11-20 15:46:23 +09002887 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002888 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002889 // The current input method is the most applicable IME.
2890 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002891 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002892 break;
satok7599a7f2010-12-22 13:45:23 +09002893 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002894 // The system input method is 2nd applicable IME.
2895 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002896 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002897 if ((imi.getServiceInfo().applicationInfo.flags
2898 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2899 foundInSystemIME = true;
2900 }
satok4e4569d2010-11-19 18:45:53 +09002901 }
2902 }
2903 }
2904 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002905 if (mostApplicableIMI != null) {
2906 Slog.w(TAG, "Most applicable shortcut input method was:"
2907 + mostApplicableIMI.getId());
2908 if (mostApplicableSubtype != null) {
2909 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2910 + "," + mostApplicableSubtype.getMode() + ","
2911 + mostApplicableSubtype.getLocale());
2912 }
2913 }
satok4e4569d2010-11-19 18:45:53 +09002914 }
satokcd7cd292010-11-20 15:46:23 +09002915 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002916 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002917 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002918 } else {
2919 return null;
2920 }
2921 }
2922
satokab751aa2010-09-14 19:17:36 +09002923 /**
2924 * @return Return the current subtype of this input method.
2925 */
satok42c5a162011-05-26 16:46:14 +09002926 @Override
satokab751aa2010-09-14 19:17:36 +09002927 public InputMethodSubtype getCurrentInputMethodSubtype() {
satokfdf419e2012-05-08 16:52:08 +09002928 if (mCurMethodId == null) {
2929 return null;
2930 }
satok4e4569d2010-11-19 18:45:53 +09002931 boolean subtypeIsSelected = false;
2932 try {
2933 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2934 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2935 } catch (SettingNotFoundException e) {
2936 }
satok3ef8b292010-11-23 06:06:29 +09002937 synchronized (mMethodMap) {
satokfdf419e2012-05-08 16:52:08 +09002938 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2939 if (imi == null || imi.getSubtypeCount() == 0) {
2940 return null;
2941 }
2942 if (!subtypeIsSelected || mCurrentSubtype == null
2943 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2944 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
satok3ef8b292010-11-23 06:06:29 +09002945 if (subtypeId == NOT_A_SUBTYPE_ID) {
satokfdf419e2012-05-08 16:52:08 +09002946 // If there are no selected subtypes, the framework will try to find
2947 // the most applicable subtype from explicitly or implicitly enabled
2948 // subtypes.
2949 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2950 getEnabledInputMethodSubtypeList(imi, true);
2951 // If there is only one explicitly or implicitly enabled subtype,
2952 // just returns it.
2953 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2954 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2955 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2956 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2957 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2958 SUBTYPE_MODE_KEYBOARD, null, true);
2959 if (mCurrentSubtype == null) {
satokd8713432011-01-18 00:55:13 +09002960 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
satokfdf419e2012-05-08 16:52:08 +09002961 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2962 true);
satokd8713432011-01-18 00:55:13 +09002963 }
satok4e4569d2010-11-19 18:45:53 +09002964 }
satokcd7cd292010-11-20 15:46:23 +09002965 } else {
satokfdf419e2012-05-08 16:52:08 +09002966 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002967 }
satok8fbb1e82010-11-02 23:15:58 +09002968 }
satok3ef8b292010-11-23 06:06:29 +09002969 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002970 }
satokab751aa2010-09-14 19:17:36 +09002971 }
2972
satokf3db1af2010-11-23 13:34:33 +09002973 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2974 InputMethodSubtype subtype) {
2975 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2976 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2977 } else {
2978 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2979 subtypes.add(subtype);
2980 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2981 }
2982 }
2983
satok4e4569d2010-11-19 18:45:53 +09002984 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002985 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002986 @Override
satok4e4569d2010-11-19 18:45:53 +09002987 public List getShortcutInputMethodsAndSubtypes() {
2988 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002989 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002990 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002991 // If there are no selected shortcut subtypes, the framework will try to find
2992 // the most applicable subtype from all subtypes whose mode is
2993 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002994 Pair<InputMethodInfo, InputMethodSubtype> info =
2995 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2996 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002997 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002998 ret.add(info.first);
2999 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003000 }
satok3da92232011-01-11 22:46:30 +09003001 return ret;
satokf3db1af2010-11-23 13:34:33 +09003002 }
satokf3db1af2010-11-23 13:34:33 +09003003 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3004 ret.add(imi);
3005 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3006 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003007 }
3008 }
satokf3db1af2010-11-23 13:34:33 +09003009 return ret;
satok4e4569d2010-11-19 18:45:53 +09003010 }
3011 }
3012
satok42c5a162011-05-26 16:46:14 +09003013 @Override
satokb66d2872010-11-10 01:04:04 +09003014 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
3015 synchronized (mMethodMap) {
3016 if (subtype != null && mCurMethodId != null) {
3017 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3018 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3019 if (subtypeId != NOT_A_SUBTYPE_ID) {
3020 setInputMethodLocked(mCurMethodId, subtypeId);
3021 return true;
3022 }
3023 }
3024 return false;
3025 }
3026 }
3027
satok688bd472012-02-09 20:09:17 +09003028 private static class InputMethodAndSubtypeListManager {
3029 private final Context mContext;
3030 private final PackageManager mPm;
3031 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003032 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003033 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3034 mContext = context;
3035 mPm = context.getPackageManager();
3036 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003037 final Locale locale = context.getResources().getConfiguration().locale;
3038 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003039 }
3040
3041 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3042 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3043 new Comparator<InputMethodInfo>() {
3044 @Override
3045 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3046 if (imi2 == null) return 0;
3047 if (imi1 == null) return 1;
3048 if (mPm == null) {
3049 return imi1.getId().compareTo(imi2.getId());
3050 }
3051 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3052 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3053 return imiId1.toString().compareTo(imiId2.toString());
3054 }
3055 });
3056
3057 public ImeSubtypeListItem getNextInputMethod(
3058 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3059 if (imi == null) {
3060 return null;
3061 }
3062 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3063 if (imList.size() <= 1) {
3064 return null;
3065 }
3066 final int N = imList.size();
3067 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003068 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003069 : NOT_A_SUBTYPE_ID;
3070 for (int i = 0; i < N; ++i) {
3071 final ImeSubtypeListItem isli = imList.get(i);
3072 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3073 if (!onlyCurrentIme) {
3074 return imList.get((i + 1) % N);
3075 }
3076 for (int j = 0; j < N - 1; ++j) {
3077 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3078 if (candidate.mImi.equals(imi)) {
3079 return candidate;
3080 }
3081 }
3082 return null;
3083 }
3084 }
3085 return null;
3086 }
3087
3088 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3089 return getSortedInputMethodAndSubtypeList(true, false, false);
3090 }
3091
3092 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3093 boolean inputShown, boolean isScreenLocked) {
3094 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3095 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3096 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3097 if (immis == null || immis.size() == 0) {
3098 return Collections.emptyList();
3099 }
3100 mSortedImmis.clear();
3101 mSortedImmis.putAll(immis);
3102 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3103 if (imi == null) continue;
3104 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3105 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3106 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3107 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3108 }
3109 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3110 final CharSequence imeLabel = imi.loadLabel(mPm);
3111 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3112 final int subtypeCount = imi.getSubtypeCount();
3113 if (DEBUG) {
3114 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3115 }
3116 for (int j = 0; j < subtypeCount; ++j) {
3117 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3118 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3119 // We show all enabled IMEs and subtypes when an IME is shown.
3120 if (enabledSubtypeSet.contains(subtypeHashCode)
3121 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3122 final CharSequence subtypeLabel =
3123 subtype.overridesImplicitlyEnabledSubtype() ? null
3124 : subtype.getDisplayName(mContext, imi.getPackageName(),
3125 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003126 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3127 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003128
3129 // Removing this subtype from enabledSubtypeSet because we no longer
3130 // need to add an entry of this subtype to imList to avoid duplicated
3131 // entries.
3132 enabledSubtypeSet.remove(subtypeHashCode);
3133 }
3134 }
3135 } else {
satok93d744d2012-05-09 17:14:08 +09003136 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3137 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003138 }
3139 }
satok93d744d2012-05-09 17:14:08 +09003140 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003141 return imList;
3142 }
3143 }
3144
satokd87c2592010-09-29 11:52:06 +09003145 /**
3146 * Utility class for putting and getting settings for InputMethod
3147 * TODO: Move all putters and getters of settings to this class.
3148 */
3149 private static class InputMethodSettings {
3150 // The string for enabled input method is saved as follows:
3151 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3152 private static final char INPUT_METHOD_SEPARATER = ':';
3153 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003154 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003155 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3156
satok723a27e2010-11-11 14:58:11 +09003157 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003158 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3159
satokdf31ae62011-01-15 06:19:44 +09003160 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003161 private final ContentResolver mResolver;
3162 private final HashMap<String, InputMethodInfo> mMethodMap;
3163 private final ArrayList<InputMethodInfo> mMethodList;
3164
3165 private String mEnabledInputMethodsStrCache;
3166
3167 private static void buildEnabledInputMethodsSettingString(
3168 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3169 String id = pair.first;
3170 ArrayList<String> subtypes = pair.second;
3171 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003172 // Inputmethod and subtypes are saved in the settings as follows:
3173 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3174 for (String subtypeId: subtypes) {
3175 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003176 }
3177 }
3178
3179 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003180 Resources res, ContentResolver resolver,
3181 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3182 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003183 mResolver = resolver;
3184 mMethodMap = methodMap;
3185 mMethodList = methodList;
3186 }
3187
3188 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3189 return createEnabledInputMethodListLocked(
3190 getEnabledInputMethodsAndSubtypeListLocked());
3191 }
3192
satok7f35c8c2010-10-07 21:13:11 +09003193 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003194 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3195 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003196 getEnabledInputMethodsAndSubtypeListLocked());
3197 }
3198
satok67ddf9c2010-11-17 09:45:54 +09003199 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3200 InputMethodInfo imi) {
3201 List<Pair<String, ArrayList<String>>> imsList =
3202 getEnabledInputMethodsAndSubtypeListLocked();
3203 ArrayList<InputMethodSubtype> enabledSubtypes =
3204 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003205 if (imi != null) {
3206 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3207 InputMethodInfo info = mMethodMap.get(imsPair.first);
3208 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003209 final int subtypeCount = info.getSubtypeCount();
3210 for (int i = 0; i < subtypeCount; ++i) {
3211 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003212 for (String s: imsPair.second) {
3213 if (String.valueOf(ims.hashCode()).equals(s)) {
3214 enabledSubtypes.add(ims);
3215 }
satok67ddf9c2010-11-17 09:45:54 +09003216 }
3217 }
satok884ef9a2010-11-18 10:39:46 +09003218 break;
satok67ddf9c2010-11-17 09:45:54 +09003219 }
satok67ddf9c2010-11-17 09:45:54 +09003220 }
3221 }
3222 return enabledSubtypes;
3223 }
3224
satokd87c2592010-09-29 11:52:06 +09003225 // At the initial boot, the settings for input methods are not set,
3226 // so we need to enable IME in that case.
3227 public void enableAllIMEsIfThereIsNoEnabledIME() {
3228 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3229 StringBuilder sb = new StringBuilder();
3230 final int N = mMethodList.size();
3231 for (int i = 0; i < N; i++) {
3232 InputMethodInfo imi = mMethodList.get(i);
3233 Slog.i(TAG, "Adding: " + imi.getId());
3234 if (i > 0) sb.append(':');
3235 sb.append(imi.getId());
3236 }
3237 putEnabledInputMethodsStr(sb.toString());
3238 }
3239 }
3240
satokbb4aa062011-01-19 21:40:27 +09003241 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003242 ArrayList<Pair<String, ArrayList<String>>> imsList
3243 = new ArrayList<Pair<String, ArrayList<String>>>();
3244 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3245 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3246 return imsList;
3247 }
satok723a27e2010-11-11 14:58:11 +09003248 mInputMethodSplitter.setString(enabledInputMethodsStr);
3249 while (mInputMethodSplitter.hasNext()) {
3250 String nextImsStr = mInputMethodSplitter.next();
3251 mSubtypeSplitter.setString(nextImsStr);
3252 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003253 ArrayList<String> subtypeHashes = new ArrayList<String>();
3254 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003255 String imeId = mSubtypeSplitter.next();
3256 while (mSubtypeSplitter.hasNext()) {
3257 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003258 }
3259 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3260 }
3261 }
3262 return imsList;
3263 }
3264
3265 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3266 if (reloadInputMethodStr) {
3267 getEnabledInputMethodsStr();
3268 }
3269 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3270 // Add in the newly enabled input method.
3271 putEnabledInputMethodsStr(id);
3272 } else {
3273 putEnabledInputMethodsStr(
3274 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3275 }
3276 }
3277
3278 /**
3279 * Build and put a string of EnabledInputMethods with removing specified Id.
3280 * @return the specified id was removed or not.
3281 */
3282 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3283 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3284 boolean isRemoved = false;
3285 boolean needsAppendSeparator = false;
3286 for (Pair<String, ArrayList<String>> ims: imsList) {
3287 String curId = ims.first;
3288 if (curId.equals(id)) {
3289 // We are disabling this input method, and it is
3290 // currently enabled. Skip it to remove from the
3291 // new list.
3292 isRemoved = true;
3293 } else {
3294 if (needsAppendSeparator) {
3295 builder.append(INPUT_METHOD_SEPARATER);
3296 } else {
3297 needsAppendSeparator = true;
3298 }
3299 buildEnabledInputMethodsSettingString(builder, ims);
3300 }
3301 }
3302 if (isRemoved) {
3303 // Update the setting with the new list of input methods.
3304 putEnabledInputMethodsStr(builder.toString());
3305 }
3306 return isRemoved;
3307 }
3308
3309 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3310 List<Pair<String, ArrayList<String>>> imsList) {
3311 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3312 for (Pair<String, ArrayList<String>> ims: imsList) {
3313 InputMethodInfo info = mMethodMap.get(ims.first);
3314 if (info != null) {
3315 res.add(info);
3316 }
3317 }
3318 return res;
3319 }
3320
satok7f35c8c2010-10-07 21:13:11 +09003321 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003322 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003323 List<Pair<String, ArrayList<String>>> imsList) {
3324 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3325 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3326 for (Pair<String, ArrayList<String>> ims : imsList) {
3327 InputMethodInfo info = mMethodMap.get(ims.first);
3328 if (info != null) {
3329 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3330 }
3331 }
3332 return res;
3333 }
3334
satokd87c2592010-09-29 11:52:06 +09003335 private void putEnabledInputMethodsStr(String str) {
3336 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3337 mEnabledInputMethodsStrCache = str;
3338 }
3339
3340 private String getEnabledInputMethodsStr() {
3341 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3342 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003343 if (DEBUG) {
3344 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3345 }
satokd87c2592010-09-29 11:52:06 +09003346 return mEnabledInputMethodsStrCache;
3347 }
satok723a27e2010-11-11 14:58:11 +09003348
3349 private void saveSubtypeHistory(
3350 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3351 StringBuilder builder = new StringBuilder();
3352 boolean isImeAdded = false;
3353 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3354 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3355 newSubtypeId);
3356 isImeAdded = true;
3357 }
3358 for (Pair<String, String> ime: savedImes) {
3359 String imeId = ime.first;
3360 String subtypeId = ime.second;
3361 if (TextUtils.isEmpty(subtypeId)) {
3362 subtypeId = NOT_A_SUBTYPE_ID_STR;
3363 }
3364 if (isImeAdded) {
3365 builder.append(INPUT_METHOD_SEPARATER);
3366 } else {
3367 isImeAdded = true;
3368 }
3369 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3370 subtypeId);
3371 }
3372 // Remove the last INPUT_METHOD_SEPARATER
3373 putSubtypeHistoryStr(builder.toString());
3374 }
3375
3376 public void addSubtypeToHistory(String imeId, String subtypeId) {
3377 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3378 for (Pair<String, String> ime: subtypeHistory) {
3379 if (ime.first.equals(imeId)) {
3380 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003381 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003382 + ime.second);
3383 }
3384 // We should break here
3385 subtypeHistory.remove(ime);
3386 break;
3387 }
3388 }
satokbb4aa062011-01-19 21:40:27 +09003389 if (DEBUG) {
3390 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3391 }
satok723a27e2010-11-11 14:58:11 +09003392 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3393 }
3394
3395 private void putSubtypeHistoryStr(String str) {
3396 if (DEBUG) {
3397 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3398 }
3399 Settings.Secure.putString(
3400 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3401 }
3402
3403 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3404 // Gets the first one from the history
3405 return getLastSubtypeForInputMethodLockedInternal(null);
3406 }
3407
3408 public String getLastSubtypeForInputMethodLocked(String imeId) {
3409 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3410 if (ime != null) {
3411 return ime.second;
3412 } else {
3413 return null;
3414 }
3415 }
3416
3417 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3418 List<Pair<String, ArrayList<String>>> enabledImes =
3419 getEnabledInputMethodsAndSubtypeListLocked();
3420 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003421 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003422 final String imeInTheHistory = imeAndSubtype.first;
3423 // If imeId is empty, returns the first IME and subtype in the history
3424 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3425 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003426 final String subtypeHashCode =
3427 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3428 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003429 if (!TextUtils.isEmpty(subtypeHashCode)) {
3430 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003431 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003432 }
3433 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3434 }
3435 }
3436 }
3437 if (DEBUG) {
3438 Slog.d(TAG, "No enabled IME found in the history");
3439 }
3440 return null;
3441 }
3442
satokdf31ae62011-01-15 06:19:44 +09003443 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003444 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3445 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3446 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003447 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003448 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003449 if (explicitlyEnabledSubtypes.size() == 0) {
3450 // If there are no explicitly enabled subtypes, applicable subtypes are
3451 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003452 // If IME is enabled and no subtypes are enabled, applicable subtypes
3453 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003454 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003455 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003456 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003457 if (implicitlySelectedSubtypes != null) {
3458 final int N = implicitlySelectedSubtypes.size();
3459 for (int i = 0; i < N; ++i) {
3460 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3461 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3462 return subtypeHashCode;
3463 }
3464 }
3465 }
3466 }
3467 } else {
satokf6cafb62011-01-17 16:29:02 +09003468 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003469 if (s.equals(subtypeHashCode)) {
3470 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003471 try {
3472 final int hashCode = Integer.valueOf(subtypeHashCode);
3473 // Check whether the subtype id is valid or not
3474 if (isValidSubtypeId(imi, hashCode)) {
3475 return s;
3476 } else {
3477 return NOT_A_SUBTYPE_ID_STR;
3478 }
3479 } catch (NumberFormatException e) {
3480 return NOT_A_SUBTYPE_ID_STR;
3481 }
satokdf31ae62011-01-15 06:19:44 +09003482 }
satok723a27e2010-11-11 14:58:11 +09003483 }
3484 }
3485 // If imeId was enabled but subtypeId was disabled.
3486 return NOT_A_SUBTYPE_ID_STR;
3487 }
3488 }
3489 // If both imeId and subtypeId are disabled, return null
3490 return null;
3491 }
3492
3493 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3494 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3495 final String subtypeHistoryStr = getSubtypeHistoryStr();
3496 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3497 return imsList;
3498 }
3499 mInputMethodSplitter.setString(subtypeHistoryStr);
3500 while (mInputMethodSplitter.hasNext()) {
3501 String nextImsStr = mInputMethodSplitter.next();
3502 mSubtypeSplitter.setString(nextImsStr);
3503 if (mSubtypeSplitter.hasNext()) {
3504 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3505 // The first element is ime id.
3506 String imeId = mSubtypeSplitter.next();
3507 while (mSubtypeSplitter.hasNext()) {
3508 subtypeId = mSubtypeSplitter.next();
3509 break;
3510 }
3511 imsList.add(new Pair<String, String>(imeId, subtypeId));
3512 }
3513 }
3514 return imsList;
3515 }
3516
3517 private String getSubtypeHistoryStr() {
3518 if (DEBUG) {
3519 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3520 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3521 }
3522 return Settings.Secure.getString(
3523 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3524 }
3525
3526 public void putSelectedInputMethod(String imeId) {
3527 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3528 }
3529
3530 public void putSelectedSubtype(int subtypeId) {
3531 Settings.Secure.putInt(
3532 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3533 }
satokd87c2592010-09-29 11:52:06 +09003534 }
3535
satoke7c6998e2011-06-03 17:57:59 +09003536 private static class InputMethodFileManager {
3537 private static final String SYSTEM_PATH = "system";
3538 private static final String INPUT_METHOD_PATH = "inputmethod";
3539 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3540 private static final String NODE_SUBTYPES = "subtypes";
3541 private static final String NODE_SUBTYPE = "subtype";
3542 private static final String NODE_IMI = "imi";
3543 private static final String ATTR_ID = "id";
3544 private static final String ATTR_LABEL = "label";
3545 private static final String ATTR_ICON = "icon";
3546 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3547 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3548 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3549 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3550 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3551 private final HashMap<String, InputMethodInfo> mMethodMap;
3552 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3553 new HashMap<String, List<InputMethodSubtype>>();
3554 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3555 if (methodMap == null) {
3556 throw new NullPointerException("methodMap is null");
3557 }
3558 mMethodMap = methodMap;
3559 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3560 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3561 if (!inputMethodDir.mkdirs()) {
3562 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3563 }
3564 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3565 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3566 if (!subtypeFile.exists()) {
3567 // If "subtypes.xml" doesn't exist, create a blank file.
3568 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3569 methodMap);
3570 } else {
3571 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3572 }
3573 }
3574
3575 private void deleteAllInputMethodSubtypes(String imiId) {
3576 synchronized (mMethodMap) {
3577 mSubtypesMap.remove(imiId);
3578 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3579 mMethodMap);
3580 }
3581 }
3582
3583 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003584 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003585 synchronized (mMethodMap) {
3586 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3587 final int N = additionalSubtypes.length;
3588 for (int i = 0; i < N; ++i) {
3589 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003590 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003591 subtypes.add(subtype);
3592 }
3593 }
satok4a28bde2011-06-29 21:03:40 +09003594 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003595 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3596 mMethodMap);
3597 }
3598 }
3599
3600 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3601 synchronized (mMethodMap) {
3602 return mSubtypesMap;
3603 }
3604 }
3605
3606 private static void writeAdditionalInputMethodSubtypes(
3607 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3608 HashMap<String, InputMethodInfo> methodMap) {
3609 // Safety net for the case that this function is called before methodMap is set.
3610 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3611 FileOutputStream fos = null;
3612 try {
3613 fos = subtypesFile.startWrite();
3614 final XmlSerializer out = new FastXmlSerializer();
3615 out.setOutput(fos, "utf-8");
3616 out.startDocument(null, true);
3617 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3618 out.startTag(null, NODE_SUBTYPES);
3619 for (String imiId : allSubtypes.keySet()) {
3620 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3621 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3622 continue;
3623 }
3624 out.startTag(null, NODE_IMI);
3625 out.attribute(null, ATTR_ID, imiId);
3626 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3627 final int N = subtypesList.size();
3628 for (int i = 0; i < N; ++i) {
3629 final InputMethodSubtype subtype = subtypesList.get(i);
3630 out.startTag(null, NODE_SUBTYPE);
3631 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3632 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3633 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3634 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3635 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3636 out.attribute(null, ATTR_IS_AUXILIARY,
3637 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3638 out.endTag(null, NODE_SUBTYPE);
3639 }
3640 out.endTag(null, NODE_IMI);
3641 }
3642 out.endTag(null, NODE_SUBTYPES);
3643 out.endDocument();
3644 subtypesFile.finishWrite(fos);
3645 } catch (java.io.IOException e) {
3646 Slog.w(TAG, "Error writing subtypes", e);
3647 if (fos != null) {
3648 subtypesFile.failWrite(fos);
3649 }
3650 }
3651 }
3652
3653 private static void readAdditionalInputMethodSubtypes(
3654 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3655 if (allSubtypes == null || subtypesFile == null) return;
3656 allSubtypes.clear();
3657 FileInputStream fis = null;
3658 try {
3659 fis = subtypesFile.openRead();
3660 final XmlPullParser parser = Xml.newPullParser();
3661 parser.setInput(fis, null);
3662 int type = parser.getEventType();
3663 // Skip parsing until START_TAG
3664 while ((type = parser.next()) != XmlPullParser.START_TAG
3665 && type != XmlPullParser.END_DOCUMENT) {}
3666 String firstNodeName = parser.getName();
3667 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3668 throw new XmlPullParserException("Xml doesn't start with subtypes");
3669 }
3670 final int depth =parser.getDepth();
3671 String currentImiId = null;
3672 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3673 while (((type = parser.next()) != XmlPullParser.END_TAG
3674 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3675 if (type != XmlPullParser.START_TAG)
3676 continue;
3677 final String nodeName = parser.getName();
3678 if (NODE_IMI.equals(nodeName)) {
3679 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3680 if (TextUtils.isEmpty(currentImiId)) {
3681 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3682 continue;
3683 }
3684 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3685 allSubtypes.put(currentImiId, tempSubtypesArray);
3686 } else if (NODE_SUBTYPE.equals(nodeName)) {
3687 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3688 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3689 continue;
3690 }
3691 final int icon = Integer.valueOf(
3692 parser.getAttributeValue(null, ATTR_ICON));
3693 final int label = Integer.valueOf(
3694 parser.getAttributeValue(null, ATTR_LABEL));
3695 final String imeSubtypeLocale =
3696 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3697 final String imeSubtypeMode =
3698 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3699 final String imeSubtypeExtraValue =
3700 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003701 final boolean isAuxiliary = "1".equals(String.valueOf(
3702 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003703 final InputMethodSubtype subtype =
3704 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3705 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3706 tempSubtypesArray.add(subtype);
3707 }
3708 }
3709 } catch (XmlPullParserException e) {
3710 Slog.w(TAG, "Error reading subtypes: " + e);
3711 return;
3712 } catch (java.io.IOException e) {
3713 Slog.w(TAG, "Error reading subtypes: " + e);
3714 return;
3715 } catch (NumberFormatException e) {
3716 Slog.w(TAG, "Error reading subtypes: " + e);
3717 return;
3718 } finally {
3719 if (fis != null) {
3720 try {
3721 fis.close();
3722 } catch (java.io.IOException e1) {
3723 Slog.w(TAG, "Failed to close.");
3724 }
3725 }
3726 }
3727 }
3728 }
3729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 @Override
3733 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3734 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3735 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3738 + Binder.getCallingPid()
3739 + ", uid=" + Binder.getCallingUid());
3740 return;
3741 }
3742
3743 IInputMethod method;
3744 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 synchronized (mMethodMap) {
3749 p.println("Current Input Method Manager state:");
3750 int N = mMethodList.size();
3751 p.println(" Input Methods:");
3752 for (int i=0; i<N; i++) {
3753 InputMethodInfo info = mMethodList.get(i);
3754 p.println(" InputMethod #" + i + ":");
3755 info.dump(p, " ");
3756 }
3757 p.println(" Clients:");
3758 for (ClientState ci : mClients.values()) {
3759 p.println(" Client " + ci + ":");
3760 p.println(" client=" + ci.client);
3761 p.println(" inputContext=" + ci.inputContext);
3762 p.println(" sessionRequested=" + ci.sessionRequested);
3763 p.println(" curSession=" + ci.curSession);
3764 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003765 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003767 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3768 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3770 + " mBoundToMethod=" + mBoundToMethod);
3771 p.println(" mCurToken=" + mCurToken);
3772 p.println(" mCurIntent=" + mCurIntent);
3773 method = mCurMethod;
3774 p.println(" mCurMethod=" + mCurMethod);
3775 p.println(" mEnabledSession=" + mEnabledSession);
3776 p.println(" mShowRequested=" + mShowRequested
3777 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3778 + " mShowForced=" + mShowForced
3779 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003780 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003782
Jeff Brownb88102f2010-09-08 11:49:43 -07003783 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 pw.flush();
3786 try {
3787 client.client.asBinder().dump(fd, args);
3788 } catch (RemoteException e) {
3789 p.println("Input method client dead: " + e);
3790 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003791 } else {
3792 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003794
Jeff Brownb88102f2010-09-08 11:49:43 -07003795 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 pw.flush();
3798 try {
3799 method.asBinder().dump(fd, args);
3800 } catch (RemoteException e) {
3801 p.println("Input method service dead: " + e);
3802 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003803 } else {
3804 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806 }
3807}