blob: 956bdb14aaa501f0cd3fedadde40d51538957d32 [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;
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 class SessionState {
200 final ClientState client;
201 final IInputMethod method;
202 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 @Override
205 public String toString() {
206 return "SessionState{uid " + client.uid + " pid " + client.pid
207 + " method " + Integer.toHexString(
208 System.identityHashCode(method))
209 + " session " + Integer.toHexString(
210 System.identityHashCode(session))
211 + "}";
212 }
213
214 SessionState(ClientState _client, IInputMethod _method,
215 IInputMethodSession _session) {
216 client = _client;
217 method = _method;
218 session = _session;
219 }
220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 class ClientState {
223 final IInputMethodClient client;
224 final IInputContext inputContext;
225 final int uid;
226 final int pid;
227 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 boolean sessionRequested;
230 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 @Override
233 public String toString() {
234 return "ClientState{" + Integer.toHexString(
235 System.identityHashCode(this)) + " uid " + uid
236 + " pid " + pid + "}";
237 }
238
239 ClientState(IInputMethodClient _client, IInputContext _inputContext,
240 int _uid, int _pid) {
241 client = _client;
242 inputContext = _inputContext;
243 uid = _uid;
244 pid = _pid;
245 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
246 }
247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final HashMap<IBinder, ClientState> mClients
250 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700253 * Set once the system is ready to run third party code.
254 */
255 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * Id of the currently selected input method.
259 */
260 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * The current binding sequence number, incremented every time there is
264 * a new bind performed.
265 */
266 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
269 * The client that is currently bound to an input method.
270 */
271 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700274 * The last window token that gained focus.
275 */
276 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * The input context last provided by the current client.
280 */
281 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * The attributes last provided by the current client.
285 */
286 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The input method ID of the input method service that we are currently
290 * connected to or in the process of connecting to.
291 */
292 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
satokab751aa2010-09-14 19:17:36 +0900295 * The current subtype of the current input method.
296 */
297 private InputMethodSubtype mCurrentSubtype;
298
satok4e4569d2010-11-19 18:45:53 +0900299 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900300 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
301 mShortcutInputMethodsAndSubtypes =
302 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900303
304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * Set to true if our ServiceConnection is currently actively bound to
306 * a service (whether or not we have gotten its IBinder back yet).
307 */
308 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * Set if the client has asked for the input method to be shown.
312 */
313 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * Set if we were explicitly told to show the input method.
317 */
318 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * Set if we were forced to be shown.
322 */
323 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * Set if we last told the input method to show itself.
327 */
328 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * The Intent used to connect to the current input method.
332 */
333 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
336 * The token we have made for the currently active input method, to
337 * identify it in the future.
338 */
339 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
342 * If non-null, this is the input method service we are currently connected
343 * to.
344 */
345 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * Time that we last initiated a bind to the input method, to determine
349 * if we should try to disconnect and reconnect to it.
350 */
351 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * Have we called mCurMethod.bindInput()?
355 */
356 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * Currently enabled session. Only touched by service thread, not
360 * protected by a lock.
361 */
362 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * True if the screen is on. The value is true initially.
366 */
367 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
Joe Onorato857fd9b2011-01-27 15:08:35 -0800369 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
370 int mImeWindowVis;
371
Ken Wakasa05dbb652011-08-22 15:22:43 +0900372 private AlertDialog.Builder mDialogBuilder;
373 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900374 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900375 private InputMethodInfo[] mIms;
376 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900377 private Locale mLastSystemLocale;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 class SettingsObserver extends ContentObserver {
380 SettingsObserver(Handler handler) {
381 super(handler);
382 ContentResolver resolver = mContext.getContentResolver();
383 resolver.registerContentObserver(Settings.Secure.getUriFor(
384 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900385 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900386 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
387 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900388 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 @Override public void onChange(boolean selfChange) {
392 synchronized (mMethodMap) {
393 updateFromSettingsLocked();
394 }
395 }
396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
399 @Override
400 public void onReceive(Context context, Intent intent) {
401 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
402 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900403 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
405 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900406 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700407 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
408 hideInputMethodMenu();
409 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800411 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413
414 // Inform the current client of the change in active status
415 try {
416 if (mCurClient != null && mCurClient.client != null) {
417 mCurClient.client.setActive(mScreenOn);
418 }
419 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800420 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 + mCurClient.pid + " uid " + mCurClient.uid);
422 }
423 }
424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800425
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800426 class MyPackageMonitor extends PackageMonitor {
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800429 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
433 final int N = mMethodList.size();
434 if (curInputMethodId != null) {
435 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 InputMethodInfo imi = mMethodList.get(i);
437 if (imi.getId().equals(curInputMethodId)) {
438 for (String pkg : packages) {
439 if (imi.getPackageName().equals(pkg)) {
440 if (!doit) {
441 return true;
442 }
satok723a27e2010-11-11 14:58:11 +0900443 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 chooseNewDefaultIMELocked();
445 return true;
446 }
447 }
448 }
449 }
450 }
451 }
452 return false;
453 }
454
455 @Override
456 public void onSomePackagesChanged() {
457 synchronized (mMethodMap) {
458 InputMethodInfo curIm = null;
459 String curInputMethodId = Settings.Secure.getString(mContext
460 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
461 final int N = mMethodList.size();
462 if (curInputMethodId != null) {
463 for (int i=0; i<N; i++) {
464 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900465 final String imiId = imi.getId();
466 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800467 curIm = imi;
468 }
satoke7c6998e2011-06-03 17:57:59 +0900469
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900471 if (isPackageModified(imi.getPackageName())) {
472 mFileManager.deleteAllInputMethodSubtypes(imiId);
473 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 if (change == PACKAGE_TEMPORARY_CHANGE
475 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800477 + imi.getComponent());
478 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480 }
481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 buildInputMethodListLocked(mMethodList, mMethodMap);
484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800486
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800487 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 int change = isPackageDisappearing(curIm.getPackageName());
489 if (change == PACKAGE_TEMPORARY_CHANGE
490 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800491 ServiceInfo si = null;
492 try {
493 si = mContext.getPackageManager().getServiceInfo(
494 curIm.getComponent(), 0);
495 } catch (PackageManager.NameNotFoundException ex) {
496 }
497 if (si == null) {
498 // Uh oh, current input method is no longer around!
499 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800500 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900501 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800503 changed = true;
504 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800505 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900506 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800507 }
508 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800509 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 }
satokab751aa2010-09-14 19:17:36 +0900511
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800512 if (curIm == null) {
513 // We currently don't have a default input method... is
514 // one now available?
515 changed = chooseNewDefaultIMELocked();
516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800517
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 if (changed) {
519 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 }
522 }
523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Jean Chalarde0d32a62011-10-20 20:36:07 +0900525 private static class MethodCallback extends IInputMethodCallback.Stub {
526 private final IInputMethod mMethod;
527 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800528
Jean Chalarde0d32a62011-10-20 20:36:07 +0900529 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900531 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800533
satoke7c6998e2011-06-03 17:57:59 +0900534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 public void finishedEvent(int seq, boolean handled) throws RemoteException {
536 }
537
satoke7c6998e2011-06-03 17:57:59 +0900538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900540 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
satok01038492012-04-09 21:08:27 +0900544 private class HardKeyboardListener
545 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
546 @Override
547 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
548 mHandler.sendMessage(mHandler.obtainMessage(
549 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
550 }
551
552 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
553 if (DEBUG) {
554 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
555 + enabled);
556 }
557 synchronized(mMethodMap) {
558 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
559 && mSwitchingDialog.isShowing()) {
560 mSwitchingDialogTitleView.findViewById(
561 com.android.internal.R.id.hard_keyboard_section).setVisibility(
562 available ? View.VISIBLE : View.GONE);
563 }
564 }
565 }
566 }
567
568 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800570 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 mHandler = new Handler(this);
572 mIWindowManager = IWindowManager.Stub.asInterface(
573 ServiceManager.getService(Context.WINDOW_SERVICE));
574 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900575 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public void executeMessage(Message msg) {
577 handleMessage(msg);
578 }
579 });
satok01038492012-04-09 21:08:27 +0900580 mWindowManagerService = windowManager;
581 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900582
satok7cfc0ed2011-06-20 21:29:36 +0900583 mImeSwitcherNotification = new Notification();
584 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
585 mImeSwitcherNotification.when = 0;
586 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
587 mImeSwitcherNotification.tickerText = null;
588 mImeSwitcherNotification.defaults = 0; // please be quiet
589 mImeSwitcherNotification.sound = null;
590 mImeSwitcherNotification.vibrate = null;
591 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900592 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satok5b927c432012-05-01 20:09:34 +0900593 mLastSystemLocale = mRes.getConfiguration().locale;
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900616 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
satokd87c2592010-09-29 11:52:06 +0900618 if (TextUtils.isEmpty(Settings.Secure.getString(
619 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
satok5b927c432012-05-01 20:09:34 +0900620 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 mSettingsObserver = new SettingsObserver(mHandler);
624 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900625
626 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
627 // according to the new system locale.
628 final IntentFilter filter = new IntentFilter();
629 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
630 mContext.registerReceiver(
631 new BroadcastReceiver() {
632 @Override
633 public void onReceive(Context context, Intent intent) {
634 synchronized(mMethodMap) {
635 checkCurrentLocaleChangedLocked();
636 }
637 }
638 }, filter);
639 }
640
641 private void checkCurrentLocaleChangedLocked() {
642 final Locale newLocale = mRes.getConfiguration().locale;
643 if (newLocale != null && !newLocale.equals(mLastSystemLocale)) {
644 if (DEBUG) {
645 Slog.i(TAG, "Locale has been changed to " + newLocale);
646 }
647 buildInputMethodListLocked(mMethodList, mMethodMap);
648 // Reset the current ime to the proper one
649 resetDefaultImeLocked(mContext);
650 mLastSystemLocale = newLocale;
651 }
652 }
653
654 private void resetDefaultImeLocked(Context context) {
655 // Do not reset the default (current) IME when it is a 3rd-party IME
656 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
657 return;
658 }
659
660 InputMethodInfo defIm = null;
661 for (InputMethodInfo imi : mMethodList) {
662 if (defIm == null) {
663 if (isValidSystemDefaultIme(imi, context)) {
664 defIm = imi;
665 Slog.i(TAG, "Selected default: " + imi.getId());
666 }
667 }
668 }
669 if (defIm == null && mMethodList.size() > 0) {
670 defIm = getMostApplicableDefaultIMELocked();
671 Slog.i(TAG, "No default found, using " + defIm.getId());
672 }
673 if (defIm != null) {
674 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
675 }
676 }
677
678 private static boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
679 if (!isSystemIme(imi)) {
680 return false;
681 }
682 if (imi.getIsDefaultResourceId() != 0) {
683 try {
684 Resources res = context.createPackageContext(
685 imi.getPackageName(), 0).getResources();
686 if (res.getBoolean(imi.getIsDefaultResourceId())
687 && containsSubtypeOf(imi, context.getResources().getConfiguration().
688 locale.getLanguage())) {
689 return true;
690 }
691 } catch (PackageManager.NameNotFoundException ex) {
692 } catch (Resources.NotFoundException ex) {
693 }
694 }
695 if (imi.getSubtypeCount() == 0) {
696 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
697 }
698 return false;
699 }
700
701 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
702 if (!isSystemIme(imi)) {
703 return false;
704 }
705 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
706 }
707
708 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
709 final int N = imi.getSubtypeCount();
710 for (int i = 0; i < N; ++i) {
711 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
712 return true;
713 }
714 }
715 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
718 @Override
719 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
720 throws RemoteException {
721 try {
722 return super.onTransact(code, data, reply, flags);
723 } catch (RuntimeException e) {
724 // The input method manager only throws security exceptions, so let's
725 // log all others.
726 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800727 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 throw e;
730 }
731 }
732
Dianne Hackborn661cd522011-08-22 00:26:20 -0700733 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700734 synchronized (mMethodMap) {
735 if (!mSystemReady) {
736 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700737 mKeyguardManager = (KeyguardManager)
738 mContext.getSystemService(Context.KEYGUARD_SERVICE);
739 mNotificationManager = (NotificationManager)
740 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
satok5b927c432012-05-01 20:09:34 +0900741 mLastSystemLocale = mContext.getResources().getConfiguration().locale;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700742 mStatusBar = statusBar;
743 statusBar.setIconVisibility("ime", false);
744 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900745 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
746 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900747 if (mShowOngoingImeSwitcherForPhones) {
748 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
749 mHardKeyboardListener);
750 }
Dianne Hackborncc278702009-09-02 23:07:23 -0700751 try {
752 startInputInnerLocked();
753 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800754 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700755 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700756 }
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
satok15452a42011-10-28 17:58:28 +0900760 private void setImeWindowVisibilityStatusHiddenLocked() {
761 mImeWindowVis = 0;
762 updateImeWindowStatusLocked();
763 }
764
satok3afd6c02011-11-18 08:38:19 +0900765 private void refreshImeWindowVisibilityLocked() {
766 final Configuration conf = mRes.getConfiguration();
767 final boolean haveHardKeyboard = conf.keyboard
768 != Configuration.KEYBOARD_NOKEYS;
769 final boolean hardKeyShown = haveHardKeyboard
770 && conf.hardKeyboardHidden
771 != Configuration.HARDKEYBOARDHIDDEN_YES;
772 final boolean isScreenLocked = mKeyguardManager != null
773 && mKeyguardManager.isKeyguardLocked()
774 && mKeyguardManager.isKeyguardSecure();
775 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
776 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
777 updateImeWindowStatusLocked();
778 }
779
satok15452a42011-10-28 17:58:28 +0900780 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900781 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700782 }
783
satoke7c6998e2011-06-03 17:57:59 +0900784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 public List<InputMethodInfo> getInputMethodList() {
786 synchronized (mMethodMap) {
787 return new ArrayList<InputMethodInfo>(mMethodList);
788 }
789 }
790
satoke7c6998e2011-06-03 17:57:59 +0900791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 public List<InputMethodInfo> getEnabledInputMethodList() {
793 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900794 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796 }
797
satokbb4aa062011-01-19 21:40:27 +0900798 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
799 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
800 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
801 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
802 for (InputMethodInfo imi: getEnabledInputMethodList()) {
803 enabledInputMethodAndSubtypes.put(
804 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
805 }
806 return enabledInputMethodAndSubtypes;
807 }
808
809 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
810 boolean allowsImplicitlySelectedSubtypes) {
811 if (imi == null && mCurMethodId != null) {
812 imi = mMethodMap.get(mCurMethodId);
813 }
satok7265d9b2011-02-14 15:47:30 +0900814 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900815 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900816 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900817 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900818 }
satok7265d9b2011-02-14 15:47:30 +0900819 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900820 }
821
satoke7c6998e2011-06-03 17:57:59 +0900822 @Override
satok16331c82010-12-20 23:48:46 +0900823 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
824 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900825 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900826 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900827 }
828 }
829
satoke7c6998e2011-06-03 17:57:59 +0900830 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 public void addClient(IInputMethodClient client,
832 IInputContext inputContext, int uid, int pid) {
833 synchronized (mMethodMap) {
834 mClients.put(client.asBinder(), new ClientState(client,
835 inputContext, uid, pid));
836 }
837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800838
satoke7c6998e2011-06-03 17:57:59 +0900839 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 public void removeClient(IInputMethodClient client) {
841 synchronized (mMethodMap) {
842 mClients.remove(client.asBinder());
843 }
844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 void executeOrSendMessage(IInterface target, Message msg) {
847 if (target.asBinder() instanceof Binder) {
848 mCaller.sendMessage(msg);
849 } else {
850 handleMessage(msg);
851 msg.recycle();
852 }
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700855 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 + mCurClient.client.asBinder());
859 if (mBoundToMethod) {
860 mBoundToMethod = false;
861 if (mCurMethod != null) {
862 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
863 MSG_UNBIND_INPUT, mCurMethod));
864 }
865 }
866 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
867 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
868 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 // Call setActive(false) on the old client
871 try {
872 mCurClient.client.setActive(false);
873 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800874 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 + mCurClient.pid + " uid " + mCurClient.uid);
876 }
877 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Project10592532009-03-18 17:39:46 -0700879 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 private int getImeShowFlags() {
884 int flags = 0;
885 if (mShowForced) {
886 flags |= InputMethod.SHOW_FORCED
887 | InputMethod.SHOW_EXPLICIT;
888 } else if (mShowExplicitlyRequested) {
889 flags |= InputMethod.SHOW_EXPLICIT;
890 }
891 return flags;
892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 private int getAppShowFlags() {
895 int flags = 0;
896 if (mShowForced) {
897 flags |= InputMethodManager.SHOW_FORCED;
898 } else if (!mShowExplicitlyRequested) {
899 flags |= InputMethodManager.SHOW_IMPLICIT;
900 }
901 return flags;
902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
Dianne Hackborn7663d802012-02-24 13:08:49 -0800904 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (!mBoundToMethod) {
906 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
907 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
908 mBoundToMethod = true;
909 }
910 final SessionState session = mCurClient.curSession;
911 if (initial) {
912 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
913 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
914 } else {
915 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
916 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
917 }
918 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800919 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800920 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800922 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800926 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 // If no method is currently selected, do nothing.
928 if (mCurMethodId == null) {
929 return mNoBinding;
930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 ClientState cs = mClients.get(client.asBinder());
933 if (cs == null) {
934 throw new IllegalArgumentException("unknown client "
935 + client.asBinder());
936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 try {
939 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
940 // Check with the window manager to make sure this client actually
941 // has a window with focus. If not, reject. This is thread safe
942 // because if the focus changes some time before or after, the
943 // next client receiving focus that has any interest in input will
944 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
947 return null;
948 }
949 } catch (RemoteException e) {
950 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951
Dianne Hackborn7663d802012-02-24 13:08:49 -0800952 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
953 }
954
955 InputBindResult startInputUncheckedLocked(ClientState cs,
956 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
957 // If no method is currently selected, do nothing.
958 if (mCurMethodId == null) {
959 return mNoBinding;
960 }
961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 if (mCurClient != cs) {
963 // If the client is changing, we need to switch over to the new
964 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700965 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800966 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 + cs.client.asBinder());
968
969 // If the screen is on, inform the new client it is active
970 if (mScreenOn) {
971 try {
972 cs.client.setActive(mScreenOn);
973 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800974 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 + cs.pid + " uid " + cs.uid);
976 }
977 }
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 // Bump up the sequence for this client and attach it.
981 mCurSeq++;
982 if (mCurSeq <= 0) mCurSeq = 1;
983 mCurClient = cs;
984 mCurInputContext = inputContext;
985 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 // Check if the input method is changing.
988 if (mCurId != null && mCurId.equals(mCurMethodId)) {
989 if (cs.curSession != null) {
990 // Fast case: if we are already connected to the input method,
991 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -0800992 return attachNewInputLocked(
993 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
995 if (mHaveConnection) {
996 if (mCurMethod != null) {
997 if (!cs.sessionRequested) {
998 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1001 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001002 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004 // Return to client, and we will get back with it when
1005 // we have had a session made for it.
1006 return new InputBindResult(null, mCurId, mCurSeq);
1007 } else if (SystemClock.uptimeMillis()
1008 < (mLastBindTime+TIME_TO_RECONNECT)) {
1009 // In this case we have connected to the service, but
1010 // don't yet have its interface. If it hasn't been too
1011 // long since we did the connection, we'll return to
1012 // the client and wait to get the service interface so
1013 // we can report back. If it has been too long, we want
1014 // to fall through so we can try a disconnect/reconnect
1015 // to see if we can get back in touch with the service.
1016 return new InputBindResult(null, mCurId, mCurSeq);
1017 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1019 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 }
1022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001024 return startInputInnerLocked();
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001027 InputBindResult startInputInnerLocked() {
1028 if (mCurMethodId == null) {
1029 return mNoBinding;
1030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001031
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001032 if (!mSystemReady) {
1033 // If the system is not yet ready, we shouldn't be running third
1034 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001035 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1039 if (info == null) {
1040 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001043 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1046 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001047 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1048 com.android.internal.R.string.input_method_binding_label);
1049 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1050 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001051 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1052 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 mLastBindTime = SystemClock.uptimeMillis();
1054 mHaveConnection = true;
1055 mCurId = info.getId();
1056 mCurToken = new Binder();
1057 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 mIWindowManager.addWindowToken(mCurToken,
1060 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1061 } catch (RemoteException e) {
1062 }
1063 return new InputBindResult(null, mCurId, mCurSeq);
1064 } else {
1065 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001066 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 + mCurIntent);
1068 }
1069 return null;
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
satoke7c6998e2011-06-03 17:57:59 +09001072 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001074 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 synchronized (mMethodMap) {
1076 final long ident = Binder.clearCallingIdentity();
1077 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001078 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 } finally {
1080 Binder.restoreCallingIdentity(ident);
1081 }
1082 }
1083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001084
satoke7c6998e2011-06-03 17:57:59 +09001085 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 public void finishInput(IInputMethodClient client) {
1087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
satoke7c6998e2011-06-03 17:57:59 +09001089 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 public void onServiceConnected(ComponentName name, IBinder service) {
1091 synchronized (mMethodMap) {
1092 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1093 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001094 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001096 unbindCurrentMethodLocked(false);
1097 return;
1098 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001100 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1101 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001103 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001104 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001106 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001107 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109 }
1110 }
1111 }
1112
1113 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1114 synchronized (mMethodMap) {
1115 if (mCurMethod != null && method != null
1116 && mCurMethod.asBinder() == method.asBinder()) {
1117 if (mCurClient != null) {
1118 mCurClient.curSession = new SessionState(mCurClient,
1119 method, session);
1120 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001121 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (res.method != null) {
1123 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1124 MSG_BIND_METHOD, mCurClient.client, res));
1125 }
1126 }
1127 }
1128 }
1129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001131 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001132 if (mVisibleBound) {
1133 mContext.unbindService(mVisibleConnection);
1134 mVisibleBound = false;
1135 }
1136
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001137 if (mHaveConnection) {
1138 mContext.unbindService(this);
1139 mHaveConnection = false;
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001142 if (mCurToken != null) {
1143 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001145 mIWindowManager.removeWindowToken(mCurToken);
1146 } catch (RemoteException e) {
1147 }
1148 mCurToken = null;
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
The Android Open Source Project10592532009-03-18 17:39:46 -07001151 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001152 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001154 if (reportToClient && mCurClient != null) {
1155 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1156 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1157 }
1158 }
1159
Devin Taylor0c33ed22010-02-23 13:26:46 -06001160 private void finishSession(SessionState sessionState) {
1161 if (sessionState != null && sessionState.session != null) {
1162 try {
1163 sessionState.session.finishSession();
1164 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001165 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001166 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001167 }
1168 }
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001171 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 if (mCurMethod != null) {
1173 for (ClientState cs : mClients.values()) {
1174 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001175 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 cs.curSession = null;
1177 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001178
1179 finishSession(mEnabledSession);
1180 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 mCurMethod = null;
1182 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001183 if (mStatusBar != null) {
1184 mStatusBar.setIconVisibility("ime", false);
1185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
satoke7c6998e2011-06-03 17:57:59 +09001188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 public void onServiceDisconnected(ComponentName name) {
1190 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001191 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 + " mCurIntent=" + mCurIntent);
1193 if (mCurMethod != null && mCurIntent != null
1194 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001195 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 // We consider this to be a new bind attempt, since the system
1197 // should now try to restart the service for us.
1198 mLastBindTime = SystemClock.uptimeMillis();
1199 mShowRequested = mInputShown;
1200 mInputShown = false;
1201 if (mCurClient != null) {
1202 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1203 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1204 }
1205 }
1206 }
1207 }
1208
satokf9f01002011-05-19 21:31:50 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001211 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 long ident = Binder.clearCallingIdentity();
1213 try {
1214 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001215 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 return;
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 synchronized (mMethodMap) {
1220 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001222 if (mStatusBar != null) {
1223 mStatusBar.setIconVisibility("ime", false);
1224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001226 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001227 CharSequence contentDescription = null;
1228 try {
1229 PackageManager packageManager = mContext.getPackageManager();
1230 contentDescription = packageManager.getApplicationLabel(
1231 packageManager.getApplicationInfo(packageName, 0));
1232 } catch (NameNotFoundException nnfe) {
1233 /* ignore */
1234 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001235 if (mStatusBar != null) {
1236 mStatusBar.setIcon("ime", packageName, iconId, 0,
1237 contentDescription != null
1238 ? contentDescription.toString() : null);
1239 mStatusBar.setIconVisibility("ime", true);
1240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 }
1243 } finally {
1244 Binder.restoreCallingIdentity(ident);
1245 }
1246 }
1247
satok7cfc0ed2011-06-20 21:29:36 +09001248 private boolean needsToShowImeSwitchOngoingNotification() {
1249 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001250 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001251 synchronized (mMethodMap) {
1252 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1253 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001254 if (N > 2) return true;
1255 if (N < 1) return false;
1256 int nonAuxCount = 0;
1257 int auxCount = 0;
1258 InputMethodSubtype nonAuxSubtype = null;
1259 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001260 for(int i = 0; i < N; ++i) {
1261 final InputMethodInfo imi = imis.get(i);
1262 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1263 imi, true);
1264 final int subtypeCount = subtypes.size();
1265 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001266 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001267 } else {
1268 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001269 final InputMethodSubtype subtype = subtypes.get(j);
1270 if (!subtype.isAuxiliary()) {
1271 ++nonAuxCount;
1272 nonAuxSubtype = subtype;
1273 } else {
1274 ++auxCount;
1275 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001276 }
1277 }
1278 }
satok7cfc0ed2011-06-20 21:29:36 +09001279 }
satokb6359412011-06-28 17:47:41 +09001280 if (nonAuxCount > 1 || auxCount > 1) {
1281 return true;
1282 } else if (nonAuxCount == 1 && auxCount == 1) {
1283 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001284 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1285 || auxSubtype.overridesImplicitlyEnabledSubtype()
1286 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001287 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1288 return false;
1289 }
1290 return true;
1291 }
1292 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001293 }
satok7cfc0ed2011-06-20 21:29:36 +09001294 }
1295
satokdbf29502011-08-25 15:28:23 +09001296 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001297 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001298 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001299 int uid = Binder.getCallingUid();
1300 long ident = Binder.clearCallingIdentity();
1301 try {
1302 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001303 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001304 return;
1305 }
1306
1307 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001308 mImeWindowVis = vis;
1309 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001310 if (mStatusBar != null) {
1311 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1312 }
satok7cfc0ed2011-06-20 21:29:36 +09001313 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001314 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1315 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001316 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001317 final CharSequence title = mRes.getText(
1318 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001319 final CharSequence imiLabel = imi.loadLabel(pm);
1320 final CharSequence summary = mCurrentSubtype != null
1321 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1322 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1323 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001324 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001325 : imiLabel;
1326
satok7cfc0ed2011-06-20 21:29:36 +09001327 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001328 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001329 if (mNotificationManager != null) {
1330 mNotificationManager.notify(
1331 com.android.internal.R.string.select_input_method,
1332 mImeSwitcherNotification);
1333 mNotificationShown = true;
1334 }
satok7cfc0ed2011-06-20 21:29:36 +09001335 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001336 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001337 mNotificationManager.cancel(
1338 com.android.internal.R.string.select_input_method);
1339 mNotificationShown = false;
1340 }
1341 }
satok06487a52010-10-29 11:37:18 +09001342 }
1343 } finally {
1344 Binder.restoreCallingIdentity(ident);
1345 }
1346 }
1347
satoke7c6998e2011-06-03 17:57:59 +09001348 @Override
satokf9f01002011-05-19 21:31:50 +09001349 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1350 synchronized (mMethodMap) {
1351 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1352 for (int i = 0; i < spans.length; ++i) {
1353 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001354 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001355 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001356 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001357 }
1358 }
1359 }
1360 }
1361
satoke7c6998e2011-06-03 17:57:59 +09001362 @Override
satokf9f01002011-05-19 21:31:50 +09001363 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1364 synchronized (mMethodMap) {
1365 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1366 // TODO: Do not send the intent if the process of the targetImi is already dead.
1367 if (targetImi != null) {
1368 final String[] suggestions = span.getSuggestions();
1369 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001370 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001371 final Intent intent = new Intent();
1372 // Ensures that only a class in the original IME package will receive the
1373 // notification.
satok42c5a162011-05-26 16:46:14 +09001374 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001375 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1376 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1377 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1378 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1379 mContext.sendBroadcast(intent);
1380 return true;
1381 }
1382 }
1383 return false;
1384 }
1385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001387 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1388 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1389 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1390 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001392 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001393 // There is no input method selected, try to choose new applicable input method.
1394 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1395 id = Settings.Secure.getString(mContext.getContentResolver(),
1396 Settings.Secure.DEFAULT_INPUT_METHOD);
1397 }
1398 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 try {
satokab751aa2010-09-14 19:17:36 +09001400 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001402 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001403 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001404 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
satokf3db1af2010-11-23 13:34:33 +09001406 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001407 } else {
1408 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001409 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001410 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
satokab751aa2010-09-14 19:17:36 +09001414 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 InputMethodInfo info = mMethodMap.get(id);
1416 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001417 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001421 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001422 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1423 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001424 }
1425 if (subtype != mCurrentSubtype) {
1426 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001427 if (subtype != null) {
1428 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1429 }
satokcd7cd292010-11-20 15:46:23 +09001430 if (mCurMethod != null) {
1431 try {
satok3afd6c02011-11-18 08:38:19 +09001432 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001433 // If subtype is null, try to find the most applicable one from
1434 // getCurrentInputMethodSubtype.
1435 if (subtype == null) {
1436 subtype = getCurrentInputMethodSubtype();
1437 }
1438 mCurMethod.changeInputMethodSubtype(subtype);
1439 } catch (RemoteException e) {
1440 return;
satokab751aa2010-09-14 19:17:36 +09001441 }
1442 }
1443 }
1444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 return;
1446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 final long ident = Binder.clearCallingIdentity();
1449 try {
satokab751aa2010-09-14 19:17:36 +09001450 // Set a subtype to this input method.
1451 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001452 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1453 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1454 // because mCurMethodId is stored as a history in
1455 // setSelectedInputMethodAndSubtypeLocked().
1456 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457
1458 if (ActivityManagerNative.isSystemReady()) {
1459 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001460 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 intent.putExtra("input_method_id", id);
1462 mContext.sendBroadcast(intent);
1463 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001464 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 } finally {
1466 Binder.restoreCallingIdentity(ident);
1467 }
1468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
satok42c5a162011-05-26 16:46:14 +09001470 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001471 public boolean showSoftInput(IInputMethodClient client, int flags,
1472 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001473 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 long ident = Binder.clearCallingIdentity();
1475 try {
1476 synchronized (mMethodMap) {
1477 if (mCurClient == null || client == null
1478 || mCurClient.client.asBinder() != client.asBinder()) {
1479 try {
1480 // We need to check if this is the current client with
1481 // focus in the window manager, to allow this call to
1482 // be made before input is started in it.
1483 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001484 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001485 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001488 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
Joe Onorato8a9b2202010-02-26 18:56:32 -08001492 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001493 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
1495 } finally {
1496 Binder.restoreCallingIdentity(ident);
1497 }
1498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001499
The Android Open Source Project4df24232009-03-05 14:34:35 -08001500 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 mShowRequested = true;
1502 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1503 mShowExplicitlyRequested = true;
1504 }
1505 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1506 mShowExplicitlyRequested = true;
1507 mShowForced = true;
1508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001509
Dianne Hackborncc278702009-09-02 23:07:23 -07001510 if (!mSystemReady) {
1511 return false;
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
The Android Open Source Project4df24232009-03-05 14:34:35 -08001514 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001516 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1517 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1518 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001520 if (mHaveConnection && !mVisibleBound) {
1521 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1522 mVisibleBound = true;
1523 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001524 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001526 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // The client has asked to have the input method shown, but
1528 // we have been sitting here too long with a connection to the
1529 // service and no interface received, so let's disconnect/connect
1530 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001533 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001535 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1536 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001538
The Android Open Source Project4df24232009-03-05 14:34:35 -08001539 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
satok42c5a162011-05-26 16:46:14 +09001542 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001543 public boolean hideSoftInput(IInputMethodClient client, int flags,
1544 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001545 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 long ident = Binder.clearCallingIdentity();
1547 try {
1548 synchronized (mMethodMap) {
1549 if (mCurClient == null || client == null
1550 || mCurClient.client.asBinder() != client.asBinder()) {
1551 try {
1552 // We need to check if this is the current client with
1553 // focus in the window manager, to allow this call to
1554 // be made before input is started in it.
1555 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001556 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1557 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001558 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001559 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001562 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001563 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001566
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001568 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
1570 } finally {
1571 Binder.restoreCallingIdentity(ident);
1572 }
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
The Android Open Source Project4df24232009-03-05 14:34:35 -08001575 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1577 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001578 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001580 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001583 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001585 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001587 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001589 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1590 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1591 res = true;
1592 } else {
1593 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001595 if (mHaveConnection && mVisibleBound) {
1596 mContext.unbindService(mVisibleConnection);
1597 mVisibleBound = false;
1598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 mInputShown = false;
1600 mShowRequested = false;
1601 mShowExplicitlyRequested = false;
1602 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001603 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
satok42c5a162011-05-26 16:46:14 +09001606 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001607 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1608 int controlFlags, int softInputMode, int windowFlags,
1609 EditorInfo attribute, IInputContext inputContext) {
1610 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 long ident = Binder.clearCallingIdentity();
1612 try {
1613 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001614 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001615 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001617 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001618
Dianne Hackborn7663d802012-02-24 13:08:49 -08001619 ClientState cs = mClients.get(client.asBinder());
1620 if (cs == null) {
1621 throw new IllegalArgumentException("unknown client "
1622 + client.asBinder());
1623 }
1624
1625 try {
1626 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1627 // Check with the window manager to make sure this client actually
1628 // has a window with focus. If not, reject. This is thread safe
1629 // because if the focus changes some time before or after, the
1630 // next client receiving focus that has any interest in input will
1631 // be calling through here after that change happens.
1632 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1633 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1634 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001636 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001639 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001640 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001641 if (attribute != null) {
1642 return startInputUncheckedLocked(cs, inputContext, attribute,
1643 controlFlags);
1644 }
1645 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001646 }
1647 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001649 // Should we auto-show the IME even if the caller has not
1650 // specified what should be done with it?
1651 // We only do this automatically if the window can resize
1652 // to accommodate the IME (so what the user sees will give
1653 // them good context without input information being obscured
1654 // by the IME) or if running on a large screen where there
1655 // is more room for the target window + IME.
1656 final boolean doAutoShow =
1657 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1658 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1659 || mRes.getConfiguration().isLayoutSizeAtLeast(
1660 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001661 final boolean isTextEditor =
1662 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1663
1664 // We want to start input before showing the IME, but after closing
1665 // it. We want to do this after closing it to help the IME disappear
1666 // more quickly (not get stuck behind it initializing itself for the
1667 // new focused input, even if its window wants to hide the IME).
1668 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1671 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001672 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1674 // There is no focus view, and this window will
1675 // be behind any soft input window, so hide the
1676 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001677 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001678 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001680 } else if (isTextEditor && doAutoShow && (softInputMode &
1681 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 // There is a focus view, and we are navigating forward
1683 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001684 // We only do this automatically if the window can resize
1685 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001686 // them good context without input information being obscured
1687 // by the IME) or if running on a large screen where there
1688 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001689 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001690 if (attribute != null) {
1691 res = startInputUncheckedLocked(cs, inputContext, attribute,
1692 controlFlags);
1693 didStart = true;
1694 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001695 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697 break;
1698 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1699 // Do nothing.
1700 break;
1701 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1702 if ((softInputMode &
1703 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001704 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001705 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
1707 break;
1708 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001709 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 break;
1712 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1713 if ((softInputMode &
1714 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001715 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001716 if (attribute != null) {
1717 res = startInputUncheckedLocked(cs, inputContext, attribute,
1718 controlFlags);
1719 didStart = true;
1720 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001721 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
1723 break;
1724 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001725 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001726 if (attribute != null) {
1727 res = startInputUncheckedLocked(cs, inputContext, attribute,
1728 controlFlags);
1729 didStart = true;
1730 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 break;
1733 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001734
1735 if (!didStart && attribute != null) {
1736 res = startInputUncheckedLocked(cs, inputContext, attribute,
1737 controlFlags);
1738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740 } finally {
1741 Binder.restoreCallingIdentity(ident);
1742 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001743
1744 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001746
satok42c5a162011-05-26 16:46:14 +09001747 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1749 synchronized (mMethodMap) {
1750 if (mCurClient == null || client == null
1751 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001752 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001753 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
1755
satok440aab52010-11-25 09:43:11 +09001756 // Always call subtype picker, because subtype picker is a superset of input method
1757 // picker.
satokab751aa2010-09-14 19:17:36 +09001758 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1759 }
1760 }
1761
satok42c5a162011-05-26 16:46:14 +09001762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001764 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1765 }
1766
satok42c5a162011-05-26 16:46:14 +09001767 @Override
satok28203512010-11-24 11:06:49 +09001768 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1769 synchronized (mMethodMap) {
1770 if (subtype != null) {
1771 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1772 mMethodMap.get(id), subtype.hashCode()));
1773 } else {
1774 setInputMethod(token, id);
1775 }
1776 }
satokab751aa2010-09-14 19:17:36 +09001777 }
1778
satok42c5a162011-05-26 16:46:14 +09001779 @Override
satokb416a712010-11-25 20:42:14 +09001780 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001781 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001782 synchronized (mMethodMap) {
1783 if (mCurClient == null || client == null
1784 || mCurClient.client.asBinder() != client.asBinder()) {
1785 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1786 }
satok7fee71f2010-12-17 18:54:26 +09001787 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1788 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001789 }
1790 }
1791
satok4fc87d62011-05-20 16:13:43 +09001792 @Override
satok735cf382010-11-11 20:40:09 +09001793 public boolean switchToLastInputMethod(IBinder token) {
1794 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001795 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001796 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001797 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001798 lastImi = mMethodMap.get(lastIme.first);
1799 } else {
1800 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001801 }
satok4fc87d62011-05-20 16:13:43 +09001802 String targetLastImiId = null;
1803 int subtypeId = NOT_A_SUBTYPE_ID;
1804 if (lastIme != null && lastImi != null) {
1805 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1806 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1807 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1808 : mCurrentSubtype.hashCode();
1809 // If the last IME is the same as the current IME and the last subtype is not
1810 // defined, there is no need to switch to the last IME.
1811 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1812 targetLastImiId = lastIme.first;
1813 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1814 }
1815 }
1816
1817 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1818 // This is a safety net. If the currentSubtype can't be added to the history
1819 // and the framework couldn't find the last ime, we will make the last ime be
1820 // the most applicable enabled keyboard subtype of the system imes.
1821 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1822 if (enabled != null) {
1823 final int N = enabled.size();
1824 final String locale = mCurrentSubtype == null
1825 ? mRes.getConfiguration().locale.toString()
1826 : mCurrentSubtype.getLocale();
1827 for (int i = 0; i < N; ++i) {
1828 final InputMethodInfo imi = enabled.get(i);
1829 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1830 InputMethodSubtype keyboardSubtype =
1831 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1832 SUBTYPE_MODE_KEYBOARD, locale, true);
1833 if (keyboardSubtype != null) {
1834 targetLastImiId = imi.getId();
1835 subtypeId = getSubtypeIdFromHashCode(
1836 imi, keyboardSubtype.hashCode());
1837 if(keyboardSubtype.getLocale().equals(locale)) {
1838 break;
1839 }
1840 }
1841 }
1842 }
1843 }
1844 }
1845
1846 if (!TextUtils.isEmpty(targetLastImiId)) {
1847 if (DEBUG) {
1848 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1849 + ", from: " + mCurMethodId + ", " + subtypeId);
1850 }
1851 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1852 return true;
1853 } else {
1854 return false;
1855 }
satok735cf382010-11-11 20:40:09 +09001856 }
1857 }
1858
satoke7c6998e2011-06-03 17:57:59 +09001859 @Override
satok688bd472012-02-09 20:09:17 +09001860 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1861 synchronized (mMethodMap) {
1862 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1863 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1864 if (nextSubtype == null) {
1865 return false;
1866 }
1867 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1868 return true;
1869 }
1870 }
1871
1872 @Override
satok68f1b782011-04-11 14:26:04 +09001873 public InputMethodSubtype getLastInputMethodSubtype() {
1874 synchronized (mMethodMap) {
1875 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1876 // TODO: Handle the case of the last IME with no subtypes
1877 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1878 || TextUtils.isEmpty(lastIme.second)) return null;
1879 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1880 if (lastImi == null) return null;
1881 try {
1882 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001883 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1884 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1885 return null;
1886 }
1887 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001888 } catch (NumberFormatException e) {
1889 return null;
1890 }
1891 }
1892 }
1893
satoke7c6998e2011-06-03 17:57:59 +09001894 @Override
satokee5e77c2011-09-02 18:50:15 +09001895 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001896 // By this IPC call, only a process which shares the same uid with the IME can add
1897 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001898 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001899 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001900 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001901 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001902 final PackageManager pm = mContext.getPackageManager();
1903 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1904 if (packageInfos != null) {
1905 final int packageNum = packageInfos.length;
1906 for (int i = 0; i < packageNum; ++i) {
1907 if (packageInfos[i].equals(imi.getPackageName())) {
1908 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001909 final long ident = Binder.clearCallingIdentity();
1910 try {
1911 buildInputMethodListLocked(mMethodList, mMethodMap);
1912 } finally {
1913 Binder.restoreCallingIdentity(ident);
1914 }
satokee5e77c2011-09-02 18:50:15 +09001915 return;
satok91e88122011-07-18 11:11:42 +09001916 }
1917 }
1918 }
satoke7c6998e2011-06-03 17:57:59 +09001919 }
satokee5e77c2011-09-02 18:50:15 +09001920 return;
satoke7c6998e2011-06-03 17:57:59 +09001921 }
1922
satok28203512010-11-24 11:06:49 +09001923 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 synchronized (mMethodMap) {
1925 if (token == null) {
1926 if (mContext.checkCallingOrSelfPermission(
1927 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1928 != PackageManager.PERMISSION_GRANTED) {
1929 throw new SecurityException(
1930 "Using null token requires permission "
1931 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1932 }
1933 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001934 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1935 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 return;
1937 }
1938
satokc5933802011-08-31 21:26:04 +09001939 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 try {
satokab751aa2010-09-14 19:17:36 +09001941 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 } finally {
1943 Binder.restoreCallingIdentity(ident);
1944 }
1945 }
1946 }
1947
satok42c5a162011-05-26 16:46:14 +09001948 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 public void hideMySoftInput(IBinder token, int flags) {
1950 synchronized (mMethodMap) {
1951 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001952 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1953 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 return;
1955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 long ident = Binder.clearCallingIdentity();
1957 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001958 hideCurrentInputLocked(flags, null);
1959 } finally {
1960 Binder.restoreCallingIdentity(ident);
1961 }
1962 }
1963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
satok42c5a162011-05-26 16:46:14 +09001965 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001966 public void showMySoftInput(IBinder token, int flags) {
1967 synchronized (mMethodMap) {
1968 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001969 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1970 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001971 return;
1972 }
1973 long ident = Binder.clearCallingIdentity();
1974 try {
1975 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 } finally {
1977 Binder.restoreCallingIdentity(ident);
1978 }
1979 }
1980 }
1981
1982 void setEnabledSessionInMainThread(SessionState session) {
1983 if (mEnabledSession != session) {
1984 if (mEnabledSession != null) {
1985 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001986 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 mEnabledSession.method.setSessionEnabled(
1988 mEnabledSession.session, false);
1989 } catch (RemoteException e) {
1990 }
1991 }
1992 mEnabledSession = session;
1993 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001994 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 session.method.setSessionEnabled(
1996 session.session, true);
1997 } catch (RemoteException e) {
1998 }
1999 }
2000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
satok42c5a162011-05-26 16:46:14 +09002002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 public boolean handleMessage(Message msg) {
2004 HandlerCaller.SomeArgs args;
2005 switch (msg.what) {
2006 case MSG_SHOW_IM_PICKER:
2007 showInputMethodMenu();
2008 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
satokab751aa2010-09-14 19:17:36 +09002010 case MSG_SHOW_IM_SUBTYPE_PICKER:
2011 showInputMethodSubtypeMenu();
2012 return true;
2013
satok47a44912010-10-06 16:03:58 +09002014 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09002015 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002016 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09002017 return true;
2018
2019 case MSG_SHOW_IM_CONFIG:
2020 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002021 return true;
2022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 case MSG_UNBIND_INPUT:
2026 try {
2027 ((IInputMethod)msg.obj).unbindInput();
2028 } catch (RemoteException e) {
2029 // There is nothing interesting about the method dying.
2030 }
2031 return true;
2032 case MSG_BIND_INPUT:
2033 args = (HandlerCaller.SomeArgs)msg.obj;
2034 try {
2035 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2036 } catch (RemoteException e) {
2037 }
2038 return true;
2039 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002040 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002042 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2043 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } catch (RemoteException e) {
2045 }
2046 return true;
2047 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002048 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002050 ((IInputMethod)args.arg1).hideSoftInput(0,
2051 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 } catch (RemoteException e) {
2053 }
2054 return true;
2055 case MSG_ATTACH_TOKEN:
2056 args = (HandlerCaller.SomeArgs)msg.obj;
2057 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002058 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2060 } catch (RemoteException e) {
2061 }
2062 return true;
2063 case MSG_CREATE_SESSION:
2064 args = (HandlerCaller.SomeArgs)msg.obj;
2065 try {
2066 ((IInputMethod)args.arg1).createSession(
2067 (IInputMethodCallback)args.arg2);
2068 } catch (RemoteException e) {
2069 }
2070 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 case MSG_START_INPUT:
2074 args = (HandlerCaller.SomeArgs)msg.obj;
2075 try {
2076 SessionState session = (SessionState)args.arg1;
2077 setEnabledSessionInMainThread(session);
2078 session.method.startInput((IInputContext)args.arg2,
2079 (EditorInfo)args.arg3);
2080 } catch (RemoteException e) {
2081 }
2082 return true;
2083 case MSG_RESTART_INPUT:
2084 args = (HandlerCaller.SomeArgs)msg.obj;
2085 try {
2086 SessionState session = (SessionState)args.arg1;
2087 setEnabledSessionInMainThread(session);
2088 session.method.restartInput((IInputContext)args.arg2,
2089 (EditorInfo)args.arg3);
2090 } catch (RemoteException e) {
2091 }
2092 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 case MSG_UNBIND_METHOD:
2097 try {
2098 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2099 } catch (RemoteException e) {
2100 // There is nothing interesting about the last client dying.
2101 }
2102 return true;
2103 case MSG_BIND_METHOD:
2104 args = (HandlerCaller.SomeArgs)msg.obj;
2105 try {
2106 ((IInputMethodClient)args.arg1).onBindMethod(
2107 (InputBindResult)args.arg2);
2108 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002109 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111 return true;
satok01038492012-04-09 21:08:27 +09002112
2113 // --------------------------------------------------------------
2114 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2115 mHardKeyboardListener.handleHardKeyboardStatusChange(
2116 msg.arg1 == 1, msg.arg2 == 1);
2117 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119 return false;
2120 }
2121
satok5b927c432012-05-01 20:09:34 +09002122 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002123 return (inputMethod.getServiceInfo().applicationInfo.flags
2124 & ApplicationInfo.FLAG_SYSTEM) != 0;
2125 }
2126
Ken Wakasa586f0512011-01-20 22:31:01 +09002127 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2128 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2129 final int subtypeCount = imi.getSubtypeCount();
2130 for (int i = 0; i < subtypeCount; ++i) {
2131 subtypes.add(imi.getSubtypeAt(i));
2132 }
2133 return subtypes;
2134 }
2135
satoka86f5e42011-09-02 17:12:42 +09002136
2137 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2138 InputMethodInfo imi, String mode) {
2139 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2140 final int subtypeCount = imi.getSubtypeCount();
2141 for (int i = 0; i < subtypeCount; ++i) {
2142 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2143 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2144 subtypes.add(subtype);
2145 }
2146 }
2147 return subtypes;
2148 }
2149
satokdc9ddae2011-10-06 12:22:36 +09002150 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002151 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002152 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002153 // We'd prefer to fall back on a system IME, since that is safer.
2154 int i=enabled.size();
2155 while (i > 0) {
2156 i--;
satokdc9ddae2011-10-06 12:22:36 +09002157 final InputMethodInfo imi = enabled.get(i);
2158 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002159 break;
2160 }
2161 }
satokdc9ddae2011-10-06 12:22:36 +09002162 return enabled.get(i);
2163 }
2164 return null;
2165 }
2166
2167 private boolean chooseNewDefaultIMELocked() {
2168 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2169 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002170 if (DEBUG) {
2171 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2172 }
satok723a27e2010-11-11 14:58:11 +09002173 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002174 return true;
2175 }
2176
2177 return false;
2178 }
2179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2181 HashMap<String, InputMethodInfo> map) {
2182 list.clear();
2183 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002186 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002187 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2188 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2189 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2190 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191
2192 List<ResolveInfo> services = pm.queryIntentServices(
2193 new Intent(InputMethod.SERVICE_INTERFACE),
2194 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002195
satoke7c6998e2011-06-03 17:57:59 +09002196 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2197 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 for (int i = 0; i < services.size(); ++i) {
2199 ResolveInfo ri = services.get(i);
2200 ServiceInfo si = ri.serviceInfo;
2201 ComponentName compName = new ComponentName(si.packageName, si.name);
2202 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2203 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002204 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 + ": it does not require the permission "
2206 + android.Manifest.permission.BIND_INPUT_METHOD);
2207 continue;
2208 }
2209
Joe Onorato8a9b2202010-02-26 18:56:32 -08002210 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211
2212 try {
satoke7c6998e2011-06-03 17:57:59 +09002213 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002215 final String id = p.getId();
2216 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217
satok5b927c432012-05-01 20:09:34 +09002218 // Valid system default IMEs and IMEs that have English subtypes are enabled
2219 // by default, unless there's a hard keyboard and the system IME was explicitly
2220 // disabled
2221 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2222 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002223 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002224 }
2225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002227 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002231 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002233 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002236
2237 String defaultIme = Settings.Secure.getString(mContext
2238 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002239 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002240 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002241 updateFromSettingsLocked();
2242 }
2243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002247
satokab751aa2010-09-14 19:17:36 +09002248 private void showInputMethodMenu() {
2249 showInputMethodMenuInternal(false);
2250 }
2251
2252 private void showInputMethodSubtypeMenu() {
2253 showInputMethodMenuInternal(true);
2254 }
2255
satok217f5482010-12-15 05:19:19 +09002256 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002257 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002258 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002259 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2260 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002261 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002262 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002263 }
satok217f5482010-12-15 05:19:19 +09002264 mContext.startActivity(intent);
2265 }
2266
2267 private void showConfigureInputMethods() {
2268 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2269 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2270 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2271 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002272 mContext.startActivity(intent);
2273 }
2274
satok2c93efc2012-04-02 19:33:47 +09002275 private boolean isScreenLocked() {
2276 return mKeyguardManager != null
2277 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2278 }
satokab751aa2010-09-14 19:17:36 +09002279 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002280 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002284 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
satok2c93efc2012-04-02 19:33:47 +09002286 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002288 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002289 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002291 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002292 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2293 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002294 if (immis == null || immis.size() == 0) {
2295 return;
2296 }
2297
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002298 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
satok688bd472012-02-09 20:09:17 +09002300 final List<ImeSubtypeListItem> imList =
2301 mImListManager.getSortedInputMethodAndSubtypeList(
2302 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002303
satokc3690562012-01-10 20:14:43 +09002304 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2305 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2306 if (currentSubtype != null) {
2307 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2308 lastInputMethodSubtypeId =
2309 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2310 }
2311 }
2312
Ken Wakasa761eb372011-03-04 19:06:18 +09002313 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002314 mIms = new InputMethodInfo[N];
2315 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002316 int checkedItem = 0;
2317 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002318 final ImeSubtypeListItem item = imList.get(i);
2319 mIms[i] = item.mImi;
2320 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002321 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002322 int subtypeId = mSubtypeIds[i];
2323 if ((subtypeId == NOT_A_SUBTYPE_ID)
2324 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2325 || (subtypeId == lastInputMethodSubtypeId)) {
2326 checkedItem = i;
2327 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002330 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002331 com.android.internal.R.styleable.DialogPreference,
2332 com.android.internal.R.attr.alertDialogStyle, 0);
2333 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002334 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002335 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002336 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002338 }
2339 })
2340 .setIcon(a.getDrawable(
2341 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2342 a.recycle();
satok01038492012-04-09 21:08:27 +09002343 final LayoutInflater inflater =
2344 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2345 final View tv = inflater.inflate(
2346 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2347 mDialogBuilder.setCustomTitle(tv);
2348
2349 // Setup layout for a toggle switch of the hardware keyboard
2350 mSwitchingDialogTitleView = tv;
2351 mSwitchingDialogTitleView.findViewById(
2352 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2353 mWindowManagerService.isHardKeyboardAvailable() ?
2354 View.VISIBLE : View.GONE);
2355 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2356 com.android.internal.R.id.hard_keyboard_switch));
2357 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2358 hardKeySwitch.setOnCheckedChangeListener(
2359 new OnCheckedChangeListener() {
2360 @Override
2361 public void onCheckedChanged(
2362 CompoundButton buttonView, boolean isChecked) {
2363 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2364 }
2365 });
satokd87c2592010-09-29 11:52:06 +09002366
Ken Wakasa05dbb652011-08-22 15:22:43 +09002367 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2368 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2369 checkedItem);
2370
2371 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002372 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002373 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002374 public void onClick(DialogInterface dialog, int which) {
2375 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002376 if (mIms == null || mIms.length <= which
2377 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002378 return;
2379 }
2380 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002381 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002382 hideInputMethodMenu();
2383 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002384 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002385 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002386 subtypeId = NOT_A_SUBTYPE_ID;
2387 }
2388 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002389 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002392 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393
satokbc81b692011-08-26 16:22:22 +09002394 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002395 mDialogBuilder.setPositiveButton(
2396 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002397 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002398 @Override
satok7f35c8c2010-10-07 21:13:11 +09002399 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002400 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002401 }
2402 });
2403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002405 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 mSwitchingDialog.getWindow().setType(
2407 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002408 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 mSwitchingDialog.show();
2410 }
2411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
Ken Wakasa05dbb652011-08-22 15:22:43 +09002413 private static class ImeSubtypeListItem {
2414 public final CharSequence mImeName;
2415 public final CharSequence mSubtypeName;
2416 public final InputMethodInfo mImi;
2417 public final int mSubtypeId;
2418 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2419 InputMethodInfo imi, int subtypeId) {
2420 mImeName = imeName;
2421 mSubtypeName = subtypeName;
2422 mImi = imi;
2423 mSubtypeId = subtypeId;
2424 }
2425 }
2426
2427 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2428 private final LayoutInflater mInflater;
2429 private final int mTextViewResourceId;
2430 private final List<ImeSubtypeListItem> mItemsList;
2431 private final int mCheckedItem;
2432 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2433 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2434 super(context, textViewResourceId, itemsList);
2435 mTextViewResourceId = textViewResourceId;
2436 mItemsList = itemsList;
2437 mCheckedItem = checkedItem;
2438 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2439 }
2440
2441 @Override
2442 public View getView(int position, View convertView, ViewGroup parent) {
2443 final View view = convertView != null ? convertView
2444 : mInflater.inflate(mTextViewResourceId, null);
2445 if (position < 0 || position >= mItemsList.size()) return view;
2446 final ImeSubtypeListItem item = mItemsList.get(position);
2447 final CharSequence imeName = item.mImeName;
2448 final CharSequence subtypeName = item.mSubtypeName;
2449 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2450 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2451 if (TextUtils.isEmpty(subtypeName)) {
2452 firstTextView.setText(imeName);
2453 secondTextView.setVisibility(View.GONE);
2454 } else {
2455 firstTextView.setText(subtypeName);
2456 secondTextView.setText(imeName);
2457 secondTextView.setVisibility(View.VISIBLE);
2458 }
2459 final RadioButton radioButton =
2460 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2461 radioButton.setChecked(position == mCheckedItem);
2462 return view;
2463 }
2464 }
2465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002467 synchronized (mMethodMap) {
2468 hideInputMethodMenuLocked();
2469 }
2470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002471
The Android Open Source Project10592532009-03-18 17:39:46 -07002472 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002473 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474
The Android Open Source Project10592532009-03-18 17:39:46 -07002475 if (mSwitchingDialog != null) {
2476 mSwitchingDialog.dismiss();
2477 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002479
The Android Open Source Project10592532009-03-18 17:39:46 -07002480 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002481 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002485
satok42c5a162011-05-26 16:46:14 +09002486 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 public boolean setInputMethodEnabled(String id, boolean enabled) {
2488 synchronized (mMethodMap) {
2489 if (mContext.checkCallingOrSelfPermission(
2490 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2491 != PackageManager.PERMISSION_GRANTED) {
2492 throw new SecurityException(
2493 "Requires permission "
2494 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2495 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 long ident = Binder.clearCallingIdentity();
2498 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002499 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 } finally {
2501 Binder.restoreCallingIdentity(ident);
2502 }
2503 }
2504 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002505
2506 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2507 // Make sure this is a valid input method.
2508 InputMethodInfo imm = mMethodMap.get(id);
2509 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002510 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002511 }
2512
satokd87c2592010-09-29 11:52:06 +09002513 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2514 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002515
satokd87c2592010-09-29 11:52:06 +09002516 if (enabled) {
2517 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2518 if (pair.first.equals(id)) {
2519 // We are enabling this input method, but it is already enabled.
2520 // Nothing to do. The previous state was enabled.
2521 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002522 }
2523 }
satokd87c2592010-09-29 11:52:06 +09002524 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2525 // Previous state was disabled.
2526 return false;
2527 } else {
2528 StringBuilder builder = new StringBuilder();
2529 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2530 builder, enabledInputMethodsList, id)) {
2531 // Disabled input method is currently selected, switch to another one.
2532 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2533 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002534 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2535 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2536 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002537 }
2538 // Previous state was enabled.
2539 return true;
2540 } else {
2541 // We are disabling the input method but it is already disabled.
2542 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002543 return false;
2544 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002545 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002546 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002547
satok57ffc002011-01-25 00:11:47 +09002548 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2549 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002550 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002551 }
2552
satok723a27e2010-11-11 14:58:11 +09002553 private void saveCurrentInputMethodAndSubtypeToHistory() {
2554 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2555 if (mCurrentSubtype != null) {
2556 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2557 }
satok57ffc002011-01-25 00:11:47 +09002558 if (canAddToLastInputMethod(mCurrentSubtype)) {
2559 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2560 }
satokab751aa2010-09-14 19:17:36 +09002561 }
2562
satok723a27e2010-11-11 14:58:11 +09002563 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2564 boolean setSubtypeOnly) {
2565 // Update the history of InputMethod and Subtype
2566 saveCurrentInputMethodAndSubtypeToHistory();
2567
2568 // Set Subtype here
2569 if (imi == null || subtypeId < 0) {
2570 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002571 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002572 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002573 if (subtypeId < imi.getSubtypeCount()) {
2574 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2575 mSettings.putSelectedSubtype(subtype.hashCode());
2576 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002577 } else {
2578 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2579 mCurrentSubtype = null;
2580 }
satokab751aa2010-09-14 19:17:36 +09002581 }
satok723a27e2010-11-11 14:58:11 +09002582
2583 if (!setSubtypeOnly) {
2584 // Set InputMethod here
2585 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2586 }
2587 }
2588
2589 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2590 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2591 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2592 // newDefaultIme is empty when there is no candidate for the selected IME.
2593 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2594 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2595 if (subtypeHashCode != null) {
2596 try {
2597 lastSubtypeId = getSubtypeIdFromHashCode(
2598 imi, Integer.valueOf(subtypeHashCode));
2599 } catch (NumberFormatException e) {
2600 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2601 }
2602 }
2603 }
2604 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002605 }
2606
2607 private int getSelectedInputMethodSubtypeId(String id) {
2608 InputMethodInfo imi = mMethodMap.get(id);
2609 if (imi == null) {
2610 return NOT_A_SUBTYPE_ID;
2611 }
satokab751aa2010-09-14 19:17:36 +09002612 int subtypeId;
2613 try {
2614 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2615 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2616 } catch (SettingNotFoundException e) {
2617 return NOT_A_SUBTYPE_ID;
2618 }
satok723a27e2010-11-11 14:58:11 +09002619 return getSubtypeIdFromHashCode(imi, subtypeId);
2620 }
2621
satokfdf419e2012-05-08 16:52:08 +09002622 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2623 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2624 }
2625
2626 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002627 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002628 final int subtypeCount = imi.getSubtypeCount();
2629 for (int i = 0; i < subtypeCount; ++i) {
2630 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002631 if (subtypeHashCode == ims.hashCode()) {
2632 return i;
2633 }
satokab751aa2010-09-14 19:17:36 +09002634 }
2635 }
2636 return NOT_A_SUBTYPE_ID;
2637 }
2638
satoka86f5e42011-09-02 17:12:42 +09002639 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2640 Resources res, InputMethodInfo imi) {
2641 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002642 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002643 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002644 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002645 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002646 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002647 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002648 // scan overriding implicitly enabled subtypes.
2649 InputMethodSubtype subtype = subtypes.get(i);
2650 if (subtype.overridesImplicitlyEnabledSubtype()) {
2651 final String mode = subtype.getMode();
2652 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2653 applicableModeAndSubtypesMap.put(mode, subtype);
2654 }
2655 }
2656 }
2657 if (applicableModeAndSubtypesMap.size() > 0) {
2658 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2659 }
2660 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002661 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002662 final String locale = subtype.getLocale();
2663 final String mode = subtype.getMode();
2664 // When system locale starts with subtype's locale, that subtype will be applicable
2665 // for system locale
2666 // For instance, it's clearly applicable for cases like system locale = en_US and
2667 // subtype = en, but it is not necessarily considered applicable for cases like system
2668 // locale = en and subtype = en_US.
2669 // We just call systemLocale.startsWith(locale) in this function because there is no
2670 // need to find applicable subtypes aggressively unlike
2671 // findLastResortApplicableSubtypeLocked.
2672 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002673 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002674 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002675 if (applicableSubtype != null) {
2676 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2677 if (!systemLocale.equals(locale)) continue;
2678 }
satok3da92232011-01-11 22:46:30 +09002679 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002680 }
2681 }
2682 final InputMethodSubtype keyboardSubtype
2683 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2684 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2685 applicableModeAndSubtypesMap.values());
2686 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2687 for (int i = 0; i < N; ++i) {
2688 final InputMethodSubtype subtype = subtypes.get(i);
2689 final String mode = subtype.getMode();
2690 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2691 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2692 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002693 }
2694 }
2695 }
satokc3690562012-01-10 20:14:43 +09002696 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002697 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002698 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002699 if (lastResortKeyboardSubtype != null) {
2700 applicableSubtypes.add(lastResortKeyboardSubtype);
2701 }
2702 }
2703 return applicableSubtypes;
2704 }
2705
satok4e4569d2010-11-19 18:45:53 +09002706 /**
2707 * If there are no selected subtypes, tries finding the most applicable one according to the
2708 * given locale.
2709 * @param subtypes this function will search the most applicable subtype in subtypes
2710 * @param mode subtypes will be filtered by mode
2711 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002712 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2713 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002714 * @return the most applicable subtypeId
2715 */
satokdf31ae62011-01-15 06:19:44 +09002716 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2717 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002718 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002719 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002720 return null;
satok8fbb1e82010-11-02 23:15:58 +09002721 }
satok4e4569d2010-11-19 18:45:53 +09002722 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002723 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002724 }
satok8fbb1e82010-11-02 23:15:58 +09002725 final String language = locale.substring(0, 2);
2726 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002727 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002728 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002729 final int N = subtypes.size();
2730 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002731 InputMethodSubtype subtype = subtypes.get(i);
2732 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002733 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2734 // and all subtypes with all modes can be candidates.
2735 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002736 if (firstMatchedModeSubtype == null) {
2737 firstMatchedModeSubtype = subtype;
2738 }
satok9ef02832010-11-04 21:17:48 +09002739 if (locale.equals(subtypeLocale)) {
2740 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002741 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002742 break;
2743 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2744 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002745 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002746 partialMatchFound = true;
2747 }
satok8fbb1e82010-11-02 23:15:58 +09002748 }
2749 }
2750
satok7599a7f2010-12-22 13:45:23 +09002751 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2752 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002753 }
2754
satok8fbb1e82010-11-02 23:15:58 +09002755 // The first subtype applicable to the system locale will be defined as the most applicable
2756 // subtype.
2757 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002758 if (applicableSubtype != null) {
2759 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2760 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2761 }
satok8fbb1e82010-11-02 23:15:58 +09002762 }
satokcd7cd292010-11-20 15:46:23 +09002763 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002764 }
2765
satok4e4569d2010-11-19 18:45:53 +09002766 // If there are no selected shortcuts, tries finding the most applicable ones.
2767 private Pair<InputMethodInfo, InputMethodSubtype>
2768 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2769 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2770 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002771 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002772 boolean foundInSystemIME = false;
2773
2774 // Search applicable subtype for each InputMethodInfo
2775 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002776 final String imiId = imi.getId();
2777 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2778 continue;
2779 }
satokcd7cd292010-11-20 15:46:23 +09002780 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002781 final List<InputMethodSubtype> enabledSubtypes =
2782 getEnabledInputMethodSubtypeList(imi, true);
2783 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002784 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002785 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002786 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002787 }
satokdf31ae62011-01-15 06:19:44 +09002788 // 2. Search by the system locale from enabledSubtypes.
2789 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002790 if (subtype == null) {
2791 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002792 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002793 }
satoka86f5e42011-09-02 17:12:42 +09002794 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2795 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2796 final ArrayList<InputMethodSubtype> subtypesForSearch =
2797 overridingImplicitlyEnabledSubtypes.isEmpty()
2798 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002799 // 4. Search by the current subtype's locale from all subtypes.
2800 if (subtype == null && mCurrentSubtype != null) {
2801 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002802 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002803 }
2804 // 5. Search by the system locale from all subtypes.
2805 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002806 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002807 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002808 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002809 }
satokcd7cd292010-11-20 15:46:23 +09002810 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002811 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002812 // The current input method is the most applicable IME.
2813 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002814 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002815 break;
satok7599a7f2010-12-22 13:45:23 +09002816 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002817 // The system input method is 2nd applicable IME.
2818 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002819 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002820 if ((imi.getServiceInfo().applicationInfo.flags
2821 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2822 foundInSystemIME = true;
2823 }
satok4e4569d2010-11-19 18:45:53 +09002824 }
2825 }
2826 }
2827 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002828 if (mostApplicableIMI != null) {
2829 Slog.w(TAG, "Most applicable shortcut input method was:"
2830 + mostApplicableIMI.getId());
2831 if (mostApplicableSubtype != null) {
2832 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2833 + "," + mostApplicableSubtype.getMode() + ","
2834 + mostApplicableSubtype.getLocale());
2835 }
2836 }
satok4e4569d2010-11-19 18:45:53 +09002837 }
satokcd7cd292010-11-20 15:46:23 +09002838 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002839 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002840 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002841 } else {
2842 return null;
2843 }
2844 }
2845
satokab751aa2010-09-14 19:17:36 +09002846 /**
2847 * @return Return the current subtype of this input method.
2848 */
satok42c5a162011-05-26 16:46:14 +09002849 @Override
satokab751aa2010-09-14 19:17:36 +09002850 public InputMethodSubtype getCurrentInputMethodSubtype() {
satokfdf419e2012-05-08 16:52:08 +09002851 if (mCurMethodId == null) {
2852 return null;
2853 }
satok4e4569d2010-11-19 18:45:53 +09002854 boolean subtypeIsSelected = false;
2855 try {
2856 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2857 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2858 } catch (SettingNotFoundException e) {
2859 }
satok3ef8b292010-11-23 06:06:29 +09002860 synchronized (mMethodMap) {
satokfdf419e2012-05-08 16:52:08 +09002861 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2862 if (imi == null || imi.getSubtypeCount() == 0) {
2863 return null;
2864 }
2865 if (!subtypeIsSelected || mCurrentSubtype == null
2866 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2867 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
satok3ef8b292010-11-23 06:06:29 +09002868 if (subtypeId == NOT_A_SUBTYPE_ID) {
satokfdf419e2012-05-08 16:52:08 +09002869 // If there are no selected subtypes, the framework will try to find
2870 // the most applicable subtype from explicitly or implicitly enabled
2871 // subtypes.
2872 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2873 getEnabledInputMethodSubtypeList(imi, true);
2874 // If there is only one explicitly or implicitly enabled subtype,
2875 // just returns it.
2876 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2877 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2878 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2879 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2880 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2881 SUBTYPE_MODE_KEYBOARD, null, true);
2882 if (mCurrentSubtype == null) {
satokd8713432011-01-18 00:55:13 +09002883 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
satokfdf419e2012-05-08 16:52:08 +09002884 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2885 true);
satokd8713432011-01-18 00:55:13 +09002886 }
satok4e4569d2010-11-19 18:45:53 +09002887 }
satokcd7cd292010-11-20 15:46:23 +09002888 } else {
satokfdf419e2012-05-08 16:52:08 +09002889 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002890 }
satok8fbb1e82010-11-02 23:15:58 +09002891 }
satok3ef8b292010-11-23 06:06:29 +09002892 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002893 }
satokab751aa2010-09-14 19:17:36 +09002894 }
2895
satokf3db1af2010-11-23 13:34:33 +09002896 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2897 InputMethodSubtype subtype) {
2898 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2899 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2900 } else {
2901 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2902 subtypes.add(subtype);
2903 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2904 }
2905 }
2906
satok4e4569d2010-11-19 18:45:53 +09002907 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002908 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002909 @Override
satok4e4569d2010-11-19 18:45:53 +09002910 public List getShortcutInputMethodsAndSubtypes() {
2911 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002912 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002913 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002914 // If there are no selected shortcut subtypes, the framework will try to find
2915 // the most applicable subtype from all subtypes whose mode is
2916 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002917 Pair<InputMethodInfo, InputMethodSubtype> info =
2918 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2919 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002920 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002921 ret.add(info.first);
2922 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002923 }
satok3da92232011-01-11 22:46:30 +09002924 return ret;
satokf3db1af2010-11-23 13:34:33 +09002925 }
satokf3db1af2010-11-23 13:34:33 +09002926 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2927 ret.add(imi);
2928 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2929 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002930 }
2931 }
satokf3db1af2010-11-23 13:34:33 +09002932 return ret;
satok4e4569d2010-11-19 18:45:53 +09002933 }
2934 }
2935
satok42c5a162011-05-26 16:46:14 +09002936 @Override
satokb66d2872010-11-10 01:04:04 +09002937 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2938 synchronized (mMethodMap) {
2939 if (subtype != null && mCurMethodId != null) {
2940 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2941 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2942 if (subtypeId != NOT_A_SUBTYPE_ID) {
2943 setInputMethodLocked(mCurMethodId, subtypeId);
2944 return true;
2945 }
2946 }
2947 return false;
2948 }
2949 }
2950
satok688bd472012-02-09 20:09:17 +09002951 private static class InputMethodAndSubtypeListManager {
2952 private final Context mContext;
2953 private final PackageManager mPm;
2954 private final InputMethodManagerService mImms;
2955 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
2956 mContext = context;
2957 mPm = context.getPackageManager();
2958 mImms = imms;
2959 }
2960
2961 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
2962 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2963 new Comparator<InputMethodInfo>() {
2964 @Override
2965 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2966 if (imi2 == null) return 0;
2967 if (imi1 == null) return 1;
2968 if (mPm == null) {
2969 return imi1.getId().compareTo(imi2.getId());
2970 }
2971 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
2972 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
2973 return imiId1.toString().compareTo(imiId2.toString());
2974 }
2975 });
2976
2977 public ImeSubtypeListItem getNextInputMethod(
2978 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
2979 if (imi == null) {
2980 return null;
2981 }
2982 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
2983 if (imList.size() <= 1) {
2984 return null;
2985 }
2986 final int N = imList.size();
2987 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09002988 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09002989 : NOT_A_SUBTYPE_ID;
2990 for (int i = 0; i < N; ++i) {
2991 final ImeSubtypeListItem isli = imList.get(i);
2992 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
2993 if (!onlyCurrentIme) {
2994 return imList.get((i + 1) % N);
2995 }
2996 for (int j = 0; j < N - 1; ++j) {
2997 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
2998 if (candidate.mImi.equals(imi)) {
2999 return candidate;
3000 }
3001 }
3002 return null;
3003 }
3004 }
3005 return null;
3006 }
3007
3008 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3009 return getSortedInputMethodAndSubtypeList(true, false, false);
3010 }
3011
3012 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3013 boolean inputShown, boolean isScreenLocked) {
3014 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3015 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3016 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3017 if (immis == null || immis.size() == 0) {
3018 return Collections.emptyList();
3019 }
3020 mSortedImmis.clear();
3021 mSortedImmis.putAll(immis);
3022 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3023 if (imi == null) continue;
3024 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3025 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3026 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3027 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3028 }
3029 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3030 final CharSequence imeLabel = imi.loadLabel(mPm);
3031 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3032 final int subtypeCount = imi.getSubtypeCount();
3033 if (DEBUG) {
3034 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3035 }
3036 for (int j = 0; j < subtypeCount; ++j) {
3037 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3038 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3039 // We show all enabled IMEs and subtypes when an IME is shown.
3040 if (enabledSubtypeSet.contains(subtypeHashCode)
3041 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3042 final CharSequence subtypeLabel =
3043 subtype.overridesImplicitlyEnabledSubtype() ? null
3044 : subtype.getDisplayName(mContext, imi.getPackageName(),
3045 imi.getServiceInfo().applicationInfo);
3046 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
3047
3048 // Removing this subtype from enabledSubtypeSet because we no longer
3049 // need to add an entry of this subtype to imList to avoid duplicated
3050 // entries.
3051 enabledSubtypeSet.remove(subtypeHashCode);
3052 }
3053 }
3054 } else {
3055 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
3056 }
3057 }
3058 return imList;
3059 }
3060 }
3061
satokd87c2592010-09-29 11:52:06 +09003062 /**
3063 * Utility class for putting and getting settings for InputMethod
3064 * TODO: Move all putters and getters of settings to this class.
3065 */
3066 private static class InputMethodSettings {
3067 // The string for enabled input method is saved as follows:
3068 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3069 private static final char INPUT_METHOD_SEPARATER = ':';
3070 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003071 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003072 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3073
satok723a27e2010-11-11 14:58:11 +09003074 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003075 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3076
satokdf31ae62011-01-15 06:19:44 +09003077 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003078 private final ContentResolver mResolver;
3079 private final HashMap<String, InputMethodInfo> mMethodMap;
3080 private final ArrayList<InputMethodInfo> mMethodList;
3081
3082 private String mEnabledInputMethodsStrCache;
3083
3084 private static void buildEnabledInputMethodsSettingString(
3085 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3086 String id = pair.first;
3087 ArrayList<String> subtypes = pair.second;
3088 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003089 // Inputmethod and subtypes are saved in the settings as follows:
3090 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3091 for (String subtypeId: subtypes) {
3092 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003093 }
3094 }
3095
3096 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003097 Resources res, ContentResolver resolver,
3098 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3099 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003100 mResolver = resolver;
3101 mMethodMap = methodMap;
3102 mMethodList = methodList;
3103 }
3104
3105 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3106 return createEnabledInputMethodListLocked(
3107 getEnabledInputMethodsAndSubtypeListLocked());
3108 }
3109
satok7f35c8c2010-10-07 21:13:11 +09003110 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003111 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3112 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003113 getEnabledInputMethodsAndSubtypeListLocked());
3114 }
3115
satok67ddf9c2010-11-17 09:45:54 +09003116 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3117 InputMethodInfo imi) {
3118 List<Pair<String, ArrayList<String>>> imsList =
3119 getEnabledInputMethodsAndSubtypeListLocked();
3120 ArrayList<InputMethodSubtype> enabledSubtypes =
3121 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003122 if (imi != null) {
3123 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3124 InputMethodInfo info = mMethodMap.get(imsPair.first);
3125 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003126 final int subtypeCount = info.getSubtypeCount();
3127 for (int i = 0; i < subtypeCount; ++i) {
3128 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003129 for (String s: imsPair.second) {
3130 if (String.valueOf(ims.hashCode()).equals(s)) {
3131 enabledSubtypes.add(ims);
3132 }
satok67ddf9c2010-11-17 09:45:54 +09003133 }
3134 }
satok884ef9a2010-11-18 10:39:46 +09003135 break;
satok67ddf9c2010-11-17 09:45:54 +09003136 }
satok67ddf9c2010-11-17 09:45:54 +09003137 }
3138 }
3139 return enabledSubtypes;
3140 }
3141
satokd87c2592010-09-29 11:52:06 +09003142 // At the initial boot, the settings for input methods are not set,
3143 // so we need to enable IME in that case.
3144 public void enableAllIMEsIfThereIsNoEnabledIME() {
3145 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3146 StringBuilder sb = new StringBuilder();
3147 final int N = mMethodList.size();
3148 for (int i = 0; i < N; i++) {
3149 InputMethodInfo imi = mMethodList.get(i);
3150 Slog.i(TAG, "Adding: " + imi.getId());
3151 if (i > 0) sb.append(':');
3152 sb.append(imi.getId());
3153 }
3154 putEnabledInputMethodsStr(sb.toString());
3155 }
3156 }
3157
satokbb4aa062011-01-19 21:40:27 +09003158 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003159 ArrayList<Pair<String, ArrayList<String>>> imsList
3160 = new ArrayList<Pair<String, ArrayList<String>>>();
3161 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3162 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3163 return imsList;
3164 }
satok723a27e2010-11-11 14:58:11 +09003165 mInputMethodSplitter.setString(enabledInputMethodsStr);
3166 while (mInputMethodSplitter.hasNext()) {
3167 String nextImsStr = mInputMethodSplitter.next();
3168 mSubtypeSplitter.setString(nextImsStr);
3169 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003170 ArrayList<String> subtypeHashes = new ArrayList<String>();
3171 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003172 String imeId = mSubtypeSplitter.next();
3173 while (mSubtypeSplitter.hasNext()) {
3174 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003175 }
3176 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3177 }
3178 }
3179 return imsList;
3180 }
3181
3182 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3183 if (reloadInputMethodStr) {
3184 getEnabledInputMethodsStr();
3185 }
3186 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3187 // Add in the newly enabled input method.
3188 putEnabledInputMethodsStr(id);
3189 } else {
3190 putEnabledInputMethodsStr(
3191 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3192 }
3193 }
3194
3195 /**
3196 * Build and put a string of EnabledInputMethods with removing specified Id.
3197 * @return the specified id was removed or not.
3198 */
3199 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3200 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3201 boolean isRemoved = false;
3202 boolean needsAppendSeparator = false;
3203 for (Pair<String, ArrayList<String>> ims: imsList) {
3204 String curId = ims.first;
3205 if (curId.equals(id)) {
3206 // We are disabling this input method, and it is
3207 // currently enabled. Skip it to remove from the
3208 // new list.
3209 isRemoved = true;
3210 } else {
3211 if (needsAppendSeparator) {
3212 builder.append(INPUT_METHOD_SEPARATER);
3213 } else {
3214 needsAppendSeparator = true;
3215 }
3216 buildEnabledInputMethodsSettingString(builder, ims);
3217 }
3218 }
3219 if (isRemoved) {
3220 // Update the setting with the new list of input methods.
3221 putEnabledInputMethodsStr(builder.toString());
3222 }
3223 return isRemoved;
3224 }
3225
3226 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3227 List<Pair<String, ArrayList<String>>> imsList) {
3228 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3229 for (Pair<String, ArrayList<String>> ims: imsList) {
3230 InputMethodInfo info = mMethodMap.get(ims.first);
3231 if (info != null) {
3232 res.add(info);
3233 }
3234 }
3235 return res;
3236 }
3237
satok7f35c8c2010-10-07 21:13:11 +09003238 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003239 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003240 List<Pair<String, ArrayList<String>>> imsList) {
3241 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3242 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3243 for (Pair<String, ArrayList<String>> ims : imsList) {
3244 InputMethodInfo info = mMethodMap.get(ims.first);
3245 if (info != null) {
3246 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3247 }
3248 }
3249 return res;
3250 }
3251
satokd87c2592010-09-29 11:52:06 +09003252 private void putEnabledInputMethodsStr(String str) {
3253 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3254 mEnabledInputMethodsStrCache = str;
3255 }
3256
3257 private String getEnabledInputMethodsStr() {
3258 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3259 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003260 if (DEBUG) {
3261 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3262 }
satokd87c2592010-09-29 11:52:06 +09003263 return mEnabledInputMethodsStrCache;
3264 }
satok723a27e2010-11-11 14:58:11 +09003265
3266 private void saveSubtypeHistory(
3267 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3268 StringBuilder builder = new StringBuilder();
3269 boolean isImeAdded = false;
3270 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3271 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3272 newSubtypeId);
3273 isImeAdded = true;
3274 }
3275 for (Pair<String, String> ime: savedImes) {
3276 String imeId = ime.first;
3277 String subtypeId = ime.second;
3278 if (TextUtils.isEmpty(subtypeId)) {
3279 subtypeId = NOT_A_SUBTYPE_ID_STR;
3280 }
3281 if (isImeAdded) {
3282 builder.append(INPUT_METHOD_SEPARATER);
3283 } else {
3284 isImeAdded = true;
3285 }
3286 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3287 subtypeId);
3288 }
3289 // Remove the last INPUT_METHOD_SEPARATER
3290 putSubtypeHistoryStr(builder.toString());
3291 }
3292
3293 public void addSubtypeToHistory(String imeId, String subtypeId) {
3294 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3295 for (Pair<String, String> ime: subtypeHistory) {
3296 if (ime.first.equals(imeId)) {
3297 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003298 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003299 + ime.second);
3300 }
3301 // We should break here
3302 subtypeHistory.remove(ime);
3303 break;
3304 }
3305 }
satokbb4aa062011-01-19 21:40:27 +09003306 if (DEBUG) {
3307 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3308 }
satok723a27e2010-11-11 14:58:11 +09003309 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3310 }
3311
3312 private void putSubtypeHistoryStr(String str) {
3313 if (DEBUG) {
3314 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3315 }
3316 Settings.Secure.putString(
3317 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3318 }
3319
3320 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3321 // Gets the first one from the history
3322 return getLastSubtypeForInputMethodLockedInternal(null);
3323 }
3324
3325 public String getLastSubtypeForInputMethodLocked(String imeId) {
3326 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3327 if (ime != null) {
3328 return ime.second;
3329 } else {
3330 return null;
3331 }
3332 }
3333
3334 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3335 List<Pair<String, ArrayList<String>>> enabledImes =
3336 getEnabledInputMethodsAndSubtypeListLocked();
3337 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003338 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003339 final String imeInTheHistory = imeAndSubtype.first;
3340 // If imeId is empty, returns the first IME and subtype in the history
3341 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3342 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003343 final String subtypeHashCode =
3344 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3345 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003346 if (!TextUtils.isEmpty(subtypeHashCode)) {
3347 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003348 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003349 }
3350 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3351 }
3352 }
3353 }
3354 if (DEBUG) {
3355 Slog.d(TAG, "No enabled IME found in the history");
3356 }
3357 return null;
3358 }
3359
satokdf31ae62011-01-15 06:19:44 +09003360 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003361 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3362 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3363 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003364 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003365 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003366 if (explicitlyEnabledSubtypes.size() == 0) {
3367 // If there are no explicitly enabled subtypes, applicable subtypes are
3368 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003369 // If IME is enabled and no subtypes are enabled, applicable subtypes
3370 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003371 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003372 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003373 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003374 if (implicitlySelectedSubtypes != null) {
3375 final int N = implicitlySelectedSubtypes.size();
3376 for (int i = 0; i < N; ++i) {
3377 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3378 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3379 return subtypeHashCode;
3380 }
3381 }
3382 }
3383 }
3384 } else {
satokf6cafb62011-01-17 16:29:02 +09003385 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003386 if (s.equals(subtypeHashCode)) {
3387 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003388 try {
3389 final int hashCode = Integer.valueOf(subtypeHashCode);
3390 // Check whether the subtype id is valid or not
3391 if (isValidSubtypeId(imi, hashCode)) {
3392 return s;
3393 } else {
3394 return NOT_A_SUBTYPE_ID_STR;
3395 }
3396 } catch (NumberFormatException e) {
3397 return NOT_A_SUBTYPE_ID_STR;
3398 }
satokdf31ae62011-01-15 06:19:44 +09003399 }
satok723a27e2010-11-11 14:58:11 +09003400 }
3401 }
3402 // If imeId was enabled but subtypeId was disabled.
3403 return NOT_A_SUBTYPE_ID_STR;
3404 }
3405 }
3406 // If both imeId and subtypeId are disabled, return null
3407 return null;
3408 }
3409
3410 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3411 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3412 final String subtypeHistoryStr = getSubtypeHistoryStr();
3413 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3414 return imsList;
3415 }
3416 mInputMethodSplitter.setString(subtypeHistoryStr);
3417 while (mInputMethodSplitter.hasNext()) {
3418 String nextImsStr = mInputMethodSplitter.next();
3419 mSubtypeSplitter.setString(nextImsStr);
3420 if (mSubtypeSplitter.hasNext()) {
3421 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3422 // The first element is ime id.
3423 String imeId = mSubtypeSplitter.next();
3424 while (mSubtypeSplitter.hasNext()) {
3425 subtypeId = mSubtypeSplitter.next();
3426 break;
3427 }
3428 imsList.add(new Pair<String, String>(imeId, subtypeId));
3429 }
3430 }
3431 return imsList;
3432 }
3433
3434 private String getSubtypeHistoryStr() {
3435 if (DEBUG) {
3436 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3437 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3438 }
3439 return Settings.Secure.getString(
3440 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3441 }
3442
3443 public void putSelectedInputMethod(String imeId) {
3444 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3445 }
3446
3447 public void putSelectedSubtype(int subtypeId) {
3448 Settings.Secure.putInt(
3449 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3450 }
satokd87c2592010-09-29 11:52:06 +09003451 }
3452
satoke7c6998e2011-06-03 17:57:59 +09003453 private static class InputMethodFileManager {
3454 private static final String SYSTEM_PATH = "system";
3455 private static final String INPUT_METHOD_PATH = "inputmethod";
3456 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3457 private static final String NODE_SUBTYPES = "subtypes";
3458 private static final String NODE_SUBTYPE = "subtype";
3459 private static final String NODE_IMI = "imi";
3460 private static final String ATTR_ID = "id";
3461 private static final String ATTR_LABEL = "label";
3462 private static final String ATTR_ICON = "icon";
3463 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3464 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3465 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3466 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3467 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3468 private final HashMap<String, InputMethodInfo> mMethodMap;
3469 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3470 new HashMap<String, List<InputMethodSubtype>>();
3471 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3472 if (methodMap == null) {
3473 throw new NullPointerException("methodMap is null");
3474 }
3475 mMethodMap = methodMap;
3476 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3477 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3478 if (!inputMethodDir.mkdirs()) {
3479 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3480 }
3481 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3482 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3483 if (!subtypeFile.exists()) {
3484 // If "subtypes.xml" doesn't exist, create a blank file.
3485 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3486 methodMap);
3487 } else {
3488 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3489 }
3490 }
3491
3492 private void deleteAllInputMethodSubtypes(String imiId) {
3493 synchronized (mMethodMap) {
3494 mSubtypesMap.remove(imiId);
3495 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3496 mMethodMap);
3497 }
3498 }
3499
3500 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003501 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003502 synchronized (mMethodMap) {
3503 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3504 final int N = additionalSubtypes.length;
3505 for (int i = 0; i < N; ++i) {
3506 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003507 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003508 subtypes.add(subtype);
3509 }
3510 }
satok4a28bde2011-06-29 21:03:40 +09003511 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003512 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3513 mMethodMap);
3514 }
3515 }
3516
3517 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3518 synchronized (mMethodMap) {
3519 return mSubtypesMap;
3520 }
3521 }
3522
3523 private static void writeAdditionalInputMethodSubtypes(
3524 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3525 HashMap<String, InputMethodInfo> methodMap) {
3526 // Safety net for the case that this function is called before methodMap is set.
3527 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3528 FileOutputStream fos = null;
3529 try {
3530 fos = subtypesFile.startWrite();
3531 final XmlSerializer out = new FastXmlSerializer();
3532 out.setOutput(fos, "utf-8");
3533 out.startDocument(null, true);
3534 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3535 out.startTag(null, NODE_SUBTYPES);
3536 for (String imiId : allSubtypes.keySet()) {
3537 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3538 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3539 continue;
3540 }
3541 out.startTag(null, NODE_IMI);
3542 out.attribute(null, ATTR_ID, imiId);
3543 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3544 final int N = subtypesList.size();
3545 for (int i = 0; i < N; ++i) {
3546 final InputMethodSubtype subtype = subtypesList.get(i);
3547 out.startTag(null, NODE_SUBTYPE);
3548 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3549 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3550 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3551 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3552 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3553 out.attribute(null, ATTR_IS_AUXILIARY,
3554 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3555 out.endTag(null, NODE_SUBTYPE);
3556 }
3557 out.endTag(null, NODE_IMI);
3558 }
3559 out.endTag(null, NODE_SUBTYPES);
3560 out.endDocument();
3561 subtypesFile.finishWrite(fos);
3562 } catch (java.io.IOException e) {
3563 Slog.w(TAG, "Error writing subtypes", e);
3564 if (fos != null) {
3565 subtypesFile.failWrite(fos);
3566 }
3567 }
3568 }
3569
3570 private static void readAdditionalInputMethodSubtypes(
3571 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3572 if (allSubtypes == null || subtypesFile == null) return;
3573 allSubtypes.clear();
3574 FileInputStream fis = null;
3575 try {
3576 fis = subtypesFile.openRead();
3577 final XmlPullParser parser = Xml.newPullParser();
3578 parser.setInput(fis, null);
3579 int type = parser.getEventType();
3580 // Skip parsing until START_TAG
3581 while ((type = parser.next()) != XmlPullParser.START_TAG
3582 && type != XmlPullParser.END_DOCUMENT) {}
3583 String firstNodeName = parser.getName();
3584 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3585 throw new XmlPullParserException("Xml doesn't start with subtypes");
3586 }
3587 final int depth =parser.getDepth();
3588 String currentImiId = null;
3589 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3590 while (((type = parser.next()) != XmlPullParser.END_TAG
3591 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3592 if (type != XmlPullParser.START_TAG)
3593 continue;
3594 final String nodeName = parser.getName();
3595 if (NODE_IMI.equals(nodeName)) {
3596 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3597 if (TextUtils.isEmpty(currentImiId)) {
3598 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3599 continue;
3600 }
3601 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3602 allSubtypes.put(currentImiId, tempSubtypesArray);
3603 } else if (NODE_SUBTYPE.equals(nodeName)) {
3604 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3605 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3606 continue;
3607 }
3608 final int icon = Integer.valueOf(
3609 parser.getAttributeValue(null, ATTR_ICON));
3610 final int label = Integer.valueOf(
3611 parser.getAttributeValue(null, ATTR_LABEL));
3612 final String imeSubtypeLocale =
3613 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3614 final String imeSubtypeMode =
3615 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3616 final String imeSubtypeExtraValue =
3617 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003618 final boolean isAuxiliary = "1".equals(String.valueOf(
3619 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003620 final InputMethodSubtype subtype =
3621 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3622 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3623 tempSubtypesArray.add(subtype);
3624 }
3625 }
3626 } catch (XmlPullParserException e) {
3627 Slog.w(TAG, "Error reading subtypes: " + e);
3628 return;
3629 } catch (java.io.IOException e) {
3630 Slog.w(TAG, "Error reading subtypes: " + e);
3631 return;
3632 } catch (NumberFormatException e) {
3633 Slog.w(TAG, "Error reading subtypes: " + e);
3634 return;
3635 } finally {
3636 if (fis != null) {
3637 try {
3638 fis.close();
3639 } catch (java.io.IOException e1) {
3640 Slog.w(TAG, "Failed to close.");
3641 }
3642 }
3643 }
3644 }
3645 }
3646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 @Override
3650 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3651 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3652 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3655 + Binder.getCallingPid()
3656 + ", uid=" + Binder.getCallingUid());
3657 return;
3658 }
3659
3660 IInputMethod method;
3661 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 synchronized (mMethodMap) {
3666 p.println("Current Input Method Manager state:");
3667 int N = mMethodList.size();
3668 p.println(" Input Methods:");
3669 for (int i=0; i<N; i++) {
3670 InputMethodInfo info = mMethodList.get(i);
3671 p.println(" InputMethod #" + i + ":");
3672 info.dump(p, " ");
3673 }
3674 p.println(" Clients:");
3675 for (ClientState ci : mClients.values()) {
3676 p.println(" Client " + ci + ":");
3677 p.println(" client=" + ci.client);
3678 p.println(" inputContext=" + ci.inputContext);
3679 p.println(" sessionRequested=" + ci.sessionRequested);
3680 p.println(" curSession=" + ci.curSession);
3681 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003682 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003684 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3685 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3687 + " mBoundToMethod=" + mBoundToMethod);
3688 p.println(" mCurToken=" + mCurToken);
3689 p.println(" mCurIntent=" + mCurIntent);
3690 method = mCurMethod;
3691 p.println(" mCurMethod=" + mCurMethod);
3692 p.println(" mEnabledSession=" + mEnabledSession);
3693 p.println(" mShowRequested=" + mShowRequested
3694 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3695 + " mShowForced=" + mShowForced
3696 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003697 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003699
Jeff Brownb88102f2010-09-08 11:49:43 -07003700 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 pw.flush();
3703 try {
3704 client.client.asBinder().dump(fd, args);
3705 } catch (RemoteException e) {
3706 p.println("Input method client dead: " + e);
3707 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003708 } else {
3709 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003711
Jeff Brownb88102f2010-09-08 11:49:43 -07003712 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 pw.flush();
3715 try {
3716 method.asBinder().dump(fd, args);
3717 } catch (RemoteException e) {
3718 p.println("Input method service dead: " + e);
3719 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003720 } else {
3721 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723 }
3724}