blob: a49ccf7109a91fb57b10b67090f061cd7952c9cf [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);
satoke0a99412012-05-10 02:22:58 +09001145 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1146 // The current IME is shown. Hence an IME switch (transition) is happening.
1147 mWindowManagerService.saveLastInputMethodWindowForTransition();
1148 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001149 mIWindowManager.removeWindowToken(mCurToken);
1150 } catch (RemoteException e) {
1151 }
1152 mCurToken = null;
1153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
The Android Open Source Project10592532009-03-18 17:39:46 -07001155 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001156 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001158 if (reportToClient && mCurClient != null) {
1159 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1160 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1161 }
1162 }
1163
Devin Taylor0c33ed22010-02-23 13:26:46 -06001164 private void finishSession(SessionState sessionState) {
1165 if (sessionState != null && sessionState.session != null) {
1166 try {
1167 sessionState.session.finishSession();
1168 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001169 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001170 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001171 }
1172 }
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001175 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 if (mCurMethod != null) {
1177 for (ClientState cs : mClients.values()) {
1178 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001179 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 cs.curSession = null;
1181 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001182
1183 finishSession(mEnabledSession);
1184 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 mCurMethod = null;
1186 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001187 if (mStatusBar != null) {
1188 mStatusBar.setIconVisibility("ime", false);
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
satoke7c6998e2011-06-03 17:57:59 +09001192 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public void onServiceDisconnected(ComponentName name) {
1194 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001195 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 + " mCurIntent=" + mCurIntent);
1197 if (mCurMethod != null && mCurIntent != null
1198 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001199 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 // We consider this to be a new bind attempt, since the system
1201 // should now try to restart the service for us.
1202 mLastBindTime = SystemClock.uptimeMillis();
1203 mShowRequested = mInputShown;
1204 mInputShown = false;
1205 if (mCurClient != null) {
1206 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1207 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1208 }
1209 }
1210 }
1211 }
1212
satokf9f01002011-05-19 21:31:50 +09001213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001215 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 long ident = Binder.clearCallingIdentity();
1217 try {
1218 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001219 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 return;
1221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 synchronized (mMethodMap) {
1224 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001225 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001226 if (mStatusBar != null) {
1227 mStatusBar.setIconVisibility("ime", false);
1228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001230 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001231 CharSequence contentDescription = null;
1232 try {
1233 PackageManager packageManager = mContext.getPackageManager();
1234 contentDescription = packageManager.getApplicationLabel(
1235 packageManager.getApplicationInfo(packageName, 0));
1236 } catch (NameNotFoundException nnfe) {
1237 /* ignore */
1238 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001239 if (mStatusBar != null) {
1240 mStatusBar.setIcon("ime", packageName, iconId, 0,
1241 contentDescription != null
1242 ? contentDescription.toString() : null);
1243 mStatusBar.setIconVisibility("ime", true);
1244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246 }
1247 } finally {
1248 Binder.restoreCallingIdentity(ident);
1249 }
1250 }
1251
satok7cfc0ed2011-06-20 21:29:36 +09001252 private boolean needsToShowImeSwitchOngoingNotification() {
1253 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001254 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001255 synchronized (mMethodMap) {
1256 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1257 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001258 if (N > 2) return true;
1259 if (N < 1) return false;
1260 int nonAuxCount = 0;
1261 int auxCount = 0;
1262 InputMethodSubtype nonAuxSubtype = null;
1263 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001264 for(int i = 0; i < N; ++i) {
1265 final InputMethodInfo imi = imis.get(i);
1266 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1267 imi, true);
1268 final int subtypeCount = subtypes.size();
1269 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001270 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001271 } else {
1272 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001273 final InputMethodSubtype subtype = subtypes.get(j);
1274 if (!subtype.isAuxiliary()) {
1275 ++nonAuxCount;
1276 nonAuxSubtype = subtype;
1277 } else {
1278 ++auxCount;
1279 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001280 }
1281 }
1282 }
satok7cfc0ed2011-06-20 21:29:36 +09001283 }
satokb6359412011-06-28 17:47:41 +09001284 if (nonAuxCount > 1 || auxCount > 1) {
1285 return true;
1286 } else if (nonAuxCount == 1 && auxCount == 1) {
1287 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001288 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1289 || auxSubtype.overridesImplicitlyEnabledSubtype()
1290 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001291 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1292 return false;
1293 }
1294 return true;
1295 }
1296 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001297 }
satok7cfc0ed2011-06-20 21:29:36 +09001298 }
1299
satokdbf29502011-08-25 15:28:23 +09001300 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001301 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001302 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001303 int uid = Binder.getCallingUid();
1304 long ident = Binder.clearCallingIdentity();
1305 try {
1306 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001307 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001308 return;
1309 }
1310
1311 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001312 mImeWindowVis = vis;
1313 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001314 if (mStatusBar != null) {
1315 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1316 }
satok7cfc0ed2011-06-20 21:29:36 +09001317 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001318 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1319 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001320 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001321 final CharSequence title = mRes.getText(
1322 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001323 final CharSequence imiLabel = imi.loadLabel(pm);
1324 final CharSequence summary = mCurrentSubtype != null
1325 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1326 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1327 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001328 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001329 : imiLabel;
1330
satok7cfc0ed2011-06-20 21:29:36 +09001331 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001332 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001333 if (mNotificationManager != null) {
1334 mNotificationManager.notify(
1335 com.android.internal.R.string.select_input_method,
1336 mImeSwitcherNotification);
1337 mNotificationShown = true;
1338 }
satok7cfc0ed2011-06-20 21:29:36 +09001339 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001340 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001341 mNotificationManager.cancel(
1342 com.android.internal.R.string.select_input_method);
1343 mNotificationShown = false;
1344 }
1345 }
satok06487a52010-10-29 11:37:18 +09001346 }
1347 } finally {
1348 Binder.restoreCallingIdentity(ident);
1349 }
1350 }
1351
satoke7c6998e2011-06-03 17:57:59 +09001352 @Override
satokf9f01002011-05-19 21:31:50 +09001353 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1354 synchronized (mMethodMap) {
1355 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1356 for (int i = 0; i < spans.length; ++i) {
1357 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001358 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001359 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001360 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001361 }
1362 }
1363 }
1364 }
1365
satoke7c6998e2011-06-03 17:57:59 +09001366 @Override
satokf9f01002011-05-19 21:31:50 +09001367 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1368 synchronized (mMethodMap) {
1369 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1370 // TODO: Do not send the intent if the process of the targetImi is already dead.
1371 if (targetImi != null) {
1372 final String[] suggestions = span.getSuggestions();
1373 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001374 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001375 final Intent intent = new Intent();
1376 // Ensures that only a class in the original IME package will receive the
1377 // notification.
satok42c5a162011-05-26 16:46:14 +09001378 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001379 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1380 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1381 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1382 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1383 mContext.sendBroadcast(intent);
1384 return true;
1385 }
1386 }
1387 return false;
1388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001391 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1392 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1393 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1394 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001396 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001397 // There is no input method selected, try to choose new applicable input method.
1398 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1399 id = Settings.Secure.getString(mContext.getContentResolver(),
1400 Settings.Secure.DEFAULT_INPUT_METHOD);
1401 }
1402 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 try {
satokab751aa2010-09-14 19:17:36 +09001404 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001406 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001407 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001408 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
satokf3db1af2010-11-23 13:34:33 +09001410 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001411 } else {
1412 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001413 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001414 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
satokab751aa2010-09-14 19:17:36 +09001418 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 InputMethodInfo info = mMethodMap.get(id);
1420 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001421 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001425 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001426 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1427 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001428 }
1429 if (subtype != mCurrentSubtype) {
1430 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001431 if (subtype != null) {
1432 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1433 }
satokcd7cd292010-11-20 15:46:23 +09001434 if (mCurMethod != null) {
1435 try {
satok3afd6c02011-11-18 08:38:19 +09001436 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001437 // If subtype is null, try to find the most applicable one from
1438 // getCurrentInputMethodSubtype.
1439 if (subtype == null) {
1440 subtype = getCurrentInputMethodSubtype();
1441 }
1442 mCurMethod.changeInputMethodSubtype(subtype);
1443 } catch (RemoteException e) {
1444 return;
satokab751aa2010-09-14 19:17:36 +09001445 }
1446 }
1447 }
1448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 return;
1450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 final long ident = Binder.clearCallingIdentity();
1453 try {
satokab751aa2010-09-14 19:17:36 +09001454 // Set a subtype to this input method.
1455 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001456 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1457 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1458 // because mCurMethodId is stored as a history in
1459 // setSelectedInputMethodAndSubtypeLocked().
1460 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
1462 if (ActivityManagerNative.isSystemReady()) {
1463 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001464 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 intent.putExtra("input_method_id", id);
1466 mContext.sendBroadcast(intent);
1467 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001468 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 } finally {
1470 Binder.restoreCallingIdentity(ident);
1471 }
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
satok42c5a162011-05-26 16:46:14 +09001474 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001475 public boolean showSoftInput(IInputMethodClient client, int flags,
1476 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001477 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 long ident = Binder.clearCallingIdentity();
1479 try {
1480 synchronized (mMethodMap) {
1481 if (mCurClient == null || client == null
1482 || mCurClient.client.asBinder() != client.asBinder()) {
1483 try {
1484 // We need to check if this is the current client with
1485 // focus in the window manager, to allow this call to
1486 // be made before input is started in it.
1487 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001488 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001489 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001492 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 }
1494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Joe Onorato8a9b2202010-02-26 18:56:32 -08001496 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001497 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499 } finally {
1500 Binder.restoreCallingIdentity(ident);
1501 }
1502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
The Android Open Source Project4df24232009-03-05 14:34:35 -08001504 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 mShowRequested = true;
1506 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1507 mShowExplicitlyRequested = true;
1508 }
1509 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1510 mShowExplicitlyRequested = true;
1511 mShowForced = true;
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Dianne Hackborncc278702009-09-02 23:07:23 -07001514 if (!mSystemReady) {
1515 return false;
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
The Android Open Source Project4df24232009-03-05 14:34:35 -08001518 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001520 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1521 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1522 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001524 if (mHaveConnection && !mVisibleBound) {
1525 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1526 mVisibleBound = true;
1527 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001528 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001530 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 // The client has asked to have the input method shown, but
1532 // we have been sitting here too long with a connection to the
1533 // service and no interface received, so let's disconnect/connect
1534 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001537 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001539 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1540 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
The Android Open Source Project4df24232009-03-05 14:34:35 -08001543 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
satok42c5a162011-05-26 16:46:14 +09001546 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001547 public boolean hideSoftInput(IInputMethodClient client, int flags,
1548 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001549 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 long ident = Binder.clearCallingIdentity();
1551 try {
1552 synchronized (mMethodMap) {
1553 if (mCurClient == null || client == null
1554 || mCurClient.client.asBinder() != client.asBinder()) {
1555 try {
1556 // We need to check if this is the current client with
1557 // focus in the window manager, to allow this call to
1558 // be made before input is started in it.
1559 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001560 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1561 + uid + ": " + client);
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 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001566 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001567 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
Joe Onorato8a9b2202010-02-26 18:56:32 -08001571 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001572 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574 } finally {
1575 Binder.restoreCallingIdentity(ident);
1576 }
1577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
The Android Open Source Project4df24232009-03-05 14:34:35 -08001579 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1581 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001584 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001587 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001589 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001591 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001593 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1594 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1595 res = true;
1596 } else {
1597 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001599 if (mHaveConnection && mVisibleBound) {
1600 mContext.unbindService(mVisibleConnection);
1601 mVisibleBound = false;
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 mInputShown = false;
1604 mShowRequested = false;
1605 mShowExplicitlyRequested = false;
1606 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001607 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
satok42c5a162011-05-26 16:46:14 +09001610 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001611 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1612 int controlFlags, int softInputMode, int windowFlags,
1613 EditorInfo attribute, IInputContext inputContext) {
1614 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 long ident = Binder.clearCallingIdentity();
1616 try {
1617 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001618 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001619 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001621 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001622
Dianne Hackborn7663d802012-02-24 13:08:49 -08001623 ClientState cs = mClients.get(client.asBinder());
1624 if (cs == null) {
1625 throw new IllegalArgumentException("unknown client "
1626 + client.asBinder());
1627 }
1628
1629 try {
1630 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1631 // Check with the window manager to make sure this client actually
1632 // has a window with focus. If not, reject. This is thread safe
1633 // because if the focus changes some time before or after, the
1634 // next client receiving focus that has any interest in input will
1635 // be calling through here after that change happens.
1636 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1637 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1638 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001640 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001642
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001643 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001644 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001645 if (attribute != null) {
1646 return startInputUncheckedLocked(cs, inputContext, attribute,
1647 controlFlags);
1648 }
1649 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001650 }
1651 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001653 // Should we auto-show the IME even if the caller has not
1654 // specified what should be done with it?
1655 // We only do this automatically if the window can resize
1656 // to accommodate the IME (so what the user sees will give
1657 // them good context without input information being obscured
1658 // by the IME) or if running on a large screen where there
1659 // is more room for the target window + IME.
1660 final boolean doAutoShow =
1661 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1662 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1663 || mRes.getConfiguration().isLayoutSizeAtLeast(
1664 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001665 final boolean isTextEditor =
1666 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1667
1668 // We want to start input before showing the IME, but after closing
1669 // it. We want to do this after closing it to help the IME disappear
1670 // more quickly (not get stuck behind it initializing itself for the
1671 // new focused input, even if its window wants to hide the IME).
1672 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1675 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001676 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1678 // There is no focus view, and this window will
1679 // be behind any soft input window, so hide the
1680 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001681 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001682 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001684 } else if (isTextEditor && doAutoShow && (softInputMode &
1685 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 // There is a focus view, and we are navigating forward
1687 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001688 // We only do this automatically if the window can resize
1689 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001690 // them good context without input information being obscured
1691 // by the IME) or if running on a large screen where there
1692 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001693 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001694 if (attribute != null) {
1695 res = startInputUncheckedLocked(cs, inputContext, attribute,
1696 controlFlags);
1697 didStart = true;
1698 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001699 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 break;
1702 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1703 // Do nothing.
1704 break;
1705 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1706 if ((softInputMode &
1707 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001708 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001709 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
1711 break;
1712 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001713 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001714 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 break;
1716 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1717 if ((softInputMode &
1718 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001719 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001720 if (attribute != null) {
1721 res = startInputUncheckedLocked(cs, inputContext, attribute,
1722 controlFlags);
1723 didStart = true;
1724 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 break;
1728 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001729 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001730 if (attribute != null) {
1731 res = startInputUncheckedLocked(cs, inputContext, attribute,
1732 controlFlags);
1733 didStart = true;
1734 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 break;
1737 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001738
1739 if (!didStart && attribute != null) {
1740 res = startInputUncheckedLocked(cs, inputContext, attribute,
1741 controlFlags);
1742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 } finally {
1745 Binder.restoreCallingIdentity(ident);
1746 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001747
1748 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
satok42c5a162011-05-26 16:46:14 +09001751 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1753 synchronized (mMethodMap) {
1754 if (mCurClient == null || client == null
1755 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001756 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001757 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 }
1759
satok440aab52010-11-25 09:43:11 +09001760 // Always call subtype picker, because subtype picker is a superset of input method
1761 // picker.
satokab751aa2010-09-14 19:17:36 +09001762 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1763 }
1764 }
1765
satok42c5a162011-05-26 16:46:14 +09001766 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001768 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1769 }
1770
satok42c5a162011-05-26 16:46:14 +09001771 @Override
satok28203512010-11-24 11:06:49 +09001772 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1773 synchronized (mMethodMap) {
1774 if (subtype != null) {
1775 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1776 mMethodMap.get(id), subtype.hashCode()));
1777 } else {
1778 setInputMethod(token, id);
1779 }
1780 }
satokab751aa2010-09-14 19:17:36 +09001781 }
1782
satok42c5a162011-05-26 16:46:14 +09001783 @Override
satokb416a712010-11-25 20:42:14 +09001784 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001785 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001786 synchronized (mMethodMap) {
1787 if (mCurClient == null || client == null
1788 || mCurClient.client.asBinder() != client.asBinder()) {
1789 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1790 }
satok7fee71f2010-12-17 18:54:26 +09001791 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1792 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001793 }
1794 }
1795
satok4fc87d62011-05-20 16:13:43 +09001796 @Override
satok735cf382010-11-11 20:40:09 +09001797 public boolean switchToLastInputMethod(IBinder token) {
1798 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001799 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001800 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001801 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001802 lastImi = mMethodMap.get(lastIme.first);
1803 } else {
1804 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001805 }
satok4fc87d62011-05-20 16:13:43 +09001806 String targetLastImiId = null;
1807 int subtypeId = NOT_A_SUBTYPE_ID;
1808 if (lastIme != null && lastImi != null) {
1809 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1810 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1811 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1812 : mCurrentSubtype.hashCode();
1813 // If the last IME is the same as the current IME and the last subtype is not
1814 // defined, there is no need to switch to the last IME.
1815 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1816 targetLastImiId = lastIme.first;
1817 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1818 }
1819 }
1820
1821 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1822 // This is a safety net. If the currentSubtype can't be added to the history
1823 // and the framework couldn't find the last ime, we will make the last ime be
1824 // the most applicable enabled keyboard subtype of the system imes.
1825 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1826 if (enabled != null) {
1827 final int N = enabled.size();
1828 final String locale = mCurrentSubtype == null
1829 ? mRes.getConfiguration().locale.toString()
1830 : mCurrentSubtype.getLocale();
1831 for (int i = 0; i < N; ++i) {
1832 final InputMethodInfo imi = enabled.get(i);
1833 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1834 InputMethodSubtype keyboardSubtype =
1835 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1836 SUBTYPE_MODE_KEYBOARD, locale, true);
1837 if (keyboardSubtype != null) {
1838 targetLastImiId = imi.getId();
1839 subtypeId = getSubtypeIdFromHashCode(
1840 imi, keyboardSubtype.hashCode());
1841 if(keyboardSubtype.getLocale().equals(locale)) {
1842 break;
1843 }
1844 }
1845 }
1846 }
1847 }
1848 }
1849
1850 if (!TextUtils.isEmpty(targetLastImiId)) {
1851 if (DEBUG) {
1852 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1853 + ", from: " + mCurMethodId + ", " + subtypeId);
1854 }
1855 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1856 return true;
1857 } else {
1858 return false;
1859 }
satok735cf382010-11-11 20:40:09 +09001860 }
1861 }
1862
satoke7c6998e2011-06-03 17:57:59 +09001863 @Override
satok688bd472012-02-09 20:09:17 +09001864 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1865 synchronized (mMethodMap) {
1866 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1867 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1868 if (nextSubtype == null) {
1869 return false;
1870 }
1871 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1872 return true;
1873 }
1874 }
1875
1876 @Override
satok68f1b782011-04-11 14:26:04 +09001877 public InputMethodSubtype getLastInputMethodSubtype() {
1878 synchronized (mMethodMap) {
1879 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1880 // TODO: Handle the case of the last IME with no subtypes
1881 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1882 || TextUtils.isEmpty(lastIme.second)) return null;
1883 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1884 if (lastImi == null) return null;
1885 try {
1886 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001887 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1888 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1889 return null;
1890 }
1891 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001892 } catch (NumberFormatException e) {
1893 return null;
1894 }
1895 }
1896 }
1897
satoke7c6998e2011-06-03 17:57:59 +09001898 @Override
satokee5e77c2011-09-02 18:50:15 +09001899 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001900 // By this IPC call, only a process which shares the same uid with the IME can add
1901 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001902 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001903 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001904 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001905 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001906 final PackageManager pm = mContext.getPackageManager();
1907 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1908 if (packageInfos != null) {
1909 final int packageNum = packageInfos.length;
1910 for (int i = 0; i < packageNum; ++i) {
1911 if (packageInfos[i].equals(imi.getPackageName())) {
1912 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001913 final long ident = Binder.clearCallingIdentity();
1914 try {
1915 buildInputMethodListLocked(mMethodList, mMethodMap);
1916 } finally {
1917 Binder.restoreCallingIdentity(ident);
1918 }
satokee5e77c2011-09-02 18:50:15 +09001919 return;
satok91e88122011-07-18 11:11:42 +09001920 }
1921 }
1922 }
satoke7c6998e2011-06-03 17:57:59 +09001923 }
satokee5e77c2011-09-02 18:50:15 +09001924 return;
satoke7c6998e2011-06-03 17:57:59 +09001925 }
1926
satok28203512010-11-24 11:06:49 +09001927 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 synchronized (mMethodMap) {
1929 if (token == null) {
1930 if (mContext.checkCallingOrSelfPermission(
1931 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1932 != PackageManager.PERMISSION_GRANTED) {
1933 throw new SecurityException(
1934 "Using null token requires permission "
1935 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1936 }
1937 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001938 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1939 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 return;
1941 }
1942
satokc5933802011-08-31 21:26:04 +09001943 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 try {
satokab751aa2010-09-14 19:17:36 +09001945 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 } finally {
1947 Binder.restoreCallingIdentity(ident);
1948 }
1949 }
1950 }
1951
satok42c5a162011-05-26 16:46:14 +09001952 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 public void hideMySoftInput(IBinder token, int flags) {
1954 synchronized (mMethodMap) {
1955 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001956 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1957 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 return;
1959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 long ident = Binder.clearCallingIdentity();
1961 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001962 hideCurrentInputLocked(flags, null);
1963 } finally {
1964 Binder.restoreCallingIdentity(ident);
1965 }
1966 }
1967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
satok42c5a162011-05-26 16:46:14 +09001969 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001970 public void showMySoftInput(IBinder token, int flags) {
1971 synchronized (mMethodMap) {
1972 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001973 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1974 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001975 return;
1976 }
1977 long ident = Binder.clearCallingIdentity();
1978 try {
1979 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 } finally {
1981 Binder.restoreCallingIdentity(ident);
1982 }
1983 }
1984 }
1985
1986 void setEnabledSessionInMainThread(SessionState session) {
1987 if (mEnabledSession != session) {
1988 if (mEnabledSession != null) {
1989 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001990 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 mEnabledSession.method.setSessionEnabled(
1992 mEnabledSession.session, false);
1993 } catch (RemoteException e) {
1994 }
1995 }
1996 mEnabledSession = session;
1997 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001998 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 session.method.setSessionEnabled(
2000 session.session, true);
2001 } catch (RemoteException e) {
2002 }
2003 }
2004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
satok42c5a162011-05-26 16:46:14 +09002006 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 public boolean handleMessage(Message msg) {
2008 HandlerCaller.SomeArgs args;
2009 switch (msg.what) {
2010 case MSG_SHOW_IM_PICKER:
2011 showInputMethodMenu();
2012 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
satokab751aa2010-09-14 19:17:36 +09002014 case MSG_SHOW_IM_SUBTYPE_PICKER:
2015 showInputMethodSubtypeMenu();
2016 return true;
2017
satok47a44912010-10-06 16:03:58 +09002018 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09002019 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002020 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09002021 return true;
2022
2023 case MSG_SHOW_IM_CONFIG:
2024 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002025 return true;
2026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 case MSG_UNBIND_INPUT:
2030 try {
2031 ((IInputMethod)msg.obj).unbindInput();
2032 } catch (RemoteException e) {
2033 // There is nothing interesting about the method dying.
2034 }
2035 return true;
2036 case MSG_BIND_INPUT:
2037 args = (HandlerCaller.SomeArgs)msg.obj;
2038 try {
2039 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2040 } catch (RemoteException e) {
2041 }
2042 return true;
2043 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002044 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2047 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 } catch (RemoteException e) {
2049 }
2050 return true;
2051 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002052 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 ((IInputMethod)args.arg1).hideSoftInput(0,
2055 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 } catch (RemoteException e) {
2057 }
2058 return true;
2059 case MSG_ATTACH_TOKEN:
2060 args = (HandlerCaller.SomeArgs)msg.obj;
2061 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002062 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2064 } catch (RemoteException e) {
2065 }
2066 return true;
2067 case MSG_CREATE_SESSION:
2068 args = (HandlerCaller.SomeArgs)msg.obj;
2069 try {
2070 ((IInputMethod)args.arg1).createSession(
2071 (IInputMethodCallback)args.arg2);
2072 } catch (RemoteException e) {
2073 }
2074 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 case MSG_START_INPUT:
2078 args = (HandlerCaller.SomeArgs)msg.obj;
2079 try {
2080 SessionState session = (SessionState)args.arg1;
2081 setEnabledSessionInMainThread(session);
2082 session.method.startInput((IInputContext)args.arg2,
2083 (EditorInfo)args.arg3);
2084 } catch (RemoteException e) {
2085 }
2086 return true;
2087 case MSG_RESTART_INPUT:
2088 args = (HandlerCaller.SomeArgs)msg.obj;
2089 try {
2090 SessionState session = (SessionState)args.arg1;
2091 setEnabledSessionInMainThread(session);
2092 session.method.restartInput((IInputContext)args.arg2,
2093 (EditorInfo)args.arg3);
2094 } catch (RemoteException e) {
2095 }
2096 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 case MSG_UNBIND_METHOD:
2101 try {
2102 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2103 } catch (RemoteException e) {
2104 // There is nothing interesting about the last client dying.
2105 }
2106 return true;
2107 case MSG_BIND_METHOD:
2108 args = (HandlerCaller.SomeArgs)msg.obj;
2109 try {
2110 ((IInputMethodClient)args.arg1).onBindMethod(
2111 (InputBindResult)args.arg2);
2112 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002113 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115 return true;
satok01038492012-04-09 21:08:27 +09002116
2117 // --------------------------------------------------------------
2118 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2119 mHardKeyboardListener.handleHardKeyboardStatusChange(
2120 msg.arg1 == 1, msg.arg2 == 1);
2121 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123 return false;
2124 }
2125
satok5b927c432012-05-01 20:09:34 +09002126 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002127 return (inputMethod.getServiceInfo().applicationInfo.flags
2128 & ApplicationInfo.FLAG_SYSTEM) != 0;
2129 }
2130
Ken Wakasa586f0512011-01-20 22:31:01 +09002131 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2132 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2133 final int subtypeCount = imi.getSubtypeCount();
2134 for (int i = 0; i < subtypeCount; ++i) {
2135 subtypes.add(imi.getSubtypeAt(i));
2136 }
2137 return subtypes;
2138 }
2139
satoka86f5e42011-09-02 17:12:42 +09002140
2141 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2142 InputMethodInfo imi, String mode) {
2143 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2144 final int subtypeCount = imi.getSubtypeCount();
2145 for (int i = 0; i < subtypeCount; ++i) {
2146 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2147 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2148 subtypes.add(subtype);
2149 }
2150 }
2151 return subtypes;
2152 }
2153
satokdc9ddae2011-10-06 12:22:36 +09002154 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002155 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002156 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002157 // We'd prefer to fall back on a system IME, since that is safer.
2158 int i=enabled.size();
2159 while (i > 0) {
2160 i--;
satokdc9ddae2011-10-06 12:22:36 +09002161 final InputMethodInfo imi = enabled.get(i);
2162 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002163 break;
2164 }
2165 }
satokdc9ddae2011-10-06 12:22:36 +09002166 return enabled.get(i);
2167 }
2168 return null;
2169 }
2170
2171 private boolean chooseNewDefaultIMELocked() {
2172 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2173 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002174 if (DEBUG) {
2175 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2176 }
satok723a27e2010-11-11 14:58:11 +09002177 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002178 return true;
2179 }
2180
2181 return false;
2182 }
2183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2185 HashMap<String, InputMethodInfo> map) {
2186 list.clear();
2187 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002190 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002191 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2192 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2193 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2194 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195
2196 List<ResolveInfo> services = pm.queryIntentServices(
2197 new Intent(InputMethod.SERVICE_INTERFACE),
2198 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002199
satoke7c6998e2011-06-03 17:57:59 +09002200 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2201 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 for (int i = 0; i < services.size(); ++i) {
2203 ResolveInfo ri = services.get(i);
2204 ServiceInfo si = ri.serviceInfo;
2205 ComponentName compName = new ComponentName(si.packageName, si.name);
2206 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2207 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002208 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 + ": it does not require the permission "
2210 + android.Manifest.permission.BIND_INPUT_METHOD);
2211 continue;
2212 }
2213
Joe Onorato8a9b2202010-02-26 18:56:32 -08002214 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215
2216 try {
satoke7c6998e2011-06-03 17:57:59 +09002217 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002219 final String id = p.getId();
2220 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
satok5b927c432012-05-01 20:09:34 +09002222 // Valid system default IMEs and IMEs that have English subtypes are enabled
2223 // by default, unless there's a hard keyboard and the system IME was explicitly
2224 // disabled
2225 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2226 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002227 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002228 }
2229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002231 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002235 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002237 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002240
2241 String defaultIme = Settings.Secure.getString(mContext
2242 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002243 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002244 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002245 updateFromSettingsLocked();
2246 }
2247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002251
satokab751aa2010-09-14 19:17:36 +09002252 private void showInputMethodMenu() {
2253 showInputMethodMenuInternal(false);
2254 }
2255
2256 private void showInputMethodSubtypeMenu() {
2257 showInputMethodMenuInternal(true);
2258 }
2259
satok217f5482010-12-15 05:19:19 +09002260 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002261 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002262 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002263 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2264 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002265 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002266 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002267 }
satok217f5482010-12-15 05:19:19 +09002268 mContext.startActivity(intent);
2269 }
2270
2271 private void showConfigureInputMethods() {
2272 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2273 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2274 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2275 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002276 mContext.startActivity(intent);
2277 }
2278
satok2c93efc2012-04-02 19:33:47 +09002279 private boolean isScreenLocked() {
2280 return mKeyguardManager != null
2281 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2282 }
satokab751aa2010-09-14 19:17:36 +09002283 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002284 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002288 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
satok2c93efc2012-04-02 19:33:47 +09002290 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002292 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002293 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002294
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002295 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002296 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2297 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002298 if (immis == null || immis.size() == 0) {
2299 return;
2300 }
2301
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002302 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303
satok688bd472012-02-09 20:09:17 +09002304 final List<ImeSubtypeListItem> imList =
2305 mImListManager.getSortedInputMethodAndSubtypeList(
2306 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002307
satokc3690562012-01-10 20:14:43 +09002308 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2309 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2310 if (currentSubtype != null) {
2311 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2312 lastInputMethodSubtypeId =
2313 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2314 }
2315 }
2316
Ken Wakasa761eb372011-03-04 19:06:18 +09002317 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002318 mIms = new InputMethodInfo[N];
2319 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002320 int checkedItem = 0;
2321 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002322 final ImeSubtypeListItem item = imList.get(i);
2323 mIms[i] = item.mImi;
2324 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002325 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002326 int subtypeId = mSubtypeIds[i];
2327 if ((subtypeId == NOT_A_SUBTYPE_ID)
2328 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2329 || (subtypeId == lastInputMethodSubtypeId)) {
2330 checkedItem = i;
2331 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002334 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002335 com.android.internal.R.styleable.DialogPreference,
2336 com.android.internal.R.attr.alertDialogStyle, 0);
2337 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002338 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002339 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002340 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002342 }
2343 })
2344 .setIcon(a.getDrawable(
2345 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2346 a.recycle();
satok01038492012-04-09 21:08:27 +09002347 final LayoutInflater inflater =
2348 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2349 final View tv = inflater.inflate(
2350 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2351 mDialogBuilder.setCustomTitle(tv);
2352
2353 // Setup layout for a toggle switch of the hardware keyboard
2354 mSwitchingDialogTitleView = tv;
2355 mSwitchingDialogTitleView.findViewById(
2356 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2357 mWindowManagerService.isHardKeyboardAvailable() ?
2358 View.VISIBLE : View.GONE);
2359 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2360 com.android.internal.R.id.hard_keyboard_switch));
2361 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2362 hardKeySwitch.setOnCheckedChangeListener(
2363 new OnCheckedChangeListener() {
2364 @Override
2365 public void onCheckedChanged(
2366 CompoundButton buttonView, boolean isChecked) {
2367 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2368 }
2369 });
satokd87c2592010-09-29 11:52:06 +09002370
Ken Wakasa05dbb652011-08-22 15:22:43 +09002371 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2372 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2373 checkedItem);
2374
2375 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002376 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002377 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002378 public void onClick(DialogInterface dialog, int which) {
2379 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002380 if (mIms == null || mIms.length <= which
2381 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002382 return;
2383 }
2384 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002385 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002386 hideInputMethodMenu();
2387 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002388 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002389 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002390 subtypeId = NOT_A_SUBTYPE_ID;
2391 }
2392 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002393 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002396 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397
satokbc81b692011-08-26 16:22:22 +09002398 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002399 mDialogBuilder.setPositiveButton(
2400 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002401 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002402 @Override
satok7f35c8c2010-10-07 21:13:11 +09002403 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002404 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002405 }
2406 });
2407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002409 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 mSwitchingDialog.getWindow().setType(
2411 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002412 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 mSwitchingDialog.show();
2414 }
2415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002416
satok93d744d2012-05-09 17:14:08 +09002417 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002418 public final CharSequence mImeName;
2419 public final CharSequence mSubtypeName;
2420 public final InputMethodInfo mImi;
2421 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002422 private final boolean mIsSystemLocale;
2423 private final boolean mIsSystemLanguage;
2424
Ken Wakasa05dbb652011-08-22 15:22:43 +09002425 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002426 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002427 mImeName = imeName;
2428 mSubtypeName = subtypeName;
2429 mImi = imi;
2430 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002431 if (TextUtils.isEmpty(subtypeLocale)) {
2432 mIsSystemLocale = false;
2433 mIsSystemLanguage = false;
2434 } else {
2435 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2436 mIsSystemLanguage = mIsSystemLocale
2437 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2438 }
2439 }
2440
2441 @Override
2442 public int compareTo(ImeSubtypeListItem other) {
2443 if (TextUtils.isEmpty(mImeName)) {
2444 return 1;
2445 }
2446 if (TextUtils.isEmpty(other.mImeName)) {
2447 return -1;
2448 }
2449 if (!TextUtils.equals(mImeName, other.mImeName)) {
2450 return mImeName.toString().compareTo(other.mImeName.toString());
2451 }
2452 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2453 return 0;
2454 }
2455 if (mIsSystemLocale) {
2456 return -1;
2457 }
2458 if (other.mIsSystemLocale) {
2459 return 1;
2460 }
2461 if (mIsSystemLanguage) {
2462 return -1;
2463 }
2464 if (other.mIsSystemLanguage) {
2465 return 1;
2466 }
2467 if (TextUtils.isEmpty(mSubtypeName)) {
2468 return 1;
2469 }
2470 if (TextUtils.isEmpty(other.mSubtypeName)) {
2471 return -1;
2472 }
2473 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002474 }
2475 }
2476
2477 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2478 private final LayoutInflater mInflater;
2479 private final int mTextViewResourceId;
2480 private final List<ImeSubtypeListItem> mItemsList;
2481 private final int mCheckedItem;
2482 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2483 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2484 super(context, textViewResourceId, itemsList);
2485 mTextViewResourceId = textViewResourceId;
2486 mItemsList = itemsList;
2487 mCheckedItem = checkedItem;
2488 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2489 }
2490
2491 @Override
2492 public View getView(int position, View convertView, ViewGroup parent) {
2493 final View view = convertView != null ? convertView
2494 : mInflater.inflate(mTextViewResourceId, null);
2495 if (position < 0 || position >= mItemsList.size()) return view;
2496 final ImeSubtypeListItem item = mItemsList.get(position);
2497 final CharSequence imeName = item.mImeName;
2498 final CharSequence subtypeName = item.mSubtypeName;
2499 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2500 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2501 if (TextUtils.isEmpty(subtypeName)) {
2502 firstTextView.setText(imeName);
2503 secondTextView.setVisibility(View.GONE);
2504 } else {
2505 firstTextView.setText(subtypeName);
2506 secondTextView.setText(imeName);
2507 secondTextView.setVisibility(View.VISIBLE);
2508 }
2509 final RadioButton radioButton =
2510 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2511 radioButton.setChecked(position == mCheckedItem);
2512 return view;
2513 }
2514 }
2515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002517 synchronized (mMethodMap) {
2518 hideInputMethodMenuLocked();
2519 }
2520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002521
The Android Open Source Project10592532009-03-18 17:39:46 -07002522 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002523 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524
The Android Open Source Project10592532009-03-18 17:39:46 -07002525 if (mSwitchingDialog != null) {
2526 mSwitchingDialog.dismiss();
2527 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002529
The Android Open Source Project10592532009-03-18 17:39:46 -07002530 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002531 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002535
satok42c5a162011-05-26 16:46:14 +09002536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 public boolean setInputMethodEnabled(String id, boolean enabled) {
2538 synchronized (mMethodMap) {
2539 if (mContext.checkCallingOrSelfPermission(
2540 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2541 != PackageManager.PERMISSION_GRANTED) {
2542 throw new SecurityException(
2543 "Requires permission "
2544 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2545 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 long ident = Binder.clearCallingIdentity();
2548 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002549 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 } finally {
2551 Binder.restoreCallingIdentity(ident);
2552 }
2553 }
2554 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002555
2556 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2557 // Make sure this is a valid input method.
2558 InputMethodInfo imm = mMethodMap.get(id);
2559 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002560 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002561 }
2562
satokd87c2592010-09-29 11:52:06 +09002563 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2564 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002565
satokd87c2592010-09-29 11:52:06 +09002566 if (enabled) {
2567 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2568 if (pair.first.equals(id)) {
2569 // We are enabling this input method, but it is already enabled.
2570 // Nothing to do. The previous state was enabled.
2571 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002572 }
2573 }
satokd87c2592010-09-29 11:52:06 +09002574 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2575 // Previous state was disabled.
2576 return false;
2577 } else {
2578 StringBuilder builder = new StringBuilder();
2579 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2580 builder, enabledInputMethodsList, id)) {
2581 // Disabled input method is currently selected, switch to another one.
2582 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2583 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002584 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2585 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2586 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002587 }
2588 // Previous state was enabled.
2589 return true;
2590 } else {
2591 // We are disabling the input method but it is already disabled.
2592 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002593 return false;
2594 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002595 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002596 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002597
satok57ffc002011-01-25 00:11:47 +09002598 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2599 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002600 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002601 }
2602
satok723a27e2010-11-11 14:58:11 +09002603 private void saveCurrentInputMethodAndSubtypeToHistory() {
2604 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2605 if (mCurrentSubtype != null) {
2606 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2607 }
satok57ffc002011-01-25 00:11:47 +09002608 if (canAddToLastInputMethod(mCurrentSubtype)) {
2609 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2610 }
satokab751aa2010-09-14 19:17:36 +09002611 }
2612
satok723a27e2010-11-11 14:58:11 +09002613 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2614 boolean setSubtypeOnly) {
2615 // Update the history of InputMethod and Subtype
2616 saveCurrentInputMethodAndSubtypeToHistory();
2617
2618 // Set Subtype here
2619 if (imi == null || subtypeId < 0) {
2620 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002621 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002622 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002623 if (subtypeId < imi.getSubtypeCount()) {
2624 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2625 mSettings.putSelectedSubtype(subtype.hashCode());
2626 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002627 } else {
2628 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2629 mCurrentSubtype = null;
2630 }
satokab751aa2010-09-14 19:17:36 +09002631 }
satok723a27e2010-11-11 14:58:11 +09002632
2633 if (!setSubtypeOnly) {
2634 // Set InputMethod here
2635 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2636 }
2637 }
2638
2639 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2640 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2641 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2642 // newDefaultIme is empty when there is no candidate for the selected IME.
2643 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2644 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2645 if (subtypeHashCode != null) {
2646 try {
2647 lastSubtypeId = getSubtypeIdFromHashCode(
2648 imi, Integer.valueOf(subtypeHashCode));
2649 } catch (NumberFormatException e) {
2650 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2651 }
2652 }
2653 }
2654 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002655 }
2656
2657 private int getSelectedInputMethodSubtypeId(String id) {
2658 InputMethodInfo imi = mMethodMap.get(id);
2659 if (imi == null) {
2660 return NOT_A_SUBTYPE_ID;
2661 }
satokab751aa2010-09-14 19:17:36 +09002662 int subtypeId;
2663 try {
2664 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2665 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2666 } catch (SettingNotFoundException e) {
2667 return NOT_A_SUBTYPE_ID;
2668 }
satok723a27e2010-11-11 14:58:11 +09002669 return getSubtypeIdFromHashCode(imi, subtypeId);
2670 }
2671
satokfdf419e2012-05-08 16:52:08 +09002672 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2673 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2674 }
2675
2676 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002677 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002678 final int subtypeCount = imi.getSubtypeCount();
2679 for (int i = 0; i < subtypeCount; ++i) {
2680 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002681 if (subtypeHashCode == ims.hashCode()) {
2682 return i;
2683 }
satokab751aa2010-09-14 19:17:36 +09002684 }
2685 }
2686 return NOT_A_SUBTYPE_ID;
2687 }
2688
satoka86f5e42011-09-02 17:12:42 +09002689 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2690 Resources res, InputMethodInfo imi) {
2691 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002692 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002693 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002694 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002695 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002696 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002697 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002698 // scan overriding implicitly enabled subtypes.
2699 InputMethodSubtype subtype = subtypes.get(i);
2700 if (subtype.overridesImplicitlyEnabledSubtype()) {
2701 final String mode = subtype.getMode();
2702 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2703 applicableModeAndSubtypesMap.put(mode, subtype);
2704 }
2705 }
2706 }
2707 if (applicableModeAndSubtypesMap.size() > 0) {
2708 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2709 }
2710 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002711 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002712 final String locale = subtype.getLocale();
2713 final String mode = subtype.getMode();
2714 // When system locale starts with subtype's locale, that subtype will be applicable
2715 // for system locale
2716 // For instance, it's clearly applicable for cases like system locale = en_US and
2717 // subtype = en, but it is not necessarily considered applicable for cases like system
2718 // locale = en and subtype = en_US.
2719 // We just call systemLocale.startsWith(locale) in this function because there is no
2720 // need to find applicable subtypes aggressively unlike
2721 // findLastResortApplicableSubtypeLocked.
2722 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002723 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002724 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002725 if (applicableSubtype != null) {
2726 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2727 if (!systemLocale.equals(locale)) continue;
2728 }
satok3da92232011-01-11 22:46:30 +09002729 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002730 }
2731 }
2732 final InputMethodSubtype keyboardSubtype
2733 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2734 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2735 applicableModeAndSubtypesMap.values());
2736 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2737 for (int i = 0; i < N; ++i) {
2738 final InputMethodSubtype subtype = subtypes.get(i);
2739 final String mode = subtype.getMode();
2740 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2741 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2742 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002743 }
2744 }
2745 }
satokc3690562012-01-10 20:14:43 +09002746 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002747 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002748 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002749 if (lastResortKeyboardSubtype != null) {
2750 applicableSubtypes.add(lastResortKeyboardSubtype);
2751 }
2752 }
2753 return applicableSubtypes;
2754 }
2755
satok4e4569d2010-11-19 18:45:53 +09002756 /**
2757 * If there are no selected subtypes, tries finding the most applicable one according to the
2758 * given locale.
2759 * @param subtypes this function will search the most applicable subtype in subtypes
2760 * @param mode subtypes will be filtered by mode
2761 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002762 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2763 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002764 * @return the most applicable subtypeId
2765 */
satokdf31ae62011-01-15 06:19:44 +09002766 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2767 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002768 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002769 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002770 return null;
satok8fbb1e82010-11-02 23:15:58 +09002771 }
satok4e4569d2010-11-19 18:45:53 +09002772 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002773 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002774 }
satok8fbb1e82010-11-02 23:15:58 +09002775 final String language = locale.substring(0, 2);
2776 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002777 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002778 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002779 final int N = subtypes.size();
2780 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002781 InputMethodSubtype subtype = subtypes.get(i);
2782 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002783 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2784 // and all subtypes with all modes can be candidates.
2785 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002786 if (firstMatchedModeSubtype == null) {
2787 firstMatchedModeSubtype = subtype;
2788 }
satok9ef02832010-11-04 21:17:48 +09002789 if (locale.equals(subtypeLocale)) {
2790 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002791 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002792 break;
2793 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2794 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002795 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002796 partialMatchFound = true;
2797 }
satok8fbb1e82010-11-02 23:15:58 +09002798 }
2799 }
2800
satok7599a7f2010-12-22 13:45:23 +09002801 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2802 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002803 }
2804
satok8fbb1e82010-11-02 23:15:58 +09002805 // The first subtype applicable to the system locale will be defined as the most applicable
2806 // subtype.
2807 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002808 if (applicableSubtype != null) {
2809 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2810 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2811 }
satok8fbb1e82010-11-02 23:15:58 +09002812 }
satokcd7cd292010-11-20 15:46:23 +09002813 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002814 }
2815
satok4e4569d2010-11-19 18:45:53 +09002816 // If there are no selected shortcuts, tries finding the most applicable ones.
2817 private Pair<InputMethodInfo, InputMethodSubtype>
2818 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2819 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2820 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002821 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002822 boolean foundInSystemIME = false;
2823
2824 // Search applicable subtype for each InputMethodInfo
2825 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002826 final String imiId = imi.getId();
2827 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2828 continue;
2829 }
satokcd7cd292010-11-20 15:46:23 +09002830 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002831 final List<InputMethodSubtype> enabledSubtypes =
2832 getEnabledInputMethodSubtypeList(imi, true);
2833 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002834 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002835 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002836 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002837 }
satokdf31ae62011-01-15 06:19:44 +09002838 // 2. Search by the system locale from enabledSubtypes.
2839 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002840 if (subtype == null) {
2841 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002842 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002843 }
satoka86f5e42011-09-02 17:12:42 +09002844 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2845 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2846 final ArrayList<InputMethodSubtype> subtypesForSearch =
2847 overridingImplicitlyEnabledSubtypes.isEmpty()
2848 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002849 // 4. Search by the current subtype's locale from all subtypes.
2850 if (subtype == null && mCurrentSubtype != null) {
2851 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002852 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002853 }
2854 // 5. Search by the system locale from all subtypes.
2855 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002856 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002857 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002858 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002859 }
satokcd7cd292010-11-20 15:46:23 +09002860 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002861 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002862 // The current input method is the most applicable IME.
2863 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002864 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002865 break;
satok7599a7f2010-12-22 13:45:23 +09002866 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002867 // The system input method is 2nd applicable IME.
2868 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002869 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002870 if ((imi.getServiceInfo().applicationInfo.flags
2871 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2872 foundInSystemIME = true;
2873 }
satok4e4569d2010-11-19 18:45:53 +09002874 }
2875 }
2876 }
2877 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002878 if (mostApplicableIMI != null) {
2879 Slog.w(TAG, "Most applicable shortcut input method was:"
2880 + mostApplicableIMI.getId());
2881 if (mostApplicableSubtype != null) {
2882 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2883 + "," + mostApplicableSubtype.getMode() + ","
2884 + mostApplicableSubtype.getLocale());
2885 }
2886 }
satok4e4569d2010-11-19 18:45:53 +09002887 }
satokcd7cd292010-11-20 15:46:23 +09002888 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002889 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002890 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002891 } else {
2892 return null;
2893 }
2894 }
2895
satokab751aa2010-09-14 19:17:36 +09002896 /**
2897 * @return Return the current subtype of this input method.
2898 */
satok42c5a162011-05-26 16:46:14 +09002899 @Override
satokab751aa2010-09-14 19:17:36 +09002900 public InputMethodSubtype getCurrentInputMethodSubtype() {
satokfdf419e2012-05-08 16:52:08 +09002901 if (mCurMethodId == null) {
2902 return null;
2903 }
satok4e4569d2010-11-19 18:45:53 +09002904 boolean subtypeIsSelected = false;
2905 try {
2906 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2907 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2908 } catch (SettingNotFoundException e) {
2909 }
satok3ef8b292010-11-23 06:06:29 +09002910 synchronized (mMethodMap) {
satokfdf419e2012-05-08 16:52:08 +09002911 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2912 if (imi == null || imi.getSubtypeCount() == 0) {
2913 return null;
2914 }
2915 if (!subtypeIsSelected || mCurrentSubtype == null
2916 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2917 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
satok3ef8b292010-11-23 06:06:29 +09002918 if (subtypeId == NOT_A_SUBTYPE_ID) {
satokfdf419e2012-05-08 16:52:08 +09002919 // If there are no selected subtypes, the framework will try to find
2920 // the most applicable subtype from explicitly or implicitly enabled
2921 // subtypes.
2922 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2923 getEnabledInputMethodSubtypeList(imi, true);
2924 // If there is only one explicitly or implicitly enabled subtype,
2925 // just returns it.
2926 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2927 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2928 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2929 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2930 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2931 SUBTYPE_MODE_KEYBOARD, null, true);
2932 if (mCurrentSubtype == null) {
satokd8713432011-01-18 00:55:13 +09002933 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
satokfdf419e2012-05-08 16:52:08 +09002934 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2935 true);
satokd8713432011-01-18 00:55:13 +09002936 }
satok4e4569d2010-11-19 18:45:53 +09002937 }
satokcd7cd292010-11-20 15:46:23 +09002938 } else {
satokfdf419e2012-05-08 16:52:08 +09002939 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002940 }
satok8fbb1e82010-11-02 23:15:58 +09002941 }
satok3ef8b292010-11-23 06:06:29 +09002942 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002943 }
satokab751aa2010-09-14 19:17:36 +09002944 }
2945
satokf3db1af2010-11-23 13:34:33 +09002946 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2947 InputMethodSubtype subtype) {
2948 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2949 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2950 } else {
2951 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2952 subtypes.add(subtype);
2953 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2954 }
2955 }
2956
satok4e4569d2010-11-19 18:45:53 +09002957 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002958 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002959 @Override
satok4e4569d2010-11-19 18:45:53 +09002960 public List getShortcutInputMethodsAndSubtypes() {
2961 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002962 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002963 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002964 // If there are no selected shortcut subtypes, the framework will try to find
2965 // the most applicable subtype from all subtypes whose mode is
2966 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002967 Pair<InputMethodInfo, InputMethodSubtype> info =
2968 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2969 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002970 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002971 ret.add(info.first);
2972 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002973 }
satok3da92232011-01-11 22:46:30 +09002974 return ret;
satokf3db1af2010-11-23 13:34:33 +09002975 }
satokf3db1af2010-11-23 13:34:33 +09002976 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2977 ret.add(imi);
2978 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2979 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002980 }
2981 }
satokf3db1af2010-11-23 13:34:33 +09002982 return ret;
satok4e4569d2010-11-19 18:45:53 +09002983 }
2984 }
2985
satok42c5a162011-05-26 16:46:14 +09002986 @Override
satokb66d2872010-11-10 01:04:04 +09002987 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2988 synchronized (mMethodMap) {
2989 if (subtype != null && mCurMethodId != null) {
2990 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2991 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2992 if (subtypeId != NOT_A_SUBTYPE_ID) {
2993 setInputMethodLocked(mCurMethodId, subtypeId);
2994 return true;
2995 }
2996 }
2997 return false;
2998 }
2999 }
3000
satok688bd472012-02-09 20:09:17 +09003001 private static class InputMethodAndSubtypeListManager {
3002 private final Context mContext;
3003 private final PackageManager mPm;
3004 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003005 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003006 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3007 mContext = context;
3008 mPm = context.getPackageManager();
3009 mImms = imms;
satok93d744d2012-05-09 17:14:08 +09003010 mSystemLocaleStr =
3011 imms.mLastSystemLocale != null ? imms.mLastSystemLocale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003012 }
3013
3014 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3015 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3016 new Comparator<InputMethodInfo>() {
3017 @Override
3018 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3019 if (imi2 == null) return 0;
3020 if (imi1 == null) return 1;
3021 if (mPm == null) {
3022 return imi1.getId().compareTo(imi2.getId());
3023 }
3024 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3025 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3026 return imiId1.toString().compareTo(imiId2.toString());
3027 }
3028 });
3029
3030 public ImeSubtypeListItem getNextInputMethod(
3031 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3032 if (imi == null) {
3033 return null;
3034 }
3035 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3036 if (imList.size() <= 1) {
3037 return null;
3038 }
3039 final int N = imList.size();
3040 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003041 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003042 : NOT_A_SUBTYPE_ID;
3043 for (int i = 0; i < N; ++i) {
3044 final ImeSubtypeListItem isli = imList.get(i);
3045 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3046 if (!onlyCurrentIme) {
3047 return imList.get((i + 1) % N);
3048 }
3049 for (int j = 0; j < N - 1; ++j) {
3050 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3051 if (candidate.mImi.equals(imi)) {
3052 return candidate;
3053 }
3054 }
3055 return null;
3056 }
3057 }
3058 return null;
3059 }
3060
3061 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3062 return getSortedInputMethodAndSubtypeList(true, false, false);
3063 }
3064
3065 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3066 boolean inputShown, boolean isScreenLocked) {
3067 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3068 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3069 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3070 if (immis == null || immis.size() == 0) {
3071 return Collections.emptyList();
3072 }
3073 mSortedImmis.clear();
3074 mSortedImmis.putAll(immis);
3075 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3076 if (imi == null) continue;
3077 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3078 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3079 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3080 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3081 }
3082 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3083 final CharSequence imeLabel = imi.loadLabel(mPm);
3084 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3085 final int subtypeCount = imi.getSubtypeCount();
3086 if (DEBUG) {
3087 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3088 }
3089 for (int j = 0; j < subtypeCount; ++j) {
3090 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3091 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3092 // We show all enabled IMEs and subtypes when an IME is shown.
3093 if (enabledSubtypeSet.contains(subtypeHashCode)
3094 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3095 final CharSequence subtypeLabel =
3096 subtype.overridesImplicitlyEnabledSubtype() ? null
3097 : subtype.getDisplayName(mContext, imi.getPackageName(),
3098 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003099 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3100 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003101
3102 // Removing this subtype from enabledSubtypeSet because we no longer
3103 // need to add an entry of this subtype to imList to avoid duplicated
3104 // entries.
3105 enabledSubtypeSet.remove(subtypeHashCode);
3106 }
3107 }
3108 } else {
satok93d744d2012-05-09 17:14:08 +09003109 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3110 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003111 }
3112 }
satok93d744d2012-05-09 17:14:08 +09003113 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003114 return imList;
3115 }
3116 }
3117
satokd87c2592010-09-29 11:52:06 +09003118 /**
3119 * Utility class for putting and getting settings for InputMethod
3120 * TODO: Move all putters and getters of settings to this class.
3121 */
3122 private static class InputMethodSettings {
3123 // The string for enabled input method is saved as follows:
3124 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3125 private static final char INPUT_METHOD_SEPARATER = ':';
3126 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003127 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003128 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3129
satok723a27e2010-11-11 14:58:11 +09003130 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003131 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3132
satokdf31ae62011-01-15 06:19:44 +09003133 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003134 private final ContentResolver mResolver;
3135 private final HashMap<String, InputMethodInfo> mMethodMap;
3136 private final ArrayList<InputMethodInfo> mMethodList;
3137
3138 private String mEnabledInputMethodsStrCache;
3139
3140 private static void buildEnabledInputMethodsSettingString(
3141 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3142 String id = pair.first;
3143 ArrayList<String> subtypes = pair.second;
3144 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003145 // Inputmethod and subtypes are saved in the settings as follows:
3146 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3147 for (String subtypeId: subtypes) {
3148 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003149 }
3150 }
3151
3152 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003153 Resources res, ContentResolver resolver,
3154 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3155 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003156 mResolver = resolver;
3157 mMethodMap = methodMap;
3158 mMethodList = methodList;
3159 }
3160
3161 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3162 return createEnabledInputMethodListLocked(
3163 getEnabledInputMethodsAndSubtypeListLocked());
3164 }
3165
satok7f35c8c2010-10-07 21:13:11 +09003166 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003167 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3168 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003169 getEnabledInputMethodsAndSubtypeListLocked());
3170 }
3171
satok67ddf9c2010-11-17 09:45:54 +09003172 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3173 InputMethodInfo imi) {
3174 List<Pair<String, ArrayList<String>>> imsList =
3175 getEnabledInputMethodsAndSubtypeListLocked();
3176 ArrayList<InputMethodSubtype> enabledSubtypes =
3177 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003178 if (imi != null) {
3179 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3180 InputMethodInfo info = mMethodMap.get(imsPair.first);
3181 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003182 final int subtypeCount = info.getSubtypeCount();
3183 for (int i = 0; i < subtypeCount; ++i) {
3184 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003185 for (String s: imsPair.second) {
3186 if (String.valueOf(ims.hashCode()).equals(s)) {
3187 enabledSubtypes.add(ims);
3188 }
satok67ddf9c2010-11-17 09:45:54 +09003189 }
3190 }
satok884ef9a2010-11-18 10:39:46 +09003191 break;
satok67ddf9c2010-11-17 09:45:54 +09003192 }
satok67ddf9c2010-11-17 09:45:54 +09003193 }
3194 }
3195 return enabledSubtypes;
3196 }
3197
satokd87c2592010-09-29 11:52:06 +09003198 // At the initial boot, the settings for input methods are not set,
3199 // so we need to enable IME in that case.
3200 public void enableAllIMEsIfThereIsNoEnabledIME() {
3201 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3202 StringBuilder sb = new StringBuilder();
3203 final int N = mMethodList.size();
3204 for (int i = 0; i < N; i++) {
3205 InputMethodInfo imi = mMethodList.get(i);
3206 Slog.i(TAG, "Adding: " + imi.getId());
3207 if (i > 0) sb.append(':');
3208 sb.append(imi.getId());
3209 }
3210 putEnabledInputMethodsStr(sb.toString());
3211 }
3212 }
3213
satokbb4aa062011-01-19 21:40:27 +09003214 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003215 ArrayList<Pair<String, ArrayList<String>>> imsList
3216 = new ArrayList<Pair<String, ArrayList<String>>>();
3217 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3218 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3219 return imsList;
3220 }
satok723a27e2010-11-11 14:58:11 +09003221 mInputMethodSplitter.setString(enabledInputMethodsStr);
3222 while (mInputMethodSplitter.hasNext()) {
3223 String nextImsStr = mInputMethodSplitter.next();
3224 mSubtypeSplitter.setString(nextImsStr);
3225 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003226 ArrayList<String> subtypeHashes = new ArrayList<String>();
3227 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003228 String imeId = mSubtypeSplitter.next();
3229 while (mSubtypeSplitter.hasNext()) {
3230 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003231 }
3232 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3233 }
3234 }
3235 return imsList;
3236 }
3237
3238 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3239 if (reloadInputMethodStr) {
3240 getEnabledInputMethodsStr();
3241 }
3242 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3243 // Add in the newly enabled input method.
3244 putEnabledInputMethodsStr(id);
3245 } else {
3246 putEnabledInputMethodsStr(
3247 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3248 }
3249 }
3250
3251 /**
3252 * Build and put a string of EnabledInputMethods with removing specified Id.
3253 * @return the specified id was removed or not.
3254 */
3255 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3256 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3257 boolean isRemoved = false;
3258 boolean needsAppendSeparator = false;
3259 for (Pair<String, ArrayList<String>> ims: imsList) {
3260 String curId = ims.first;
3261 if (curId.equals(id)) {
3262 // We are disabling this input method, and it is
3263 // currently enabled. Skip it to remove from the
3264 // new list.
3265 isRemoved = true;
3266 } else {
3267 if (needsAppendSeparator) {
3268 builder.append(INPUT_METHOD_SEPARATER);
3269 } else {
3270 needsAppendSeparator = true;
3271 }
3272 buildEnabledInputMethodsSettingString(builder, ims);
3273 }
3274 }
3275 if (isRemoved) {
3276 // Update the setting with the new list of input methods.
3277 putEnabledInputMethodsStr(builder.toString());
3278 }
3279 return isRemoved;
3280 }
3281
3282 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3283 List<Pair<String, ArrayList<String>>> imsList) {
3284 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3285 for (Pair<String, ArrayList<String>> ims: imsList) {
3286 InputMethodInfo info = mMethodMap.get(ims.first);
3287 if (info != null) {
3288 res.add(info);
3289 }
3290 }
3291 return res;
3292 }
3293
satok7f35c8c2010-10-07 21:13:11 +09003294 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003295 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003296 List<Pair<String, ArrayList<String>>> imsList) {
3297 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3298 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3299 for (Pair<String, ArrayList<String>> ims : imsList) {
3300 InputMethodInfo info = mMethodMap.get(ims.first);
3301 if (info != null) {
3302 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3303 }
3304 }
3305 return res;
3306 }
3307
satokd87c2592010-09-29 11:52:06 +09003308 private void putEnabledInputMethodsStr(String str) {
3309 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3310 mEnabledInputMethodsStrCache = str;
3311 }
3312
3313 private String getEnabledInputMethodsStr() {
3314 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3315 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003316 if (DEBUG) {
3317 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3318 }
satokd87c2592010-09-29 11:52:06 +09003319 return mEnabledInputMethodsStrCache;
3320 }
satok723a27e2010-11-11 14:58:11 +09003321
3322 private void saveSubtypeHistory(
3323 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3324 StringBuilder builder = new StringBuilder();
3325 boolean isImeAdded = false;
3326 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3327 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3328 newSubtypeId);
3329 isImeAdded = true;
3330 }
3331 for (Pair<String, String> ime: savedImes) {
3332 String imeId = ime.first;
3333 String subtypeId = ime.second;
3334 if (TextUtils.isEmpty(subtypeId)) {
3335 subtypeId = NOT_A_SUBTYPE_ID_STR;
3336 }
3337 if (isImeAdded) {
3338 builder.append(INPUT_METHOD_SEPARATER);
3339 } else {
3340 isImeAdded = true;
3341 }
3342 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3343 subtypeId);
3344 }
3345 // Remove the last INPUT_METHOD_SEPARATER
3346 putSubtypeHistoryStr(builder.toString());
3347 }
3348
3349 public void addSubtypeToHistory(String imeId, String subtypeId) {
3350 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3351 for (Pair<String, String> ime: subtypeHistory) {
3352 if (ime.first.equals(imeId)) {
3353 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003354 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003355 + ime.second);
3356 }
3357 // We should break here
3358 subtypeHistory.remove(ime);
3359 break;
3360 }
3361 }
satokbb4aa062011-01-19 21:40:27 +09003362 if (DEBUG) {
3363 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3364 }
satok723a27e2010-11-11 14:58:11 +09003365 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3366 }
3367
3368 private void putSubtypeHistoryStr(String str) {
3369 if (DEBUG) {
3370 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3371 }
3372 Settings.Secure.putString(
3373 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3374 }
3375
3376 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3377 // Gets the first one from the history
3378 return getLastSubtypeForInputMethodLockedInternal(null);
3379 }
3380
3381 public String getLastSubtypeForInputMethodLocked(String imeId) {
3382 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3383 if (ime != null) {
3384 return ime.second;
3385 } else {
3386 return null;
3387 }
3388 }
3389
3390 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3391 List<Pair<String, ArrayList<String>>> enabledImes =
3392 getEnabledInputMethodsAndSubtypeListLocked();
3393 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003394 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003395 final String imeInTheHistory = imeAndSubtype.first;
3396 // If imeId is empty, returns the first IME and subtype in the history
3397 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3398 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003399 final String subtypeHashCode =
3400 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3401 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003402 if (!TextUtils.isEmpty(subtypeHashCode)) {
3403 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003404 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003405 }
3406 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3407 }
3408 }
3409 }
3410 if (DEBUG) {
3411 Slog.d(TAG, "No enabled IME found in the history");
3412 }
3413 return null;
3414 }
3415
satokdf31ae62011-01-15 06:19:44 +09003416 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003417 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3418 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3419 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003420 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003421 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003422 if (explicitlyEnabledSubtypes.size() == 0) {
3423 // If there are no explicitly enabled subtypes, applicable subtypes are
3424 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003425 // If IME is enabled and no subtypes are enabled, applicable subtypes
3426 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003427 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003428 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003429 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003430 if (implicitlySelectedSubtypes != null) {
3431 final int N = implicitlySelectedSubtypes.size();
3432 for (int i = 0; i < N; ++i) {
3433 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3434 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3435 return subtypeHashCode;
3436 }
3437 }
3438 }
3439 }
3440 } else {
satokf6cafb62011-01-17 16:29:02 +09003441 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003442 if (s.equals(subtypeHashCode)) {
3443 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003444 try {
3445 final int hashCode = Integer.valueOf(subtypeHashCode);
3446 // Check whether the subtype id is valid or not
3447 if (isValidSubtypeId(imi, hashCode)) {
3448 return s;
3449 } else {
3450 return NOT_A_SUBTYPE_ID_STR;
3451 }
3452 } catch (NumberFormatException e) {
3453 return NOT_A_SUBTYPE_ID_STR;
3454 }
satokdf31ae62011-01-15 06:19:44 +09003455 }
satok723a27e2010-11-11 14:58:11 +09003456 }
3457 }
3458 // If imeId was enabled but subtypeId was disabled.
3459 return NOT_A_SUBTYPE_ID_STR;
3460 }
3461 }
3462 // If both imeId and subtypeId are disabled, return null
3463 return null;
3464 }
3465
3466 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3467 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3468 final String subtypeHistoryStr = getSubtypeHistoryStr();
3469 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3470 return imsList;
3471 }
3472 mInputMethodSplitter.setString(subtypeHistoryStr);
3473 while (mInputMethodSplitter.hasNext()) {
3474 String nextImsStr = mInputMethodSplitter.next();
3475 mSubtypeSplitter.setString(nextImsStr);
3476 if (mSubtypeSplitter.hasNext()) {
3477 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3478 // The first element is ime id.
3479 String imeId = mSubtypeSplitter.next();
3480 while (mSubtypeSplitter.hasNext()) {
3481 subtypeId = mSubtypeSplitter.next();
3482 break;
3483 }
3484 imsList.add(new Pair<String, String>(imeId, subtypeId));
3485 }
3486 }
3487 return imsList;
3488 }
3489
3490 private String getSubtypeHistoryStr() {
3491 if (DEBUG) {
3492 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3493 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3494 }
3495 return Settings.Secure.getString(
3496 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3497 }
3498
3499 public void putSelectedInputMethod(String imeId) {
3500 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3501 }
3502
3503 public void putSelectedSubtype(int subtypeId) {
3504 Settings.Secure.putInt(
3505 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3506 }
satokd87c2592010-09-29 11:52:06 +09003507 }
3508
satoke7c6998e2011-06-03 17:57:59 +09003509 private static class InputMethodFileManager {
3510 private static final String SYSTEM_PATH = "system";
3511 private static final String INPUT_METHOD_PATH = "inputmethod";
3512 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3513 private static final String NODE_SUBTYPES = "subtypes";
3514 private static final String NODE_SUBTYPE = "subtype";
3515 private static final String NODE_IMI = "imi";
3516 private static final String ATTR_ID = "id";
3517 private static final String ATTR_LABEL = "label";
3518 private static final String ATTR_ICON = "icon";
3519 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3520 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3521 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3522 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3523 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3524 private final HashMap<String, InputMethodInfo> mMethodMap;
3525 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3526 new HashMap<String, List<InputMethodSubtype>>();
3527 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3528 if (methodMap == null) {
3529 throw new NullPointerException("methodMap is null");
3530 }
3531 mMethodMap = methodMap;
3532 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3533 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3534 if (!inputMethodDir.mkdirs()) {
3535 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3536 }
3537 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3538 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3539 if (!subtypeFile.exists()) {
3540 // If "subtypes.xml" doesn't exist, create a blank file.
3541 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3542 methodMap);
3543 } else {
3544 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3545 }
3546 }
3547
3548 private void deleteAllInputMethodSubtypes(String imiId) {
3549 synchronized (mMethodMap) {
3550 mSubtypesMap.remove(imiId);
3551 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3552 mMethodMap);
3553 }
3554 }
3555
3556 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003557 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003558 synchronized (mMethodMap) {
3559 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3560 final int N = additionalSubtypes.length;
3561 for (int i = 0; i < N; ++i) {
3562 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003563 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003564 subtypes.add(subtype);
3565 }
3566 }
satok4a28bde2011-06-29 21:03:40 +09003567 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003568 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3569 mMethodMap);
3570 }
3571 }
3572
3573 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3574 synchronized (mMethodMap) {
3575 return mSubtypesMap;
3576 }
3577 }
3578
3579 private static void writeAdditionalInputMethodSubtypes(
3580 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3581 HashMap<String, InputMethodInfo> methodMap) {
3582 // Safety net for the case that this function is called before methodMap is set.
3583 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3584 FileOutputStream fos = null;
3585 try {
3586 fos = subtypesFile.startWrite();
3587 final XmlSerializer out = new FastXmlSerializer();
3588 out.setOutput(fos, "utf-8");
3589 out.startDocument(null, true);
3590 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3591 out.startTag(null, NODE_SUBTYPES);
3592 for (String imiId : allSubtypes.keySet()) {
3593 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3594 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3595 continue;
3596 }
3597 out.startTag(null, NODE_IMI);
3598 out.attribute(null, ATTR_ID, imiId);
3599 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3600 final int N = subtypesList.size();
3601 for (int i = 0; i < N; ++i) {
3602 final InputMethodSubtype subtype = subtypesList.get(i);
3603 out.startTag(null, NODE_SUBTYPE);
3604 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3605 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3606 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3607 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3608 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3609 out.attribute(null, ATTR_IS_AUXILIARY,
3610 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3611 out.endTag(null, NODE_SUBTYPE);
3612 }
3613 out.endTag(null, NODE_IMI);
3614 }
3615 out.endTag(null, NODE_SUBTYPES);
3616 out.endDocument();
3617 subtypesFile.finishWrite(fos);
3618 } catch (java.io.IOException e) {
3619 Slog.w(TAG, "Error writing subtypes", e);
3620 if (fos != null) {
3621 subtypesFile.failWrite(fos);
3622 }
3623 }
3624 }
3625
3626 private static void readAdditionalInputMethodSubtypes(
3627 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3628 if (allSubtypes == null || subtypesFile == null) return;
3629 allSubtypes.clear();
3630 FileInputStream fis = null;
3631 try {
3632 fis = subtypesFile.openRead();
3633 final XmlPullParser parser = Xml.newPullParser();
3634 parser.setInput(fis, null);
3635 int type = parser.getEventType();
3636 // Skip parsing until START_TAG
3637 while ((type = parser.next()) != XmlPullParser.START_TAG
3638 && type != XmlPullParser.END_DOCUMENT) {}
3639 String firstNodeName = parser.getName();
3640 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3641 throw new XmlPullParserException("Xml doesn't start with subtypes");
3642 }
3643 final int depth =parser.getDepth();
3644 String currentImiId = null;
3645 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3646 while (((type = parser.next()) != XmlPullParser.END_TAG
3647 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3648 if (type != XmlPullParser.START_TAG)
3649 continue;
3650 final String nodeName = parser.getName();
3651 if (NODE_IMI.equals(nodeName)) {
3652 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3653 if (TextUtils.isEmpty(currentImiId)) {
3654 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3655 continue;
3656 }
3657 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3658 allSubtypes.put(currentImiId, tempSubtypesArray);
3659 } else if (NODE_SUBTYPE.equals(nodeName)) {
3660 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3661 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3662 continue;
3663 }
3664 final int icon = Integer.valueOf(
3665 parser.getAttributeValue(null, ATTR_ICON));
3666 final int label = Integer.valueOf(
3667 parser.getAttributeValue(null, ATTR_LABEL));
3668 final String imeSubtypeLocale =
3669 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3670 final String imeSubtypeMode =
3671 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3672 final String imeSubtypeExtraValue =
3673 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003674 final boolean isAuxiliary = "1".equals(String.valueOf(
3675 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003676 final InputMethodSubtype subtype =
3677 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3678 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3679 tempSubtypesArray.add(subtype);
3680 }
3681 }
3682 } catch (XmlPullParserException e) {
3683 Slog.w(TAG, "Error reading subtypes: " + e);
3684 return;
3685 } catch (java.io.IOException e) {
3686 Slog.w(TAG, "Error reading subtypes: " + e);
3687 return;
3688 } catch (NumberFormatException e) {
3689 Slog.w(TAG, "Error reading subtypes: " + e);
3690 return;
3691 } finally {
3692 if (fis != null) {
3693 try {
3694 fis.close();
3695 } catch (java.io.IOException e1) {
3696 Slog.w(TAG, "Failed to close.");
3697 }
3698 }
3699 }
3700 }
3701 }
3702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 @Override
3706 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3707 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3708 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3711 + Binder.getCallingPid()
3712 + ", uid=" + Binder.getCallingUid());
3713 return;
3714 }
3715
3716 IInputMethod method;
3717 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 synchronized (mMethodMap) {
3722 p.println("Current Input Method Manager state:");
3723 int N = mMethodList.size();
3724 p.println(" Input Methods:");
3725 for (int i=0; i<N; i++) {
3726 InputMethodInfo info = mMethodList.get(i);
3727 p.println(" InputMethod #" + i + ":");
3728 info.dump(p, " ");
3729 }
3730 p.println(" Clients:");
3731 for (ClientState ci : mClients.values()) {
3732 p.println(" Client " + ci + ":");
3733 p.println(" client=" + ci.client);
3734 p.println(" inputContext=" + ci.inputContext);
3735 p.println(" sessionRequested=" + ci.sessionRequested);
3736 p.println(" curSession=" + ci.curSession);
3737 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003738 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003740 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3741 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3743 + " mBoundToMethod=" + mBoundToMethod);
3744 p.println(" mCurToken=" + mCurToken);
3745 p.println(" mCurIntent=" + mCurIntent);
3746 method = mCurMethod;
3747 p.println(" mCurMethod=" + mCurMethod);
3748 p.println(" mEnabledSession=" + mEnabledSession);
3749 p.println(" mShowRequested=" + mShowRequested
3750 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3751 + " mShowForced=" + mShowForced
3752 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003753 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003755
Jeff Brownb88102f2010-09-08 11:49:43 -07003756 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 pw.flush();
3759 try {
3760 client.client.asBinder().dump(fd, args);
3761 } catch (RemoteException e) {
3762 p.println("Input method client dead: " + e);
3763 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003764 } else {
3765 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003767
Jeff Brownb88102f2010-09-08 11:49:43 -07003768 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 pw.flush();
3771 try {
3772 method.asBinder().dump(fd, args);
3773 } catch (RemoteException e) {
3774 p.println("Input method service dead: " + e);
3775 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003776 } else {
3777 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 }
3779 }
3780}