blob: ff14568c8956618a08f4830deba3d8c07c5a19cc [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);
satokd81e9502012-05-21 12:58:45 +0900660 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900661 mLastSystemLocale = newLocale;
662 }
663 }
664
665 private void resetDefaultImeLocked(Context context) {
666 // Do not reset the default (current) IME when it is a 3rd-party IME
667 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
668 return;
669 }
670
671 InputMethodInfo defIm = null;
672 for (InputMethodInfo imi : mMethodList) {
673 if (defIm == null) {
674 if (isValidSystemDefaultIme(imi, context)) {
675 defIm = imi;
676 Slog.i(TAG, "Selected default: " + imi.getId());
677 }
678 }
679 }
680 if (defIm == null && mMethodList.size() > 0) {
681 defIm = getMostApplicableDefaultIMELocked();
682 Slog.i(TAG, "No default found, using " + defIm.getId());
683 }
684 if (defIm != null) {
685 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
686 }
687 }
688
satok0a1bcf42012-05-16 19:26:31 +0900689 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
690 if (!mSystemReady) {
691 return false;
692 }
satok5b927c432012-05-01 20:09:34 +0900693 if (!isSystemIme(imi)) {
694 return false;
695 }
696 if (imi.getIsDefaultResourceId() != 0) {
697 try {
698 Resources res = context.createPackageContext(
699 imi.getPackageName(), 0).getResources();
700 if (res.getBoolean(imi.getIsDefaultResourceId())
701 && containsSubtypeOf(imi, context.getResources().getConfiguration().
702 locale.getLanguage())) {
703 return true;
704 }
705 } catch (PackageManager.NameNotFoundException ex) {
706 } catch (Resources.NotFoundException ex) {
707 }
708 }
709 if (imi.getSubtypeCount() == 0) {
710 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
711 }
712 return false;
713 }
714
715 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
716 if (!isSystemIme(imi)) {
717 return false;
718 }
719 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
720 }
721
722 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
723 final int N = imi.getSubtypeCount();
724 for (int i = 0; i < N; ++i) {
725 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
726 return true;
727 }
728 }
729 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 @Override
733 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
734 throws RemoteException {
735 try {
736 return super.onTransact(code, data, reply, flags);
737 } catch (RuntimeException e) {
738 // The input method manager only throws security exceptions, so let's
739 // log all others.
740 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800741 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743 throw e;
744 }
745 }
746
Dianne Hackborn661cd522011-08-22 00:26:20 -0700747 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700748 synchronized (mMethodMap) {
749 if (!mSystemReady) {
750 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700751 mKeyguardManager = (KeyguardManager)
752 mContext.getSystemService(Context.KEYGUARD_SERVICE);
753 mNotificationManager = (NotificationManager)
754 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
755 mStatusBar = statusBar;
756 statusBar.setIconVisibility("ime", false);
757 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900758 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
759 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900760 if (mShowOngoingImeSwitcherForPhones) {
761 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
762 mHardKeyboardListener);
763 }
satok0a1bcf42012-05-16 19:26:31 +0900764 buildInputMethodListLocked(mMethodList, mMethodMap);
765 if (!mImeSelectedOnBoot) {
766 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
767 checkCurrentLocaleChangedLocked();
768 }
769 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700770 try {
771 startInputInnerLocked();
772 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700774 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700775 }
776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
satok15452a42011-10-28 17:58:28 +0900779 private void setImeWindowVisibilityStatusHiddenLocked() {
780 mImeWindowVis = 0;
781 updateImeWindowStatusLocked();
782 }
783
satok3afd6c02011-11-18 08:38:19 +0900784 private void refreshImeWindowVisibilityLocked() {
785 final Configuration conf = mRes.getConfiguration();
786 final boolean haveHardKeyboard = conf.keyboard
787 != Configuration.KEYBOARD_NOKEYS;
788 final boolean hardKeyShown = haveHardKeyboard
789 && conf.hardKeyboardHidden
790 != Configuration.HARDKEYBOARDHIDDEN_YES;
791 final boolean isScreenLocked = mKeyguardManager != null
792 && mKeyguardManager.isKeyguardLocked()
793 && mKeyguardManager.isKeyguardSecure();
794 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
795 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
796 updateImeWindowStatusLocked();
797 }
798
satok15452a42011-10-28 17:58:28 +0900799 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900800 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700801 }
802
satoke7c6998e2011-06-03 17:57:59 +0900803 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 public List<InputMethodInfo> getInputMethodList() {
805 synchronized (mMethodMap) {
806 return new ArrayList<InputMethodInfo>(mMethodList);
807 }
808 }
809
satoke7c6998e2011-06-03 17:57:59 +0900810 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 public List<InputMethodInfo> getEnabledInputMethodList() {
812 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900813 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815 }
816
satokbb4aa062011-01-19 21:40:27 +0900817 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
818 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
819 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
820 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
821 for (InputMethodInfo imi: getEnabledInputMethodList()) {
822 enabledInputMethodAndSubtypes.put(
823 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
824 }
825 return enabledInputMethodAndSubtypes;
826 }
827
828 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
829 boolean allowsImplicitlySelectedSubtypes) {
830 if (imi == null && mCurMethodId != null) {
831 imi = mMethodMap.get(mCurMethodId);
832 }
satok7265d9b2011-02-14 15:47:30 +0900833 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900834 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900835 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900836 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900837 }
satok7265d9b2011-02-14 15:47:30 +0900838 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900839 }
840
satoke7c6998e2011-06-03 17:57:59 +0900841 @Override
satok16331c82010-12-20 23:48:46 +0900842 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
843 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900844 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900845 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900846 }
847 }
848
satoke7c6998e2011-06-03 17:57:59 +0900849 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 public void addClient(IInputMethodClient client,
851 IInputContext inputContext, int uid, int pid) {
852 synchronized (mMethodMap) {
853 mClients.put(client.asBinder(), new ClientState(client,
854 inputContext, uid, pid));
855 }
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
satoke7c6998e2011-06-03 17:57:59 +0900858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 public void removeClient(IInputMethodClient client) {
860 synchronized (mMethodMap) {
861 mClients.remove(client.asBinder());
862 }
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 void executeOrSendMessage(IInterface target, Message msg) {
866 if (target.asBinder() instanceof Binder) {
867 mCaller.sendMessage(msg);
868 } else {
869 handleMessage(msg);
870 msg.recycle();
871 }
872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700874 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800876 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 + mCurClient.client.asBinder());
878 if (mBoundToMethod) {
879 mBoundToMethod = false;
880 if (mCurMethod != null) {
881 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
882 MSG_UNBIND_INPUT, mCurMethod));
883 }
884 }
885 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
886 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
887 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 // Call setActive(false) on the old client
890 try {
891 mCurClient.client.setActive(false);
892 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800893 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 + mCurClient.pid + " uid " + mCurClient.uid);
895 }
896 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Project10592532009-03-18 17:39:46 -0700898 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 private int getImeShowFlags() {
903 int flags = 0;
904 if (mShowForced) {
905 flags |= InputMethod.SHOW_FORCED
906 | InputMethod.SHOW_EXPLICIT;
907 } else if (mShowExplicitlyRequested) {
908 flags |= InputMethod.SHOW_EXPLICIT;
909 }
910 return flags;
911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 private int getAppShowFlags() {
914 int flags = 0;
915 if (mShowForced) {
916 flags |= InputMethodManager.SHOW_FORCED;
917 } else if (!mShowExplicitlyRequested) {
918 flags |= InputMethodManager.SHOW_IMPLICIT;
919 }
920 return flags;
921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
Dianne Hackborn7663d802012-02-24 13:08:49 -0800923 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (!mBoundToMethod) {
925 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
926 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
927 mBoundToMethod = true;
928 }
929 final SessionState session = mCurClient.curSession;
930 if (initial) {
931 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
932 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
933 } else {
934 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
935 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
936 }
937 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800939 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800941 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800945 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 // If no method is currently selected, do nothing.
947 if (mCurMethodId == null) {
948 return mNoBinding;
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 ClientState cs = mClients.get(client.asBinder());
952 if (cs == null) {
953 throw new IllegalArgumentException("unknown client "
954 + client.asBinder());
955 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 try {
958 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
959 // Check with the window manager to make sure this client actually
960 // has a window with focus. If not, reject. This is thread safe
961 // because if the focus changes some time before or after, the
962 // next client receiving focus that has any interest in input will
963 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800964 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
966 return null;
967 }
968 } catch (RemoteException e) {
969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800970
Dianne Hackborn7663d802012-02-24 13:08:49 -0800971 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
972 }
973
974 InputBindResult startInputUncheckedLocked(ClientState cs,
975 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
976 // If no method is currently selected, do nothing.
977 if (mCurMethodId == null) {
978 return mNoBinding;
979 }
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (mCurClient != cs) {
982 // If the client is changing, we need to switch over to the new
983 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800985 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 + cs.client.asBinder());
987
988 // If the screen is on, inform the new client it is active
989 if (mScreenOn) {
990 try {
991 cs.client.setActive(mScreenOn);
992 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800993 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 + cs.pid + " uid " + cs.uid);
995 }
996 }
997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 // Bump up the sequence for this client and attach it.
1000 mCurSeq++;
1001 if (mCurSeq <= 0) mCurSeq = 1;
1002 mCurClient = cs;
1003 mCurInputContext = inputContext;
1004 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 // Check if the input method is changing.
1007 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1008 if (cs.curSession != null) {
1009 // Fast case: if we are already connected to the input method,
1010 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001011 return attachNewInputLocked(
1012 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014 if (mHaveConnection) {
1015 if (mCurMethod != null) {
1016 if (!cs.sessionRequested) {
1017 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1020 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001021 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 // Return to client, and we will get back with it when
1024 // we have had a session made for it.
1025 return new InputBindResult(null, mCurId, mCurSeq);
1026 } else if (SystemClock.uptimeMillis()
1027 < (mLastBindTime+TIME_TO_RECONNECT)) {
1028 // In this case we have connected to the service, but
1029 // don't yet have its interface. If it hasn't been too
1030 // long since we did the connection, we'll return to
1031 // the client and wait to get the service interface so
1032 // we can report back. If it has been too long, we want
1033 // to fall through so we can try a disconnect/reconnect
1034 // to see if we can get back in touch with the service.
1035 return new InputBindResult(null, mCurId, mCurSeq);
1036 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1038 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 }
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001043 return startInputInnerLocked();
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001046 InputBindResult startInputInnerLocked() {
1047 if (mCurMethodId == null) {
1048 return mNoBinding;
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001051 if (!mSystemReady) {
1052 // If the system is not yet ready, we shouldn't be running third
1053 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001054 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1058 if (info == null) {
1059 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001062 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1065 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001066 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1067 com.android.internal.R.string.input_method_binding_label);
1068 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1069 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001070 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1071 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 mLastBindTime = SystemClock.uptimeMillis();
1073 mHaveConnection = true;
1074 mCurId = info.getId();
1075 mCurToken = new Binder();
1076 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mIWindowManager.addWindowToken(mCurToken,
1079 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1080 } catch (RemoteException e) {
1081 }
1082 return new InputBindResult(null, mCurId, mCurSeq);
1083 } else {
1084 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001085 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 + mCurIntent);
1087 }
1088 return null;
1089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001090
satoke7c6998e2011-06-03 17:57:59 +09001091 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001093 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 synchronized (mMethodMap) {
1095 final long ident = Binder.clearCallingIdentity();
1096 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001097 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } finally {
1099 Binder.restoreCallingIdentity(ident);
1100 }
1101 }
1102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103
satoke7c6998e2011-06-03 17:57:59 +09001104 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 public void finishInput(IInputMethodClient client) {
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
satoke7c6998e2011-06-03 17:57:59 +09001108 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 public void onServiceConnected(ComponentName name, IBinder service) {
1110 synchronized (mMethodMap) {
1111 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1112 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001113 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001114 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001115 unbindCurrentMethodLocked(false);
1116 return;
1117 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001118 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001119 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1120 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001122 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001123 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001125 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001126 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 }
1129 }
1130 }
1131
1132 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1133 synchronized (mMethodMap) {
1134 if (mCurMethod != null && method != null
1135 && mCurMethod.asBinder() == method.asBinder()) {
1136 if (mCurClient != null) {
1137 mCurClient.curSession = new SessionState(mCurClient,
1138 method, session);
1139 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001140 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 if (res.method != null) {
1142 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1143 MSG_BIND_METHOD, mCurClient.client, res));
1144 }
1145 }
1146 }
1147 }
1148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001150 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001151 if (mVisibleBound) {
1152 mContext.unbindService(mVisibleConnection);
1153 mVisibleBound = false;
1154 }
1155
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001156 if (mHaveConnection) {
1157 mContext.unbindService(this);
1158 mHaveConnection = false;
1159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001161 if (mCurToken != null) {
1162 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001163 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
satoke0a99412012-05-10 02:22:58 +09001164 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1165 // The current IME is shown. Hence an IME switch (transition) is happening.
1166 mWindowManagerService.saveLastInputMethodWindowForTransition();
1167 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001168 mIWindowManager.removeWindowToken(mCurToken);
1169 } catch (RemoteException e) {
1170 }
1171 mCurToken = null;
1172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173
The Android Open Source Project10592532009-03-18 17:39:46 -07001174 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001175 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001176
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001177 if (reportToClient && mCurClient != null) {
1178 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1179 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1180 }
1181 }
1182
Devin Taylor0c33ed22010-02-23 13:26:46 -06001183 private void finishSession(SessionState sessionState) {
1184 if (sessionState != null && sessionState.session != null) {
1185 try {
1186 sessionState.session.finishSession();
1187 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001188 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001189 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001190 }
1191 }
1192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001194 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 if (mCurMethod != null) {
1196 for (ClientState cs : mClients.values()) {
1197 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001198 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 cs.curSession = null;
1200 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001201
1202 finishSession(mEnabledSession);
1203 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 mCurMethod = null;
1205 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001206 if (mStatusBar != null) {
1207 mStatusBar.setIconVisibility("ime", false);
1208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
satoke7c6998e2011-06-03 17:57:59 +09001211 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 public void onServiceDisconnected(ComponentName name) {
1213 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001214 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 + " mCurIntent=" + mCurIntent);
1216 if (mCurMethod != null && mCurIntent != null
1217 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001218 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 // We consider this to be a new bind attempt, since the system
1220 // should now try to restart the service for us.
1221 mLastBindTime = SystemClock.uptimeMillis();
1222 mShowRequested = mInputShown;
1223 mInputShown = false;
1224 if (mCurClient != null) {
1225 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1226 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1227 }
1228 }
1229 }
1230 }
1231
satokf9f01002011-05-19 21:31:50 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001234 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 long ident = Binder.clearCallingIdentity();
1236 try {
1237 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001238 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 return;
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 synchronized (mMethodMap) {
1243 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001244 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001245 if (mStatusBar != null) {
1246 mStatusBar.setIconVisibility("ime", false);
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001250 CharSequence contentDescription = null;
1251 try {
1252 PackageManager packageManager = mContext.getPackageManager();
1253 contentDescription = packageManager.getApplicationLabel(
1254 packageManager.getApplicationInfo(packageName, 0));
1255 } catch (NameNotFoundException nnfe) {
1256 /* ignore */
1257 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001258 if (mStatusBar != null) {
1259 mStatusBar.setIcon("ime", packageName, iconId, 0,
1260 contentDescription != null
1261 ? contentDescription.toString() : null);
1262 mStatusBar.setIconVisibility("ime", true);
1263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 }
1266 } finally {
1267 Binder.restoreCallingIdentity(ident);
1268 }
1269 }
1270
satok7cfc0ed2011-06-20 21:29:36 +09001271 private boolean needsToShowImeSwitchOngoingNotification() {
1272 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001273 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001274 synchronized (mMethodMap) {
1275 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1276 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001277 if (N > 2) return true;
1278 if (N < 1) return false;
1279 int nonAuxCount = 0;
1280 int auxCount = 0;
1281 InputMethodSubtype nonAuxSubtype = null;
1282 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001283 for(int i = 0; i < N; ++i) {
1284 final InputMethodInfo imi = imis.get(i);
1285 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1286 imi, true);
1287 final int subtypeCount = subtypes.size();
1288 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001289 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001290 } else {
1291 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001292 final InputMethodSubtype subtype = subtypes.get(j);
1293 if (!subtype.isAuxiliary()) {
1294 ++nonAuxCount;
1295 nonAuxSubtype = subtype;
1296 } else {
1297 ++auxCount;
1298 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001299 }
1300 }
1301 }
satok7cfc0ed2011-06-20 21:29:36 +09001302 }
satokb6359412011-06-28 17:47:41 +09001303 if (nonAuxCount > 1 || auxCount > 1) {
1304 return true;
1305 } else if (nonAuxCount == 1 && auxCount == 1) {
1306 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001307 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1308 || auxSubtype.overridesImplicitlyEnabledSubtype()
1309 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001310 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1311 return false;
1312 }
1313 return true;
1314 }
1315 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001316 }
satok7cfc0ed2011-06-20 21:29:36 +09001317 }
1318
satokdbf29502011-08-25 15:28:23 +09001319 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001320 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001321 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001322 int uid = Binder.getCallingUid();
1323 long ident = Binder.clearCallingIdentity();
1324 try {
1325 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001326 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001327 return;
1328 }
1329
1330 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001331 mImeWindowVis = vis;
1332 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001333 if (mStatusBar != null) {
1334 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1335 }
satok7cfc0ed2011-06-20 21:29:36 +09001336 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001337 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1338 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001339 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001340 final CharSequence title = mRes.getText(
1341 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001342 final CharSequence imiLabel = imi.loadLabel(pm);
1343 final CharSequence summary = mCurrentSubtype != null
1344 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1345 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1346 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001347 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001348 : imiLabel;
1349
satok7cfc0ed2011-06-20 21:29:36 +09001350 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001351 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001352 if (mNotificationManager != null) {
1353 mNotificationManager.notify(
1354 com.android.internal.R.string.select_input_method,
1355 mImeSwitcherNotification);
1356 mNotificationShown = true;
1357 }
satok7cfc0ed2011-06-20 21:29:36 +09001358 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001359 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001360 mNotificationManager.cancel(
1361 com.android.internal.R.string.select_input_method);
1362 mNotificationShown = false;
1363 }
1364 }
satok06487a52010-10-29 11:37:18 +09001365 }
1366 } finally {
1367 Binder.restoreCallingIdentity(ident);
1368 }
1369 }
1370
satoke7c6998e2011-06-03 17:57:59 +09001371 @Override
satokf9f01002011-05-19 21:31:50 +09001372 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1373 synchronized (mMethodMap) {
1374 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1375 for (int i = 0; i < spans.length; ++i) {
1376 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001377 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001378 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001379 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001380 }
1381 }
1382 }
1383 }
1384
satoke7c6998e2011-06-03 17:57:59 +09001385 @Override
satokf9f01002011-05-19 21:31:50 +09001386 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1387 synchronized (mMethodMap) {
1388 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1389 // TODO: Do not send the intent if the process of the targetImi is already dead.
1390 if (targetImi != null) {
1391 final String[] suggestions = span.getSuggestions();
1392 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001393 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001394 final Intent intent = new Intent();
1395 // Ensures that only a class in the original IME package will receive the
1396 // notification.
satok42c5a162011-05-26 16:46:14 +09001397 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001398 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1399 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1400 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1401 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1402 mContext.sendBroadcast(intent);
1403 return true;
1404 }
1405 }
1406 return false;
1407 }
1408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001410 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1411 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1412 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1413 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001415 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001416 // There is no input method selected, try to choose new applicable input method.
1417 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1418 id = Settings.Secure.getString(mContext.getContentResolver(),
1419 Settings.Secure.DEFAULT_INPUT_METHOD);
1420 }
1421 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 try {
satokab751aa2010-09-14 19:17:36 +09001423 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001425 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001426 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001427 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
satokf3db1af2010-11-23 13:34:33 +09001429 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001430 } else {
1431 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001432 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001433 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
satokab751aa2010-09-14 19:17:36 +09001437 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 InputMethodInfo info = mMethodMap.get(id);
1439 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001440 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
satokd81e9502012-05-21 12:58:45 +09001443 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001445 final int subtypeCount = info.getSubtypeCount();
1446 if (subtypeCount <= 0) {
1447 return;
satokcd7cd292010-11-20 15:46:23 +09001448 }
satokd81e9502012-05-21 12:58:45 +09001449 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1450 final InputMethodSubtype newSubtype;
1451 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1452 newSubtype = info.getSubtypeAt(subtypeId);
1453 } else {
1454 // If subtype is null, try to find the most applicable one from
1455 // getCurrentInputMethodSubtype.
1456 newSubtype = getCurrentInputMethodSubtype();
1457 }
1458 if (newSubtype == null || oldSubtype == null) {
1459 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1460 + ", new subtype = " + newSubtype);
1461 return;
1462 }
1463 if (newSubtype != oldSubtype) {
1464 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1465 if (mCurMethod != null) {
1466 try {
1467 refreshImeWindowVisibilityLocked();
1468 mCurMethod.changeInputMethodSubtype(newSubtype);
1469 } catch (RemoteException e) {
1470 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001471 }
1472 }
1473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 return;
1475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
satokd81e9502012-05-21 12:58:45 +09001477 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 final long ident = Binder.clearCallingIdentity();
1479 try {
satokab751aa2010-09-14 19:17:36 +09001480 // Set a subtype to this input method.
1481 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001482 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1483 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1484 // because mCurMethodId is stored as a history in
1485 // setSelectedInputMethodAndSubtypeLocked().
1486 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487
1488 if (ActivityManagerNative.isSystemReady()) {
1489 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001490 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 intent.putExtra("input_method_id", id);
1492 mContext.sendBroadcast(intent);
1493 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001494 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 } finally {
1496 Binder.restoreCallingIdentity(ident);
1497 }
1498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001499
satok42c5a162011-05-26 16:46:14 +09001500 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001501 public boolean showSoftInput(IInputMethodClient client, int flags,
1502 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001503 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 long ident = Binder.clearCallingIdentity();
1505 try {
1506 synchronized (mMethodMap) {
1507 if (mCurClient == null || client == null
1508 || mCurClient.client.asBinder() != client.asBinder()) {
1509 try {
1510 // We need to check if this is the current client with
1511 // focus in the window manager, to allow this call to
1512 // be made before input is started in it.
1513 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001514 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001515 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001518 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
Joe Onorato8a9b2202010-02-26 18:56:32 -08001522 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001523 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 } finally {
1526 Binder.restoreCallingIdentity(ident);
1527 }
1528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529
The Android Open Source Project4df24232009-03-05 14:34:35 -08001530 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 mShowRequested = true;
1532 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1533 mShowExplicitlyRequested = true;
1534 }
1535 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1536 mShowExplicitlyRequested = true;
1537 mShowForced = true;
1538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
Dianne Hackborncc278702009-09-02 23:07:23 -07001540 if (!mSystemReady) {
1541 return false;
1542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
The Android Open Source Project4df24232009-03-05 14:34:35 -08001544 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001546 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1547 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1548 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001550 if (mHaveConnection && !mVisibleBound) {
1551 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1552 mVisibleBound = true;
1553 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001554 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001556 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 // The client has asked to have the input method shown, but
1558 // we have been sitting here too long with a connection to the
1559 // service and no interface received, so let's disconnect/connect
1560 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001563 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001565 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1566 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
The Android Open Source Project4df24232009-03-05 14:34:35 -08001569 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001571
satok42c5a162011-05-26 16:46:14 +09001572 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001573 public boolean hideSoftInput(IInputMethodClient client, int flags,
1574 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001575 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 long ident = Binder.clearCallingIdentity();
1577 try {
1578 synchronized (mMethodMap) {
1579 if (mCurClient == null || client == null
1580 || mCurClient.client.asBinder() != client.asBinder()) {
1581 try {
1582 // We need to check if this is the current client with
1583 // focus in the window manager, to allow this call to
1584 // be made before input is started in it.
1585 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001586 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1587 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001588 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001589 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001592 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001593 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
Joe Onorato8a9b2202010-02-26 18:56:32 -08001597 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001598 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600 } finally {
1601 Binder.restoreCallingIdentity(ident);
1602 }
1603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Project4df24232009-03-05 14:34:35 -08001605 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1607 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001608 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001610 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001615 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001617 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001619 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1620 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1621 res = true;
1622 } else {
1623 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001625 if (mHaveConnection && mVisibleBound) {
1626 mContext.unbindService(mVisibleConnection);
1627 mVisibleBound = false;
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 mInputShown = false;
1630 mShowRequested = false;
1631 mShowExplicitlyRequested = false;
1632 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001633 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
satok42c5a162011-05-26 16:46:14 +09001636 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001637 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1638 int controlFlags, int softInputMode, int windowFlags,
1639 EditorInfo attribute, IInputContext inputContext) {
1640 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 long ident = Binder.clearCallingIdentity();
1642 try {
1643 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001644 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001645 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001647 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
Dianne Hackborn7663d802012-02-24 13:08:49 -08001649 ClientState cs = mClients.get(client.asBinder());
1650 if (cs == null) {
1651 throw new IllegalArgumentException("unknown client "
1652 + client.asBinder());
1653 }
1654
1655 try {
1656 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1657 // Check with the window manager to make sure this client actually
1658 // has a window with focus. If not, reject. This is thread safe
1659 // because if the focus changes some time before or after, the
1660 // next client receiving focus that has any interest in input will
1661 // be calling through here after that change happens.
1662 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1663 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1664 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001666 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001669 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001671 if (attribute != null) {
1672 return startInputUncheckedLocked(cs, inputContext, attribute,
1673 controlFlags);
1674 }
1675 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001676 }
1677 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001678
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001679 // Should we auto-show the IME even if the caller has not
1680 // specified what should be done with it?
1681 // We only do this automatically if the window can resize
1682 // to accommodate the IME (so what the user sees will give
1683 // them good context without input information being obscured
1684 // by the IME) or if running on a large screen where there
1685 // is more room for the target window + IME.
1686 final boolean doAutoShow =
1687 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1688 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1689 || mRes.getConfiguration().isLayoutSizeAtLeast(
1690 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001691 final boolean isTextEditor =
1692 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1693
1694 // We want to start input before showing the IME, but after closing
1695 // it. We want to do this after closing it to help the IME disappear
1696 // more quickly (not get stuck behind it initializing itself for the
1697 // new focused input, even if its window wants to hide the IME).
1698 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1701 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001702 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1704 // There is no focus view, and this window will
1705 // be behind any soft input window, so hide the
1706 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001707 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001708 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001710 } else if (isTextEditor && doAutoShow && (softInputMode &
1711 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 // There is a focus view, and we are navigating forward
1713 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001714 // We only do this automatically if the window can resize
1715 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001716 // them good context without input information being obscured
1717 // by the IME) or if running on a large screen where there
1718 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001719 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001720 if (attribute != null) {
1721 res = startInputUncheckedLocked(cs, inputContext, attribute,
1722 controlFlags);
1723 didStart = true;
1724 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 break;
1728 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1729 // Do nothing.
1730 break;
1731 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1732 if ((softInputMode &
1733 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737 break;
1738 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001739 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001740 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 break;
1742 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1743 if ((softInputMode &
1744 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001745 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001746 if (attribute != null) {
1747 res = startInputUncheckedLocked(cs, inputContext, attribute,
1748 controlFlags);
1749 didStart = true;
1750 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001751 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753 break;
1754 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001755 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001756 if (attribute != null) {
1757 res = startInputUncheckedLocked(cs, inputContext, attribute,
1758 controlFlags);
1759 didStart = true;
1760 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001761 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 break;
1763 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001764
1765 if (!didStart && attribute != null) {
1766 res = startInputUncheckedLocked(cs, inputContext, attribute,
1767 controlFlags);
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770 } finally {
1771 Binder.restoreCallingIdentity(ident);
1772 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001773
1774 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
satok42c5a162011-05-26 16:46:14 +09001777 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1779 synchronized (mMethodMap) {
1780 if (mCurClient == null || client == null
1781 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001782 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001783 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785
satok440aab52010-11-25 09:43:11 +09001786 // Always call subtype picker, because subtype picker is a superset of input method
1787 // picker.
satokab751aa2010-09-14 19:17:36 +09001788 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1789 }
1790 }
1791
satok42c5a162011-05-26 16:46:14 +09001792 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001794 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1795 }
1796
satok42c5a162011-05-26 16:46:14 +09001797 @Override
satok28203512010-11-24 11:06:49 +09001798 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1799 synchronized (mMethodMap) {
1800 if (subtype != null) {
1801 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1802 mMethodMap.get(id), subtype.hashCode()));
1803 } else {
1804 setInputMethod(token, id);
1805 }
1806 }
satokab751aa2010-09-14 19:17:36 +09001807 }
1808
satok42c5a162011-05-26 16:46:14 +09001809 @Override
satokb416a71e2010-11-25 20:42:14 +09001810 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001811 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001812 synchronized (mMethodMap) {
1813 if (mCurClient == null || client == null
1814 || mCurClient.client.asBinder() != client.asBinder()) {
1815 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1816 }
satok7fee71f2010-12-17 18:54:26 +09001817 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1818 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001819 }
1820 }
1821
satok4fc87d62011-05-20 16:13:43 +09001822 @Override
satok735cf382010-11-11 20:40:09 +09001823 public boolean switchToLastInputMethod(IBinder token) {
1824 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001825 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001826 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001827 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001828 lastImi = mMethodMap.get(lastIme.first);
1829 } else {
1830 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001831 }
satok4fc87d62011-05-20 16:13:43 +09001832 String targetLastImiId = null;
1833 int subtypeId = NOT_A_SUBTYPE_ID;
1834 if (lastIme != null && lastImi != null) {
1835 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1836 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1837 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1838 : mCurrentSubtype.hashCode();
1839 // If the last IME is the same as the current IME and the last subtype is not
1840 // defined, there is no need to switch to the last IME.
1841 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1842 targetLastImiId = lastIme.first;
1843 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1844 }
1845 }
1846
1847 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1848 // This is a safety net. If the currentSubtype can't be added to the history
1849 // and the framework couldn't find the last ime, we will make the last ime be
1850 // the most applicable enabled keyboard subtype of the system imes.
1851 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1852 if (enabled != null) {
1853 final int N = enabled.size();
1854 final String locale = mCurrentSubtype == null
1855 ? mRes.getConfiguration().locale.toString()
1856 : mCurrentSubtype.getLocale();
1857 for (int i = 0; i < N; ++i) {
1858 final InputMethodInfo imi = enabled.get(i);
1859 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1860 InputMethodSubtype keyboardSubtype =
1861 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1862 SUBTYPE_MODE_KEYBOARD, locale, true);
1863 if (keyboardSubtype != null) {
1864 targetLastImiId = imi.getId();
1865 subtypeId = getSubtypeIdFromHashCode(
1866 imi, keyboardSubtype.hashCode());
1867 if(keyboardSubtype.getLocale().equals(locale)) {
1868 break;
1869 }
1870 }
1871 }
1872 }
1873 }
1874 }
1875
1876 if (!TextUtils.isEmpty(targetLastImiId)) {
1877 if (DEBUG) {
1878 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1879 + ", from: " + mCurMethodId + ", " + subtypeId);
1880 }
1881 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1882 return true;
1883 } else {
1884 return false;
1885 }
satok735cf382010-11-11 20:40:09 +09001886 }
1887 }
1888
satoke7c6998e2011-06-03 17:57:59 +09001889 @Override
satok688bd472012-02-09 20:09:17 +09001890 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1891 synchronized (mMethodMap) {
1892 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1893 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1894 if (nextSubtype == null) {
1895 return false;
1896 }
1897 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1898 return true;
1899 }
1900 }
1901
1902 @Override
satok68f1b782011-04-11 14:26:04 +09001903 public InputMethodSubtype getLastInputMethodSubtype() {
1904 synchronized (mMethodMap) {
1905 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1906 // TODO: Handle the case of the last IME with no subtypes
1907 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1908 || TextUtils.isEmpty(lastIme.second)) return null;
1909 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1910 if (lastImi == null) return null;
1911 try {
1912 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001913 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1914 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1915 return null;
1916 }
1917 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001918 } catch (NumberFormatException e) {
1919 return null;
1920 }
1921 }
1922 }
1923
satoke7c6998e2011-06-03 17:57:59 +09001924 @Override
satokee5e77c2011-09-02 18:50:15 +09001925 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001926 // By this IPC call, only a process which shares the same uid with the IME can add
1927 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001928 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001929 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001930 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001931 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001932 final PackageManager pm = mContext.getPackageManager();
1933 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1934 if (packageInfos != null) {
1935 final int packageNum = packageInfos.length;
1936 for (int i = 0; i < packageNum; ++i) {
1937 if (packageInfos[i].equals(imi.getPackageName())) {
1938 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001939 final long ident = Binder.clearCallingIdentity();
1940 try {
1941 buildInputMethodListLocked(mMethodList, mMethodMap);
1942 } finally {
1943 Binder.restoreCallingIdentity(ident);
1944 }
satokee5e77c2011-09-02 18:50:15 +09001945 return;
satok91e88122011-07-18 11:11:42 +09001946 }
1947 }
1948 }
satoke7c6998e2011-06-03 17:57:59 +09001949 }
satokee5e77c2011-09-02 18:50:15 +09001950 return;
satoke7c6998e2011-06-03 17:57:59 +09001951 }
1952
satok28203512010-11-24 11:06:49 +09001953 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 synchronized (mMethodMap) {
1955 if (token == null) {
1956 if (mContext.checkCallingOrSelfPermission(
1957 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1958 != PackageManager.PERMISSION_GRANTED) {
1959 throw new SecurityException(
1960 "Using null token requires permission "
1961 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1962 }
1963 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001964 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1965 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 return;
1967 }
1968
satokc5933802011-08-31 21:26:04 +09001969 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 try {
satokab751aa2010-09-14 19:17:36 +09001971 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 } finally {
1973 Binder.restoreCallingIdentity(ident);
1974 }
1975 }
1976 }
1977
satok42c5a162011-05-26 16:46:14 +09001978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public void hideMySoftInput(IBinder token, int flags) {
1980 synchronized (mMethodMap) {
1981 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001982 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1983 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 return;
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 long ident = Binder.clearCallingIdentity();
1987 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 hideCurrentInputLocked(flags, null);
1989 } finally {
1990 Binder.restoreCallingIdentity(ident);
1991 }
1992 }
1993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001994
satok42c5a162011-05-26 16:46:14 +09001995 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001996 public void showMySoftInput(IBinder token, int flags) {
1997 synchronized (mMethodMap) {
1998 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001999 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2000 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002001 return;
2002 }
2003 long ident = Binder.clearCallingIdentity();
2004 try {
2005 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 } finally {
2007 Binder.restoreCallingIdentity(ident);
2008 }
2009 }
2010 }
2011
2012 void setEnabledSessionInMainThread(SessionState session) {
2013 if (mEnabledSession != session) {
2014 if (mEnabledSession != null) {
2015 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002016 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 mEnabledSession.method.setSessionEnabled(
2018 mEnabledSession.session, false);
2019 } catch (RemoteException e) {
2020 }
2021 }
2022 mEnabledSession = session;
2023 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002024 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 session.method.setSessionEnabled(
2026 session.session, true);
2027 } catch (RemoteException e) {
2028 }
2029 }
2030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002031
satok42c5a162011-05-26 16:46:14 +09002032 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 public boolean handleMessage(Message msg) {
2034 HandlerCaller.SomeArgs args;
2035 switch (msg.what) {
2036 case MSG_SHOW_IM_PICKER:
2037 showInputMethodMenu();
2038 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002039
satokab751aa2010-09-14 19:17:36 +09002040 case MSG_SHOW_IM_SUBTYPE_PICKER:
2041 showInputMethodSubtypeMenu();
2042 return true;
2043
satok47a44912010-10-06 16:03:58 +09002044 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09002045 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002046 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09002047 return true;
2048
2049 case MSG_SHOW_IM_CONFIG:
2050 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002051 return true;
2052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 case MSG_UNBIND_INPUT:
2056 try {
2057 ((IInputMethod)msg.obj).unbindInput();
2058 } catch (RemoteException e) {
2059 // There is nothing interesting about the method dying.
2060 }
2061 return true;
2062 case MSG_BIND_INPUT:
2063 args = (HandlerCaller.SomeArgs)msg.obj;
2064 try {
2065 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2066 } catch (RemoteException e) {
2067 }
2068 return true;
2069 case MSG_SHOW_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).showSoftInput(msg.arg1,
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_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002080 ((IInputMethod)args.arg1).hideSoftInput(0,
2081 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 } catch (RemoteException e) {
2083 }
2084 return true;
2085 case MSG_ATTACH_TOKEN:
2086 args = (HandlerCaller.SomeArgs)msg.obj;
2087 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002088 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2090 } catch (RemoteException e) {
2091 }
2092 return true;
2093 case MSG_CREATE_SESSION:
2094 args = (HandlerCaller.SomeArgs)msg.obj;
2095 try {
2096 ((IInputMethod)args.arg1).createSession(
2097 (IInputMethodCallback)args.arg2);
2098 } catch (RemoteException e) {
2099 }
2100 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 case MSG_START_INPUT:
2104 args = (HandlerCaller.SomeArgs)msg.obj;
2105 try {
2106 SessionState session = (SessionState)args.arg1;
2107 setEnabledSessionInMainThread(session);
2108 session.method.startInput((IInputContext)args.arg2,
2109 (EditorInfo)args.arg3);
2110 } catch (RemoteException e) {
2111 }
2112 return true;
2113 case MSG_RESTART_INPUT:
2114 args = (HandlerCaller.SomeArgs)msg.obj;
2115 try {
2116 SessionState session = (SessionState)args.arg1;
2117 setEnabledSessionInMainThread(session);
2118 session.method.restartInput((IInputContext)args.arg2,
2119 (EditorInfo)args.arg3);
2120 } catch (RemoteException e) {
2121 }
2122 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 case MSG_UNBIND_METHOD:
2127 try {
2128 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2129 } catch (RemoteException e) {
2130 // There is nothing interesting about the last client dying.
2131 }
2132 return true;
2133 case MSG_BIND_METHOD:
2134 args = (HandlerCaller.SomeArgs)msg.obj;
2135 try {
2136 ((IInputMethodClient)args.arg1).onBindMethod(
2137 (InputBindResult)args.arg2);
2138 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002139 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141 return true;
satok01038492012-04-09 21:08:27 +09002142
2143 // --------------------------------------------------------------
2144 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2145 mHardKeyboardListener.handleHardKeyboardStatusChange(
2146 msg.arg1 == 1, msg.arg2 == 1);
2147 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149 return false;
2150 }
2151
satok5b927c432012-05-01 20:09:34 +09002152 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002153 return (inputMethod.getServiceInfo().applicationInfo.flags
2154 & ApplicationInfo.FLAG_SYSTEM) != 0;
2155 }
2156
Ken Wakasa586f0512011-01-20 22:31:01 +09002157 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2158 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2159 final int subtypeCount = imi.getSubtypeCount();
2160 for (int i = 0; i < subtypeCount; ++i) {
2161 subtypes.add(imi.getSubtypeAt(i));
2162 }
2163 return subtypes;
2164 }
2165
satoka86f5e42011-09-02 17:12:42 +09002166 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2167 InputMethodInfo imi, String mode) {
2168 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2169 final int subtypeCount = imi.getSubtypeCount();
2170 for (int i = 0; i < subtypeCount; ++i) {
2171 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2172 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2173 subtypes.add(subtype);
2174 }
2175 }
2176 return subtypes;
2177 }
2178
satokdc9ddae2011-10-06 12:22:36 +09002179 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002180 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002181 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002182 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002183 int i = enabled.size();
2184 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002185 while (i > 0) {
2186 i--;
satokdc9ddae2011-10-06 12:22:36 +09002187 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002188 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2189 return imi;
2190 }
2191 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2192 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002193 }
2194 }
satok0a1bcf42012-05-16 19:26:31 +09002195 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002196 }
2197 return null;
2198 }
2199
2200 private boolean chooseNewDefaultIMELocked() {
2201 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2202 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002203 if (DEBUG) {
2204 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2205 }
satok723a27e2010-11-11 14:58:11 +09002206 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002207 return true;
2208 }
2209
2210 return false;
2211 }
2212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2214 HashMap<String, InputMethodInfo> map) {
2215 list.clear();
2216 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002219 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002220 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2221 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2222 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2223 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224
2225 List<ResolveInfo> services = pm.queryIntentServices(
2226 new Intent(InputMethod.SERVICE_INTERFACE),
2227 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002228
satoke7c6998e2011-06-03 17:57:59 +09002229 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2230 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 for (int i = 0; i < services.size(); ++i) {
2232 ResolveInfo ri = services.get(i);
2233 ServiceInfo si = ri.serviceInfo;
2234 ComponentName compName = new ComponentName(si.packageName, si.name);
2235 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2236 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002237 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 + ": it does not require the permission "
2239 + android.Manifest.permission.BIND_INPUT_METHOD);
2240 continue;
2241 }
2242
Joe Onorato8a9b2202010-02-26 18:56:32 -08002243 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244
2245 try {
satoke7c6998e2011-06-03 17:57:59 +09002246 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002248 final String id = p.getId();
2249 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250
satok5b927c432012-05-01 20:09:34 +09002251 // Valid system default IMEs and IMEs that have English subtypes are enabled
2252 // by default, unless there's a hard keyboard and the system IME was explicitly
2253 // disabled
2254 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2255 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002256 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002257 }
2258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002260 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002264 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002266 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
2268 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002269
satok0a1bcf42012-05-16 19:26:31 +09002270 final String defaultImiId = Settings.Secure.getString(mContext
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002271 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok0a1bcf42012-05-16 19:26:31 +09002272 if (!TextUtils.isEmpty(defaultImiId)) {
2273 if (!map.containsKey(defaultImiId)) {
2274 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2275 if (chooseNewDefaultIMELocked()) {
2276 updateFromSettingsLocked();
2277 }
2278 } else {
2279 // Double check that the default IME is certainly enabled.
2280 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002281 }
2282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
satokab751aa2010-09-14 19:17:36 +09002287 private void showInputMethodMenu() {
2288 showInputMethodMenuInternal(false);
2289 }
2290
2291 private void showInputMethodSubtypeMenu() {
2292 showInputMethodMenuInternal(true);
2293 }
2294
satok217f5482010-12-15 05:19:19 +09002295 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002296 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002297 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002298 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2299 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002300 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002301 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002302 }
satok217f5482010-12-15 05:19:19 +09002303 mContext.startActivity(intent);
2304 }
2305
2306 private void showConfigureInputMethods() {
2307 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2308 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2309 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2310 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002311 mContext.startActivity(intent);
2312 }
2313
satok2c93efc2012-04-02 19:33:47 +09002314 private boolean isScreenLocked() {
2315 return mKeyguardManager != null
2316 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2317 }
satokab751aa2010-09-14 19:17:36 +09002318 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002319 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002323 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002324
satok2c93efc2012-04-02 19:33:47 +09002325 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002327 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002328 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002329
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002330 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002331 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2332 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002333 if (immis == null || immis.size() == 0) {
2334 return;
2335 }
2336
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002337 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338
satok688bd472012-02-09 20:09:17 +09002339 final List<ImeSubtypeListItem> imList =
2340 mImListManager.getSortedInputMethodAndSubtypeList(
2341 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002342
satokc3690562012-01-10 20:14:43 +09002343 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2344 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2345 if (currentSubtype != null) {
2346 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2347 lastInputMethodSubtypeId =
2348 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2349 }
2350 }
2351
Ken Wakasa761eb372011-03-04 19:06:18 +09002352 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002353 mIms = new InputMethodInfo[N];
2354 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002355 int checkedItem = 0;
2356 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002357 final ImeSubtypeListItem item = imList.get(i);
2358 mIms[i] = item.mImi;
2359 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002360 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002361 int subtypeId = mSubtypeIds[i];
2362 if ((subtypeId == NOT_A_SUBTYPE_ID)
2363 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2364 || (subtypeId == lastInputMethodSubtypeId)) {
2365 checkedItem = i;
2366 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002369 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002370 com.android.internal.R.styleable.DialogPreference,
2371 com.android.internal.R.attr.alertDialogStyle, 0);
2372 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002373 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002374 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002375 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002377 }
2378 })
2379 .setIcon(a.getDrawable(
2380 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2381 a.recycle();
satok01038492012-04-09 21:08:27 +09002382 final LayoutInflater inflater =
2383 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2384 final View tv = inflater.inflate(
2385 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2386 mDialogBuilder.setCustomTitle(tv);
2387
2388 // Setup layout for a toggle switch of the hardware keyboard
2389 mSwitchingDialogTitleView = tv;
2390 mSwitchingDialogTitleView.findViewById(
2391 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2392 mWindowManagerService.isHardKeyboardAvailable() ?
2393 View.VISIBLE : View.GONE);
2394 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2395 com.android.internal.R.id.hard_keyboard_switch));
2396 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2397 hardKeySwitch.setOnCheckedChangeListener(
2398 new OnCheckedChangeListener() {
2399 @Override
2400 public void onCheckedChanged(
2401 CompoundButton buttonView, boolean isChecked) {
2402 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2403 }
2404 });
satokd87c2592010-09-29 11:52:06 +09002405
Ken Wakasa05dbb652011-08-22 15:22:43 +09002406 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2407 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2408 checkedItem);
2409
2410 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002411 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002412 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002413 public void onClick(DialogInterface dialog, int which) {
2414 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002415 if (mIms == null || mIms.length <= which
2416 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002417 return;
2418 }
2419 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002420 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002421 hideInputMethodMenu();
2422 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002423 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002424 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002425 subtypeId = NOT_A_SUBTYPE_ID;
2426 }
2427 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002428 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002431 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
satokbc81b692011-08-26 16:22:22 +09002433 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002434 mDialogBuilder.setPositiveButton(
2435 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002436 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002437 @Override
satok7f35c8c2010-10-07 21:13:11 +09002438 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002439 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002440 }
2441 });
2442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002444 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 mSwitchingDialog.getWindow().setType(
2446 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002447 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 mSwitchingDialog.show();
2449 }
2450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002451
satok93d744d2012-05-09 17:14:08 +09002452 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002453 public final CharSequence mImeName;
2454 public final CharSequence mSubtypeName;
2455 public final InputMethodInfo mImi;
2456 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002457 private final boolean mIsSystemLocale;
2458 private final boolean mIsSystemLanguage;
2459
Ken Wakasa05dbb652011-08-22 15:22:43 +09002460 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002461 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002462 mImeName = imeName;
2463 mSubtypeName = subtypeName;
2464 mImi = imi;
2465 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002466 if (TextUtils.isEmpty(subtypeLocale)) {
2467 mIsSystemLocale = false;
2468 mIsSystemLanguage = false;
2469 } else {
2470 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2471 mIsSystemLanguage = mIsSystemLocale
2472 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2473 }
2474 }
2475
2476 @Override
2477 public int compareTo(ImeSubtypeListItem other) {
2478 if (TextUtils.isEmpty(mImeName)) {
2479 return 1;
2480 }
2481 if (TextUtils.isEmpty(other.mImeName)) {
2482 return -1;
2483 }
2484 if (!TextUtils.equals(mImeName, other.mImeName)) {
2485 return mImeName.toString().compareTo(other.mImeName.toString());
2486 }
2487 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2488 return 0;
2489 }
2490 if (mIsSystemLocale) {
2491 return -1;
2492 }
2493 if (other.mIsSystemLocale) {
2494 return 1;
2495 }
2496 if (mIsSystemLanguage) {
2497 return -1;
2498 }
2499 if (other.mIsSystemLanguage) {
2500 return 1;
2501 }
2502 if (TextUtils.isEmpty(mSubtypeName)) {
2503 return 1;
2504 }
2505 if (TextUtils.isEmpty(other.mSubtypeName)) {
2506 return -1;
2507 }
2508 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002509 }
2510 }
2511
2512 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2513 private final LayoutInflater mInflater;
2514 private final int mTextViewResourceId;
2515 private final List<ImeSubtypeListItem> mItemsList;
2516 private final int mCheckedItem;
2517 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2518 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2519 super(context, textViewResourceId, itemsList);
2520 mTextViewResourceId = textViewResourceId;
2521 mItemsList = itemsList;
2522 mCheckedItem = checkedItem;
2523 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2524 }
2525
2526 @Override
2527 public View getView(int position, View convertView, ViewGroup parent) {
2528 final View view = convertView != null ? convertView
2529 : mInflater.inflate(mTextViewResourceId, null);
2530 if (position < 0 || position >= mItemsList.size()) return view;
2531 final ImeSubtypeListItem item = mItemsList.get(position);
2532 final CharSequence imeName = item.mImeName;
2533 final CharSequence subtypeName = item.mSubtypeName;
2534 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2535 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2536 if (TextUtils.isEmpty(subtypeName)) {
2537 firstTextView.setText(imeName);
2538 secondTextView.setVisibility(View.GONE);
2539 } else {
2540 firstTextView.setText(subtypeName);
2541 secondTextView.setText(imeName);
2542 secondTextView.setVisibility(View.VISIBLE);
2543 }
2544 final RadioButton radioButton =
2545 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2546 radioButton.setChecked(position == mCheckedItem);
2547 return view;
2548 }
2549 }
2550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002552 synchronized (mMethodMap) {
2553 hideInputMethodMenuLocked();
2554 }
2555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002556
The Android Open Source Project10592532009-03-18 17:39:46 -07002557 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002558 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559
The Android Open Source Project10592532009-03-18 17:39:46 -07002560 if (mSwitchingDialog != null) {
2561 mSwitchingDialog.dismiss();
2562 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002564
The Android Open Source Project10592532009-03-18 17:39:46 -07002565 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002566 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002570
satok42c5a162011-05-26 16:46:14 +09002571 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 public boolean setInputMethodEnabled(String id, boolean enabled) {
2573 synchronized (mMethodMap) {
2574 if (mContext.checkCallingOrSelfPermission(
2575 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2576 != PackageManager.PERMISSION_GRANTED) {
2577 throw new SecurityException(
2578 "Requires permission "
2579 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2580 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 long ident = Binder.clearCallingIdentity();
2583 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002584 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 } finally {
2586 Binder.restoreCallingIdentity(ident);
2587 }
2588 }
2589 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002590
2591 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2592 // Make sure this is a valid input method.
2593 InputMethodInfo imm = mMethodMap.get(id);
2594 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002595 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002596 }
2597
satokd87c2592010-09-29 11:52:06 +09002598 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2599 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002600
satokd87c2592010-09-29 11:52:06 +09002601 if (enabled) {
2602 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2603 if (pair.first.equals(id)) {
2604 // We are enabling this input method, but it is already enabled.
2605 // Nothing to do. The previous state was enabled.
2606 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002607 }
2608 }
satokd87c2592010-09-29 11:52:06 +09002609 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2610 // Previous state was disabled.
2611 return false;
2612 } else {
2613 StringBuilder builder = new StringBuilder();
2614 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2615 builder, enabledInputMethodsList, id)) {
2616 // Disabled input method is currently selected, switch to another one.
2617 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2618 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002619 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2620 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2621 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002622 }
2623 // Previous state was enabled.
2624 return true;
2625 } else {
2626 // We are disabling the input method but it is already disabled.
2627 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002628 return false;
2629 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002630 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002631 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002632
satok57ffc002011-01-25 00:11:47 +09002633 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2634 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002635 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002636 }
2637
satok723a27e2010-11-11 14:58:11 +09002638 private void saveCurrentInputMethodAndSubtypeToHistory() {
2639 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2640 if (mCurrentSubtype != null) {
2641 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2642 }
satok57ffc002011-01-25 00:11:47 +09002643 if (canAddToLastInputMethod(mCurrentSubtype)) {
2644 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2645 }
satokab751aa2010-09-14 19:17:36 +09002646 }
2647
satok723a27e2010-11-11 14:58:11 +09002648 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2649 boolean setSubtypeOnly) {
2650 // Update the history of InputMethod and Subtype
2651 saveCurrentInputMethodAndSubtypeToHistory();
2652
2653 // Set Subtype here
2654 if (imi == null || subtypeId < 0) {
2655 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002656 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002657 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002658 if (subtypeId < imi.getSubtypeCount()) {
2659 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2660 mSettings.putSelectedSubtype(subtype.hashCode());
2661 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002662 } else {
2663 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002664 // If the subtype is not specified, choose the most applicable one
2665 mCurrentSubtype = getCurrentInputMethodSubtype();
satok723a27e2010-11-11 14:58:11 +09002666 }
satokab751aa2010-09-14 19:17:36 +09002667 }
satok723a27e2010-11-11 14:58:11 +09002668
2669 if (!setSubtypeOnly) {
2670 // Set InputMethod here
2671 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2672 }
2673 }
2674
2675 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2676 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2677 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2678 // newDefaultIme is empty when there is no candidate for the selected IME.
2679 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2680 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2681 if (subtypeHashCode != null) {
2682 try {
2683 lastSubtypeId = getSubtypeIdFromHashCode(
2684 imi, Integer.valueOf(subtypeHashCode));
2685 } catch (NumberFormatException e) {
2686 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2687 }
2688 }
2689 }
2690 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002691 }
2692
2693 private int getSelectedInputMethodSubtypeId(String id) {
2694 InputMethodInfo imi = mMethodMap.get(id);
2695 if (imi == null) {
2696 return NOT_A_SUBTYPE_ID;
2697 }
satokab751aa2010-09-14 19:17:36 +09002698 int subtypeId;
2699 try {
2700 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2701 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2702 } catch (SettingNotFoundException e) {
2703 return NOT_A_SUBTYPE_ID;
2704 }
satok723a27e2010-11-11 14:58:11 +09002705 return getSubtypeIdFromHashCode(imi, subtypeId);
2706 }
2707
satokfdf419e2012-05-08 16:52:08 +09002708 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2709 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2710 }
2711
2712 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002713 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002714 final int subtypeCount = imi.getSubtypeCount();
2715 for (int i = 0; i < subtypeCount; ++i) {
2716 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002717 if (subtypeHashCode == ims.hashCode()) {
2718 return i;
2719 }
satokab751aa2010-09-14 19:17:36 +09002720 }
2721 }
2722 return NOT_A_SUBTYPE_ID;
2723 }
2724
satoka86f5e42011-09-02 17:12:42 +09002725 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2726 Resources res, InputMethodInfo imi) {
2727 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002728 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002729 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002730 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002731 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002732 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002733 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002734 // scan overriding implicitly enabled subtypes.
2735 InputMethodSubtype subtype = subtypes.get(i);
2736 if (subtype.overridesImplicitlyEnabledSubtype()) {
2737 final String mode = subtype.getMode();
2738 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2739 applicableModeAndSubtypesMap.put(mode, subtype);
2740 }
2741 }
2742 }
2743 if (applicableModeAndSubtypesMap.size() > 0) {
2744 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2745 }
2746 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002747 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002748 final String locale = subtype.getLocale();
2749 final String mode = subtype.getMode();
2750 // When system locale starts with subtype's locale, that subtype will be applicable
2751 // for system locale
2752 // For instance, it's clearly applicable for cases like system locale = en_US and
2753 // subtype = en, but it is not necessarily considered applicable for cases like system
2754 // locale = en and subtype = en_US.
2755 // We just call systemLocale.startsWith(locale) in this function because there is no
2756 // need to find applicable subtypes aggressively unlike
2757 // findLastResortApplicableSubtypeLocked.
2758 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002759 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002760 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002761 if (applicableSubtype != null) {
2762 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2763 if (!systemLocale.equals(locale)) continue;
2764 }
satok3da92232011-01-11 22:46:30 +09002765 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002766 }
2767 }
2768 final InputMethodSubtype keyboardSubtype
2769 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2770 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2771 applicableModeAndSubtypesMap.values());
2772 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2773 for (int i = 0; i < N; ++i) {
2774 final InputMethodSubtype subtype = subtypes.get(i);
2775 final String mode = subtype.getMode();
2776 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2777 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2778 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002779 }
2780 }
2781 }
satokc3690562012-01-10 20:14:43 +09002782 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002783 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002784 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002785 if (lastResortKeyboardSubtype != null) {
2786 applicableSubtypes.add(lastResortKeyboardSubtype);
2787 }
2788 }
2789 return applicableSubtypes;
2790 }
2791
satok4e4569d2010-11-19 18:45:53 +09002792 /**
2793 * If there are no selected subtypes, tries finding the most applicable one according to the
2794 * given locale.
2795 * @param subtypes this function will search the most applicable subtype in subtypes
2796 * @param mode subtypes will be filtered by mode
2797 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002798 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2799 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002800 * @return the most applicable subtypeId
2801 */
satokdf31ae62011-01-15 06:19:44 +09002802 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2803 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002804 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002805 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002806 return null;
satok8fbb1e82010-11-02 23:15:58 +09002807 }
satok4e4569d2010-11-19 18:45:53 +09002808 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002809 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002810 }
satok8fbb1e82010-11-02 23:15:58 +09002811 final String language = locale.substring(0, 2);
2812 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002813 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002814 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002815 final int N = subtypes.size();
2816 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002817 InputMethodSubtype subtype = subtypes.get(i);
2818 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002819 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2820 // and all subtypes with all modes can be candidates.
2821 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002822 if (firstMatchedModeSubtype == null) {
2823 firstMatchedModeSubtype = subtype;
2824 }
satok9ef02832010-11-04 21:17:48 +09002825 if (locale.equals(subtypeLocale)) {
2826 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002827 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002828 break;
2829 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2830 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002831 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002832 partialMatchFound = true;
2833 }
satok8fbb1e82010-11-02 23:15:58 +09002834 }
2835 }
2836
satok7599a7fb2010-12-22 13:45:23 +09002837 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2838 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002839 }
2840
satok8fbb1e82010-11-02 23:15:58 +09002841 // The first subtype applicable to the system locale will be defined as the most applicable
2842 // subtype.
2843 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002844 if (applicableSubtype != null) {
2845 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2846 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2847 }
satok8fbb1e82010-11-02 23:15:58 +09002848 }
satokcd7cd292010-11-20 15:46:23 +09002849 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002850 }
2851
satok4e4569d2010-11-19 18:45:53 +09002852 // If there are no selected shortcuts, tries finding the most applicable ones.
2853 private Pair<InputMethodInfo, InputMethodSubtype>
2854 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2855 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2856 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002857 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002858 boolean foundInSystemIME = false;
2859
2860 // Search applicable subtype for each InputMethodInfo
2861 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002862 final String imiId = imi.getId();
2863 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2864 continue;
2865 }
satokcd7cd292010-11-20 15:46:23 +09002866 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002867 final List<InputMethodSubtype> enabledSubtypes =
2868 getEnabledInputMethodSubtypeList(imi, true);
2869 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002870 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002871 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002872 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002873 }
satokdf31ae62011-01-15 06:19:44 +09002874 // 2. Search by the system locale from enabledSubtypes.
2875 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002876 if (subtype == null) {
2877 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002878 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002879 }
satoka86f5e42011-09-02 17:12:42 +09002880 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2881 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2882 final ArrayList<InputMethodSubtype> subtypesForSearch =
2883 overridingImplicitlyEnabledSubtypes.isEmpty()
2884 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002885 // 4. Search by the current subtype's locale from all subtypes.
2886 if (subtype == null && mCurrentSubtype != null) {
2887 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002888 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002889 }
2890 // 5. Search by the system locale from all subtypes.
2891 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002892 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002893 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002894 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002895 }
satokcd7cd292010-11-20 15:46:23 +09002896 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002897 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002898 // The current input method is the most applicable IME.
2899 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002900 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002901 break;
satok7599a7fb2010-12-22 13:45:23 +09002902 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002903 // The system input method is 2nd applicable IME.
2904 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002905 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002906 if ((imi.getServiceInfo().applicationInfo.flags
2907 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2908 foundInSystemIME = true;
2909 }
satok4e4569d2010-11-19 18:45:53 +09002910 }
2911 }
2912 }
2913 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002914 if (mostApplicableIMI != null) {
2915 Slog.w(TAG, "Most applicable shortcut input method was:"
2916 + mostApplicableIMI.getId());
2917 if (mostApplicableSubtype != null) {
2918 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2919 + "," + mostApplicableSubtype.getMode() + ","
2920 + mostApplicableSubtype.getLocale());
2921 }
2922 }
satok4e4569d2010-11-19 18:45:53 +09002923 }
satokcd7cd292010-11-20 15:46:23 +09002924 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002925 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002926 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002927 } else {
2928 return null;
2929 }
2930 }
2931
satokab751aa2010-09-14 19:17:36 +09002932 /**
2933 * @return Return the current subtype of this input method.
2934 */
satok42c5a162011-05-26 16:46:14 +09002935 @Override
satokab751aa2010-09-14 19:17:36 +09002936 public InputMethodSubtype getCurrentInputMethodSubtype() {
satokfdf419e2012-05-08 16:52:08 +09002937 if (mCurMethodId == null) {
2938 return null;
2939 }
satok4e4569d2010-11-19 18:45:53 +09002940 boolean subtypeIsSelected = false;
2941 try {
2942 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2943 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2944 } catch (SettingNotFoundException e) {
2945 }
satok3ef8b292010-11-23 06:06:29 +09002946 synchronized (mMethodMap) {
satokfdf419e2012-05-08 16:52:08 +09002947 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2948 if (imi == null || imi.getSubtypeCount() == 0) {
2949 return null;
2950 }
2951 if (!subtypeIsSelected || mCurrentSubtype == null
2952 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2953 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
satok3ef8b292010-11-23 06:06:29 +09002954 if (subtypeId == NOT_A_SUBTYPE_ID) {
satokfdf419e2012-05-08 16:52:08 +09002955 // If there are no selected subtypes, the framework will try to find
2956 // the most applicable subtype from explicitly or implicitly enabled
2957 // subtypes.
2958 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2959 getEnabledInputMethodSubtypeList(imi, true);
2960 // If there is only one explicitly or implicitly enabled subtype,
2961 // just returns it.
2962 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2963 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2964 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2965 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2966 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2967 SUBTYPE_MODE_KEYBOARD, null, true);
2968 if (mCurrentSubtype == null) {
satokd8713432011-01-18 00:55:13 +09002969 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
satokfdf419e2012-05-08 16:52:08 +09002970 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2971 true);
satokd8713432011-01-18 00:55:13 +09002972 }
satok4e4569d2010-11-19 18:45:53 +09002973 }
satokcd7cd292010-11-20 15:46:23 +09002974 } else {
satokfdf419e2012-05-08 16:52:08 +09002975 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002976 }
satok8fbb1e82010-11-02 23:15:58 +09002977 }
satok3ef8b292010-11-23 06:06:29 +09002978 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002979 }
satokab751aa2010-09-14 19:17:36 +09002980 }
2981
satokf3db1af2010-11-23 13:34:33 +09002982 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2983 InputMethodSubtype subtype) {
2984 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2985 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2986 } else {
2987 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2988 subtypes.add(subtype);
2989 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2990 }
2991 }
2992
satok4e4569d2010-11-19 18:45:53 +09002993 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002994 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002995 @Override
satok4e4569d2010-11-19 18:45:53 +09002996 public List getShortcutInputMethodsAndSubtypes() {
2997 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002998 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002999 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003000 // If there are no selected shortcut subtypes, the framework will try to find
3001 // the most applicable subtype from all subtypes whose mode is
3002 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003003 Pair<InputMethodInfo, InputMethodSubtype> info =
3004 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3005 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003006 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003007 ret.add(info.first);
3008 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003009 }
satok3da92232011-01-11 22:46:30 +09003010 return ret;
satokf3db1af2010-11-23 13:34:33 +09003011 }
satokf3db1af2010-11-23 13:34:33 +09003012 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3013 ret.add(imi);
3014 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3015 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003016 }
3017 }
satokf3db1af2010-11-23 13:34:33 +09003018 return ret;
satok4e4569d2010-11-19 18:45:53 +09003019 }
3020 }
3021
satok42c5a162011-05-26 16:46:14 +09003022 @Override
satokb66d2872010-11-10 01:04:04 +09003023 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
3024 synchronized (mMethodMap) {
3025 if (subtype != null && mCurMethodId != null) {
3026 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3027 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3028 if (subtypeId != NOT_A_SUBTYPE_ID) {
3029 setInputMethodLocked(mCurMethodId, subtypeId);
3030 return true;
3031 }
3032 }
3033 return false;
3034 }
3035 }
3036
satok688bd472012-02-09 20:09:17 +09003037 private static class InputMethodAndSubtypeListManager {
3038 private final Context mContext;
3039 private final PackageManager mPm;
3040 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003041 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003042 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3043 mContext = context;
3044 mPm = context.getPackageManager();
3045 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003046 final Locale locale = context.getResources().getConfiguration().locale;
3047 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003048 }
3049
3050 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3051 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3052 new Comparator<InputMethodInfo>() {
3053 @Override
3054 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3055 if (imi2 == null) return 0;
3056 if (imi1 == null) return 1;
3057 if (mPm == null) {
3058 return imi1.getId().compareTo(imi2.getId());
3059 }
3060 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3061 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3062 return imiId1.toString().compareTo(imiId2.toString());
3063 }
3064 });
3065
3066 public ImeSubtypeListItem getNextInputMethod(
3067 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3068 if (imi == null) {
3069 return null;
3070 }
3071 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3072 if (imList.size() <= 1) {
3073 return null;
3074 }
3075 final int N = imList.size();
3076 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003077 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003078 : NOT_A_SUBTYPE_ID;
3079 for (int i = 0; i < N; ++i) {
3080 final ImeSubtypeListItem isli = imList.get(i);
3081 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3082 if (!onlyCurrentIme) {
3083 return imList.get((i + 1) % N);
3084 }
3085 for (int j = 0; j < N - 1; ++j) {
3086 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3087 if (candidate.mImi.equals(imi)) {
3088 return candidate;
3089 }
3090 }
3091 return null;
3092 }
3093 }
3094 return null;
3095 }
3096
3097 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3098 return getSortedInputMethodAndSubtypeList(true, false, false);
3099 }
3100
3101 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3102 boolean inputShown, boolean isScreenLocked) {
3103 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3104 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3105 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3106 if (immis == null || immis.size() == 0) {
3107 return Collections.emptyList();
3108 }
3109 mSortedImmis.clear();
3110 mSortedImmis.putAll(immis);
3111 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3112 if (imi == null) continue;
3113 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3114 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3115 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3116 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3117 }
3118 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3119 final CharSequence imeLabel = imi.loadLabel(mPm);
3120 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3121 final int subtypeCount = imi.getSubtypeCount();
3122 if (DEBUG) {
3123 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3124 }
3125 for (int j = 0; j < subtypeCount; ++j) {
3126 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3127 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3128 // We show all enabled IMEs and subtypes when an IME is shown.
3129 if (enabledSubtypeSet.contains(subtypeHashCode)
3130 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3131 final CharSequence subtypeLabel =
3132 subtype.overridesImplicitlyEnabledSubtype() ? null
3133 : subtype.getDisplayName(mContext, imi.getPackageName(),
3134 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003135 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3136 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003137
3138 // Removing this subtype from enabledSubtypeSet because we no longer
3139 // need to add an entry of this subtype to imList to avoid duplicated
3140 // entries.
3141 enabledSubtypeSet.remove(subtypeHashCode);
3142 }
3143 }
3144 } else {
satok93d744d2012-05-09 17:14:08 +09003145 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3146 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003147 }
3148 }
satok93d744d2012-05-09 17:14:08 +09003149 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003150 return imList;
3151 }
3152 }
3153
satokd87c2592010-09-29 11:52:06 +09003154 /**
3155 * Utility class for putting and getting settings for InputMethod
3156 * TODO: Move all putters and getters of settings to this class.
3157 */
3158 private static class InputMethodSettings {
3159 // The string for enabled input method is saved as follows:
3160 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3161 private static final char INPUT_METHOD_SEPARATER = ':';
3162 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003163 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003164 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3165
satok723a27e2010-11-11 14:58:11 +09003166 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003167 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3168
satokdf31ae62011-01-15 06:19:44 +09003169 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003170 private final ContentResolver mResolver;
3171 private final HashMap<String, InputMethodInfo> mMethodMap;
3172 private final ArrayList<InputMethodInfo> mMethodList;
3173
3174 private String mEnabledInputMethodsStrCache;
3175
3176 private static void buildEnabledInputMethodsSettingString(
3177 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3178 String id = pair.first;
3179 ArrayList<String> subtypes = pair.second;
3180 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003181 // Inputmethod and subtypes are saved in the settings as follows:
3182 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3183 for (String subtypeId: subtypes) {
3184 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003185 }
3186 }
3187
3188 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003189 Resources res, ContentResolver resolver,
3190 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3191 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003192 mResolver = resolver;
3193 mMethodMap = methodMap;
3194 mMethodList = methodList;
3195 }
3196
3197 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3198 return createEnabledInputMethodListLocked(
3199 getEnabledInputMethodsAndSubtypeListLocked());
3200 }
3201
satok7f35c8c2010-10-07 21:13:11 +09003202 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003203 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3204 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003205 getEnabledInputMethodsAndSubtypeListLocked());
3206 }
3207
satok67ddf9c2010-11-17 09:45:54 +09003208 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3209 InputMethodInfo imi) {
3210 List<Pair<String, ArrayList<String>>> imsList =
3211 getEnabledInputMethodsAndSubtypeListLocked();
3212 ArrayList<InputMethodSubtype> enabledSubtypes =
3213 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003214 if (imi != null) {
3215 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3216 InputMethodInfo info = mMethodMap.get(imsPair.first);
3217 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003218 final int subtypeCount = info.getSubtypeCount();
3219 for (int i = 0; i < subtypeCount; ++i) {
3220 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003221 for (String s: imsPair.second) {
3222 if (String.valueOf(ims.hashCode()).equals(s)) {
3223 enabledSubtypes.add(ims);
3224 }
satok67ddf9c2010-11-17 09:45:54 +09003225 }
3226 }
satok884ef9a2010-11-18 10:39:46 +09003227 break;
satok67ddf9c2010-11-17 09:45:54 +09003228 }
satok67ddf9c2010-11-17 09:45:54 +09003229 }
3230 }
3231 return enabledSubtypes;
3232 }
3233
satokd87c2592010-09-29 11:52:06 +09003234 // At the initial boot, the settings for input methods are not set,
3235 // so we need to enable IME in that case.
3236 public void enableAllIMEsIfThereIsNoEnabledIME() {
3237 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3238 StringBuilder sb = new StringBuilder();
3239 final int N = mMethodList.size();
3240 for (int i = 0; i < N; i++) {
3241 InputMethodInfo imi = mMethodList.get(i);
3242 Slog.i(TAG, "Adding: " + imi.getId());
3243 if (i > 0) sb.append(':');
3244 sb.append(imi.getId());
3245 }
3246 putEnabledInputMethodsStr(sb.toString());
3247 }
3248 }
3249
satokbb4aa062011-01-19 21:40:27 +09003250 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003251 ArrayList<Pair<String, ArrayList<String>>> imsList
3252 = new ArrayList<Pair<String, ArrayList<String>>>();
3253 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3254 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3255 return imsList;
3256 }
satok723a27e2010-11-11 14:58:11 +09003257 mInputMethodSplitter.setString(enabledInputMethodsStr);
3258 while (mInputMethodSplitter.hasNext()) {
3259 String nextImsStr = mInputMethodSplitter.next();
3260 mSubtypeSplitter.setString(nextImsStr);
3261 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003262 ArrayList<String> subtypeHashes = new ArrayList<String>();
3263 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003264 String imeId = mSubtypeSplitter.next();
3265 while (mSubtypeSplitter.hasNext()) {
3266 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003267 }
3268 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3269 }
3270 }
3271 return imsList;
3272 }
3273
3274 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3275 if (reloadInputMethodStr) {
3276 getEnabledInputMethodsStr();
3277 }
3278 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3279 // Add in the newly enabled input method.
3280 putEnabledInputMethodsStr(id);
3281 } else {
3282 putEnabledInputMethodsStr(
3283 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3284 }
3285 }
3286
3287 /**
3288 * Build and put a string of EnabledInputMethods with removing specified Id.
3289 * @return the specified id was removed or not.
3290 */
3291 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3292 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3293 boolean isRemoved = false;
3294 boolean needsAppendSeparator = false;
3295 for (Pair<String, ArrayList<String>> ims: imsList) {
3296 String curId = ims.first;
3297 if (curId.equals(id)) {
3298 // We are disabling this input method, and it is
3299 // currently enabled. Skip it to remove from the
3300 // new list.
3301 isRemoved = true;
3302 } else {
3303 if (needsAppendSeparator) {
3304 builder.append(INPUT_METHOD_SEPARATER);
3305 } else {
3306 needsAppendSeparator = true;
3307 }
3308 buildEnabledInputMethodsSettingString(builder, ims);
3309 }
3310 }
3311 if (isRemoved) {
3312 // Update the setting with the new list of input methods.
3313 putEnabledInputMethodsStr(builder.toString());
3314 }
3315 return isRemoved;
3316 }
3317
3318 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3319 List<Pair<String, ArrayList<String>>> imsList) {
3320 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3321 for (Pair<String, ArrayList<String>> ims: imsList) {
3322 InputMethodInfo info = mMethodMap.get(ims.first);
3323 if (info != null) {
3324 res.add(info);
3325 }
3326 }
3327 return res;
3328 }
3329
satok7f35c8c2010-10-07 21:13:11 +09003330 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003331 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003332 List<Pair<String, ArrayList<String>>> imsList) {
3333 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3334 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3335 for (Pair<String, ArrayList<String>> ims : imsList) {
3336 InputMethodInfo info = mMethodMap.get(ims.first);
3337 if (info != null) {
3338 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3339 }
3340 }
3341 return res;
3342 }
3343
satokd87c2592010-09-29 11:52:06 +09003344 private void putEnabledInputMethodsStr(String str) {
3345 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3346 mEnabledInputMethodsStrCache = str;
3347 }
3348
3349 private String getEnabledInputMethodsStr() {
3350 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3351 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003352 if (DEBUG) {
3353 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3354 }
satokd87c2592010-09-29 11:52:06 +09003355 return mEnabledInputMethodsStrCache;
3356 }
satok723a27e2010-11-11 14:58:11 +09003357
3358 private void saveSubtypeHistory(
3359 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3360 StringBuilder builder = new StringBuilder();
3361 boolean isImeAdded = false;
3362 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3363 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3364 newSubtypeId);
3365 isImeAdded = true;
3366 }
3367 for (Pair<String, String> ime: savedImes) {
3368 String imeId = ime.first;
3369 String subtypeId = ime.second;
3370 if (TextUtils.isEmpty(subtypeId)) {
3371 subtypeId = NOT_A_SUBTYPE_ID_STR;
3372 }
3373 if (isImeAdded) {
3374 builder.append(INPUT_METHOD_SEPARATER);
3375 } else {
3376 isImeAdded = true;
3377 }
3378 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3379 subtypeId);
3380 }
3381 // Remove the last INPUT_METHOD_SEPARATER
3382 putSubtypeHistoryStr(builder.toString());
3383 }
3384
3385 public void addSubtypeToHistory(String imeId, String subtypeId) {
3386 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3387 for (Pair<String, String> ime: subtypeHistory) {
3388 if (ime.first.equals(imeId)) {
3389 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003390 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003391 + ime.second);
3392 }
3393 // We should break here
3394 subtypeHistory.remove(ime);
3395 break;
3396 }
3397 }
satokbb4aa062011-01-19 21:40:27 +09003398 if (DEBUG) {
3399 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3400 }
satok723a27e2010-11-11 14:58:11 +09003401 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3402 }
3403
3404 private void putSubtypeHistoryStr(String str) {
3405 if (DEBUG) {
3406 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3407 }
3408 Settings.Secure.putString(
3409 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3410 }
3411
3412 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3413 // Gets the first one from the history
3414 return getLastSubtypeForInputMethodLockedInternal(null);
3415 }
3416
3417 public String getLastSubtypeForInputMethodLocked(String imeId) {
3418 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3419 if (ime != null) {
3420 return ime.second;
3421 } else {
3422 return null;
3423 }
3424 }
3425
3426 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3427 List<Pair<String, ArrayList<String>>> enabledImes =
3428 getEnabledInputMethodsAndSubtypeListLocked();
3429 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003430 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003431 final String imeInTheHistory = imeAndSubtype.first;
3432 // If imeId is empty, returns the first IME and subtype in the history
3433 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3434 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003435 final String subtypeHashCode =
3436 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3437 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003438 if (!TextUtils.isEmpty(subtypeHashCode)) {
3439 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003440 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003441 }
3442 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3443 }
3444 }
3445 }
3446 if (DEBUG) {
3447 Slog.d(TAG, "No enabled IME found in the history");
3448 }
3449 return null;
3450 }
3451
satokdf31ae62011-01-15 06:19:44 +09003452 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003453 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3454 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3455 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003456 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003457 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003458 if (explicitlyEnabledSubtypes.size() == 0) {
3459 // If there are no explicitly enabled subtypes, applicable subtypes are
3460 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003461 // If IME is enabled and no subtypes are enabled, applicable subtypes
3462 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003463 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003464 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003465 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003466 if (implicitlySelectedSubtypes != null) {
3467 final int N = implicitlySelectedSubtypes.size();
3468 for (int i = 0; i < N; ++i) {
3469 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3470 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3471 return subtypeHashCode;
3472 }
3473 }
3474 }
3475 }
3476 } else {
satokf6cafb62011-01-17 16:29:02 +09003477 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003478 if (s.equals(subtypeHashCode)) {
3479 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003480 try {
3481 final int hashCode = Integer.valueOf(subtypeHashCode);
3482 // Check whether the subtype id is valid or not
3483 if (isValidSubtypeId(imi, hashCode)) {
3484 return s;
3485 } else {
3486 return NOT_A_SUBTYPE_ID_STR;
3487 }
3488 } catch (NumberFormatException e) {
3489 return NOT_A_SUBTYPE_ID_STR;
3490 }
satokdf31ae62011-01-15 06:19:44 +09003491 }
satok723a27e2010-11-11 14:58:11 +09003492 }
3493 }
3494 // If imeId was enabled but subtypeId was disabled.
3495 return NOT_A_SUBTYPE_ID_STR;
3496 }
3497 }
3498 // If both imeId and subtypeId are disabled, return null
3499 return null;
3500 }
3501
3502 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3503 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3504 final String subtypeHistoryStr = getSubtypeHistoryStr();
3505 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3506 return imsList;
3507 }
3508 mInputMethodSplitter.setString(subtypeHistoryStr);
3509 while (mInputMethodSplitter.hasNext()) {
3510 String nextImsStr = mInputMethodSplitter.next();
3511 mSubtypeSplitter.setString(nextImsStr);
3512 if (mSubtypeSplitter.hasNext()) {
3513 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3514 // The first element is ime id.
3515 String imeId = mSubtypeSplitter.next();
3516 while (mSubtypeSplitter.hasNext()) {
3517 subtypeId = mSubtypeSplitter.next();
3518 break;
3519 }
3520 imsList.add(new Pair<String, String>(imeId, subtypeId));
3521 }
3522 }
3523 return imsList;
3524 }
3525
3526 private String getSubtypeHistoryStr() {
3527 if (DEBUG) {
3528 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3529 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3530 }
3531 return Settings.Secure.getString(
3532 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3533 }
3534
3535 public void putSelectedInputMethod(String imeId) {
3536 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3537 }
3538
3539 public void putSelectedSubtype(int subtypeId) {
3540 Settings.Secure.putInt(
3541 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3542 }
satokd87c2592010-09-29 11:52:06 +09003543 }
3544
satoke7c6998e2011-06-03 17:57:59 +09003545 private static class InputMethodFileManager {
3546 private static final String SYSTEM_PATH = "system";
3547 private static final String INPUT_METHOD_PATH = "inputmethod";
3548 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3549 private static final String NODE_SUBTYPES = "subtypes";
3550 private static final String NODE_SUBTYPE = "subtype";
3551 private static final String NODE_IMI = "imi";
3552 private static final String ATTR_ID = "id";
3553 private static final String ATTR_LABEL = "label";
3554 private static final String ATTR_ICON = "icon";
3555 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3556 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3557 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3558 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3559 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3560 private final HashMap<String, InputMethodInfo> mMethodMap;
3561 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3562 new HashMap<String, List<InputMethodSubtype>>();
3563 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3564 if (methodMap == null) {
3565 throw new NullPointerException("methodMap is null");
3566 }
3567 mMethodMap = methodMap;
3568 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3569 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3570 if (!inputMethodDir.mkdirs()) {
3571 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3572 }
3573 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3574 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3575 if (!subtypeFile.exists()) {
3576 // If "subtypes.xml" doesn't exist, create a blank file.
3577 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3578 methodMap);
3579 } else {
3580 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3581 }
3582 }
3583
3584 private void deleteAllInputMethodSubtypes(String imiId) {
3585 synchronized (mMethodMap) {
3586 mSubtypesMap.remove(imiId);
3587 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3588 mMethodMap);
3589 }
3590 }
3591
3592 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003593 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003594 synchronized (mMethodMap) {
3595 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3596 final int N = additionalSubtypes.length;
3597 for (int i = 0; i < N; ++i) {
3598 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003599 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003600 subtypes.add(subtype);
3601 }
3602 }
satok4a28bde2011-06-29 21:03:40 +09003603 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003604 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3605 mMethodMap);
3606 }
3607 }
3608
3609 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3610 synchronized (mMethodMap) {
3611 return mSubtypesMap;
3612 }
3613 }
3614
3615 private static void writeAdditionalInputMethodSubtypes(
3616 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3617 HashMap<String, InputMethodInfo> methodMap) {
3618 // Safety net for the case that this function is called before methodMap is set.
3619 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3620 FileOutputStream fos = null;
3621 try {
3622 fos = subtypesFile.startWrite();
3623 final XmlSerializer out = new FastXmlSerializer();
3624 out.setOutput(fos, "utf-8");
3625 out.startDocument(null, true);
3626 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3627 out.startTag(null, NODE_SUBTYPES);
3628 for (String imiId : allSubtypes.keySet()) {
3629 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3630 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3631 continue;
3632 }
3633 out.startTag(null, NODE_IMI);
3634 out.attribute(null, ATTR_ID, imiId);
3635 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3636 final int N = subtypesList.size();
3637 for (int i = 0; i < N; ++i) {
3638 final InputMethodSubtype subtype = subtypesList.get(i);
3639 out.startTag(null, NODE_SUBTYPE);
3640 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3641 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3642 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3643 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3644 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3645 out.attribute(null, ATTR_IS_AUXILIARY,
3646 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3647 out.endTag(null, NODE_SUBTYPE);
3648 }
3649 out.endTag(null, NODE_IMI);
3650 }
3651 out.endTag(null, NODE_SUBTYPES);
3652 out.endDocument();
3653 subtypesFile.finishWrite(fos);
3654 } catch (java.io.IOException e) {
3655 Slog.w(TAG, "Error writing subtypes", e);
3656 if (fos != null) {
3657 subtypesFile.failWrite(fos);
3658 }
3659 }
3660 }
3661
3662 private static void readAdditionalInputMethodSubtypes(
3663 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3664 if (allSubtypes == null || subtypesFile == null) return;
3665 allSubtypes.clear();
3666 FileInputStream fis = null;
3667 try {
3668 fis = subtypesFile.openRead();
3669 final XmlPullParser parser = Xml.newPullParser();
3670 parser.setInput(fis, null);
3671 int type = parser.getEventType();
3672 // Skip parsing until START_TAG
3673 while ((type = parser.next()) != XmlPullParser.START_TAG
3674 && type != XmlPullParser.END_DOCUMENT) {}
3675 String firstNodeName = parser.getName();
3676 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3677 throw new XmlPullParserException("Xml doesn't start with subtypes");
3678 }
3679 final int depth =parser.getDepth();
3680 String currentImiId = null;
3681 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3682 while (((type = parser.next()) != XmlPullParser.END_TAG
3683 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3684 if (type != XmlPullParser.START_TAG)
3685 continue;
3686 final String nodeName = parser.getName();
3687 if (NODE_IMI.equals(nodeName)) {
3688 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3689 if (TextUtils.isEmpty(currentImiId)) {
3690 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3691 continue;
3692 }
3693 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3694 allSubtypes.put(currentImiId, tempSubtypesArray);
3695 } else if (NODE_SUBTYPE.equals(nodeName)) {
3696 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3697 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3698 continue;
3699 }
3700 final int icon = Integer.valueOf(
3701 parser.getAttributeValue(null, ATTR_ICON));
3702 final int label = Integer.valueOf(
3703 parser.getAttributeValue(null, ATTR_LABEL));
3704 final String imeSubtypeLocale =
3705 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3706 final String imeSubtypeMode =
3707 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3708 final String imeSubtypeExtraValue =
3709 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003710 final boolean isAuxiliary = "1".equals(String.valueOf(
3711 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003712 final InputMethodSubtype subtype =
3713 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3714 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3715 tempSubtypesArray.add(subtype);
3716 }
3717 }
3718 } catch (XmlPullParserException e) {
3719 Slog.w(TAG, "Error reading subtypes: " + e);
3720 return;
3721 } catch (java.io.IOException e) {
3722 Slog.w(TAG, "Error reading subtypes: " + e);
3723 return;
3724 } catch (NumberFormatException e) {
3725 Slog.w(TAG, "Error reading subtypes: " + e);
3726 return;
3727 } finally {
3728 if (fis != null) {
3729 try {
3730 fis.close();
3731 } catch (java.io.IOException e1) {
3732 Slog.w(TAG, "Failed to close.");
3733 }
3734 }
3735 }
3736 }
3737 }
3738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 @Override
3742 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3743 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3744 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3747 + Binder.getCallingPid()
3748 + ", uid=" + Binder.getCallingUid());
3749 return;
3750 }
3751
3752 IInputMethod method;
3753 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 synchronized (mMethodMap) {
3758 p.println("Current Input Method Manager state:");
3759 int N = mMethodList.size();
3760 p.println(" Input Methods:");
3761 for (int i=0; i<N; i++) {
3762 InputMethodInfo info = mMethodList.get(i);
3763 p.println(" InputMethod #" + i + ":");
3764 info.dump(p, " ");
3765 }
3766 p.println(" Clients:");
3767 for (ClientState ci : mClients.values()) {
3768 p.println(" Client " + ci + ":");
3769 p.println(" client=" + ci.client);
3770 p.println(" inputContext=" + ci.inputContext);
3771 p.println(" sessionRequested=" + ci.sessionRequested);
3772 p.println(" curSession=" + ci.curSession);
3773 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003774 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003776 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3777 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3779 + " mBoundToMethod=" + mBoundToMethod);
3780 p.println(" mCurToken=" + mCurToken);
3781 p.println(" mCurIntent=" + mCurIntent);
3782 method = mCurMethod;
3783 p.println(" mCurMethod=" + mCurMethod);
3784 p.println(" mEnabledSession=" + mEnabledSession);
3785 p.println(" mShowRequested=" + mShowRequested
3786 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3787 + " mShowForced=" + mShowForced
3788 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003789 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003791
Jeff Brownb88102f2010-09-08 11:49:43 -07003792 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 pw.flush();
3795 try {
3796 client.client.asBinder().dump(fd, args);
3797 } catch (RemoteException e) {
3798 p.println("Input method client dead: " + e);
3799 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003800 } else {
3801 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003803
Jeff Brownb88102f2010-09-08 11:49:43 -07003804 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 pw.flush();
3807 try {
3808 method.asBinder().dump(fd, args);
3809 } catch (RemoteException e) {
3810 p.println("Input method service dead: " + e);
3811 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003812 } else {
3813 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 }
3815 }
3816}