blob: 146bc944724da786310cd8c6a5e7d6dad80ca033 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
satoke7c6998e2011-06-03 17:57:59 +090031import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090033import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35import android.app.ActivityManagerNative;
36import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090037import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090038import android.app.Notification;
39import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070040import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.DialogInterface.OnCancelListener;
46import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090047import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070054import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.Resources;
56import android.content.res.TypedArray;
57import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080058import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
63import android.os.IInterface;
64import android.os.Message;
65import android.os.Parcel;
66import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080067import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090072import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090074import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090076import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090077import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.PrintWriterPrinter;
79import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090080import android.util.Slog;
81import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090083import android.view.LayoutInflater;
84import android.view.View;
85import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090087import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.inputmethod.InputBinding;
89import android.view.inputmethod.InputMethod;
90import android.view.inputmethod.InputMethodInfo;
91import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090092import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090093import android.widget.ArrayAdapter;
94import android.widget.RadioButton;
95import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
satoke7c6998e2011-06-03 17:57:59 +090097import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090099import java.io.FileInputStream;
100import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.IOException;
102import java.io.PrintWriter;
103import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +0900104import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900106import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.util.List;
satok913a8922010-08-26 21:53:41 +0900108import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110/**
111 * This class provides a system service that manages input methods.
112 */
113public class InputMethodManagerService extends IInputMethodManager.Stub
114 implements ServiceConnection, Handler.Callback {
115 static final boolean DEBUG = false;
116 static final String TAG = "InputManagerService";
117
118 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900119 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900120 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900121 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static final int MSG_UNBIND_INPUT = 1000;
124 static final int MSG_BIND_INPUT = 1010;
125 static final int MSG_SHOW_SOFT_INPUT = 1020;
126 static final int MSG_HIDE_SOFT_INPUT = 1030;
127 static final int MSG_ATTACH_TOKEN = 1040;
128 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_START_INPUT = 2000;
131 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 static final int MSG_UNBIND_METHOD = 3000;
134 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
satokf9f01002011-05-19 21:31:50 +0900138 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
139
satokab751aa2010-09-14 19:17:36 +0900140 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900141 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900142 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
143 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900144 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800147 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900149 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final IWindowManager mIWindowManager;
152 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900153 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // All known input methods. mMethodMap also serves as the global
158 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900159 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
160 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900161 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
162 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
satok7cfc0ed2011-06-20 21:29:36 +0900164 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700165 private NotificationManager mNotificationManager;
166 private KeyguardManager mKeyguardManager;
167 private StatusBarManagerService mStatusBar;
168 private Notification mImeSwitcherNotification;
169 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900170 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900171 private boolean mNotificationShown;
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 class SessionState {
174 final ClientState client;
175 final IInputMethod method;
176 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
179 public String toString() {
180 return "SessionState{uid " + client.uid + " pid " + client.pid
181 + " method " + Integer.toHexString(
182 System.identityHashCode(method))
183 + " session " + Integer.toHexString(
184 System.identityHashCode(session))
185 + "}";
186 }
187
188 SessionState(ClientState _client, IInputMethod _method,
189 IInputMethodSession _session) {
190 client = _client;
191 method = _method;
192 session = _session;
193 }
194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 class ClientState {
197 final IInputMethodClient client;
198 final IInputContext inputContext;
199 final int uid;
200 final int pid;
201 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 boolean sessionRequested;
204 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "ClientState{" + Integer.toHexString(
209 System.identityHashCode(this)) + " uid " + uid
210 + " pid " + pid + "}";
211 }
212
213 ClientState(IInputMethodClient _client, IInputContext _inputContext,
214 int _uid, int _pid) {
215 client = _client;
216 inputContext = _inputContext;
217 uid = _uid;
218 pid = _pid;
219 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
220 }
221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final HashMap<IBinder, ClientState> mClients
224 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700227 * Set once the system is ready to run third party code.
228 */
229 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * Id of the currently selected input method.
233 */
234 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The current binding sequence number, incremented every time there is
238 * a new bind performed.
239 */
240 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * The client that is currently bound to an input method.
244 */
245 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700248 * The last window token that gained focus.
249 */
250 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * The input context last provided by the current client.
254 */
255 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The attributes last provided by the current client.
259 */
260 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * The input method ID of the input method service that we are currently
264 * connected to or in the process of connecting to.
265 */
266 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
satokab751aa2010-09-14 19:17:36 +0900269 * The current subtype of the current input method.
270 */
271 private InputMethodSubtype mCurrentSubtype;
272
satok4e4569d2010-11-19 18:45:53 +0900273 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900274 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
275 mShortcutInputMethodsAndSubtypes =
276 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900277
278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * Set to true if our ServiceConnection is currently actively bound to
280 * a service (whether or not we have gotten its IBinder back yet).
281 */
282 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Set if the client has asked for the input method to be shown.
286 */
287 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Set if we were explicitly told to show the input method.
291 */
292 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if we were forced to be shown.
296 */
297 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Set if we last told the input method to show itself.
301 */
302 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * The Intent used to connect to the current input method.
306 */
307 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The token we have made for the currently active input method, to
311 * identify it in the future.
312 */
313 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * If non-null, this is the input method service we are currently connected
317 * to.
318 */
319 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Time that we last initiated a bind to the input method, to determine
323 * if we should try to disconnect and reconnect to it.
324 */
325 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Have we called mCurMethod.bindInput()?
329 */
330 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Currently enabled session. Only touched by service thread, not
334 * protected by a lock.
335 */
336 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * True if the screen is on. The value is true initially.
340 */
341 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
Joe Onorato857fd9b2011-01-27 15:08:35 -0800343 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
344 int mImeWindowVis;
345
Ken Wakasa05dbb652011-08-22 15:22:43 +0900346 private AlertDialog.Builder mDialogBuilder;
347 private AlertDialog mSwitchingDialog;
348 private InputMethodInfo[] mIms;
349 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 class SettingsObserver extends ContentObserver {
352 SettingsObserver(Handler handler) {
353 super(handler);
354 ContentResolver resolver = mContext.getContentResolver();
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
356 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900358 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
359 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900360 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override public void onChange(boolean selfChange) {
364 synchronized (mMethodMap) {
365 updateFromSettingsLocked();
366 }
367 }
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
371 @Override
372 public void onReceive(Context context, Intent intent) {
373 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
374 mScreenOn = true;
375 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
376 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
378 hideInputMethodMenu();
379 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 // Inform the current client of the change in active status
385 try {
386 if (mCurClient != null && mCurClient.client != null) {
387 mCurClient.client.setActive(mScreenOn);
388 }
389 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800390 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + mCurClient.pid + " uid " + mCurClient.uid);
392 }
393 }
394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 class MyPackageMonitor extends PackageMonitor {
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
403 final int N = mMethodList.size();
404 if (curInputMethodId != null) {
405 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 InputMethodInfo imi = mMethodList.get(i);
407 if (imi.getId().equals(curInputMethodId)) {
408 for (String pkg : packages) {
409 if (imi.getPackageName().equals(pkg)) {
410 if (!doit) {
411 return true;
412 }
satok723a27e2010-11-11 14:58:11 +0900413 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 chooseNewDefaultIMELocked();
415 return true;
416 }
417 }
418 }
419 }
420 }
421 }
422 return false;
423 }
424
425 @Override
426 public void onSomePackagesChanged() {
427 synchronized (mMethodMap) {
428 InputMethodInfo curIm = null;
429 String curInputMethodId = Settings.Secure.getString(mContext
430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
434 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900435 final String imiId = imi.getId();
436 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 curIm = imi;
438 }
satoke7c6998e2011-06-03 17:57:59 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900441 if (isPackageModified(imi.getPackageName())) {
442 mFileManager.deleteAllInputMethodSubtypes(imiId);
443 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800446 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 + imi.getComponent());
448 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 buildInputMethodListLocked(mMethodList, mMethodMap);
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 int change = isPackageDisappearing(curIm.getPackageName());
459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 ServiceInfo si = null;
462 try {
463 si = mContext.getPackageManager().getServiceInfo(
464 curIm.getComponent(), 0);
465 } catch (PackageManager.NameNotFoundException ex) {
466 }
467 if (si == null) {
468 // Uh oh, current input method is no longer around!
469 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700472 updateImeWindowStatusLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 changed = true;
475 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900477 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 }
479 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 }
satokab751aa2010-09-14 19:17:36 +0900482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 if (curIm == null) {
484 // We currently don't have a default input method... is
485 // one now available?
486 changed = chooseNewDefaultIMELocked();
487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 if (changed) {
490 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 }
494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 class MethodCallback extends IInputMethodCallback.Stub {
497 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 MethodCallback(IInputMethod method) {
500 mMethod = method;
501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
satoke7c6998e2011-06-03 17:57:59 +0900503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public void finishedEvent(int seq, boolean handled) throws RemoteException {
505 }
506
satoke7c6998e2011-06-03 17:57:59 +0900507 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public void sessionCreated(IInputMethodSession session) throws RemoteException {
509 onSessionCreated(mMethod, session);
510 }
511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800512
Dianne Hackborn661cd522011-08-22 00:26:20 -0700513 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800515 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 mHandler = new Handler(this);
517 mIWindowManager = IWindowManager.Stub.asInterface(
518 ServiceManager.getService(Context.WINDOW_SERVICE));
519 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public void executeMessage(Message msg) {
522 handleMessage(msg);
523 }
524 });
satok7cfc0ed2011-06-20 21:29:36 +0900525
satok7cfc0ed2011-06-20 21:29:36 +0900526 mImeSwitcherNotification = new Notification();
527 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
528 mImeSwitcherNotification.when = 0;
529 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
530 mImeSwitcherNotification.tickerText = null;
531 mImeSwitcherNotification.defaults = 0; // please be quiet
532 mImeSwitcherNotification.sound = null;
533 mImeSwitcherNotification.vibrate = null;
534 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900535 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900536
537 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900538
satoke7c6998e2011-06-03 17:57:59 +0900539 synchronized (mMethodMap) {
540 mFileManager = new InputMethodFileManager(mMethodMap);
541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800542
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800543 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 IntentFilter screenOnOffFilt = new IntentFilter();
546 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
547 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700548 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
satok7cfc0ed2011-06-20 21:29:36 +0900551 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900552
satokd87c2592010-09-29 11:52:06 +0900553 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900554 mSettings = new InputMethodSettings(
555 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900557 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558
satokd87c2592010-09-29 11:52:06 +0900559 if (TextUtils.isEmpty(Settings.Secure.getString(
560 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900562 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
564 try {
satokd87c2592010-09-29 11:52:06 +0900565 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 imi.getPackageName(), 0).getResources();
567 if (res.getBoolean(imi.getIsDefaultResourceId())) {
568 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 } catch (PackageManager.NameNotFoundException ex) {
572 } catch (Resources.NotFoundException ex) {
573 }
574 }
575 }
satokd87c2592010-09-29 11:52:06 +0900576 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800578 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900581 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 mSettingsObserver = new SettingsObserver(mHandler);
586 updateFromSettingsLocked();
587 }
588
589 @Override
590 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
591 throws RemoteException {
592 try {
593 return super.onTransact(code, data, reply, flags);
594 } catch (RuntimeException e) {
595 // The input method manager only throws security exceptions, so let's
596 // log all others.
597 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800598 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600 throw e;
601 }
602 }
603
Dianne Hackborn661cd522011-08-22 00:26:20 -0700604 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700605 synchronized (mMethodMap) {
606 if (!mSystemReady) {
607 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700608 mKeyguardManager = (KeyguardManager)
609 mContext.getSystemService(Context.KEYGUARD_SERVICE);
610 mNotificationManager = (NotificationManager)
611 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
612 mStatusBar = statusBar;
613 statusBar.setIconVisibility("ime", false);
614 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900615 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
616 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700617 try {
618 startInputInnerLocked();
619 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700621 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700622 }
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
Dianne Hackborn661cd522011-08-22 00:26:20 -0700626 void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900627 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700628 }
629
satoke7c6998e2011-06-03 17:57:59 +0900630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public List<InputMethodInfo> getInputMethodList() {
632 synchronized (mMethodMap) {
633 return new ArrayList<InputMethodInfo>(mMethodList);
634 }
635 }
636
satoke7c6998e2011-06-03 17:57:59 +0900637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public List<InputMethodInfo> getEnabledInputMethodList() {
639 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900640 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642 }
643
satokbb4aa062011-01-19 21:40:27 +0900644 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
645 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
646 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
647 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
648 for (InputMethodInfo imi: getEnabledInputMethodList()) {
649 enabledInputMethodAndSubtypes.put(
650 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
651 }
652 return enabledInputMethodAndSubtypes;
653 }
654
655 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
656 boolean allowsImplicitlySelectedSubtypes) {
657 if (imi == null && mCurMethodId != null) {
658 imi = mMethodMap.get(mCurMethodId);
659 }
satok7265d9b2011-02-14 15:47:30 +0900660 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900661 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900662 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900663 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900664 }
satok7265d9b2011-02-14 15:47:30 +0900665 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900666 }
667
satoke7c6998e2011-06-03 17:57:59 +0900668 @Override
satok16331c82010-12-20 23:48:46 +0900669 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
670 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900671 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900672 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900673 }
674 }
675
satoke7c6998e2011-06-03 17:57:59 +0900676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public void addClient(IInputMethodClient client,
678 IInputContext inputContext, int uid, int pid) {
679 synchronized (mMethodMap) {
680 mClients.put(client.asBinder(), new ClientState(client,
681 inputContext, uid, pid));
682 }
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
satoke7c6998e2011-06-03 17:57:59 +0900685 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 public void removeClient(IInputMethodClient client) {
687 synchronized (mMethodMap) {
688 mClients.remove(client.asBinder());
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 void executeOrSendMessage(IInterface target, Message msg) {
693 if (target.asBinder() instanceof Binder) {
694 mCaller.sendMessage(msg);
695 } else {
696 handleMessage(msg);
697 msg.recycle();
698 }
699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700701 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 + mCurClient.client.asBinder());
705 if (mBoundToMethod) {
706 mBoundToMethod = false;
707 if (mCurMethod != null) {
708 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
709 MSG_UNBIND_INPUT, mCurMethod));
710 }
711 }
712 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
713 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
714 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 // Call setActive(false) on the old client
717 try {
718 mCurClient.client.setActive(false);
719 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 + mCurClient.pid + " uid " + mCurClient.uid);
722 }
723 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project10592532009-03-18 17:39:46 -0700725 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 private int getImeShowFlags() {
730 int flags = 0;
731 if (mShowForced) {
732 flags |= InputMethod.SHOW_FORCED
733 | InputMethod.SHOW_EXPLICIT;
734 } else if (mShowExplicitlyRequested) {
735 flags |= InputMethod.SHOW_EXPLICIT;
736 }
737 return flags;
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 private int getAppShowFlags() {
741 int flags = 0;
742 if (mShowForced) {
743 flags |= InputMethodManager.SHOW_FORCED;
744 } else if (!mShowExplicitlyRequested) {
745 flags |= InputMethodManager.SHOW_IMPLICIT;
746 }
747 return flags;
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
751 if (!mBoundToMethod) {
752 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
753 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
754 mBoundToMethod = true;
755 }
756 final SessionState session = mCurClient.curSession;
757 if (initial) {
758 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
759 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
760 } else {
761 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
762 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
763 }
764 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800765 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800766 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768 return needResult
769 ? new InputBindResult(session.session, mCurId, mCurSeq)
770 : null;
771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 InputBindResult startInputLocked(IInputMethodClient client,
774 IInputContext inputContext, EditorInfo attribute,
775 boolean initial, boolean needResult) {
776 // If no method is currently selected, do nothing.
777 if (mCurMethodId == null) {
778 return mNoBinding;
779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 ClientState cs = mClients.get(client.asBinder());
782 if (cs == null) {
783 throw new IllegalArgumentException("unknown client "
784 + client.asBinder());
785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 try {
788 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
789 // Check with the window manager to make sure this client actually
790 // has a window with focus. If not, reject. This is thread safe
791 // because if the focus changes some time before or after, the
792 // next client receiving focus that has any interest in input will
793 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
796 return null;
797 }
798 } catch (RemoteException e) {
799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if (mCurClient != cs) {
802 // If the client is changing, we need to switch over to the new
803 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700804 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800805 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 + cs.client.asBinder());
807
808 // If the screen is on, inform the new client it is active
809 if (mScreenOn) {
810 try {
811 cs.client.setActive(mScreenOn);
812 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800813 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 + cs.pid + " uid " + cs.uid);
815 }
816 }
817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Bump up the sequence for this client and attach it.
820 mCurSeq++;
821 if (mCurSeq <= 0) mCurSeq = 1;
822 mCurClient = cs;
823 mCurInputContext = inputContext;
824 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Check if the input method is changing.
827 if (mCurId != null && mCurId.equals(mCurMethodId)) {
828 if (cs.curSession != null) {
829 // Fast case: if we are already connected to the input method,
830 // then just return it.
831 return attachNewInputLocked(initial, needResult);
832 }
833 if (mHaveConnection) {
834 if (mCurMethod != null) {
835 if (!cs.sessionRequested) {
836 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
839 MSG_CREATE_SESSION, mCurMethod,
840 new MethodCallback(mCurMethod)));
841 }
842 // Return to client, and we will get back with it when
843 // we have had a session made for it.
844 return new InputBindResult(null, mCurId, mCurSeq);
845 } else if (SystemClock.uptimeMillis()
846 < (mLastBindTime+TIME_TO_RECONNECT)) {
847 // In this case we have connected to the service, but
848 // don't yet have its interface. If it hasn't been too
849 // long since we did the connection, we'll return to
850 // the client and wait to get the service interface so
851 // we can report back. If it has been too long, we want
852 // to fall through so we can try a disconnect/reconnect
853 // to see if we can get back in touch with the service.
854 return new InputBindResult(null, mCurId, mCurSeq);
855 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
857 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859 }
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700862 return startInputInnerLocked();
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 InputBindResult startInputInnerLocked() {
866 if (mCurMethodId == null) {
867 return mNoBinding;
868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700870 if (!mSystemReady) {
871 // If the system is not yet ready, we shouldn't be running third
872 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700873 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700874 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 InputMethodInfo info = mMethodMap.get(mCurMethodId);
877 if (info == null) {
878 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800880
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
884 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700885 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
886 com.android.internal.R.string.input_method_binding_label);
887 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
888 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
890 mLastBindTime = SystemClock.uptimeMillis();
891 mHaveConnection = true;
892 mCurId = info.getId();
893 mCurToken = new Binder();
894 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800895 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 mIWindowManager.addWindowToken(mCurToken,
897 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
898 } catch (RemoteException e) {
899 }
900 return new InputBindResult(null, mCurId, mCurSeq);
901 } else {
902 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800903 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 + mCurIntent);
905 }
906 return null;
907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
satoke7c6998e2011-06-03 17:57:59 +0900909 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public InputBindResult startInput(IInputMethodClient client,
911 IInputContext inputContext, EditorInfo attribute,
912 boolean initial, boolean needResult) {
913 synchronized (mMethodMap) {
914 final long ident = Binder.clearCallingIdentity();
915 try {
916 return startInputLocked(client, inputContext, attribute,
917 initial, needResult);
918 } finally {
919 Binder.restoreCallingIdentity(ident);
920 }
921 }
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public void finishInput(IInputMethodClient client) {
926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927
satoke7c6998e2011-06-03 17:57:59 +0900928 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public void onServiceConnected(ComponentName name, IBinder service) {
930 synchronized (mMethodMap) {
931 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
932 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700933 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 unbindCurrentMethodLocked(false);
936 return;
937 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700939 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
940 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800942 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700943 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700945 MSG_CREATE_SESSION, mCurMethod,
946 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 }
948 }
949 }
950 }
951
952 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
953 synchronized (mMethodMap) {
954 if (mCurMethod != null && method != null
955 && mCurMethod.asBinder() == method.asBinder()) {
956 if (mCurClient != null) {
957 mCurClient.curSession = new SessionState(mCurClient,
958 method, session);
959 mCurClient.sessionRequested = false;
960 InputBindResult res = attachNewInputLocked(true, true);
961 if (res.method != null) {
962 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
963 MSG_BIND_METHOD, mCurClient.client, res));
964 }
965 }
966 }
967 }
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 void unbindCurrentMethodLocked(boolean reportToClient) {
971 if (mHaveConnection) {
972 mContext.unbindService(this);
973 mHaveConnection = false;
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 if (mCurToken != null) {
977 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800978 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 mIWindowManager.removeWindowToken(mCurToken);
980 } catch (RemoteException e) {
981 }
982 mCurToken = null;
983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800984
The Android Open Source Project10592532009-03-18 17:39:46 -0700985 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700988 if (reportToClient && mCurClient != null) {
989 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
990 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
991 }
992 }
993
Devin Taylor0c33ed22010-02-23 13:26:46 -0600994 private void finishSession(SessionState sessionState) {
995 if (sessionState != null && sessionState.session != null) {
996 try {
997 sessionState.session.finishSession();
998 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700999 Slog.w(TAG, "Session failed to close due to remote exception", e);
satokdbf29502011-08-25 15:28:23 +09001000 mImeWindowVis = 0;
1001 updateImeWindowStatusLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001002 }
1003 }
1004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001006 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (mCurMethod != null) {
1008 for (ClientState cs : mClients.values()) {
1009 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001010 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 cs.curSession = null;
1012 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001013
1014 finishSession(mEnabledSession);
1015 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 mCurMethod = null;
1017 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001018 if (mStatusBar != null) {
1019 mStatusBar.setIconVisibility("ime", false);
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
satoke7c6998e2011-06-03 17:57:59 +09001023 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 public void onServiceDisconnected(ComponentName name) {
1025 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 + " mCurIntent=" + mCurIntent);
1028 if (mCurMethod != null && mCurIntent != null
1029 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001030 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 // We consider this to be a new bind attempt, since the system
1032 // should now try to restart the service for us.
1033 mLastBindTime = SystemClock.uptimeMillis();
1034 mShowRequested = mInputShown;
1035 mInputShown = false;
1036 if (mCurClient != null) {
1037 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1038 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1039 }
1040 }
1041 }
1042 }
1043
satokf9f01002011-05-19 21:31:50 +09001044 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001046 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 long ident = Binder.clearCallingIdentity();
1048 try {
1049 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001050 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 return;
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 synchronized (mMethodMap) {
1055 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001057 if (mStatusBar != null) {
1058 mStatusBar.setIconVisibility("ime", false);
1059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001061 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001062 CharSequence contentDescription = null;
1063 try {
1064 PackageManager packageManager = mContext.getPackageManager();
1065 contentDescription = packageManager.getApplicationLabel(
1066 packageManager.getApplicationInfo(packageName, 0));
1067 } catch (NameNotFoundException nnfe) {
1068 /* ignore */
1069 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001070 if (mStatusBar != null) {
1071 mStatusBar.setIcon("ime", packageName, iconId, 0,
1072 contentDescription != null
1073 ? contentDescription.toString() : null);
1074 mStatusBar.setIconVisibility("ime", true);
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 }
1078 } finally {
1079 Binder.restoreCallingIdentity(ident);
1080 }
1081 }
1082
satok7cfc0ed2011-06-20 21:29:36 +09001083 private boolean needsToShowImeSwitchOngoingNotification() {
1084 if (!mShowOngoingImeSwitcherForPhones) return false;
1085 synchronized (mMethodMap) {
1086 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1087 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001088 if (N > 2) return true;
1089 if (N < 1) return false;
1090 int nonAuxCount = 0;
1091 int auxCount = 0;
1092 InputMethodSubtype nonAuxSubtype = null;
1093 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001094 for(int i = 0; i < N; ++i) {
1095 final InputMethodInfo imi = imis.get(i);
1096 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1097 imi, true);
1098 final int subtypeCount = subtypes.size();
1099 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001100 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001101 } else {
1102 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001103 final InputMethodSubtype subtype = subtypes.get(j);
1104 if (!subtype.isAuxiliary()) {
1105 ++nonAuxCount;
1106 nonAuxSubtype = subtype;
1107 } else {
1108 ++auxCount;
1109 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001110 }
1111 }
1112 }
satok7cfc0ed2011-06-20 21:29:36 +09001113 }
satokb6359412011-06-28 17:47:41 +09001114 if (nonAuxCount > 1 || auxCount > 1) {
1115 return true;
1116 } else if (nonAuxCount == 1 && auxCount == 1) {
1117 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001118 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1119 || auxSubtype.overridesImplicitlyEnabledSubtype()
1120 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001121 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1122 return false;
1123 }
1124 return true;
1125 }
1126 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001127 }
satok7cfc0ed2011-06-20 21:29:36 +09001128 }
1129
satokdbf29502011-08-25 15:28:23 +09001130 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001131 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001132 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001133 int uid = Binder.getCallingUid();
1134 long ident = Binder.clearCallingIdentity();
1135 try {
1136 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001137 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001138 return;
1139 }
1140
1141 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001142 mImeWindowVis = vis;
1143 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001144 if (mStatusBar != null) {
1145 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1146 }
satok7cfc0ed2011-06-20 21:29:36 +09001147 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001148 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1149 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001150 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001151 final CharSequence title = mRes.getText(
1152 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001153 final CharSequence imiLabel = imi.loadLabel(pm);
1154 final CharSequence summary = mCurrentSubtype != null
1155 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1156 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1157 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001158 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001159 : imiLabel;
1160
satok7cfc0ed2011-06-20 21:29:36 +09001161 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001162 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001163 if (mNotificationManager != null) {
1164 mNotificationManager.notify(
1165 com.android.internal.R.string.select_input_method,
1166 mImeSwitcherNotification);
1167 mNotificationShown = true;
1168 }
satok7cfc0ed2011-06-20 21:29:36 +09001169 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001170 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001171 mNotificationManager.cancel(
1172 com.android.internal.R.string.select_input_method);
1173 mNotificationShown = false;
1174 }
1175 }
satok06487a52010-10-29 11:37:18 +09001176 }
1177 } finally {
1178 Binder.restoreCallingIdentity(ident);
1179 }
1180 }
1181
satoke7c6998e2011-06-03 17:57:59 +09001182 @Override
satokf9f01002011-05-19 21:31:50 +09001183 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1184 synchronized (mMethodMap) {
1185 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1186 for (int i = 0; i < spans.length; ++i) {
1187 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001188 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001189 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001190 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001191 }
1192 }
1193 }
1194 }
1195
satoke7c6998e2011-06-03 17:57:59 +09001196 @Override
satokf9f01002011-05-19 21:31:50 +09001197 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1198 synchronized (mMethodMap) {
1199 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1200 // TODO: Do not send the intent if the process of the targetImi is already dead.
1201 if (targetImi != null) {
1202 final String[] suggestions = span.getSuggestions();
1203 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001204 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001205 final Intent intent = new Intent();
1206 // Ensures that only a class in the original IME package will receive the
1207 // notification.
satok42c5a162011-05-26 16:46:14 +09001208 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001209 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1210 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1211 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1212 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1213 mContext.sendBroadcast(intent);
1214 return true;
1215 }
1216 }
1217 return false;
1218 }
1219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001221 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1222 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1223 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1224 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001226 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001227 // There is no input method selected, try to choose new applicable input method.
1228 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1229 id = Settings.Secure.getString(mContext.getContentResolver(),
1230 Settings.Secure.DEFAULT_INPUT_METHOD);
1231 }
1232 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 try {
satokab751aa2010-09-14 19:17:36 +09001234 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001236 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001237 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001238 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
satokf3db1af2010-11-23 13:34:33 +09001240 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001241 } else {
1242 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001243 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001244 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
satokab751aa2010-09-14 19:17:36 +09001248 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 InputMethodInfo info = mMethodMap.get(id);
1250 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001251 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001255 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001256 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1257 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001258 }
1259 if (subtype != mCurrentSubtype) {
1260 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001261 if (subtype != null) {
1262 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1263 }
satokcd7cd292010-11-20 15:46:23 +09001264 if (mCurMethod != null) {
1265 try {
satoke40dea02011-01-30 01:14:02 +09001266 final Configuration conf = mRes.getConfiguration();
1267 final boolean haveHardKeyboard = conf.keyboard
1268 != Configuration.KEYBOARD_NOKEYS;
1269 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001270 && conf.hardKeyboardHidden
1271 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001272 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1273 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1274 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001275 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001276 // If subtype is null, try to find the most applicable one from
1277 // getCurrentInputMethodSubtype.
1278 if (subtype == null) {
1279 subtype = getCurrentInputMethodSubtype();
1280 }
1281 mCurMethod.changeInputMethodSubtype(subtype);
1282 } catch (RemoteException e) {
1283 return;
satokab751aa2010-09-14 19:17:36 +09001284 }
1285 }
1286 }
1287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 return;
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 final long ident = Binder.clearCallingIdentity();
1292 try {
satokab751aa2010-09-14 19:17:36 +09001293 // Set a subtype to this input method.
1294 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001295 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1296 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1297 // because mCurMethodId is stored as a history in
1298 // setSelectedInputMethodAndSubtypeLocked().
1299 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300
1301 if (ActivityManagerNative.isSystemReady()) {
1302 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001303 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 intent.putExtra("input_method_id", id);
1305 mContext.sendBroadcast(intent);
1306 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001307 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 } finally {
1309 Binder.restoreCallingIdentity(ident);
1310 }
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
satok42c5a162011-05-26 16:46:14 +09001313 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001314 public boolean showSoftInput(IInputMethodClient client, int flags,
1315 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001316 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 long ident = Binder.clearCallingIdentity();
1318 try {
1319 synchronized (mMethodMap) {
1320 if (mCurClient == null || client == null
1321 || mCurClient.client.asBinder() != client.asBinder()) {
1322 try {
1323 // We need to check if this is the current client with
1324 // focus in the window manager, to allow this call to
1325 // be made before input is started in it.
1326 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001327 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001331 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 } finally {
1339 Binder.restoreCallingIdentity(ident);
1340 }
1341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
The Android Open Source Project4df24232009-03-05 14:34:35 -08001343 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 mShowRequested = true;
1345 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1346 mShowExplicitlyRequested = true;
1347 }
1348 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1349 mShowExplicitlyRequested = true;
1350 mShowForced = true;
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
Dianne Hackborncc278702009-09-02 23:07:23 -07001353 if (!mSystemReady) {
1354 return false;
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1360 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1361 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 } else if (mHaveConnection && SystemClock.uptimeMillis()
1365 < (mLastBindTime+TIME_TO_RECONNECT)) {
1366 // The client has asked to have the input method shown, but
1367 // we have been sitting here too long with a connection to the
1368 // service and no interface received, so let's disconnect/connect
1369 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 SystemClock.uptimeMillis()-mLastBindTime,1);
1372 mContext.unbindService(this);
1373 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project4df24232009-03-05 14:34:35 -08001376 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
satok42c5a162011-05-26 16:46:14 +09001379 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001380 public boolean hideSoftInput(IInputMethodClient client, int flags,
1381 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001382 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 long ident = Binder.clearCallingIdentity();
1384 try {
1385 synchronized (mMethodMap) {
1386 if (mCurClient == null || client == null
1387 || mCurClient.client.asBinder() != client.asBinder()) {
1388 try {
1389 // We need to check if this is the current client with
1390 // focus in the window manager, to allow this call to
1391 // be made before input is started in it.
1392 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001393 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1394 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001395 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001396 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001397 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001400 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001401 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001402 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
Joe Onorato8a9b2202010-02-26 18:56:32 -08001406 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001407 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 } finally {
1410 Binder.restoreCallingIdentity(ident);
1411 }
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project4df24232009-03-05 14:34:35 -08001414 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1416 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001419 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001422 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001424 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001426 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001428 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1429 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1430 res = true;
1431 } else {
1432 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
1434 mInputShown = false;
1435 mShowRequested = false;
1436 mShowExplicitlyRequested = false;
1437 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001438 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
satok42c5a162011-05-26 16:46:14 +09001441 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001442 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1444 boolean first, int windowFlags) {
1445 long ident = Binder.clearCallingIdentity();
1446 try {
1447 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001448 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 + " viewHasFocus=" + viewHasFocus
1450 + " isTextEditor=" + isTextEditor
1451 + " softInputMode=#" + Integer.toHexString(softInputMode)
1452 + " first=" + first + " flags=#"
1453 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 if (mCurClient == null || client == null
1456 || mCurClient.client.asBinder() != client.asBinder()) {
1457 try {
1458 // We need to check if this is the current client with
1459 // focus in the window manager, to allow this call to
1460 // be made before input is started in it.
1461 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 return;
1464 }
1465 } catch (RemoteException e) {
1466 }
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001469 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001470 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001471 return;
1472 }
1473 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001474
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001475 // Should we auto-show the IME even if the caller has not
1476 // specified what should be done with it?
1477 // We only do this automatically if the window can resize
1478 // to accommodate the IME (so what the user sees will give
1479 // them good context without input information being obscured
1480 // by the IME) or if running on a large screen where there
1481 // is more room for the target window + IME.
1482 final boolean doAutoShow =
1483 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1484 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1485 || mRes.getConfiguration().isLayoutSizeAtLeast(
1486 Configuration.SCREENLAYOUT_SIZE_LARGE);
1487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1489 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001490 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1492 // There is no focus view, and this window will
1493 // be behind any soft input window, so hide the
1494 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001496 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001498 } else if (isTextEditor && doAutoShow && (softInputMode &
1499 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 // There is a focus view, and we are navigating forward
1501 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001502 // We only do this automatically if the window an resize
1503 // to accomodate the IME (so what the user sees will give
1504 // them good context without input information being obscured
1505 // by the IME) or if running on a large screen where there
1506 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001508 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510 break;
1511 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1512 // Do nothing.
1513 break;
1514 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1515 if ((softInputMode &
1516 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001517 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001518 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520 break;
1521 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001522 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001523 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 break;
1525 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1526 if ((softInputMode &
1527 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001528 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001529 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531 break;
1532 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001534 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 break;
1536 }
1537 }
1538 } finally {
1539 Binder.restoreCallingIdentity(ident);
1540 }
1541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
satok42c5a162011-05-26 16:46:14 +09001543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1545 synchronized (mMethodMap) {
1546 if (mCurClient == null || client == null
1547 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001548 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001549 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551
satok440aab52010-11-25 09:43:11 +09001552 // Always call subtype picker, because subtype picker is a superset of input method
1553 // picker.
satokab751aa2010-09-14 19:17:36 +09001554 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1555 }
1556 }
1557
satok42c5a162011-05-26 16:46:14 +09001558 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001560 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1561 }
1562
satok42c5a162011-05-26 16:46:14 +09001563 @Override
satok28203512010-11-24 11:06:49 +09001564 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1565 synchronized (mMethodMap) {
1566 if (subtype != null) {
1567 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1568 mMethodMap.get(id), subtype.hashCode()));
1569 } else {
1570 setInputMethod(token, id);
1571 }
1572 }
satokab751aa2010-09-14 19:17:36 +09001573 }
1574
satok42c5a162011-05-26 16:46:14 +09001575 @Override
satokb416a71e2010-11-25 20:42:14 +09001576 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001577 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001578 synchronized (mMethodMap) {
1579 if (mCurClient == null || client == null
1580 || mCurClient.client.asBinder() != client.asBinder()) {
1581 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1582 }
satok7fee71f2010-12-17 18:54:26 +09001583 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1584 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001585 }
1586 }
1587
satok4fc87d62011-05-20 16:13:43 +09001588 @Override
satok735cf382010-11-11 20:40:09 +09001589 public boolean switchToLastInputMethod(IBinder token) {
1590 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001591 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001592 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001593 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001594 lastImi = mMethodMap.get(lastIme.first);
1595 } else {
1596 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001597 }
satok4fc87d62011-05-20 16:13:43 +09001598 String targetLastImiId = null;
1599 int subtypeId = NOT_A_SUBTYPE_ID;
1600 if (lastIme != null && lastImi != null) {
1601 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1602 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1603 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1604 : mCurrentSubtype.hashCode();
1605 // If the last IME is the same as the current IME and the last subtype is not
1606 // defined, there is no need to switch to the last IME.
1607 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1608 targetLastImiId = lastIme.first;
1609 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1610 }
1611 }
1612
1613 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1614 // This is a safety net. If the currentSubtype can't be added to the history
1615 // and the framework couldn't find the last ime, we will make the last ime be
1616 // the most applicable enabled keyboard subtype of the system imes.
1617 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1618 if (enabled != null) {
1619 final int N = enabled.size();
1620 final String locale = mCurrentSubtype == null
1621 ? mRes.getConfiguration().locale.toString()
1622 : mCurrentSubtype.getLocale();
1623 for (int i = 0; i < N; ++i) {
1624 final InputMethodInfo imi = enabled.get(i);
1625 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1626 InputMethodSubtype keyboardSubtype =
1627 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1628 SUBTYPE_MODE_KEYBOARD, locale, true);
1629 if (keyboardSubtype != null) {
1630 targetLastImiId = imi.getId();
1631 subtypeId = getSubtypeIdFromHashCode(
1632 imi, keyboardSubtype.hashCode());
1633 if(keyboardSubtype.getLocale().equals(locale)) {
1634 break;
1635 }
1636 }
1637 }
1638 }
1639 }
1640 }
1641
1642 if (!TextUtils.isEmpty(targetLastImiId)) {
1643 if (DEBUG) {
1644 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1645 + ", from: " + mCurMethodId + ", " + subtypeId);
1646 }
1647 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1648 return true;
1649 } else {
1650 return false;
1651 }
satok735cf382010-11-11 20:40:09 +09001652 }
1653 }
1654
satoke7c6998e2011-06-03 17:57:59 +09001655 @Override
satok68f1b782011-04-11 14:26:04 +09001656 public InputMethodSubtype getLastInputMethodSubtype() {
1657 synchronized (mMethodMap) {
1658 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1659 // TODO: Handle the case of the last IME with no subtypes
1660 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1661 || TextUtils.isEmpty(lastIme.second)) return null;
1662 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1663 if (lastImi == null) return null;
1664 try {
1665 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001666 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1667 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1668 return null;
1669 }
1670 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001671 } catch (NumberFormatException e) {
1672 return null;
1673 }
1674 }
1675 }
1676
satoke7c6998e2011-06-03 17:57:59 +09001677 @Override
satokee5e77c2011-09-02 18:50:15 +09001678 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001679 // By this IPC call, only a process which shares the same uid with the IME can add
1680 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001681 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001682 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001683 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001684 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001685 final PackageManager pm = mContext.getPackageManager();
1686 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1687 if (packageInfos != null) {
1688 final int packageNum = packageInfos.length;
1689 for (int i = 0; i < packageNum; ++i) {
1690 if (packageInfos[i].equals(imi.getPackageName())) {
1691 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001692 final long ident = Binder.clearCallingIdentity();
1693 try {
1694 buildInputMethodListLocked(mMethodList, mMethodMap);
1695 } finally {
1696 Binder.restoreCallingIdentity(ident);
1697 }
satokee5e77c2011-09-02 18:50:15 +09001698 return;
satok91e88122011-07-18 11:11:42 +09001699 }
1700 }
1701 }
satoke7c6998e2011-06-03 17:57:59 +09001702 }
satokee5e77c2011-09-02 18:50:15 +09001703 return;
satoke7c6998e2011-06-03 17:57:59 +09001704 }
1705
satok28203512010-11-24 11:06:49 +09001706 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 synchronized (mMethodMap) {
1708 if (token == null) {
1709 if (mContext.checkCallingOrSelfPermission(
1710 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1711 != PackageManager.PERMISSION_GRANTED) {
1712 throw new SecurityException(
1713 "Using null token requires permission "
1714 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1715 }
1716 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001717 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1718 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 return;
1720 }
1721
satokc5933802011-08-31 21:26:04 +09001722 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 try {
satokab751aa2010-09-14 19:17:36 +09001724 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 } finally {
1726 Binder.restoreCallingIdentity(ident);
1727 }
1728 }
1729 }
1730
satok42c5a162011-05-26 16:46:14 +09001731 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 public void hideMySoftInput(IBinder token, int flags) {
1733 synchronized (mMethodMap) {
1734 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001735 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1736 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 return;
1738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 long ident = Binder.clearCallingIdentity();
1740 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001741 hideCurrentInputLocked(flags, null);
1742 } finally {
1743 Binder.restoreCallingIdentity(ident);
1744 }
1745 }
1746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
satok42c5a162011-05-26 16:46:14 +09001748 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001749 public void showMySoftInput(IBinder token, int flags) {
1750 synchronized (mMethodMap) {
1751 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001752 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1753 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 return;
1755 }
1756 long ident = Binder.clearCallingIdentity();
1757 try {
1758 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 } finally {
1760 Binder.restoreCallingIdentity(ident);
1761 }
1762 }
1763 }
1764
1765 void setEnabledSessionInMainThread(SessionState session) {
1766 if (mEnabledSession != session) {
1767 if (mEnabledSession != null) {
1768 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001769 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 mEnabledSession.method.setSessionEnabled(
1771 mEnabledSession.session, false);
1772 } catch (RemoteException e) {
1773 }
1774 }
1775 mEnabledSession = session;
1776 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001777 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 session.method.setSessionEnabled(
1779 session.session, true);
1780 } catch (RemoteException e) {
1781 }
1782 }
1783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001784
satok42c5a162011-05-26 16:46:14 +09001785 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 public boolean handleMessage(Message msg) {
1787 HandlerCaller.SomeArgs args;
1788 switch (msg.what) {
1789 case MSG_SHOW_IM_PICKER:
1790 showInputMethodMenu();
1791 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001792
satokab751aa2010-09-14 19:17:36 +09001793 case MSG_SHOW_IM_SUBTYPE_PICKER:
1794 showInputMethodSubtypeMenu();
1795 return true;
1796
satok47a44912010-10-06 16:03:58 +09001797 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001798 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001799 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001800 return true;
1801
1802 case MSG_SHOW_IM_CONFIG:
1803 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001804 return true;
1805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 case MSG_UNBIND_INPUT:
1809 try {
1810 ((IInputMethod)msg.obj).unbindInput();
1811 } catch (RemoteException e) {
1812 // There is nothing interesting about the method dying.
1813 }
1814 return true;
1815 case MSG_BIND_INPUT:
1816 args = (HandlerCaller.SomeArgs)msg.obj;
1817 try {
1818 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1819 } catch (RemoteException e) {
1820 }
1821 return true;
1822 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001823 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1826 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 } catch (RemoteException e) {
1828 }
1829 return true;
1830 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001831 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001833 ((IInputMethod)args.arg1).hideSoftInput(0,
1834 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 } catch (RemoteException e) {
1836 }
1837 return true;
1838 case MSG_ATTACH_TOKEN:
1839 args = (HandlerCaller.SomeArgs)msg.obj;
1840 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001841 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1843 } catch (RemoteException e) {
1844 }
1845 return true;
1846 case MSG_CREATE_SESSION:
1847 args = (HandlerCaller.SomeArgs)msg.obj;
1848 try {
1849 ((IInputMethod)args.arg1).createSession(
1850 (IInputMethodCallback)args.arg2);
1851 } catch (RemoteException e) {
1852 }
1853 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 case MSG_START_INPUT:
1857 args = (HandlerCaller.SomeArgs)msg.obj;
1858 try {
1859 SessionState session = (SessionState)args.arg1;
1860 setEnabledSessionInMainThread(session);
1861 session.method.startInput((IInputContext)args.arg2,
1862 (EditorInfo)args.arg3);
1863 } catch (RemoteException e) {
1864 }
1865 return true;
1866 case MSG_RESTART_INPUT:
1867 args = (HandlerCaller.SomeArgs)msg.obj;
1868 try {
1869 SessionState session = (SessionState)args.arg1;
1870 setEnabledSessionInMainThread(session);
1871 session.method.restartInput((IInputContext)args.arg2,
1872 (EditorInfo)args.arg3);
1873 } catch (RemoteException e) {
1874 }
1875 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 case MSG_UNBIND_METHOD:
1880 try {
1881 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1882 } catch (RemoteException e) {
1883 // There is nothing interesting about the last client dying.
1884 }
1885 return true;
1886 case MSG_BIND_METHOD:
1887 args = (HandlerCaller.SomeArgs)msg.obj;
1888 try {
1889 ((IInputMethodClient)args.arg1).onBindMethod(
1890 (InputBindResult)args.arg2);
1891 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001892 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894 return true;
1895 }
1896 return false;
1897 }
1898
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001899 private boolean isSystemIme(InputMethodInfo inputMethod) {
1900 return (inputMethod.getServiceInfo().applicationInfo.flags
1901 & ApplicationInfo.FLAG_SYSTEM) != 0;
1902 }
1903
Ken Wakasa586f0512011-01-20 22:31:01 +09001904 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1905 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1906 final int subtypeCount = imi.getSubtypeCount();
1907 for (int i = 0; i < subtypeCount; ++i) {
1908 subtypes.add(imi.getSubtypeAt(i));
1909 }
1910 return subtypes;
1911 }
1912
satoka86f5e42011-09-02 17:12:42 +09001913
1914 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1915 InputMethodInfo imi, String mode) {
1916 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1917 final int subtypeCount = imi.getSubtypeCount();
1918 for (int i = 0; i < subtypeCount; ++i) {
1919 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1920 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1921 subtypes.add(subtype);
1922 }
1923 }
1924 return subtypes;
1925 }
1926
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001927 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001928 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001929 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001930 // We'd prefer to fall back on a system IME, since that is safer.
1931 int i=enabled.size();
1932 while (i > 0) {
1933 i--;
1934 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1935 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1936 break;
1937 }
1938 }
satokab751aa2010-09-14 19:17:36 +09001939 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001940 if (DEBUG) {
1941 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1942 }
satok723a27e2010-11-11 14:58:11 +09001943 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001944 return true;
1945 }
1946
1947 return false;
1948 }
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1951 HashMap<String, InputMethodInfo> map) {
1952 list.clear();
1953 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001956 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001957 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1958 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1959 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1960 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961
1962 List<ResolveInfo> services = pm.queryIntentServices(
1963 new Intent(InputMethod.SERVICE_INTERFACE),
1964 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001965
satoke7c6998e2011-06-03 17:57:59 +09001966 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1967 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 for (int i = 0; i < services.size(); ++i) {
1969 ResolveInfo ri = services.get(i);
1970 ServiceInfo si = ri.serviceInfo;
1971 ComponentName compName = new ComponentName(si.packageName, si.name);
1972 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1973 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001974 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 + ": it does not require the permission "
1976 + android.Manifest.permission.BIND_INPUT_METHOD);
1977 continue;
1978 }
1979
Joe Onorato8a9b2202010-02-26 18:56:32 -08001980 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981
1982 try {
satoke7c6998e2011-06-03 17:57:59 +09001983 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001985 final String id = p.getId();
1986 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987
Amith Yamasanie861ec12010-03-24 21:39:27 -07001988 // System IMEs are enabled by default, unless there's a hard keyboard
1989 // and the system IME was explicitly disabled
1990 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1991 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001992 }
1993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001995 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001999 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002001 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002004
2005 String defaultIme = Settings.Secure.getString(mContext
2006 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002007 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002008 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002009 updateFromSettingsLocked();
2010 }
2011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002015
satokab751aa2010-09-14 19:17:36 +09002016 private void showInputMethodMenu() {
2017 showInputMethodMenuInternal(false);
2018 }
2019
2020 private void showInputMethodSubtypeMenu() {
2021 showInputMethodMenuInternal(true);
2022 }
2023
satok217f5482010-12-15 05:19:19 +09002024 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002025 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002026 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002027 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2028 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002029 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002030 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002031 }
satok217f5482010-12-15 05:19:19 +09002032 mContext.startActivity(intent);
2033 }
2034
2035 private void showConfigureInputMethods() {
2036 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2037 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2038 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2039 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002040 mContext.startActivity(intent);
2041 }
2042
satokab751aa2010-09-14 19:17:36 +09002043 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002044 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002048 final boolean isScreenLocked = mKeyguardManager != null
2049 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 String lastInputMethodId = Settings.Secure.getString(context
2052 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002053 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002054 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002056 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002057 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2058 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002059 if (immis == null || immis.size() == 0) {
2060 return;
2061 }
2062
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002063 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064
Ken Wakasa761eb372011-03-04 19:06:18 +09002065 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2066 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2067 new Comparator<InputMethodInfo>() {
2068 @Override
2069 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2070 if (imi2 == null) return 0;
2071 if (imi1 == null) return 1;
2072 if (pm == null) {
2073 return imi1.getId().compareTo(imi2.getId());
2074 }
2075 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2076 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2077 return imiId1.toString().compareTo(imiId2.toString());
2078 }
2079 });
satok913a8922010-08-26 21:53:41 +09002080
Ken Wakasa761eb372011-03-04 19:06:18 +09002081 sortedImmis.putAll(immis);
2082
Ken Wakasa05dbb652011-08-22 15:22:43 +09002083 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002084
2085 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002086 if (imi == null) continue;
2087 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002088 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002089 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2090 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002091 }
satokbb4aa062011-01-19 21:40:27 +09002092 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002093 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002094 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002095 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002096 if (DEBUG) {
2097 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2098 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002099 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002100 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2101 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2102 // We show all enabled IMEs and subtypes when an IME is shown.
2103 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002104 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002105 final CharSequence subtypeLabel =
2106 subtype.overridesImplicitlyEnabledSubtype() ? null
2107 : subtype.getDisplayName(context, imi.getPackageName(),
2108 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002109 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2110
satok4a28bde2011-06-29 21:03:40 +09002111 // Removing this subtype from enabledSubtypeSet because we no longer
2112 // need to add an entry of this subtype to imList to avoid duplicated
2113 // entries.
2114 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002115 }
satokab751aa2010-09-14 19:17:36 +09002116 }
2117 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002118 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002119 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002120 }
satok913a8922010-08-26 21:53:41 +09002121
Ken Wakasa761eb372011-03-04 19:06:18 +09002122 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002123 mIms = new InputMethodInfo[N];
2124 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002125 int checkedItem = 0;
2126 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002127 final ImeSubtypeListItem item = imList.get(i);
2128 mIms[i] = item.mImi;
2129 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002130 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002131 int subtypeId = mSubtypeIds[i];
2132 if ((subtypeId == NOT_A_SUBTYPE_ID)
2133 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2134 || (subtypeId == lastInputMethodSubtypeId)) {
2135 checkedItem = i;
2136 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
satokab751aa2010-09-14 19:17:36 +09002139
Ken Wakasa05dbb652011-08-22 15:22:43 +09002140 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002141 com.android.internal.R.styleable.DialogPreference,
2142 com.android.internal.R.attr.alertDialogStyle, 0);
2143 mDialogBuilder = new AlertDialog.Builder(context)
2144 .setTitle(com.android.internal.R.string.select_input_method)
2145 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002146 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002147 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002149 }
2150 })
2151 .setIcon(a.getDrawable(
2152 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2153 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002154
Ken Wakasa05dbb652011-08-22 15:22:43 +09002155 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2156 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2157 checkedItem);
2158
2159 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002160 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002161 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002162 public void onClick(DialogInterface dialog, int which) {
2163 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002164 if (mIms == null || mIms.length <= which
2165 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002166 return;
2167 }
2168 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002169 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002170 hideInputMethodMenu();
2171 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002172 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002173 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002174 subtypeId = NOT_A_SUBTYPE_ID;
2175 }
2176 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002177 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002180 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181
satokbc81b692011-08-26 16:22:22 +09002182 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002183 mDialogBuilder.setPositiveButton(
2184 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002185 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002186 @Override
satok7f35c8c2010-10-07 21:13:11 +09002187 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002188 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002189 }
2190 });
2191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002193 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 mSwitchingDialog.getWindow().setType(
2195 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002196 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 mSwitchingDialog.show();
2198 }
2199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002200
Ken Wakasa05dbb652011-08-22 15:22:43 +09002201 private static class ImeSubtypeListItem {
2202 public final CharSequence mImeName;
2203 public final CharSequence mSubtypeName;
2204 public final InputMethodInfo mImi;
2205 public final int mSubtypeId;
2206 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2207 InputMethodInfo imi, int subtypeId) {
2208 mImeName = imeName;
2209 mSubtypeName = subtypeName;
2210 mImi = imi;
2211 mSubtypeId = subtypeId;
2212 }
2213 }
2214
2215 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2216 private final LayoutInflater mInflater;
2217 private final int mTextViewResourceId;
2218 private final List<ImeSubtypeListItem> mItemsList;
2219 private final int mCheckedItem;
2220 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2221 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2222 super(context, textViewResourceId, itemsList);
2223 mTextViewResourceId = textViewResourceId;
2224 mItemsList = itemsList;
2225 mCheckedItem = checkedItem;
2226 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2227 }
2228
2229 @Override
2230 public View getView(int position, View convertView, ViewGroup parent) {
2231 final View view = convertView != null ? convertView
2232 : mInflater.inflate(mTextViewResourceId, null);
2233 if (position < 0 || position >= mItemsList.size()) return view;
2234 final ImeSubtypeListItem item = mItemsList.get(position);
2235 final CharSequence imeName = item.mImeName;
2236 final CharSequence subtypeName = item.mSubtypeName;
2237 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2238 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2239 if (TextUtils.isEmpty(subtypeName)) {
2240 firstTextView.setText(imeName);
2241 secondTextView.setVisibility(View.GONE);
2242 } else {
2243 firstTextView.setText(subtypeName);
2244 secondTextView.setText(imeName);
2245 secondTextView.setVisibility(View.VISIBLE);
2246 }
2247 final RadioButton radioButton =
2248 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2249 radioButton.setChecked(position == mCheckedItem);
2250 return view;
2251 }
2252 }
2253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002255 synchronized (mMethodMap) {
2256 hideInputMethodMenuLocked();
2257 }
2258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002259
The Android Open Source Project10592532009-03-18 17:39:46 -07002260 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002261 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262
The Android Open Source Project10592532009-03-18 17:39:46 -07002263 if (mSwitchingDialog != null) {
2264 mSwitchingDialog.dismiss();
2265 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
The Android Open Source Project10592532009-03-18 17:39:46 -07002268 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002269 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002273
satok42c5a162011-05-26 16:46:14 +09002274 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 public boolean setInputMethodEnabled(String id, boolean enabled) {
2276 synchronized (mMethodMap) {
2277 if (mContext.checkCallingOrSelfPermission(
2278 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2279 != PackageManager.PERMISSION_GRANTED) {
2280 throw new SecurityException(
2281 "Requires permission "
2282 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2283 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 long ident = Binder.clearCallingIdentity();
2286 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002287 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 } finally {
2289 Binder.restoreCallingIdentity(ident);
2290 }
2291 }
2292 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002293
2294 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2295 // Make sure this is a valid input method.
2296 InputMethodInfo imm = mMethodMap.get(id);
2297 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002298 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002299 }
2300
satokd87c2592010-09-29 11:52:06 +09002301 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2302 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002303
satokd87c2592010-09-29 11:52:06 +09002304 if (enabled) {
2305 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2306 if (pair.first.equals(id)) {
2307 // We are enabling this input method, but it is already enabled.
2308 // Nothing to do. The previous state was enabled.
2309 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002310 }
2311 }
satokd87c2592010-09-29 11:52:06 +09002312 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2313 // Previous state was disabled.
2314 return false;
2315 } else {
2316 StringBuilder builder = new StringBuilder();
2317 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2318 builder, enabledInputMethodsList, id)) {
2319 // Disabled input method is currently selected, switch to another one.
2320 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2321 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002322 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2323 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2324 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002325 }
2326 // Previous state was enabled.
2327 return true;
2328 } else {
2329 // We are disabling the input method but it is already disabled.
2330 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002331 return false;
2332 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002333 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002334 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335
satok57ffc002011-01-25 00:11:47 +09002336 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2337 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002338 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002339 }
2340
satok723a27e2010-11-11 14:58:11 +09002341 private void saveCurrentInputMethodAndSubtypeToHistory() {
2342 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2343 if (mCurrentSubtype != null) {
2344 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2345 }
satok57ffc002011-01-25 00:11:47 +09002346 if (canAddToLastInputMethod(mCurrentSubtype)) {
2347 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2348 }
satokab751aa2010-09-14 19:17:36 +09002349 }
2350
satok723a27e2010-11-11 14:58:11 +09002351 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2352 boolean setSubtypeOnly) {
2353 // Update the history of InputMethod and Subtype
2354 saveCurrentInputMethodAndSubtypeToHistory();
2355
2356 // Set Subtype here
2357 if (imi == null || subtypeId < 0) {
2358 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002359 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002360 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002361 if (subtypeId < imi.getSubtypeCount()) {
2362 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2363 mSettings.putSelectedSubtype(subtype.hashCode());
2364 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002365 } else {
2366 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2367 mCurrentSubtype = null;
2368 }
satokab751aa2010-09-14 19:17:36 +09002369 }
satok723a27e2010-11-11 14:58:11 +09002370
2371 if (!setSubtypeOnly) {
2372 // Set InputMethod here
2373 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2374 }
2375 }
2376
2377 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2378 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2379 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2380 // newDefaultIme is empty when there is no candidate for the selected IME.
2381 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2382 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2383 if (subtypeHashCode != null) {
2384 try {
2385 lastSubtypeId = getSubtypeIdFromHashCode(
2386 imi, Integer.valueOf(subtypeHashCode));
2387 } catch (NumberFormatException e) {
2388 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2389 }
2390 }
2391 }
2392 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002393 }
2394
2395 private int getSelectedInputMethodSubtypeId(String id) {
2396 InputMethodInfo imi = mMethodMap.get(id);
2397 if (imi == null) {
2398 return NOT_A_SUBTYPE_ID;
2399 }
satokab751aa2010-09-14 19:17:36 +09002400 int subtypeId;
2401 try {
2402 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2403 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2404 } catch (SettingNotFoundException e) {
2405 return NOT_A_SUBTYPE_ID;
2406 }
satok723a27e2010-11-11 14:58:11 +09002407 return getSubtypeIdFromHashCode(imi, subtypeId);
2408 }
2409
2410 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002411 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002412 final int subtypeCount = imi.getSubtypeCount();
2413 for (int i = 0; i < subtypeCount; ++i) {
2414 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002415 if (subtypeHashCode == ims.hashCode()) {
2416 return i;
2417 }
satokab751aa2010-09-14 19:17:36 +09002418 }
2419 }
2420 return NOT_A_SUBTYPE_ID;
2421 }
2422
satoka86f5e42011-09-02 17:12:42 +09002423 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2424 Resources res, InputMethodInfo imi) {
2425 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002426 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002427 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2428 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2429 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002430 final int N = subtypes.size();
2431 boolean containsKeyboardSubtype = false;
2432 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002433 // scan overriding implicitly enabled subtypes.
2434 InputMethodSubtype subtype = subtypes.get(i);
2435 if (subtype.overridesImplicitlyEnabledSubtype()) {
2436 final String mode = subtype.getMode();
2437 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2438 applicableModeAndSubtypesMap.put(mode, subtype);
2439 }
2440 }
2441 }
2442 if (applicableModeAndSubtypesMap.size() > 0) {
2443 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2444 }
2445 for (int i = 0; i < N; ++i) {
satok16331c82010-12-20 23:48:46 +09002446 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002447 final String locale = subtype.getLocale();
2448 final String mode = subtype.getMode();
2449 // When system locale starts with subtype's locale, that subtype will be applicable
2450 // for system locale
2451 // For instance, it's clearly applicable for cases like system locale = en_US and
2452 // subtype = en, but it is not necessarily considered applicable for cases like system
2453 // locale = en and subtype = en_US.
2454 // We just call systemLocale.startsWith(locale) in this function because there is no
2455 // need to find applicable subtypes aggressively unlike
2456 // findLastResortApplicableSubtypeLocked.
2457 if (systemLocale.startsWith(locale)) {
2458 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2459 // If more applicable subtypes are contained, skip.
2460 if (applicableSubtype != null
2461 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2462 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002463 if (!containsKeyboardSubtype
2464 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2465 containsKeyboardSubtype = true;
2466 }
2467 }
2468 }
satok4a28bde2011-06-29 21:03:40 +09002469 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002470 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002471 if (!containsKeyboardSubtype) {
2472 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002473 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002474 if (lastResortKeyboardSubtype != null) {
2475 applicableSubtypes.add(lastResortKeyboardSubtype);
2476 }
2477 }
2478 return applicableSubtypes;
2479 }
2480
satok4e4569d2010-11-19 18:45:53 +09002481 /**
2482 * If there are no selected subtypes, tries finding the most applicable one according to the
2483 * given locale.
2484 * @param subtypes this function will search the most applicable subtype in subtypes
2485 * @param mode subtypes will be filtered by mode
2486 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002487 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2488 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002489 * @return the most applicable subtypeId
2490 */
satokdf31ae62011-01-15 06:19:44 +09002491 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2492 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002493 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002494 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002495 return null;
satok8fbb1e82010-11-02 23:15:58 +09002496 }
satok4e4569d2010-11-19 18:45:53 +09002497 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002498 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002499 }
satok8fbb1e82010-11-02 23:15:58 +09002500 final String language = locale.substring(0, 2);
2501 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002502 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002503 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002504 final int N = subtypes.size();
2505 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002506 InputMethodSubtype subtype = subtypes.get(i);
2507 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002508 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2509 // and all subtypes with all modes can be candidates.
2510 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002511 if (firstMatchedModeSubtype == null) {
2512 firstMatchedModeSubtype = subtype;
2513 }
satok9ef02832010-11-04 21:17:48 +09002514 if (locale.equals(subtypeLocale)) {
2515 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002516 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002517 break;
2518 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2519 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002520 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002521 partialMatchFound = true;
2522 }
satok8fbb1e82010-11-02 23:15:58 +09002523 }
2524 }
2525
satok7599a7fb2010-12-22 13:45:23 +09002526 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2527 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002528 }
2529
satok8fbb1e82010-11-02 23:15:58 +09002530 // The first subtype applicable to the system locale will be defined as the most applicable
2531 // subtype.
2532 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002533 if (applicableSubtype != null) {
2534 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2535 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2536 }
satok8fbb1e82010-11-02 23:15:58 +09002537 }
satokcd7cd292010-11-20 15:46:23 +09002538 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002539 }
2540
satok4e4569d2010-11-19 18:45:53 +09002541 // If there are no selected shortcuts, tries finding the most applicable ones.
2542 private Pair<InputMethodInfo, InputMethodSubtype>
2543 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2544 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2545 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002546 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002547 boolean foundInSystemIME = false;
2548
2549 // Search applicable subtype for each InputMethodInfo
2550 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002551 final String imiId = imi.getId();
2552 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2553 continue;
2554 }
satokcd7cd292010-11-20 15:46:23 +09002555 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002556 final List<InputMethodSubtype> enabledSubtypes =
2557 getEnabledInputMethodSubtypeList(imi, true);
2558 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002559 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002560 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002561 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002562 }
satokdf31ae62011-01-15 06:19:44 +09002563 // 2. Search by the system locale from enabledSubtypes.
2564 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002565 if (subtype == null) {
2566 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002567 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002568 }
satoka86f5e42011-09-02 17:12:42 +09002569 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2570 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2571 final ArrayList<InputMethodSubtype> subtypesForSearch =
2572 overridingImplicitlyEnabledSubtypes.isEmpty()
2573 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002574 // 4. Search by the current subtype's locale from all subtypes.
2575 if (subtype == null && mCurrentSubtype != null) {
2576 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002577 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002578 }
2579 // 5. Search by the system locale from all subtypes.
2580 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002581 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002582 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002583 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002584 }
satokcd7cd292010-11-20 15:46:23 +09002585 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002586 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002587 // The current input method is the most applicable IME.
2588 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002589 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002590 break;
satok7599a7fb2010-12-22 13:45:23 +09002591 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002592 // The system input method is 2nd applicable IME.
2593 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002594 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002595 if ((imi.getServiceInfo().applicationInfo.flags
2596 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2597 foundInSystemIME = true;
2598 }
satok4e4569d2010-11-19 18:45:53 +09002599 }
2600 }
2601 }
2602 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002603 if (mostApplicableIMI != null) {
2604 Slog.w(TAG, "Most applicable shortcut input method was:"
2605 + mostApplicableIMI.getId());
2606 if (mostApplicableSubtype != null) {
2607 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2608 + "," + mostApplicableSubtype.getMode() + ","
2609 + mostApplicableSubtype.getLocale());
2610 }
2611 }
satok4e4569d2010-11-19 18:45:53 +09002612 }
satokcd7cd292010-11-20 15:46:23 +09002613 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002614 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002615 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002616 } else {
2617 return null;
2618 }
2619 }
2620
satokab751aa2010-09-14 19:17:36 +09002621 /**
2622 * @return Return the current subtype of this input method.
2623 */
satok42c5a162011-05-26 16:46:14 +09002624 @Override
satokab751aa2010-09-14 19:17:36 +09002625 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002626 boolean subtypeIsSelected = false;
2627 try {
2628 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2629 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2630 } catch (SettingNotFoundException e) {
2631 }
satok3ef8b292010-11-23 06:06:29 +09002632 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002633 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002634 String lastInputMethodId = Settings.Secure.getString(
2635 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002636 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2637 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002638 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2639 if (imi != null) {
2640 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002641 // the most applicable subtype from explicitly or implicitly enabled
2642 // subtypes.
2643 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2644 getEnabledInputMethodSubtypeList(imi, true);
2645 // If there is only one explicitly or implicitly enabled subtype,
2646 // just returns it.
2647 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2648 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2649 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2650 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2651 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2652 SUBTYPE_MODE_KEYBOARD, null, true);
2653 if (mCurrentSubtype == null) {
2654 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2655 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2656 true);
2657 }
2658 }
satok4e4569d2010-11-19 18:45:53 +09002659 }
satokcd7cd292010-11-20 15:46:23 +09002660 } else {
satok3ef8b292010-11-23 06:06:29 +09002661 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002662 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002663 }
satok8fbb1e82010-11-02 23:15:58 +09002664 }
satok3ef8b292010-11-23 06:06:29 +09002665 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002666 }
satokab751aa2010-09-14 19:17:36 +09002667 }
2668
satokf3db1af2010-11-23 13:34:33 +09002669 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2670 InputMethodSubtype subtype) {
2671 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2672 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2673 } else {
2674 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2675 subtypes.add(subtype);
2676 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2677 }
2678 }
2679
satok4e4569d2010-11-19 18:45:53 +09002680 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002681 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002682 @Override
satok4e4569d2010-11-19 18:45:53 +09002683 public List getShortcutInputMethodsAndSubtypes() {
2684 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002685 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002686 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002687 // If there are no selected shortcut subtypes, the framework will try to find
2688 // the most applicable subtype from all subtypes whose mode is
2689 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002690 Pair<InputMethodInfo, InputMethodSubtype> info =
2691 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2692 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002693 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002694 ret.add(info.first);
2695 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002696 }
satok3da92232011-01-11 22:46:30 +09002697 return ret;
satokf3db1af2010-11-23 13:34:33 +09002698 }
satokf3db1af2010-11-23 13:34:33 +09002699 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2700 ret.add(imi);
2701 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2702 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002703 }
2704 }
satokf3db1af2010-11-23 13:34:33 +09002705 return ret;
satok4e4569d2010-11-19 18:45:53 +09002706 }
2707 }
2708
satok42c5a162011-05-26 16:46:14 +09002709 @Override
satokb66d2872010-11-10 01:04:04 +09002710 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2711 synchronized (mMethodMap) {
2712 if (subtype != null && mCurMethodId != null) {
2713 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2714 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2715 if (subtypeId != NOT_A_SUBTYPE_ID) {
2716 setInputMethodLocked(mCurMethodId, subtypeId);
2717 return true;
2718 }
2719 }
2720 return false;
2721 }
2722 }
2723
satokd87c2592010-09-29 11:52:06 +09002724 /**
2725 * Utility class for putting and getting settings for InputMethod
2726 * TODO: Move all putters and getters of settings to this class.
2727 */
2728 private static class InputMethodSettings {
2729 // The string for enabled input method is saved as follows:
2730 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2731 private static final char INPUT_METHOD_SEPARATER = ':';
2732 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002733 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002734 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2735
satok723a27e2010-11-11 14:58:11 +09002736 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002737 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2738
satokdf31ae62011-01-15 06:19:44 +09002739 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002740 private final ContentResolver mResolver;
2741 private final HashMap<String, InputMethodInfo> mMethodMap;
2742 private final ArrayList<InputMethodInfo> mMethodList;
2743
2744 private String mEnabledInputMethodsStrCache;
2745
2746 private static void buildEnabledInputMethodsSettingString(
2747 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2748 String id = pair.first;
2749 ArrayList<String> subtypes = pair.second;
2750 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002751 // Inputmethod and subtypes are saved in the settings as follows:
2752 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2753 for (String subtypeId: subtypes) {
2754 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002755 }
2756 }
2757
2758 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002759 Resources res, ContentResolver resolver,
2760 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2761 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002762 mResolver = resolver;
2763 mMethodMap = methodMap;
2764 mMethodList = methodList;
2765 }
2766
2767 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2768 return createEnabledInputMethodListLocked(
2769 getEnabledInputMethodsAndSubtypeListLocked());
2770 }
2771
satok7f35c8c2010-10-07 21:13:11 +09002772 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002773 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2774 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002775 getEnabledInputMethodsAndSubtypeListLocked());
2776 }
2777
satok67ddf9c2010-11-17 09:45:54 +09002778 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2779 InputMethodInfo imi) {
2780 List<Pair<String, ArrayList<String>>> imsList =
2781 getEnabledInputMethodsAndSubtypeListLocked();
2782 ArrayList<InputMethodSubtype> enabledSubtypes =
2783 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002784 if (imi != null) {
2785 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2786 InputMethodInfo info = mMethodMap.get(imsPair.first);
2787 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002788 final int subtypeCount = info.getSubtypeCount();
2789 for (int i = 0; i < subtypeCount; ++i) {
2790 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002791 for (String s: imsPair.second) {
2792 if (String.valueOf(ims.hashCode()).equals(s)) {
2793 enabledSubtypes.add(ims);
2794 }
satok67ddf9c2010-11-17 09:45:54 +09002795 }
2796 }
satok884ef9a2010-11-18 10:39:46 +09002797 break;
satok67ddf9c2010-11-17 09:45:54 +09002798 }
satok67ddf9c2010-11-17 09:45:54 +09002799 }
2800 }
2801 return enabledSubtypes;
2802 }
2803
satokd87c2592010-09-29 11:52:06 +09002804 // At the initial boot, the settings for input methods are not set,
2805 // so we need to enable IME in that case.
2806 public void enableAllIMEsIfThereIsNoEnabledIME() {
2807 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2808 StringBuilder sb = new StringBuilder();
2809 final int N = mMethodList.size();
2810 for (int i = 0; i < N; i++) {
2811 InputMethodInfo imi = mMethodList.get(i);
2812 Slog.i(TAG, "Adding: " + imi.getId());
2813 if (i > 0) sb.append(':');
2814 sb.append(imi.getId());
2815 }
2816 putEnabledInputMethodsStr(sb.toString());
2817 }
2818 }
2819
satokbb4aa062011-01-19 21:40:27 +09002820 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002821 ArrayList<Pair<String, ArrayList<String>>> imsList
2822 = new ArrayList<Pair<String, ArrayList<String>>>();
2823 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2824 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2825 return imsList;
2826 }
satok723a27e2010-11-11 14:58:11 +09002827 mInputMethodSplitter.setString(enabledInputMethodsStr);
2828 while (mInputMethodSplitter.hasNext()) {
2829 String nextImsStr = mInputMethodSplitter.next();
2830 mSubtypeSplitter.setString(nextImsStr);
2831 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002832 ArrayList<String> subtypeHashes = new ArrayList<String>();
2833 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002834 String imeId = mSubtypeSplitter.next();
2835 while (mSubtypeSplitter.hasNext()) {
2836 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002837 }
2838 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2839 }
2840 }
2841 return imsList;
2842 }
2843
2844 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2845 if (reloadInputMethodStr) {
2846 getEnabledInputMethodsStr();
2847 }
2848 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2849 // Add in the newly enabled input method.
2850 putEnabledInputMethodsStr(id);
2851 } else {
2852 putEnabledInputMethodsStr(
2853 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2854 }
2855 }
2856
2857 /**
2858 * Build and put a string of EnabledInputMethods with removing specified Id.
2859 * @return the specified id was removed or not.
2860 */
2861 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2862 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2863 boolean isRemoved = false;
2864 boolean needsAppendSeparator = false;
2865 for (Pair<String, ArrayList<String>> ims: imsList) {
2866 String curId = ims.first;
2867 if (curId.equals(id)) {
2868 // We are disabling this input method, and it is
2869 // currently enabled. Skip it to remove from the
2870 // new list.
2871 isRemoved = true;
2872 } else {
2873 if (needsAppendSeparator) {
2874 builder.append(INPUT_METHOD_SEPARATER);
2875 } else {
2876 needsAppendSeparator = true;
2877 }
2878 buildEnabledInputMethodsSettingString(builder, ims);
2879 }
2880 }
2881 if (isRemoved) {
2882 // Update the setting with the new list of input methods.
2883 putEnabledInputMethodsStr(builder.toString());
2884 }
2885 return isRemoved;
2886 }
2887
2888 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2889 List<Pair<String, ArrayList<String>>> imsList) {
2890 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2891 for (Pair<String, ArrayList<String>> ims: imsList) {
2892 InputMethodInfo info = mMethodMap.get(ims.first);
2893 if (info != null) {
2894 res.add(info);
2895 }
2896 }
2897 return res;
2898 }
2899
satok7f35c8c2010-10-07 21:13:11 +09002900 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002901 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002902 List<Pair<String, ArrayList<String>>> imsList) {
2903 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2904 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2905 for (Pair<String, ArrayList<String>> ims : imsList) {
2906 InputMethodInfo info = mMethodMap.get(ims.first);
2907 if (info != null) {
2908 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2909 }
2910 }
2911 return res;
2912 }
2913
satokd87c2592010-09-29 11:52:06 +09002914 private void putEnabledInputMethodsStr(String str) {
2915 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2916 mEnabledInputMethodsStrCache = str;
2917 }
2918
2919 private String getEnabledInputMethodsStr() {
2920 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2921 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002922 if (DEBUG) {
2923 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2924 }
satokd87c2592010-09-29 11:52:06 +09002925 return mEnabledInputMethodsStrCache;
2926 }
satok723a27e2010-11-11 14:58:11 +09002927
2928 private void saveSubtypeHistory(
2929 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2930 StringBuilder builder = new StringBuilder();
2931 boolean isImeAdded = false;
2932 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2933 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2934 newSubtypeId);
2935 isImeAdded = true;
2936 }
2937 for (Pair<String, String> ime: savedImes) {
2938 String imeId = ime.first;
2939 String subtypeId = ime.second;
2940 if (TextUtils.isEmpty(subtypeId)) {
2941 subtypeId = NOT_A_SUBTYPE_ID_STR;
2942 }
2943 if (isImeAdded) {
2944 builder.append(INPUT_METHOD_SEPARATER);
2945 } else {
2946 isImeAdded = true;
2947 }
2948 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2949 subtypeId);
2950 }
2951 // Remove the last INPUT_METHOD_SEPARATER
2952 putSubtypeHistoryStr(builder.toString());
2953 }
2954
2955 public void addSubtypeToHistory(String imeId, String subtypeId) {
2956 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2957 for (Pair<String, String> ime: subtypeHistory) {
2958 if (ime.first.equals(imeId)) {
2959 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002960 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002961 + ime.second);
2962 }
2963 // We should break here
2964 subtypeHistory.remove(ime);
2965 break;
2966 }
2967 }
satokbb4aa062011-01-19 21:40:27 +09002968 if (DEBUG) {
2969 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2970 }
satok723a27e2010-11-11 14:58:11 +09002971 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2972 }
2973
2974 private void putSubtypeHistoryStr(String str) {
2975 if (DEBUG) {
2976 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2977 }
2978 Settings.Secure.putString(
2979 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2980 }
2981
2982 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2983 // Gets the first one from the history
2984 return getLastSubtypeForInputMethodLockedInternal(null);
2985 }
2986
2987 public String getLastSubtypeForInputMethodLocked(String imeId) {
2988 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2989 if (ime != null) {
2990 return ime.second;
2991 } else {
2992 return null;
2993 }
2994 }
2995
2996 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2997 List<Pair<String, ArrayList<String>>> enabledImes =
2998 getEnabledInputMethodsAndSubtypeListLocked();
2999 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003000 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003001 final String imeInTheHistory = imeAndSubtype.first;
3002 // If imeId is empty, returns the first IME and subtype in the history
3003 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3004 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003005 final String subtypeHashCode =
3006 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3007 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003008 if (!TextUtils.isEmpty(subtypeHashCode)) {
3009 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003010 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003011 }
3012 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3013 }
3014 }
3015 }
3016 if (DEBUG) {
3017 Slog.d(TAG, "No enabled IME found in the history");
3018 }
3019 return null;
3020 }
3021
satokdf31ae62011-01-15 06:19:44 +09003022 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003023 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3024 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3025 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003026 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3027 if (explicitlyEnabledSubtypes.size() == 0) {
3028 // If there are no explicitly enabled subtypes, applicable subtypes are
3029 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003030 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003031 // If IME is enabled and no subtypes are enabled, applicable subtypes
3032 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003033 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003034 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003035 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003036 if (implicitlySelectedSubtypes != null) {
3037 final int N = implicitlySelectedSubtypes.size();
3038 for (int i = 0; i < N; ++i) {
3039 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3040 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3041 return subtypeHashCode;
3042 }
3043 }
3044 }
3045 }
3046 } else {
satokf6cafb62011-01-17 16:29:02 +09003047 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003048 if (s.equals(subtypeHashCode)) {
3049 // If both imeId and subtypeId are enabled, return subtypeId.
3050 return s;
3051 }
satok723a27e2010-11-11 14:58:11 +09003052 }
3053 }
3054 // If imeId was enabled but subtypeId was disabled.
3055 return NOT_A_SUBTYPE_ID_STR;
3056 }
3057 }
3058 // If both imeId and subtypeId are disabled, return null
3059 return null;
3060 }
3061
3062 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3063 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3064 final String subtypeHistoryStr = getSubtypeHistoryStr();
3065 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3066 return imsList;
3067 }
3068 mInputMethodSplitter.setString(subtypeHistoryStr);
3069 while (mInputMethodSplitter.hasNext()) {
3070 String nextImsStr = mInputMethodSplitter.next();
3071 mSubtypeSplitter.setString(nextImsStr);
3072 if (mSubtypeSplitter.hasNext()) {
3073 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3074 // The first element is ime id.
3075 String imeId = mSubtypeSplitter.next();
3076 while (mSubtypeSplitter.hasNext()) {
3077 subtypeId = mSubtypeSplitter.next();
3078 break;
3079 }
3080 imsList.add(new Pair<String, String>(imeId, subtypeId));
3081 }
3082 }
3083 return imsList;
3084 }
3085
3086 private String getSubtypeHistoryStr() {
3087 if (DEBUG) {
3088 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3089 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3090 }
3091 return Settings.Secure.getString(
3092 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3093 }
3094
3095 public void putSelectedInputMethod(String imeId) {
3096 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3097 }
3098
3099 public void putSelectedSubtype(int subtypeId) {
3100 Settings.Secure.putInt(
3101 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3102 }
satokd87c2592010-09-29 11:52:06 +09003103 }
3104
satoke7c6998e2011-06-03 17:57:59 +09003105 private static class InputMethodFileManager {
3106 private static final String SYSTEM_PATH = "system";
3107 private static final String INPUT_METHOD_PATH = "inputmethod";
3108 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3109 private static final String NODE_SUBTYPES = "subtypes";
3110 private static final String NODE_SUBTYPE = "subtype";
3111 private static final String NODE_IMI = "imi";
3112 private static final String ATTR_ID = "id";
3113 private static final String ATTR_LABEL = "label";
3114 private static final String ATTR_ICON = "icon";
3115 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3116 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3117 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3118 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3119 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3120 private final HashMap<String, InputMethodInfo> mMethodMap;
3121 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3122 new HashMap<String, List<InputMethodSubtype>>();
3123 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3124 if (methodMap == null) {
3125 throw new NullPointerException("methodMap is null");
3126 }
3127 mMethodMap = methodMap;
3128 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3129 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3130 if (!inputMethodDir.mkdirs()) {
3131 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3132 }
3133 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3134 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3135 if (!subtypeFile.exists()) {
3136 // If "subtypes.xml" doesn't exist, create a blank file.
3137 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3138 methodMap);
3139 } else {
3140 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3141 }
3142 }
3143
3144 private void deleteAllInputMethodSubtypes(String imiId) {
3145 synchronized (mMethodMap) {
3146 mSubtypesMap.remove(imiId);
3147 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3148 mMethodMap);
3149 }
3150 }
3151
3152 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003153 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003154 synchronized (mMethodMap) {
3155 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3156 final int N = additionalSubtypes.length;
3157 for (int i = 0; i < N; ++i) {
3158 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003159 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003160 subtypes.add(subtype);
3161 }
3162 }
satok4a28bde2011-06-29 21:03:40 +09003163 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003164 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3165 mMethodMap);
3166 }
3167 }
3168
3169 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3170 synchronized (mMethodMap) {
3171 return mSubtypesMap;
3172 }
3173 }
3174
3175 private static void writeAdditionalInputMethodSubtypes(
3176 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3177 HashMap<String, InputMethodInfo> methodMap) {
3178 // Safety net for the case that this function is called before methodMap is set.
3179 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3180 FileOutputStream fos = null;
3181 try {
3182 fos = subtypesFile.startWrite();
3183 final XmlSerializer out = new FastXmlSerializer();
3184 out.setOutput(fos, "utf-8");
3185 out.startDocument(null, true);
3186 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3187 out.startTag(null, NODE_SUBTYPES);
3188 for (String imiId : allSubtypes.keySet()) {
3189 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3190 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3191 continue;
3192 }
3193 out.startTag(null, NODE_IMI);
3194 out.attribute(null, ATTR_ID, imiId);
3195 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3196 final int N = subtypesList.size();
3197 for (int i = 0; i < N; ++i) {
3198 final InputMethodSubtype subtype = subtypesList.get(i);
3199 out.startTag(null, NODE_SUBTYPE);
3200 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3201 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3202 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3203 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3204 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3205 out.attribute(null, ATTR_IS_AUXILIARY,
3206 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3207 out.endTag(null, NODE_SUBTYPE);
3208 }
3209 out.endTag(null, NODE_IMI);
3210 }
3211 out.endTag(null, NODE_SUBTYPES);
3212 out.endDocument();
3213 subtypesFile.finishWrite(fos);
3214 } catch (java.io.IOException e) {
3215 Slog.w(TAG, "Error writing subtypes", e);
3216 if (fos != null) {
3217 subtypesFile.failWrite(fos);
3218 }
3219 }
3220 }
3221
3222 private static void readAdditionalInputMethodSubtypes(
3223 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3224 if (allSubtypes == null || subtypesFile == null) return;
3225 allSubtypes.clear();
3226 FileInputStream fis = null;
3227 try {
3228 fis = subtypesFile.openRead();
3229 final XmlPullParser parser = Xml.newPullParser();
3230 parser.setInput(fis, null);
3231 int type = parser.getEventType();
3232 // Skip parsing until START_TAG
3233 while ((type = parser.next()) != XmlPullParser.START_TAG
3234 && type != XmlPullParser.END_DOCUMENT) {}
3235 String firstNodeName = parser.getName();
3236 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3237 throw new XmlPullParserException("Xml doesn't start with subtypes");
3238 }
3239 final int depth =parser.getDepth();
3240 String currentImiId = null;
3241 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3242 while (((type = parser.next()) != XmlPullParser.END_TAG
3243 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3244 if (type != XmlPullParser.START_TAG)
3245 continue;
3246 final String nodeName = parser.getName();
3247 if (NODE_IMI.equals(nodeName)) {
3248 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3249 if (TextUtils.isEmpty(currentImiId)) {
3250 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3251 continue;
3252 }
3253 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3254 allSubtypes.put(currentImiId, tempSubtypesArray);
3255 } else if (NODE_SUBTYPE.equals(nodeName)) {
3256 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3257 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3258 continue;
3259 }
3260 final int icon = Integer.valueOf(
3261 parser.getAttributeValue(null, ATTR_ICON));
3262 final int label = Integer.valueOf(
3263 parser.getAttributeValue(null, ATTR_LABEL));
3264 final String imeSubtypeLocale =
3265 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3266 final String imeSubtypeMode =
3267 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3268 final String imeSubtypeExtraValue =
3269 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003270 final boolean isAuxiliary = "1".equals(String.valueOf(
3271 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003272 final InputMethodSubtype subtype =
3273 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3274 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3275 tempSubtypesArray.add(subtype);
3276 }
3277 }
3278 } catch (XmlPullParserException e) {
3279 Slog.w(TAG, "Error reading subtypes: " + e);
3280 return;
3281 } catch (java.io.IOException e) {
3282 Slog.w(TAG, "Error reading subtypes: " + e);
3283 return;
3284 } catch (NumberFormatException e) {
3285 Slog.w(TAG, "Error reading subtypes: " + e);
3286 return;
3287 } finally {
3288 if (fis != null) {
3289 try {
3290 fis.close();
3291 } catch (java.io.IOException e1) {
3292 Slog.w(TAG, "Failed to close.");
3293 }
3294 }
3295 }
3296 }
3297 }
3298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 @Override
3302 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3303 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3304 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3307 + Binder.getCallingPid()
3308 + ", uid=" + Binder.getCallingUid());
3309 return;
3310 }
3311
3312 IInputMethod method;
3313 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 synchronized (mMethodMap) {
3318 p.println("Current Input Method Manager state:");
3319 int N = mMethodList.size();
3320 p.println(" Input Methods:");
3321 for (int i=0; i<N; i++) {
3322 InputMethodInfo info = mMethodList.get(i);
3323 p.println(" InputMethod #" + i + ":");
3324 info.dump(p, " ");
3325 }
3326 p.println(" Clients:");
3327 for (ClientState ci : mClients.values()) {
3328 p.println(" Client " + ci + ":");
3329 p.println(" client=" + ci.client);
3330 p.println(" inputContext=" + ci.inputContext);
3331 p.println(" sessionRequested=" + ci.sessionRequested);
3332 p.println(" curSession=" + ci.curSession);
3333 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003334 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003336 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3337 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3339 + " mBoundToMethod=" + mBoundToMethod);
3340 p.println(" mCurToken=" + mCurToken);
3341 p.println(" mCurIntent=" + mCurIntent);
3342 method = mCurMethod;
3343 p.println(" mCurMethod=" + mCurMethod);
3344 p.println(" mEnabledSession=" + mEnabledSession);
3345 p.println(" mShowRequested=" + mShowRequested
3346 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3347 + " mShowForced=" + mShowForced
3348 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003349 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003351
Jeff Brownb88102f2010-09-08 11:49:43 -07003352 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 pw.flush();
3355 try {
3356 client.client.asBinder().dump(fd, args);
3357 } catch (RemoteException e) {
3358 p.println("Input method client dead: " + e);
3359 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003360 } else {
3361 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
Jeff Brownb88102f2010-09-08 11:49:43 -07003364 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 pw.flush();
3367 try {
3368 method.asBinder().dump(fd, args);
3369 } catch (RemoteException e) {
3370 p.println("Input method service dead: " + e);
3371 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003372 } else {
3373 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 }
3375 }
3376}