blob: ddac35c5d533b77d0ae6f4ec3e1a3e4ad74be154 [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
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700164 // Used to bring IME service up to visible adjustment while it is being shown.
165 final ServiceConnection mVisibleConnection = new ServiceConnection() {
166 @Override public void onServiceConnected(ComponentName name, IBinder service) {
167 }
168
169 @Override public void onServiceDisconnected(ComponentName name) {
170 }
171 };
172 boolean mVisibleBound = false;
173
satok7cfc0ed2011-06-20 21:29:36 +0900174 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700175 private NotificationManager mNotificationManager;
176 private KeyguardManager mKeyguardManager;
177 private StatusBarManagerService mStatusBar;
178 private Notification mImeSwitcherNotification;
179 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900180 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900181 private boolean mNotificationShown;
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 class SessionState {
184 final ClientState client;
185 final IInputMethod method;
186 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 @Override
189 public String toString() {
190 return "SessionState{uid " + client.uid + " pid " + client.pid
191 + " method " + Integer.toHexString(
192 System.identityHashCode(method))
193 + " session " + Integer.toHexString(
194 System.identityHashCode(session))
195 + "}";
196 }
197
198 SessionState(ClientState _client, IInputMethod _method,
199 IInputMethodSession _session) {
200 client = _client;
201 method = _method;
202 session = _session;
203 }
204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 class ClientState {
207 final IInputMethodClient client;
208 final IInputContext inputContext;
209 final int uid;
210 final int pid;
211 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 boolean sessionRequested;
214 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 @Override
217 public String toString() {
218 return "ClientState{" + Integer.toHexString(
219 System.identityHashCode(this)) + " uid " + uid
220 + " pid " + pid + "}";
221 }
222
223 ClientState(IInputMethodClient _client, IInputContext _inputContext,
224 int _uid, int _pid) {
225 client = _client;
226 inputContext = _inputContext;
227 uid = _uid;
228 pid = _pid;
229 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
230 }
231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final HashMap<IBinder, ClientState> mClients
234 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700237 * Set once the system is ready to run third party code.
238 */
239 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * Id of the currently selected input method.
243 */
244 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * The current binding sequence number, incremented every time there is
248 * a new bind performed.
249 */
250 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * The client that is currently bound to an input method.
254 */
255 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700258 * The last window token that gained focus.
259 */
260 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * The input context last provided by the current client.
264 */
265 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * The attributes last provided by the current client.
269 */
270 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The input method ID of the input method service that we are currently
274 * connected to or in the process of connecting to.
275 */
276 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
satokab751aa2010-09-14 19:17:36 +0900279 * The current subtype of the current input method.
280 */
281 private InputMethodSubtype mCurrentSubtype;
282
satok4e4569d2010-11-19 18:45:53 +0900283 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900284 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
285 mShortcutInputMethodsAndSubtypes =
286 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900287
288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * Set to true if our ServiceConnection is currently actively bound to
290 * a service (whether or not we have gotten its IBinder back yet).
291 */
292 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if the client has asked for the input method to be shown.
296 */
297 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Set if we were explicitly told to show the input method.
301 */
302 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * Set if we were forced to be shown.
306 */
307 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * Set if we last told the input method to show itself.
311 */
312 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The Intent used to connect to the current input method.
316 */
317 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * The token we have made for the currently active input method, to
321 * identify it in the future.
322 */
323 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * If non-null, this is the input method service we are currently connected
327 * to.
328 */
329 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * Time that we last initiated a bind to the input method, to determine
333 * if we should try to disconnect and reconnect to it.
334 */
335 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * Have we called mCurMethod.bindInput()?
339 */
340 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Currently enabled session. Only touched by service thread, not
344 * protected by a lock.
345 */
346 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * True if the screen is on. The value is true initially.
350 */
351 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Joe Onorato857fd9b2011-01-27 15:08:35 -0800353 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
354 int mImeWindowVis;
355
Ken Wakasa05dbb652011-08-22 15:22:43 +0900356 private AlertDialog.Builder mDialogBuilder;
357 private AlertDialog mSwitchingDialog;
358 private InputMethodInfo[] mIms;
359 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 class SettingsObserver extends ContentObserver {
362 SettingsObserver(Handler handler) {
363 super(handler);
364 ContentResolver resolver = mContext.getContentResolver();
365 resolver.registerContentObserver(Settings.Secure.getUriFor(
366 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900367 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900368 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
369 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900370 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 @Override public void onChange(boolean selfChange) {
374 synchronized (mMethodMap) {
375 updateFromSettingsLocked();
376 }
377 }
378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
381 @Override
382 public void onReceive(Context context, Intent intent) {
383 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
384 mScreenOn = true;
385 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
386 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900387 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700388 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
389 hideInputMethodMenu();
390 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800392 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394
395 // Inform the current client of the change in active status
396 try {
397 if (mCurClient != null && mCurClient.client != null) {
398 mCurClient.client.setActive(mScreenOn);
399 }
400 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800401 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 + mCurClient.pid + " uid " + mCurClient.uid);
403 }
404 }
405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800407 class MyPackageMonitor extends PackageMonitor {
408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800412 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
414 final int N = mMethodList.size();
415 if (curInputMethodId != null) {
416 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 InputMethodInfo imi = mMethodList.get(i);
418 if (imi.getId().equals(curInputMethodId)) {
419 for (String pkg : packages) {
420 if (imi.getPackageName().equals(pkg)) {
421 if (!doit) {
422 return true;
423 }
satok723a27e2010-11-11 14:58:11 +0900424 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800425 chooseNewDefaultIMELocked();
426 return true;
427 }
428 }
429 }
430 }
431 }
432 }
433 return false;
434 }
435
436 @Override
437 public void onSomePackagesChanged() {
438 synchronized (mMethodMap) {
439 InputMethodInfo curIm = null;
440 String curInputMethodId = Settings.Secure.getString(mContext
441 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
442 final int N = mMethodList.size();
443 if (curInputMethodId != null) {
444 for (int i=0; i<N; i++) {
445 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900446 final String imiId = imi.getId();
447 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 curIm = imi;
449 }
satoke7c6998e2011-06-03 17:57:59 +0900450
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800451 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900452 if (isPackageModified(imi.getPackageName())) {
453 mFileManager.deleteAllInputMethodSubtypes(imiId);
454 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800455 if (change == PACKAGE_TEMPORARY_CHANGE
456 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800457 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 + imi.getComponent());
459 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 buildInputMethodListLocked(mMethodList, mMethodMap);
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800468 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 int change = isPackageDisappearing(curIm.getPackageName());
470 if (change == PACKAGE_TEMPORARY_CHANGE
471 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800472 ServiceInfo si = null;
473 try {
474 si = mContext.getPackageManager().getServiceInfo(
475 curIm.getComponent(), 0);
476 } catch (PackageManager.NameNotFoundException ex) {
477 }
478 if (si == null) {
479 // Uh oh, current input method is no longer around!
480 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800481 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900482 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800484 changed = true;
485 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800486 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900487 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800488 }
489 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800490 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800491 }
satokab751aa2010-09-14 19:17:36 +0900492
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800493 if (curIm == null) {
494 // We currently don't have a default input method... is
495 // one now available?
496 changed = chooseNewDefaultIMELocked();
497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800499 if (changed) {
500 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
502 }
503 }
504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800505
Jean Chalarde0d32a62011-10-20 20:36:07 +0900506 private static class MethodCallback extends IInputMethodCallback.Stub {
507 private final IInputMethod mMethod;
508 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Jean Chalarde0d32a62011-10-20 20:36:07 +0900510 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900512 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
satoke7c6998e2011-06-03 17:57:59 +0900515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public void finishedEvent(int seq, boolean handled) throws RemoteException {
517 }
518
satoke7c6998e2011-06-03 17:57:59 +0900519 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900521 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Dianne Hackborn661cd522011-08-22 00:26:20 -0700525 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800527 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mHandler = new Handler(this);
529 mIWindowManager = IWindowManager.Stub.asInterface(
530 ServiceManager.getService(Context.WINDOW_SERVICE));
531 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public void executeMessage(Message msg) {
534 handleMessage(msg);
535 }
536 });
satok7cfc0ed2011-06-20 21:29:36 +0900537
satok7cfc0ed2011-06-20 21:29:36 +0900538 mImeSwitcherNotification = new Notification();
539 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
540 mImeSwitcherNotification.when = 0;
541 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
542 mImeSwitcherNotification.tickerText = null;
543 mImeSwitcherNotification.defaults = 0; // please be quiet
544 mImeSwitcherNotification.sound = null;
545 mImeSwitcherNotification.vibrate = null;
546 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900547 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900548
549 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900550
satoke7c6998e2011-06-03 17:57:59 +0900551 synchronized (mMethodMap) {
552 mFileManager = new InputMethodFileManager(mMethodMap);
553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800555 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 IntentFilter screenOnOffFilt = new IntentFilter();
558 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
559 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700560 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800562
satok7cfc0ed2011-06-20 21:29:36 +0900563 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900564
satokd87c2592010-09-29 11:52:06 +0900565 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900566 mSettings = new InputMethodSettings(
567 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900569 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
satokd87c2592010-09-29 11:52:06 +0900571 if (TextUtils.isEmpty(Settings.Secure.getString(
572 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900574 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
576 try {
satokd87c2592010-09-29 11:52:06 +0900577 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 imi.getPackageName(), 0).getResources();
579 if (res.getBoolean(imi.getIsDefaultResourceId())) {
580 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800581 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583 } catch (PackageManager.NameNotFoundException ex) {
584 } catch (Resources.NotFoundException ex) {
585 }
586 }
587 }
satokd87c2592010-09-29 11:52:06 +0900588 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900589 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800590 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900593 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 mSettingsObserver = new SettingsObserver(mHandler);
598 updateFromSettingsLocked();
599 }
600
601 @Override
602 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
603 throws RemoteException {
604 try {
605 return super.onTransact(code, data, reply, flags);
606 } catch (RuntimeException e) {
607 // The input method manager only throws security exceptions, so let's
608 // log all others.
609 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800610 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612 throw e;
613 }
614 }
615
Dianne Hackborn661cd522011-08-22 00:26:20 -0700616 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700617 synchronized (mMethodMap) {
618 if (!mSystemReady) {
619 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700620 mKeyguardManager = (KeyguardManager)
621 mContext.getSystemService(Context.KEYGUARD_SERVICE);
622 mNotificationManager = (NotificationManager)
623 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
624 mStatusBar = statusBar;
625 statusBar.setIconVisibility("ime", false);
626 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900627 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
628 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700629 try {
630 startInputInnerLocked();
631 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800632 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700633 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700634 }
635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
satok15452a42011-10-28 17:58:28 +0900638 private void setImeWindowVisibilityStatusHiddenLocked() {
639 mImeWindowVis = 0;
640 updateImeWindowStatusLocked();
641 }
642
643 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900644 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700645 }
646
satoke7c6998e2011-06-03 17:57:59 +0900647 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 public List<InputMethodInfo> getInputMethodList() {
649 synchronized (mMethodMap) {
650 return new ArrayList<InputMethodInfo>(mMethodList);
651 }
652 }
653
satoke7c6998e2011-06-03 17:57:59 +0900654 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 public List<InputMethodInfo> getEnabledInputMethodList() {
656 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900657 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
660
satokbb4aa062011-01-19 21:40:27 +0900661 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
662 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
663 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
664 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
665 for (InputMethodInfo imi: getEnabledInputMethodList()) {
666 enabledInputMethodAndSubtypes.put(
667 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
668 }
669 return enabledInputMethodAndSubtypes;
670 }
671
672 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
673 boolean allowsImplicitlySelectedSubtypes) {
674 if (imi == null && mCurMethodId != null) {
675 imi = mMethodMap.get(mCurMethodId);
676 }
satok7265d9b2011-02-14 15:47:30 +0900677 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900678 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900679 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900680 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900681 }
satok7265d9b2011-02-14 15:47:30 +0900682 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900683 }
684
satoke7c6998e2011-06-03 17:57:59 +0900685 @Override
satok16331c82010-12-20 23:48:46 +0900686 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
687 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900688 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900689 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900690 }
691 }
692
satoke7c6998e2011-06-03 17:57:59 +0900693 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 public void addClient(IInputMethodClient client,
695 IInputContext inputContext, int uid, int pid) {
696 synchronized (mMethodMap) {
697 mClients.put(client.asBinder(), new ClientState(client,
698 inputContext, uid, pid));
699 }
700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
satoke7c6998e2011-06-03 17:57:59 +0900702 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 public void removeClient(IInputMethodClient client) {
704 synchronized (mMethodMap) {
705 mClients.remove(client.asBinder());
706 }
707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 void executeOrSendMessage(IInterface target, Message msg) {
710 if (target.asBinder() instanceof Binder) {
711 mCaller.sendMessage(msg);
712 } else {
713 handleMessage(msg);
714 msg.recycle();
715 }
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700718 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 + mCurClient.client.asBinder());
722 if (mBoundToMethod) {
723 mBoundToMethod = false;
724 if (mCurMethod != null) {
725 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
726 MSG_UNBIND_INPUT, mCurMethod));
727 }
728 }
729 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
730 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
731 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 // Call setActive(false) on the old client
734 try {
735 mCurClient.client.setActive(false);
736 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800737 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 + mCurClient.pid + " uid " + mCurClient.uid);
739 }
740 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
The Android Open Source Project10592532009-03-18 17:39:46 -0700742 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 private int getImeShowFlags() {
747 int flags = 0;
748 if (mShowForced) {
749 flags |= InputMethod.SHOW_FORCED
750 | InputMethod.SHOW_EXPLICIT;
751 } else if (mShowExplicitlyRequested) {
752 flags |= InputMethod.SHOW_EXPLICIT;
753 }
754 return flags;
755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 private int getAppShowFlags() {
758 int flags = 0;
759 if (mShowForced) {
760 flags |= InputMethodManager.SHOW_FORCED;
761 } else if (!mShowExplicitlyRequested) {
762 flags |= InputMethodManager.SHOW_IMPLICIT;
763 }
764 return flags;
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
768 if (!mBoundToMethod) {
769 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
770 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
771 mBoundToMethod = true;
772 }
773 final SessionState session = mCurClient.curSession;
774 if (initial) {
775 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
776 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
777 } else {
778 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
779 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
780 }
781 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800782 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800783 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785 return needResult
786 ? new InputBindResult(session.session, mCurId, mCurSeq)
787 : null;
788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 InputBindResult startInputLocked(IInputMethodClient client,
791 IInputContext inputContext, EditorInfo attribute,
792 boolean initial, boolean needResult) {
793 // If no method is currently selected, do nothing.
794 if (mCurMethodId == null) {
795 return mNoBinding;
796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 ClientState cs = mClients.get(client.asBinder());
799 if (cs == null) {
800 throw new IllegalArgumentException("unknown client "
801 + client.asBinder());
802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 try {
805 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
806 // Check with the window manager to make sure this client actually
807 // has a window with focus. If not, reject. This is thread safe
808 // because if the focus changes some time before or after, the
809 // next client receiving focus that has any interest in input will
810 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
813 return null;
814 }
815 } catch (RemoteException e) {
816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 if (mCurClient != cs) {
819 // If the client is changing, we need to switch over to the new
820 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700821 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800822 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 + cs.client.asBinder());
824
825 // If the screen is on, inform the new client it is active
826 if (mScreenOn) {
827 try {
828 cs.client.setActive(mScreenOn);
829 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 + cs.pid + " uid " + cs.uid);
832 }
833 }
834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 // Bump up the sequence for this client and attach it.
837 mCurSeq++;
838 if (mCurSeq <= 0) mCurSeq = 1;
839 mCurClient = cs;
840 mCurInputContext = inputContext;
841 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 // Check if the input method is changing.
844 if (mCurId != null && mCurId.equals(mCurMethodId)) {
845 if (cs.curSession != null) {
846 // Fast case: if we are already connected to the input method,
847 // then just return it.
848 return attachNewInputLocked(initial, needResult);
849 }
850 if (mHaveConnection) {
851 if (mCurMethod != null) {
852 if (!cs.sessionRequested) {
853 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800854 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
856 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900857 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859 // Return to client, and we will get back with it when
860 // we have had a session made for it.
861 return new InputBindResult(null, mCurId, mCurSeq);
862 } else if (SystemClock.uptimeMillis()
863 < (mLastBindTime+TIME_TO_RECONNECT)) {
864 // In this case we have connected to the service, but
865 // don't yet have its interface. If it hasn't been too
866 // long since we did the connection, we'll return to
867 // the client and wait to get the service interface so
868 // we can report back. If it has been too long, we want
869 // to fall through so we can try a disconnect/reconnect
870 // to see if we can get back in touch with the service.
871 return new InputBindResult(null, mCurId, mCurSeq);
872 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
874 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876 }
877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700879 return startInputInnerLocked();
880 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700882 InputBindResult startInputInnerLocked() {
883 if (mCurMethodId == null) {
884 return mNoBinding;
885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700887 if (!mSystemReady) {
888 // If the system is not yet ready, we shouldn't be running third
889 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700890 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 InputMethodInfo info = mMethodMap.get(mCurMethodId);
894 if (info == null) {
895 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700898 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
901 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700902 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
903 com.android.internal.R.string.input_method_binding_label);
904 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
905 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700906 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
907 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mLastBindTime = SystemClock.uptimeMillis();
909 mHaveConnection = true;
910 mCurId = info.getId();
911 mCurToken = new Binder();
912 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800913 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 mIWindowManager.addWindowToken(mCurToken,
915 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
916 } catch (RemoteException e) {
917 }
918 return new InputBindResult(null, mCurId, mCurSeq);
919 } else {
920 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800921 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 + mCurIntent);
923 }
924 return null;
925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800926
satoke7c6998e2011-06-03 17:57:59 +0900927 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 public InputBindResult startInput(IInputMethodClient client,
929 IInputContext inputContext, EditorInfo attribute,
930 boolean initial, boolean needResult) {
931 synchronized (mMethodMap) {
932 final long ident = Binder.clearCallingIdentity();
933 try {
934 return startInputLocked(client, inputContext, attribute,
935 initial, needResult);
936 } finally {
937 Binder.restoreCallingIdentity(ident);
938 }
939 }
940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941
satoke7c6998e2011-06-03 17:57:59 +0900942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 public void finishInput(IInputMethodClient client) {
944 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945
satoke7c6998e2011-06-03 17:57:59 +0900946 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 public void onServiceConnected(ComponentName name, IBinder service) {
948 synchronized (mMethodMap) {
949 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
950 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700951 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800952 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700953 unbindCurrentMethodLocked(false);
954 return;
955 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800956 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700957 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
958 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800960 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700961 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700963 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900964 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966 }
967 }
968 }
969
970 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
971 synchronized (mMethodMap) {
972 if (mCurMethod != null && method != null
973 && mCurMethod.asBinder() == method.asBinder()) {
974 if (mCurClient != null) {
975 mCurClient.curSession = new SessionState(mCurClient,
976 method, session);
977 mCurClient.sessionRequested = false;
978 InputBindResult res = attachNewInputLocked(true, true);
979 if (res.method != null) {
980 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
981 MSG_BIND_METHOD, mCurClient.client, res));
982 }
983 }
984 }
985 }
986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700988 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700989 if (mVisibleBound) {
990 mContext.unbindService(mVisibleConnection);
991 mVisibleBound = false;
992 }
993
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700994 if (mHaveConnection) {
995 mContext.unbindService(this);
996 mHaveConnection = false;
997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800998
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700999 if (mCurToken != null) {
1000 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001001 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001002 mIWindowManager.removeWindowToken(mCurToken);
1003 } catch (RemoteException e) {
1004 }
1005 mCurToken = null;
1006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
The Android Open Source Project10592532009-03-18 17:39:46 -07001008 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001009 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001011 if (reportToClient && mCurClient != null) {
1012 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1013 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1014 }
1015 }
1016
Devin Taylor0c33ed22010-02-23 13:26:46 -06001017 private void finishSession(SessionState sessionState) {
1018 if (sessionState != null && sessionState.session != null) {
1019 try {
1020 sessionState.session.finishSession();
1021 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001022 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001023 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001024 }
1025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001028 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (mCurMethod != null) {
1030 for (ClientState cs : mClients.values()) {
1031 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001032 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 cs.curSession = null;
1034 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001035
1036 finishSession(mEnabledSession);
1037 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 mCurMethod = null;
1039 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001040 if (mStatusBar != null) {
1041 mStatusBar.setIconVisibility("ime", false);
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
satoke7c6998e2011-06-03 17:57:59 +09001045 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 public void onServiceDisconnected(ComponentName name) {
1047 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 + " mCurIntent=" + mCurIntent);
1050 if (mCurMethod != null && mCurIntent != null
1051 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001052 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 // We consider this to be a new bind attempt, since the system
1054 // should now try to restart the service for us.
1055 mLastBindTime = SystemClock.uptimeMillis();
1056 mShowRequested = mInputShown;
1057 mInputShown = false;
1058 if (mCurClient != null) {
1059 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1060 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1061 }
1062 }
1063 }
1064 }
1065
satokf9f01002011-05-19 21:31:50 +09001066 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001068 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 long ident = Binder.clearCallingIdentity();
1070 try {
1071 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001072 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 return;
1074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 synchronized (mMethodMap) {
1077 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001079 if (mStatusBar != null) {
1080 mStatusBar.setIconVisibility("ime", false);
1081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001083 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001084 CharSequence contentDescription = null;
1085 try {
1086 PackageManager packageManager = mContext.getPackageManager();
1087 contentDescription = packageManager.getApplicationLabel(
1088 packageManager.getApplicationInfo(packageName, 0));
1089 } catch (NameNotFoundException nnfe) {
1090 /* ignore */
1091 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001092 if (mStatusBar != null) {
1093 mStatusBar.setIcon("ime", packageName, iconId, 0,
1094 contentDescription != null
1095 ? contentDescription.toString() : null);
1096 mStatusBar.setIconVisibility("ime", true);
1097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
1100 } finally {
1101 Binder.restoreCallingIdentity(ident);
1102 }
1103 }
1104
satok7cfc0ed2011-06-20 21:29:36 +09001105 private boolean needsToShowImeSwitchOngoingNotification() {
1106 if (!mShowOngoingImeSwitcherForPhones) return false;
1107 synchronized (mMethodMap) {
1108 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1109 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001110 if (N > 2) return true;
1111 if (N < 1) return false;
1112 int nonAuxCount = 0;
1113 int auxCount = 0;
1114 InputMethodSubtype nonAuxSubtype = null;
1115 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001116 for(int i = 0; i < N; ++i) {
1117 final InputMethodInfo imi = imis.get(i);
1118 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1119 imi, true);
1120 final int subtypeCount = subtypes.size();
1121 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001122 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001123 } else {
1124 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001125 final InputMethodSubtype subtype = subtypes.get(j);
1126 if (!subtype.isAuxiliary()) {
1127 ++nonAuxCount;
1128 nonAuxSubtype = subtype;
1129 } else {
1130 ++auxCount;
1131 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001132 }
1133 }
1134 }
satok7cfc0ed2011-06-20 21:29:36 +09001135 }
satokb6359412011-06-28 17:47:41 +09001136 if (nonAuxCount > 1 || auxCount > 1) {
1137 return true;
1138 } else if (nonAuxCount == 1 && auxCount == 1) {
1139 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001140 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1141 || auxSubtype.overridesImplicitlyEnabledSubtype()
1142 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001143 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1144 return false;
1145 }
1146 return true;
1147 }
1148 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001149 }
satok7cfc0ed2011-06-20 21:29:36 +09001150 }
1151
satokdbf29502011-08-25 15:28:23 +09001152 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001153 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001154 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001155 int uid = Binder.getCallingUid();
1156 long ident = Binder.clearCallingIdentity();
1157 try {
1158 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001159 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001160 return;
1161 }
1162
1163 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001164 mImeWindowVis = vis;
1165 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001166 if (mStatusBar != null) {
1167 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1168 }
satok7cfc0ed2011-06-20 21:29:36 +09001169 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001170 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1171 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001172 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001173 final CharSequence title = mRes.getText(
1174 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001175 final CharSequence imiLabel = imi.loadLabel(pm);
1176 final CharSequence summary = mCurrentSubtype != null
1177 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1178 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1179 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001180 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001181 : imiLabel;
1182
satok7cfc0ed2011-06-20 21:29:36 +09001183 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001184 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001185 if (mNotificationManager != null) {
1186 mNotificationManager.notify(
1187 com.android.internal.R.string.select_input_method,
1188 mImeSwitcherNotification);
1189 mNotificationShown = true;
1190 }
satok7cfc0ed2011-06-20 21:29:36 +09001191 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001192 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001193 mNotificationManager.cancel(
1194 com.android.internal.R.string.select_input_method);
1195 mNotificationShown = false;
1196 }
1197 }
satok06487a52010-10-29 11:37:18 +09001198 }
1199 } finally {
1200 Binder.restoreCallingIdentity(ident);
1201 }
1202 }
1203
satoke7c6998e2011-06-03 17:57:59 +09001204 @Override
satokf9f01002011-05-19 21:31:50 +09001205 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1206 synchronized (mMethodMap) {
1207 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1208 for (int i = 0; i < spans.length; ++i) {
1209 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001210 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001211 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001212 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001213 }
1214 }
1215 }
1216 }
1217
satoke7c6998e2011-06-03 17:57:59 +09001218 @Override
satokf9f01002011-05-19 21:31:50 +09001219 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1220 synchronized (mMethodMap) {
1221 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1222 // TODO: Do not send the intent if the process of the targetImi is already dead.
1223 if (targetImi != null) {
1224 final String[] suggestions = span.getSuggestions();
1225 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001226 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001227 final Intent intent = new Intent();
1228 // Ensures that only a class in the original IME package will receive the
1229 // notification.
satok42c5a162011-05-26 16:46:14 +09001230 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001231 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1232 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1233 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1234 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1235 mContext.sendBroadcast(intent);
1236 return true;
1237 }
1238 }
1239 return false;
1240 }
1241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001243 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1244 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1245 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1246 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001248 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001249 // There is no input method selected, try to choose new applicable input method.
1250 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1251 id = Settings.Secure.getString(mContext.getContentResolver(),
1252 Settings.Secure.DEFAULT_INPUT_METHOD);
1253 }
1254 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 try {
satokab751aa2010-09-14 19:17:36 +09001256 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001258 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001259 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001260 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
satokf3db1af2010-11-23 13:34:33 +09001262 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001263 } else {
1264 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001265 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001266 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
satokab751aa2010-09-14 19:17:36 +09001270 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 InputMethodInfo info = mMethodMap.get(id);
1272 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001273 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001277 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001278 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1279 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001280 }
1281 if (subtype != mCurrentSubtype) {
1282 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001283 if (subtype != null) {
1284 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1285 }
satokcd7cd292010-11-20 15:46:23 +09001286 if (mCurMethod != null) {
1287 try {
satoke40dea02011-01-30 01:14:02 +09001288 final Configuration conf = mRes.getConfiguration();
1289 final boolean haveHardKeyboard = conf.keyboard
1290 != Configuration.KEYBOARD_NOKEYS;
1291 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001292 && conf.hardKeyboardHidden
1293 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001294 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1295 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1296 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001297 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001298 // If subtype is null, try to find the most applicable one from
1299 // getCurrentInputMethodSubtype.
1300 if (subtype == null) {
1301 subtype = getCurrentInputMethodSubtype();
1302 }
1303 mCurMethod.changeInputMethodSubtype(subtype);
1304 } catch (RemoteException e) {
1305 return;
satokab751aa2010-09-14 19:17:36 +09001306 }
1307 }
1308 }
1309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 return;
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 final long ident = Binder.clearCallingIdentity();
1314 try {
satokab751aa2010-09-14 19:17:36 +09001315 // Set a subtype to this input method.
1316 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001317 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1318 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1319 // because mCurMethodId is stored as a history in
1320 // setSelectedInputMethodAndSubtypeLocked().
1321 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322
1323 if (ActivityManagerNative.isSystemReady()) {
1324 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001325 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 intent.putExtra("input_method_id", id);
1327 mContext.sendBroadcast(intent);
1328 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001329 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 } finally {
1331 Binder.restoreCallingIdentity(ident);
1332 }
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
satok42c5a162011-05-26 16:46:14 +09001335 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 public boolean showSoftInput(IInputMethodClient client, int flags,
1337 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001338 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 long ident = Binder.clearCallingIdentity();
1340 try {
1341 synchronized (mMethodMap) {
1342 if (mCurClient == null || client == null
1343 || mCurClient.client.asBinder() != client.asBinder()) {
1344 try {
1345 // We need to check if this is the current client with
1346 // focus in the window manager, to allow this call to
1347 // be made before input is started in it.
1348 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001349 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001350 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001353 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
Joe Onorato8a9b2202010-02-26 18:56:32 -08001357 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001358 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
1360 } finally {
1361 Binder.restoreCallingIdentity(ident);
1362 }
1363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364
The Android Open Source Project4df24232009-03-05 14:34:35 -08001365 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 mShowRequested = true;
1367 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1368 mShowExplicitlyRequested = true;
1369 }
1370 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1371 mShowExplicitlyRequested = true;
1372 mShowForced = true;
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
Dianne Hackborncc278702009-09-02 23:07:23 -07001375 if (!mSystemReady) {
1376 return false;
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001381 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1382 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1383 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001385 if (mHaveConnection && !mVisibleBound) {
1386 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1387 mVisibleBound = true;
1388 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001389 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001391 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 // The client has asked to have the input method shown, but
1393 // we have been sitting here too long with a connection to the
1394 // service and no interface received, so let's disconnect/connect
1395 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001396 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001398 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001400 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1401 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403
The Android Open Source Project4df24232009-03-05 14:34:35 -08001404 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
satok42c5a162011-05-26 16:46:14 +09001407 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001408 public boolean hideSoftInput(IInputMethodClient client, int flags,
1409 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001410 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 long ident = Binder.clearCallingIdentity();
1412 try {
1413 synchronized (mMethodMap) {
1414 if (mCurClient == null || client == null
1415 || mCurClient.client.asBinder() != client.asBinder()) {
1416 try {
1417 // We need to check if this is the current client with
1418 // focus in the window manager, to allow this call to
1419 // be made before input is started in it.
1420 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001421 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1422 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001423 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001424 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001427 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001428 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
Joe Onorato8a9b2202010-02-26 18:56:32 -08001432 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001433 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 } finally {
1436 Binder.restoreCallingIdentity(ident);
1437 }
1438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
The Android Open Source Project4df24232009-03-05 14:34:35 -08001440 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1442 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001443 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001445 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
1447 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001448 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001450 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001452 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001454 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1455 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1456 res = true;
1457 } else {
1458 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001460 if (mHaveConnection && mVisibleBound) {
1461 mContext.unbindService(mVisibleConnection);
1462 mVisibleBound = false;
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mInputShown = false;
1465 mShowRequested = false;
1466 mShowExplicitlyRequested = false;
1467 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001470
satok42c5a162011-05-26 16:46:14 +09001471 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001472 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1474 boolean first, int windowFlags) {
1475 long ident = Binder.clearCallingIdentity();
1476 try {
1477 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001478 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 + " viewHasFocus=" + viewHasFocus
1480 + " isTextEditor=" + isTextEditor
1481 + " softInputMode=#" + Integer.toHexString(softInputMode)
1482 + " first=" + first + " flags=#"
1483 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (mCurClient == null || client == null
1486 || mCurClient.client.asBinder() != client.asBinder()) {
1487 try {
1488 // We need to check if this is the current client with
1489 // focus in the window manager, to allow this call to
1490 // be made before input is started in it.
1491 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001492 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 return;
1494 }
1495 } catch (RemoteException e) {
1496 }
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001499 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001501 return;
1502 }
1503 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001505 // Should we auto-show the IME even if the caller has not
1506 // specified what should be done with it?
1507 // We only do this automatically if the window can resize
1508 // to accommodate the IME (so what the user sees will give
1509 // them good context without input information being obscured
1510 // by the IME) or if running on a large screen where there
1511 // is more room for the target window + IME.
1512 final boolean doAutoShow =
1513 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1514 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1515 || mRes.getConfiguration().isLayoutSizeAtLeast(
1516 Configuration.SCREENLAYOUT_SIZE_LARGE);
1517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1519 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001520 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1522 // There is no focus view, and this window will
1523 // be behind any soft input window, so hide the
1524 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001525 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001526 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001528 } else if (isTextEditor && doAutoShow && (softInputMode &
1529 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 // There is a focus view, and we are navigating forward
1531 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001532 // We only do this automatically if the window an resize
1533 // to accomodate the IME (so what the user sees will give
1534 // them good context without input information being obscured
1535 // by the IME) or if running on a large screen where there
1536 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001537 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001538 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540 break;
1541 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1542 // Do nothing.
1543 break;
1544 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1545 if ((softInputMode &
1546 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001547 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001548 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 break;
1551 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001552 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001553 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 break;
1555 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1556 if ((softInputMode &
1557 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001558 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001559 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561 break;
1562 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001563 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001564 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 break;
1566 }
1567 }
1568 } finally {
1569 Binder.restoreCallingIdentity(ident);
1570 }
1571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
satok42c5a162011-05-26 16:46:14 +09001573 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1575 synchronized (mMethodMap) {
1576 if (mCurClient == null || client == null
1577 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001578 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001579 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
1581
satok440aab52010-11-25 09:43:11 +09001582 // Always call subtype picker, because subtype picker is a superset of input method
1583 // picker.
satokab751aa2010-09-14 19:17:36 +09001584 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1585 }
1586 }
1587
satok42c5a162011-05-26 16:46:14 +09001588 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001590 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1591 }
1592
satok42c5a162011-05-26 16:46:14 +09001593 @Override
satok28203512010-11-24 11:06:49 +09001594 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1595 synchronized (mMethodMap) {
1596 if (subtype != null) {
1597 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1598 mMethodMap.get(id), subtype.hashCode()));
1599 } else {
1600 setInputMethod(token, id);
1601 }
1602 }
satokab751aa2010-09-14 19:17:36 +09001603 }
1604
satok42c5a162011-05-26 16:46:14 +09001605 @Override
satokb416a71e2010-11-25 20:42:14 +09001606 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001607 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001608 synchronized (mMethodMap) {
1609 if (mCurClient == null || client == null
1610 || mCurClient.client.asBinder() != client.asBinder()) {
1611 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1612 }
satok7fee71f2010-12-17 18:54:26 +09001613 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1614 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001615 }
1616 }
1617
satok4fc87d62011-05-20 16:13:43 +09001618 @Override
satok735cf382010-11-11 20:40:09 +09001619 public boolean switchToLastInputMethod(IBinder token) {
1620 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001621 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001622 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001623 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001624 lastImi = mMethodMap.get(lastIme.first);
1625 } else {
1626 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001627 }
satok4fc87d62011-05-20 16:13:43 +09001628 String targetLastImiId = null;
1629 int subtypeId = NOT_A_SUBTYPE_ID;
1630 if (lastIme != null && lastImi != null) {
1631 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1632 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1633 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1634 : mCurrentSubtype.hashCode();
1635 // If the last IME is the same as the current IME and the last subtype is not
1636 // defined, there is no need to switch to the last IME.
1637 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1638 targetLastImiId = lastIme.first;
1639 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1640 }
1641 }
1642
1643 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1644 // This is a safety net. If the currentSubtype can't be added to the history
1645 // and the framework couldn't find the last ime, we will make the last ime be
1646 // the most applicable enabled keyboard subtype of the system imes.
1647 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1648 if (enabled != null) {
1649 final int N = enabled.size();
1650 final String locale = mCurrentSubtype == null
1651 ? mRes.getConfiguration().locale.toString()
1652 : mCurrentSubtype.getLocale();
1653 for (int i = 0; i < N; ++i) {
1654 final InputMethodInfo imi = enabled.get(i);
1655 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1656 InputMethodSubtype keyboardSubtype =
1657 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1658 SUBTYPE_MODE_KEYBOARD, locale, true);
1659 if (keyboardSubtype != null) {
1660 targetLastImiId = imi.getId();
1661 subtypeId = getSubtypeIdFromHashCode(
1662 imi, keyboardSubtype.hashCode());
1663 if(keyboardSubtype.getLocale().equals(locale)) {
1664 break;
1665 }
1666 }
1667 }
1668 }
1669 }
1670 }
1671
1672 if (!TextUtils.isEmpty(targetLastImiId)) {
1673 if (DEBUG) {
1674 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1675 + ", from: " + mCurMethodId + ", " + subtypeId);
1676 }
1677 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1678 return true;
1679 } else {
1680 return false;
1681 }
satok735cf382010-11-11 20:40:09 +09001682 }
1683 }
1684
satoke7c6998e2011-06-03 17:57:59 +09001685 @Override
satok68f1b782011-04-11 14:26:04 +09001686 public InputMethodSubtype getLastInputMethodSubtype() {
1687 synchronized (mMethodMap) {
1688 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1689 // TODO: Handle the case of the last IME with no subtypes
1690 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1691 || TextUtils.isEmpty(lastIme.second)) return null;
1692 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1693 if (lastImi == null) return null;
1694 try {
1695 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001696 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1697 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1698 return null;
1699 }
1700 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001701 } catch (NumberFormatException e) {
1702 return null;
1703 }
1704 }
1705 }
1706
satoke7c6998e2011-06-03 17:57:59 +09001707 @Override
satokee5e77c2011-09-02 18:50:15 +09001708 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001709 // By this IPC call, only a process which shares the same uid with the IME can add
1710 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001711 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001712 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001713 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001714 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001715 final PackageManager pm = mContext.getPackageManager();
1716 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1717 if (packageInfos != null) {
1718 final int packageNum = packageInfos.length;
1719 for (int i = 0; i < packageNum; ++i) {
1720 if (packageInfos[i].equals(imi.getPackageName())) {
1721 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001722 final long ident = Binder.clearCallingIdentity();
1723 try {
1724 buildInputMethodListLocked(mMethodList, mMethodMap);
1725 } finally {
1726 Binder.restoreCallingIdentity(ident);
1727 }
satokee5e77c2011-09-02 18:50:15 +09001728 return;
satok91e88122011-07-18 11:11:42 +09001729 }
1730 }
1731 }
satoke7c6998e2011-06-03 17:57:59 +09001732 }
satokee5e77c2011-09-02 18:50:15 +09001733 return;
satoke7c6998e2011-06-03 17:57:59 +09001734 }
1735
satok28203512010-11-24 11:06:49 +09001736 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 synchronized (mMethodMap) {
1738 if (token == null) {
1739 if (mContext.checkCallingOrSelfPermission(
1740 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1741 != PackageManager.PERMISSION_GRANTED) {
1742 throw new SecurityException(
1743 "Using null token requires permission "
1744 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1745 }
1746 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001747 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1748 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 return;
1750 }
1751
satokc5933802011-08-31 21:26:04 +09001752 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 try {
satokab751aa2010-09-14 19:17:36 +09001754 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 } finally {
1756 Binder.restoreCallingIdentity(ident);
1757 }
1758 }
1759 }
1760
satok42c5a162011-05-26 16:46:14 +09001761 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 public void hideMySoftInput(IBinder token, int flags) {
1763 synchronized (mMethodMap) {
1764 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001765 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1766 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 return;
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 long ident = Binder.clearCallingIdentity();
1770 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001771 hideCurrentInputLocked(flags, null);
1772 } finally {
1773 Binder.restoreCallingIdentity(ident);
1774 }
1775 }
1776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001777
satok42c5a162011-05-26 16:46:14 +09001778 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001779 public void showMySoftInput(IBinder token, int flags) {
1780 synchronized (mMethodMap) {
1781 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001782 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1783 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 return;
1785 }
1786 long ident = Binder.clearCallingIdentity();
1787 try {
1788 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 } finally {
1790 Binder.restoreCallingIdentity(ident);
1791 }
1792 }
1793 }
1794
1795 void setEnabledSessionInMainThread(SessionState session) {
1796 if (mEnabledSession != session) {
1797 if (mEnabledSession != null) {
1798 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001799 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 mEnabledSession.method.setSessionEnabled(
1801 mEnabledSession.session, false);
1802 } catch (RemoteException e) {
1803 }
1804 }
1805 mEnabledSession = session;
1806 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001807 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 session.method.setSessionEnabled(
1809 session.session, true);
1810 } catch (RemoteException e) {
1811 }
1812 }
1813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001814
satok42c5a162011-05-26 16:46:14 +09001815 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 public boolean handleMessage(Message msg) {
1817 HandlerCaller.SomeArgs args;
1818 switch (msg.what) {
1819 case MSG_SHOW_IM_PICKER:
1820 showInputMethodMenu();
1821 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
satokab751aa2010-09-14 19:17:36 +09001823 case MSG_SHOW_IM_SUBTYPE_PICKER:
1824 showInputMethodSubtypeMenu();
1825 return true;
1826
satok47a44912010-10-06 16:03:58 +09001827 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001828 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001829 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001830 return true;
1831
1832 case MSG_SHOW_IM_CONFIG:
1833 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001834 return true;
1835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 case MSG_UNBIND_INPUT:
1839 try {
1840 ((IInputMethod)msg.obj).unbindInput();
1841 } catch (RemoteException e) {
1842 // There is nothing interesting about the method dying.
1843 }
1844 return true;
1845 case MSG_BIND_INPUT:
1846 args = (HandlerCaller.SomeArgs)msg.obj;
1847 try {
1848 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1849 } catch (RemoteException e) {
1850 }
1851 return true;
1852 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001855 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1856 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 } catch (RemoteException e) {
1858 }
1859 return true;
1860 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001861 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 ((IInputMethod)args.arg1).hideSoftInput(0,
1864 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 } catch (RemoteException e) {
1866 }
1867 return true;
1868 case MSG_ATTACH_TOKEN:
1869 args = (HandlerCaller.SomeArgs)msg.obj;
1870 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001871 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1873 } catch (RemoteException e) {
1874 }
1875 return true;
1876 case MSG_CREATE_SESSION:
1877 args = (HandlerCaller.SomeArgs)msg.obj;
1878 try {
1879 ((IInputMethod)args.arg1).createSession(
1880 (IInputMethodCallback)args.arg2);
1881 } catch (RemoteException e) {
1882 }
1883 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 case MSG_START_INPUT:
1887 args = (HandlerCaller.SomeArgs)msg.obj;
1888 try {
1889 SessionState session = (SessionState)args.arg1;
1890 setEnabledSessionInMainThread(session);
1891 session.method.startInput((IInputContext)args.arg2,
1892 (EditorInfo)args.arg3);
1893 } catch (RemoteException e) {
1894 }
1895 return true;
1896 case MSG_RESTART_INPUT:
1897 args = (HandlerCaller.SomeArgs)msg.obj;
1898 try {
1899 SessionState session = (SessionState)args.arg1;
1900 setEnabledSessionInMainThread(session);
1901 session.method.restartInput((IInputContext)args.arg2,
1902 (EditorInfo)args.arg3);
1903 } catch (RemoteException e) {
1904 }
1905 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 case MSG_UNBIND_METHOD:
1910 try {
1911 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1912 } catch (RemoteException e) {
1913 // There is nothing interesting about the last client dying.
1914 }
1915 return true;
1916 case MSG_BIND_METHOD:
1917 args = (HandlerCaller.SomeArgs)msg.obj;
1918 try {
1919 ((IInputMethodClient)args.arg1).onBindMethod(
1920 (InputBindResult)args.arg2);
1921 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001922 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924 return true;
1925 }
1926 return false;
1927 }
1928
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001929 private boolean isSystemIme(InputMethodInfo inputMethod) {
1930 return (inputMethod.getServiceInfo().applicationInfo.flags
1931 & ApplicationInfo.FLAG_SYSTEM) != 0;
1932 }
1933
Ken Wakasa586f0512011-01-20 22:31:01 +09001934 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1935 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1936 final int subtypeCount = imi.getSubtypeCount();
1937 for (int i = 0; i < subtypeCount; ++i) {
1938 subtypes.add(imi.getSubtypeAt(i));
1939 }
1940 return subtypes;
1941 }
1942
satoka86f5e42011-09-02 17:12:42 +09001943
1944 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1945 InputMethodInfo imi, String mode) {
1946 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1947 final int subtypeCount = imi.getSubtypeCount();
1948 for (int i = 0; i < subtypeCount; ++i) {
1949 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1950 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1951 subtypes.add(subtype);
1952 }
1953 }
1954 return subtypes;
1955 }
1956
satokdc9ddae2011-10-06 12:22:36 +09001957 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001958 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001959 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001960 // We'd prefer to fall back on a system IME, since that is safer.
1961 int i=enabled.size();
1962 while (i > 0) {
1963 i--;
satokdc9ddae2011-10-06 12:22:36 +09001964 final InputMethodInfo imi = enabled.get(i);
1965 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001966 break;
1967 }
1968 }
satokdc9ddae2011-10-06 12:22:36 +09001969 return enabled.get(i);
1970 }
1971 return null;
1972 }
1973
1974 private boolean chooseNewDefaultIMELocked() {
1975 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1976 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001977 if (DEBUG) {
1978 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1979 }
satok723a27e2010-11-11 14:58:11 +09001980 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001981 return true;
1982 }
1983
1984 return false;
1985 }
1986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1988 HashMap<String, InputMethodInfo> map) {
1989 list.clear();
1990 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001993 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001994 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1995 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1996 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1997 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998
1999 List<ResolveInfo> services = pm.queryIntentServices(
2000 new Intent(InputMethod.SERVICE_INTERFACE),
2001 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002002
satoke7c6998e2011-06-03 17:57:59 +09002003 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2004 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 for (int i = 0; i < services.size(); ++i) {
2006 ResolveInfo ri = services.get(i);
2007 ServiceInfo si = ri.serviceInfo;
2008 ComponentName compName = new ComponentName(si.packageName, si.name);
2009 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2010 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002011 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 + ": it does not require the permission "
2013 + android.Manifest.permission.BIND_INPUT_METHOD);
2014 continue;
2015 }
2016
Joe Onorato8a9b2202010-02-26 18:56:32 -08002017 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018
2019 try {
satoke7c6998e2011-06-03 17:57:59 +09002020 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002022 final String id = p.getId();
2023 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024
Amith Yamasanie861ec12010-03-24 21:39:27 -07002025 // System IMEs are enabled by default, unless there's a hard keyboard
2026 // and the system IME was explicitly disabled
2027 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2028 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002029 }
2030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002032 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002036 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002038 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002041
2042 String defaultIme = Settings.Secure.getString(mContext
2043 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002044 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002045 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002046 updateFromSettingsLocked();
2047 }
2048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
satokab751aa2010-09-14 19:17:36 +09002053 private void showInputMethodMenu() {
2054 showInputMethodMenuInternal(false);
2055 }
2056
2057 private void showInputMethodSubtypeMenu() {
2058 showInputMethodMenuInternal(true);
2059 }
2060
satok217f5482010-12-15 05:19:19 +09002061 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002062 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002063 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002064 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2065 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002066 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002067 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002068 }
satok217f5482010-12-15 05:19:19 +09002069 mContext.startActivity(intent);
2070 }
2071
2072 private void showConfigureInputMethods() {
2073 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2074 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2075 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2076 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002077 mContext.startActivity(intent);
2078 }
2079
satokab751aa2010-09-14 19:17:36 +09002080 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002081 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002085 final boolean isScreenLocked = mKeyguardManager != null
2086 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 String lastInputMethodId = Settings.Secure.getString(context
2089 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002090 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002091 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002093 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002094 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2095 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002096 if (immis == null || immis.size() == 0) {
2097 return;
2098 }
2099
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002100 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101
Ken Wakasa761eb372011-03-04 19:06:18 +09002102 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2103 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2104 new Comparator<InputMethodInfo>() {
2105 @Override
2106 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2107 if (imi2 == null) return 0;
2108 if (imi1 == null) return 1;
2109 if (pm == null) {
2110 return imi1.getId().compareTo(imi2.getId());
2111 }
2112 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2113 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2114 return imiId1.toString().compareTo(imiId2.toString());
2115 }
2116 });
satok913a8922010-08-26 21:53:41 +09002117
Ken Wakasa761eb372011-03-04 19:06:18 +09002118 sortedImmis.putAll(immis);
2119
Ken Wakasa05dbb652011-08-22 15:22:43 +09002120 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002121
2122 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002123 if (imi == null) continue;
2124 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002125 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002126 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2127 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002128 }
satokbb4aa062011-01-19 21:40:27 +09002129 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002130 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002131 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002132 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002133 if (DEBUG) {
2134 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2135 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002136 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002137 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2138 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2139 // We show all enabled IMEs and subtypes when an IME is shown.
2140 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002141 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002142 final CharSequence subtypeLabel =
2143 subtype.overridesImplicitlyEnabledSubtype() ? null
2144 : subtype.getDisplayName(context, imi.getPackageName(),
2145 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002146 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2147
satok4a28bde2011-06-29 21:03:40 +09002148 // Removing this subtype from enabledSubtypeSet because we no longer
2149 // need to add an entry of this subtype to imList to avoid duplicated
2150 // entries.
2151 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002152 }
satokab751aa2010-09-14 19:17:36 +09002153 }
2154 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002155 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002156 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002157 }
satok913a8922010-08-26 21:53:41 +09002158
Ken Wakasa761eb372011-03-04 19:06:18 +09002159 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002160 mIms = new InputMethodInfo[N];
2161 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002162 int checkedItem = 0;
2163 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002164 final ImeSubtypeListItem item = imList.get(i);
2165 mIms[i] = item.mImi;
2166 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002167 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002168 int subtypeId = mSubtypeIds[i];
2169 if ((subtypeId == NOT_A_SUBTYPE_ID)
2170 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2171 || (subtypeId == lastInputMethodSubtypeId)) {
2172 checkedItem = i;
2173 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
satokab751aa2010-09-14 19:17:36 +09002176
Ken Wakasa05dbb652011-08-22 15:22:43 +09002177 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002178 com.android.internal.R.styleable.DialogPreference,
2179 com.android.internal.R.attr.alertDialogStyle, 0);
2180 mDialogBuilder = new AlertDialog.Builder(context)
2181 .setTitle(com.android.internal.R.string.select_input_method)
2182 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002183 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002184 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002186 }
2187 })
2188 .setIcon(a.getDrawable(
2189 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2190 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002191
Ken Wakasa05dbb652011-08-22 15:22:43 +09002192 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2193 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2194 checkedItem);
2195
2196 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002197 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002198 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002199 public void onClick(DialogInterface dialog, int which) {
2200 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002201 if (mIms == null || mIms.length <= which
2202 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002203 return;
2204 }
2205 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002206 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002207 hideInputMethodMenu();
2208 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002209 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002210 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002211 subtypeId = NOT_A_SUBTYPE_ID;
2212 }
2213 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002214 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002217 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218
satokbc81b692011-08-26 16:22:22 +09002219 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002220 mDialogBuilder.setPositiveButton(
2221 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002222 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002223 @Override
satok7f35c8c2010-10-07 21:13:11 +09002224 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002225 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002226 }
2227 });
2228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002230 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 mSwitchingDialog.getWindow().setType(
2232 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002233 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 mSwitchingDialog.show();
2235 }
2236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002237
Ken Wakasa05dbb652011-08-22 15:22:43 +09002238 private static class ImeSubtypeListItem {
2239 public final CharSequence mImeName;
2240 public final CharSequence mSubtypeName;
2241 public final InputMethodInfo mImi;
2242 public final int mSubtypeId;
2243 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2244 InputMethodInfo imi, int subtypeId) {
2245 mImeName = imeName;
2246 mSubtypeName = subtypeName;
2247 mImi = imi;
2248 mSubtypeId = subtypeId;
2249 }
2250 }
2251
2252 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2253 private final LayoutInflater mInflater;
2254 private final int mTextViewResourceId;
2255 private final List<ImeSubtypeListItem> mItemsList;
2256 private final int mCheckedItem;
2257 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2258 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2259 super(context, textViewResourceId, itemsList);
2260 mTextViewResourceId = textViewResourceId;
2261 mItemsList = itemsList;
2262 mCheckedItem = checkedItem;
2263 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2264 }
2265
2266 @Override
2267 public View getView(int position, View convertView, ViewGroup parent) {
2268 final View view = convertView != null ? convertView
2269 : mInflater.inflate(mTextViewResourceId, null);
2270 if (position < 0 || position >= mItemsList.size()) return view;
2271 final ImeSubtypeListItem item = mItemsList.get(position);
2272 final CharSequence imeName = item.mImeName;
2273 final CharSequence subtypeName = item.mSubtypeName;
2274 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2275 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2276 if (TextUtils.isEmpty(subtypeName)) {
2277 firstTextView.setText(imeName);
2278 secondTextView.setVisibility(View.GONE);
2279 } else {
2280 firstTextView.setText(subtypeName);
2281 secondTextView.setText(imeName);
2282 secondTextView.setVisibility(View.VISIBLE);
2283 }
2284 final RadioButton radioButton =
2285 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2286 radioButton.setChecked(position == mCheckedItem);
2287 return view;
2288 }
2289 }
2290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002292 synchronized (mMethodMap) {
2293 hideInputMethodMenuLocked();
2294 }
2295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002296
The Android Open Source Project10592532009-03-18 17:39:46 -07002297 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002298 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
The Android Open Source Project10592532009-03-18 17:39:46 -07002300 if (mSwitchingDialog != null) {
2301 mSwitchingDialog.dismiss();
2302 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002304
The Android Open Source Project10592532009-03-18 17:39:46 -07002305 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002306 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002310
satok42c5a162011-05-26 16:46:14 +09002311 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 public boolean setInputMethodEnabled(String id, boolean enabled) {
2313 synchronized (mMethodMap) {
2314 if (mContext.checkCallingOrSelfPermission(
2315 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2316 != PackageManager.PERMISSION_GRANTED) {
2317 throw new SecurityException(
2318 "Requires permission "
2319 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2320 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 long ident = Binder.clearCallingIdentity();
2323 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002324 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 } finally {
2326 Binder.restoreCallingIdentity(ident);
2327 }
2328 }
2329 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002330
2331 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2332 // Make sure this is a valid input method.
2333 InputMethodInfo imm = mMethodMap.get(id);
2334 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002335 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002336 }
2337
satokd87c2592010-09-29 11:52:06 +09002338 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2339 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002340
satokd87c2592010-09-29 11:52:06 +09002341 if (enabled) {
2342 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2343 if (pair.first.equals(id)) {
2344 // We are enabling this input method, but it is already enabled.
2345 // Nothing to do. The previous state was enabled.
2346 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002347 }
2348 }
satokd87c2592010-09-29 11:52:06 +09002349 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2350 // Previous state was disabled.
2351 return false;
2352 } else {
2353 StringBuilder builder = new StringBuilder();
2354 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2355 builder, enabledInputMethodsList, id)) {
2356 // Disabled input method is currently selected, switch to another one.
2357 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2358 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002359 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2360 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2361 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002362 }
2363 // Previous state was enabled.
2364 return true;
2365 } else {
2366 // We are disabling the input method but it is already disabled.
2367 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002368 return false;
2369 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002370 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002371 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002372
satok57ffc002011-01-25 00:11:47 +09002373 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2374 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002375 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002376 }
2377
satok723a27e2010-11-11 14:58:11 +09002378 private void saveCurrentInputMethodAndSubtypeToHistory() {
2379 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2380 if (mCurrentSubtype != null) {
2381 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2382 }
satok57ffc002011-01-25 00:11:47 +09002383 if (canAddToLastInputMethod(mCurrentSubtype)) {
2384 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2385 }
satokab751aa2010-09-14 19:17:36 +09002386 }
2387
satok723a27e2010-11-11 14:58:11 +09002388 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2389 boolean setSubtypeOnly) {
2390 // Update the history of InputMethod and Subtype
2391 saveCurrentInputMethodAndSubtypeToHistory();
2392
2393 // Set Subtype here
2394 if (imi == null || subtypeId < 0) {
2395 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002396 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002397 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002398 if (subtypeId < imi.getSubtypeCount()) {
2399 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2400 mSettings.putSelectedSubtype(subtype.hashCode());
2401 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002402 } else {
2403 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2404 mCurrentSubtype = null;
2405 }
satokab751aa2010-09-14 19:17:36 +09002406 }
satok723a27e2010-11-11 14:58:11 +09002407
2408 if (!setSubtypeOnly) {
2409 // Set InputMethod here
2410 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2411 }
2412 }
2413
2414 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2415 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2416 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2417 // newDefaultIme is empty when there is no candidate for the selected IME.
2418 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2419 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2420 if (subtypeHashCode != null) {
2421 try {
2422 lastSubtypeId = getSubtypeIdFromHashCode(
2423 imi, Integer.valueOf(subtypeHashCode));
2424 } catch (NumberFormatException e) {
2425 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2426 }
2427 }
2428 }
2429 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002430 }
2431
2432 private int getSelectedInputMethodSubtypeId(String id) {
2433 InputMethodInfo imi = mMethodMap.get(id);
2434 if (imi == null) {
2435 return NOT_A_SUBTYPE_ID;
2436 }
satokab751aa2010-09-14 19:17:36 +09002437 int subtypeId;
2438 try {
2439 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2440 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2441 } catch (SettingNotFoundException e) {
2442 return NOT_A_SUBTYPE_ID;
2443 }
satok723a27e2010-11-11 14:58:11 +09002444 return getSubtypeIdFromHashCode(imi, subtypeId);
2445 }
2446
2447 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002448 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002449 final int subtypeCount = imi.getSubtypeCount();
2450 for (int i = 0; i < subtypeCount; ++i) {
2451 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002452 if (subtypeHashCode == ims.hashCode()) {
2453 return i;
2454 }
satokab751aa2010-09-14 19:17:36 +09002455 }
2456 }
2457 return NOT_A_SUBTYPE_ID;
2458 }
2459
satoka86f5e42011-09-02 17:12:42 +09002460 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2461 Resources res, InputMethodInfo imi) {
2462 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002463 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002464 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002465 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002466 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002467 final int N = subtypes.size();
2468 boolean containsKeyboardSubtype = false;
2469 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002470 // scan overriding implicitly enabled subtypes.
2471 InputMethodSubtype subtype = subtypes.get(i);
2472 if (subtype.overridesImplicitlyEnabledSubtype()) {
2473 final String mode = subtype.getMode();
2474 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2475 applicableModeAndSubtypesMap.put(mode, subtype);
2476 }
2477 }
2478 }
2479 if (applicableModeAndSubtypesMap.size() > 0) {
2480 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2481 }
2482 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002483 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002484 final String locale = subtype.getLocale();
2485 final String mode = subtype.getMode();
2486 // When system locale starts with subtype's locale, that subtype will be applicable
2487 // for system locale
2488 // For instance, it's clearly applicable for cases like system locale = en_US and
2489 // subtype = en, but it is not necessarily considered applicable for cases like system
2490 // locale = en and subtype = en_US.
2491 // We just call systemLocale.startsWith(locale) in this function because there is no
2492 // need to find applicable subtypes aggressively unlike
2493 // findLastResortApplicableSubtypeLocked.
2494 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002495 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002496 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002497 if (applicableSubtype != null) {
2498 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2499 if (!systemLocale.equals(locale)) continue;
2500 }
satok3da92232011-01-11 22:46:30 +09002501 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002502 if (!containsKeyboardSubtype
2503 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2504 containsKeyboardSubtype = true;
2505 }
2506 }
2507 }
satok4a28bde2011-06-29 21:03:40 +09002508 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002509 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002510 if (!containsKeyboardSubtype) {
2511 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002512 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002513 if (lastResortKeyboardSubtype != null) {
2514 applicableSubtypes.add(lastResortKeyboardSubtype);
2515 }
2516 }
2517 return applicableSubtypes;
2518 }
2519
satok4e4569d2010-11-19 18:45:53 +09002520 /**
2521 * If there are no selected subtypes, tries finding the most applicable one according to the
2522 * given locale.
2523 * @param subtypes this function will search the most applicable subtype in subtypes
2524 * @param mode subtypes will be filtered by mode
2525 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002526 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2527 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002528 * @return the most applicable subtypeId
2529 */
satokdf31ae62011-01-15 06:19:44 +09002530 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2531 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002532 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002533 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002534 return null;
satok8fbb1e82010-11-02 23:15:58 +09002535 }
satok4e4569d2010-11-19 18:45:53 +09002536 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002537 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002538 }
satok8fbb1e82010-11-02 23:15:58 +09002539 final String language = locale.substring(0, 2);
2540 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002541 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002542 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002543 final int N = subtypes.size();
2544 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002545 InputMethodSubtype subtype = subtypes.get(i);
2546 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002547 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2548 // and all subtypes with all modes can be candidates.
2549 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002550 if (firstMatchedModeSubtype == null) {
2551 firstMatchedModeSubtype = subtype;
2552 }
satok9ef02832010-11-04 21:17:48 +09002553 if (locale.equals(subtypeLocale)) {
2554 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002555 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002556 break;
2557 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2558 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002559 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002560 partialMatchFound = true;
2561 }
satok8fbb1e82010-11-02 23:15:58 +09002562 }
2563 }
2564
satok7599a7fb2010-12-22 13:45:23 +09002565 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2566 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002567 }
2568
satok8fbb1e82010-11-02 23:15:58 +09002569 // The first subtype applicable to the system locale will be defined as the most applicable
2570 // subtype.
2571 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002572 if (applicableSubtype != null) {
2573 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2574 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2575 }
satok8fbb1e82010-11-02 23:15:58 +09002576 }
satokcd7cd292010-11-20 15:46:23 +09002577 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002578 }
2579
satok4e4569d2010-11-19 18:45:53 +09002580 // If there are no selected shortcuts, tries finding the most applicable ones.
2581 private Pair<InputMethodInfo, InputMethodSubtype>
2582 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2583 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2584 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002585 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002586 boolean foundInSystemIME = false;
2587
2588 // Search applicable subtype for each InputMethodInfo
2589 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002590 final String imiId = imi.getId();
2591 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2592 continue;
2593 }
satokcd7cd292010-11-20 15:46:23 +09002594 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002595 final List<InputMethodSubtype> enabledSubtypes =
2596 getEnabledInputMethodSubtypeList(imi, true);
2597 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002598 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002599 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002600 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002601 }
satokdf31ae62011-01-15 06:19:44 +09002602 // 2. Search by the system locale from enabledSubtypes.
2603 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002604 if (subtype == null) {
2605 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002606 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002607 }
satoka86f5e42011-09-02 17:12:42 +09002608 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2609 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2610 final ArrayList<InputMethodSubtype> subtypesForSearch =
2611 overridingImplicitlyEnabledSubtypes.isEmpty()
2612 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002613 // 4. Search by the current subtype's locale from all subtypes.
2614 if (subtype == null && mCurrentSubtype != null) {
2615 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002616 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002617 }
2618 // 5. Search by the system locale from all subtypes.
2619 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002620 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002621 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002622 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002623 }
satokcd7cd292010-11-20 15:46:23 +09002624 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002625 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002626 // The current input method is the most applicable IME.
2627 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002628 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002629 break;
satok7599a7fb2010-12-22 13:45:23 +09002630 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002631 // The system input method is 2nd applicable IME.
2632 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002633 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002634 if ((imi.getServiceInfo().applicationInfo.flags
2635 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2636 foundInSystemIME = true;
2637 }
satok4e4569d2010-11-19 18:45:53 +09002638 }
2639 }
2640 }
2641 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002642 if (mostApplicableIMI != null) {
2643 Slog.w(TAG, "Most applicable shortcut input method was:"
2644 + mostApplicableIMI.getId());
2645 if (mostApplicableSubtype != null) {
2646 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2647 + "," + mostApplicableSubtype.getMode() + ","
2648 + mostApplicableSubtype.getLocale());
2649 }
2650 }
satok4e4569d2010-11-19 18:45:53 +09002651 }
satokcd7cd292010-11-20 15:46:23 +09002652 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002653 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002654 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002655 } else {
2656 return null;
2657 }
2658 }
2659
satokab751aa2010-09-14 19:17:36 +09002660 /**
2661 * @return Return the current subtype of this input method.
2662 */
satok42c5a162011-05-26 16:46:14 +09002663 @Override
satokab751aa2010-09-14 19:17:36 +09002664 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002665 boolean subtypeIsSelected = false;
2666 try {
2667 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2668 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2669 } catch (SettingNotFoundException e) {
2670 }
satok3ef8b292010-11-23 06:06:29 +09002671 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002672 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002673 String lastInputMethodId = Settings.Secure.getString(
2674 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002675 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2676 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002677 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2678 if (imi != null) {
2679 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002680 // the most applicable subtype from explicitly or implicitly enabled
2681 // subtypes.
2682 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2683 getEnabledInputMethodSubtypeList(imi, true);
2684 // If there is only one explicitly or implicitly enabled subtype,
2685 // just returns it.
2686 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2687 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2688 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2689 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2690 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2691 SUBTYPE_MODE_KEYBOARD, null, true);
2692 if (mCurrentSubtype == null) {
2693 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2694 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2695 true);
2696 }
2697 }
satok4e4569d2010-11-19 18:45:53 +09002698 }
satokcd7cd292010-11-20 15:46:23 +09002699 } else {
satok3ef8b292010-11-23 06:06:29 +09002700 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002701 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002702 }
satok8fbb1e82010-11-02 23:15:58 +09002703 }
satok3ef8b292010-11-23 06:06:29 +09002704 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002705 }
satokab751aa2010-09-14 19:17:36 +09002706 }
2707
satokf3db1af2010-11-23 13:34:33 +09002708 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2709 InputMethodSubtype subtype) {
2710 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2711 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2712 } else {
2713 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2714 subtypes.add(subtype);
2715 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2716 }
2717 }
2718
satok4e4569d2010-11-19 18:45:53 +09002719 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002720 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002721 @Override
satok4e4569d2010-11-19 18:45:53 +09002722 public List getShortcutInputMethodsAndSubtypes() {
2723 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002724 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002725 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002726 // If there are no selected shortcut subtypes, the framework will try to find
2727 // the most applicable subtype from all subtypes whose mode is
2728 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002729 Pair<InputMethodInfo, InputMethodSubtype> info =
2730 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2731 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002732 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002733 ret.add(info.first);
2734 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002735 }
satok3da92232011-01-11 22:46:30 +09002736 return ret;
satokf3db1af2010-11-23 13:34:33 +09002737 }
satokf3db1af2010-11-23 13:34:33 +09002738 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2739 ret.add(imi);
2740 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2741 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002742 }
2743 }
satokf3db1af2010-11-23 13:34:33 +09002744 return ret;
satok4e4569d2010-11-19 18:45:53 +09002745 }
2746 }
2747
satok42c5a162011-05-26 16:46:14 +09002748 @Override
satokb66d2872010-11-10 01:04:04 +09002749 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2750 synchronized (mMethodMap) {
2751 if (subtype != null && mCurMethodId != null) {
2752 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2753 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2754 if (subtypeId != NOT_A_SUBTYPE_ID) {
2755 setInputMethodLocked(mCurMethodId, subtypeId);
2756 return true;
2757 }
2758 }
2759 return false;
2760 }
2761 }
2762
satokd87c2592010-09-29 11:52:06 +09002763 /**
2764 * Utility class for putting and getting settings for InputMethod
2765 * TODO: Move all putters and getters of settings to this class.
2766 */
2767 private static class InputMethodSettings {
2768 // The string for enabled input method is saved as follows:
2769 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2770 private static final char INPUT_METHOD_SEPARATER = ':';
2771 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002772 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002773 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2774
satok723a27e2010-11-11 14:58:11 +09002775 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002776 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2777
satokdf31ae62011-01-15 06:19:44 +09002778 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002779 private final ContentResolver mResolver;
2780 private final HashMap<String, InputMethodInfo> mMethodMap;
2781 private final ArrayList<InputMethodInfo> mMethodList;
2782
2783 private String mEnabledInputMethodsStrCache;
2784
2785 private static void buildEnabledInputMethodsSettingString(
2786 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2787 String id = pair.first;
2788 ArrayList<String> subtypes = pair.second;
2789 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002790 // Inputmethod and subtypes are saved in the settings as follows:
2791 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2792 for (String subtypeId: subtypes) {
2793 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002794 }
2795 }
2796
2797 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002798 Resources res, ContentResolver resolver,
2799 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2800 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002801 mResolver = resolver;
2802 mMethodMap = methodMap;
2803 mMethodList = methodList;
2804 }
2805
2806 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2807 return createEnabledInputMethodListLocked(
2808 getEnabledInputMethodsAndSubtypeListLocked());
2809 }
2810
satok7f35c8c2010-10-07 21:13:11 +09002811 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002812 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2813 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002814 getEnabledInputMethodsAndSubtypeListLocked());
2815 }
2816
satok67ddf9c2010-11-17 09:45:54 +09002817 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2818 InputMethodInfo imi) {
2819 List<Pair<String, ArrayList<String>>> imsList =
2820 getEnabledInputMethodsAndSubtypeListLocked();
2821 ArrayList<InputMethodSubtype> enabledSubtypes =
2822 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002823 if (imi != null) {
2824 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2825 InputMethodInfo info = mMethodMap.get(imsPair.first);
2826 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002827 final int subtypeCount = info.getSubtypeCount();
2828 for (int i = 0; i < subtypeCount; ++i) {
2829 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002830 for (String s: imsPair.second) {
2831 if (String.valueOf(ims.hashCode()).equals(s)) {
2832 enabledSubtypes.add(ims);
2833 }
satok67ddf9c2010-11-17 09:45:54 +09002834 }
2835 }
satok884ef9a2010-11-18 10:39:46 +09002836 break;
satok67ddf9c2010-11-17 09:45:54 +09002837 }
satok67ddf9c2010-11-17 09:45:54 +09002838 }
2839 }
2840 return enabledSubtypes;
2841 }
2842
satokd87c2592010-09-29 11:52:06 +09002843 // At the initial boot, the settings for input methods are not set,
2844 // so we need to enable IME in that case.
2845 public void enableAllIMEsIfThereIsNoEnabledIME() {
2846 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2847 StringBuilder sb = new StringBuilder();
2848 final int N = mMethodList.size();
2849 for (int i = 0; i < N; i++) {
2850 InputMethodInfo imi = mMethodList.get(i);
2851 Slog.i(TAG, "Adding: " + imi.getId());
2852 if (i > 0) sb.append(':');
2853 sb.append(imi.getId());
2854 }
2855 putEnabledInputMethodsStr(sb.toString());
2856 }
2857 }
2858
satokbb4aa062011-01-19 21:40:27 +09002859 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002860 ArrayList<Pair<String, ArrayList<String>>> imsList
2861 = new ArrayList<Pair<String, ArrayList<String>>>();
2862 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2863 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2864 return imsList;
2865 }
satok723a27e2010-11-11 14:58:11 +09002866 mInputMethodSplitter.setString(enabledInputMethodsStr);
2867 while (mInputMethodSplitter.hasNext()) {
2868 String nextImsStr = mInputMethodSplitter.next();
2869 mSubtypeSplitter.setString(nextImsStr);
2870 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002871 ArrayList<String> subtypeHashes = new ArrayList<String>();
2872 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002873 String imeId = mSubtypeSplitter.next();
2874 while (mSubtypeSplitter.hasNext()) {
2875 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002876 }
2877 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2878 }
2879 }
2880 return imsList;
2881 }
2882
2883 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2884 if (reloadInputMethodStr) {
2885 getEnabledInputMethodsStr();
2886 }
2887 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2888 // Add in the newly enabled input method.
2889 putEnabledInputMethodsStr(id);
2890 } else {
2891 putEnabledInputMethodsStr(
2892 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2893 }
2894 }
2895
2896 /**
2897 * Build and put a string of EnabledInputMethods with removing specified Id.
2898 * @return the specified id was removed or not.
2899 */
2900 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2901 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2902 boolean isRemoved = false;
2903 boolean needsAppendSeparator = false;
2904 for (Pair<String, ArrayList<String>> ims: imsList) {
2905 String curId = ims.first;
2906 if (curId.equals(id)) {
2907 // We are disabling this input method, and it is
2908 // currently enabled. Skip it to remove from the
2909 // new list.
2910 isRemoved = true;
2911 } else {
2912 if (needsAppendSeparator) {
2913 builder.append(INPUT_METHOD_SEPARATER);
2914 } else {
2915 needsAppendSeparator = true;
2916 }
2917 buildEnabledInputMethodsSettingString(builder, ims);
2918 }
2919 }
2920 if (isRemoved) {
2921 // Update the setting with the new list of input methods.
2922 putEnabledInputMethodsStr(builder.toString());
2923 }
2924 return isRemoved;
2925 }
2926
2927 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2928 List<Pair<String, ArrayList<String>>> imsList) {
2929 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2930 for (Pair<String, ArrayList<String>> ims: imsList) {
2931 InputMethodInfo info = mMethodMap.get(ims.first);
2932 if (info != null) {
2933 res.add(info);
2934 }
2935 }
2936 return res;
2937 }
2938
satok7f35c8c2010-10-07 21:13:11 +09002939 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002940 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002941 List<Pair<String, ArrayList<String>>> imsList) {
2942 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2943 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2944 for (Pair<String, ArrayList<String>> ims : imsList) {
2945 InputMethodInfo info = mMethodMap.get(ims.first);
2946 if (info != null) {
2947 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2948 }
2949 }
2950 return res;
2951 }
2952
satokd87c2592010-09-29 11:52:06 +09002953 private void putEnabledInputMethodsStr(String str) {
2954 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2955 mEnabledInputMethodsStrCache = str;
2956 }
2957
2958 private String getEnabledInputMethodsStr() {
2959 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2960 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002961 if (DEBUG) {
2962 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2963 }
satokd87c2592010-09-29 11:52:06 +09002964 return mEnabledInputMethodsStrCache;
2965 }
satok723a27e2010-11-11 14:58:11 +09002966
2967 private void saveSubtypeHistory(
2968 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2969 StringBuilder builder = new StringBuilder();
2970 boolean isImeAdded = false;
2971 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2972 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2973 newSubtypeId);
2974 isImeAdded = true;
2975 }
2976 for (Pair<String, String> ime: savedImes) {
2977 String imeId = ime.first;
2978 String subtypeId = ime.second;
2979 if (TextUtils.isEmpty(subtypeId)) {
2980 subtypeId = NOT_A_SUBTYPE_ID_STR;
2981 }
2982 if (isImeAdded) {
2983 builder.append(INPUT_METHOD_SEPARATER);
2984 } else {
2985 isImeAdded = true;
2986 }
2987 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2988 subtypeId);
2989 }
2990 // Remove the last INPUT_METHOD_SEPARATER
2991 putSubtypeHistoryStr(builder.toString());
2992 }
2993
2994 public void addSubtypeToHistory(String imeId, String subtypeId) {
2995 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2996 for (Pair<String, String> ime: subtypeHistory) {
2997 if (ime.first.equals(imeId)) {
2998 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002999 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003000 + ime.second);
3001 }
3002 // We should break here
3003 subtypeHistory.remove(ime);
3004 break;
3005 }
3006 }
satokbb4aa062011-01-19 21:40:27 +09003007 if (DEBUG) {
3008 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3009 }
satok723a27e2010-11-11 14:58:11 +09003010 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3011 }
3012
3013 private void putSubtypeHistoryStr(String str) {
3014 if (DEBUG) {
3015 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3016 }
3017 Settings.Secure.putString(
3018 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3019 }
3020
3021 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3022 // Gets the first one from the history
3023 return getLastSubtypeForInputMethodLockedInternal(null);
3024 }
3025
3026 public String getLastSubtypeForInputMethodLocked(String imeId) {
3027 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3028 if (ime != null) {
3029 return ime.second;
3030 } else {
3031 return null;
3032 }
3033 }
3034
3035 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3036 List<Pair<String, ArrayList<String>>> enabledImes =
3037 getEnabledInputMethodsAndSubtypeListLocked();
3038 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003039 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003040 final String imeInTheHistory = imeAndSubtype.first;
3041 // If imeId is empty, returns the first IME and subtype in the history
3042 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3043 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003044 final String subtypeHashCode =
3045 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3046 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003047 if (!TextUtils.isEmpty(subtypeHashCode)) {
3048 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003049 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003050 }
3051 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3052 }
3053 }
3054 }
3055 if (DEBUG) {
3056 Slog.d(TAG, "No enabled IME found in the history");
3057 }
3058 return null;
3059 }
3060
satokdf31ae62011-01-15 06:19:44 +09003061 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003062 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3063 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3064 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003065 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3066 if (explicitlyEnabledSubtypes.size() == 0) {
3067 // If there are no explicitly enabled subtypes, applicable subtypes are
3068 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003069 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003070 // If IME is enabled and no subtypes are enabled, applicable subtypes
3071 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003072 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003073 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003074 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003075 if (implicitlySelectedSubtypes != null) {
3076 final int N = implicitlySelectedSubtypes.size();
3077 for (int i = 0; i < N; ++i) {
3078 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3079 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3080 return subtypeHashCode;
3081 }
3082 }
3083 }
3084 }
3085 } else {
satokf6cafb62011-01-17 16:29:02 +09003086 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003087 if (s.equals(subtypeHashCode)) {
3088 // If both imeId and subtypeId are enabled, return subtypeId.
3089 return s;
3090 }
satok723a27e2010-11-11 14:58:11 +09003091 }
3092 }
3093 // If imeId was enabled but subtypeId was disabled.
3094 return NOT_A_SUBTYPE_ID_STR;
3095 }
3096 }
3097 // If both imeId and subtypeId are disabled, return null
3098 return null;
3099 }
3100
3101 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3102 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3103 final String subtypeHistoryStr = getSubtypeHistoryStr();
3104 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3105 return imsList;
3106 }
3107 mInputMethodSplitter.setString(subtypeHistoryStr);
3108 while (mInputMethodSplitter.hasNext()) {
3109 String nextImsStr = mInputMethodSplitter.next();
3110 mSubtypeSplitter.setString(nextImsStr);
3111 if (mSubtypeSplitter.hasNext()) {
3112 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3113 // The first element is ime id.
3114 String imeId = mSubtypeSplitter.next();
3115 while (mSubtypeSplitter.hasNext()) {
3116 subtypeId = mSubtypeSplitter.next();
3117 break;
3118 }
3119 imsList.add(new Pair<String, String>(imeId, subtypeId));
3120 }
3121 }
3122 return imsList;
3123 }
3124
3125 private String getSubtypeHistoryStr() {
3126 if (DEBUG) {
3127 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3128 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3129 }
3130 return Settings.Secure.getString(
3131 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3132 }
3133
3134 public void putSelectedInputMethod(String imeId) {
3135 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3136 }
3137
3138 public void putSelectedSubtype(int subtypeId) {
3139 Settings.Secure.putInt(
3140 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3141 }
satokd87c2592010-09-29 11:52:06 +09003142 }
3143
satoke7c6998e2011-06-03 17:57:59 +09003144 private static class InputMethodFileManager {
3145 private static final String SYSTEM_PATH = "system";
3146 private static final String INPUT_METHOD_PATH = "inputmethod";
3147 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3148 private static final String NODE_SUBTYPES = "subtypes";
3149 private static final String NODE_SUBTYPE = "subtype";
3150 private static final String NODE_IMI = "imi";
3151 private static final String ATTR_ID = "id";
3152 private static final String ATTR_LABEL = "label";
3153 private static final String ATTR_ICON = "icon";
3154 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3155 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3156 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3157 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3158 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3159 private final HashMap<String, InputMethodInfo> mMethodMap;
3160 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3161 new HashMap<String, List<InputMethodSubtype>>();
3162 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3163 if (methodMap == null) {
3164 throw new NullPointerException("methodMap is null");
3165 }
3166 mMethodMap = methodMap;
3167 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3168 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3169 if (!inputMethodDir.mkdirs()) {
3170 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3171 }
3172 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3173 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3174 if (!subtypeFile.exists()) {
3175 // If "subtypes.xml" doesn't exist, create a blank file.
3176 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3177 methodMap);
3178 } else {
3179 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3180 }
3181 }
3182
3183 private void deleteAllInputMethodSubtypes(String imiId) {
3184 synchronized (mMethodMap) {
3185 mSubtypesMap.remove(imiId);
3186 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3187 mMethodMap);
3188 }
3189 }
3190
3191 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003192 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003193 synchronized (mMethodMap) {
3194 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3195 final int N = additionalSubtypes.length;
3196 for (int i = 0; i < N; ++i) {
3197 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003198 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003199 subtypes.add(subtype);
3200 }
3201 }
satok4a28bde2011-06-29 21:03:40 +09003202 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003203 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3204 mMethodMap);
3205 }
3206 }
3207
3208 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3209 synchronized (mMethodMap) {
3210 return mSubtypesMap;
3211 }
3212 }
3213
3214 private static void writeAdditionalInputMethodSubtypes(
3215 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3216 HashMap<String, InputMethodInfo> methodMap) {
3217 // Safety net for the case that this function is called before methodMap is set.
3218 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3219 FileOutputStream fos = null;
3220 try {
3221 fos = subtypesFile.startWrite();
3222 final XmlSerializer out = new FastXmlSerializer();
3223 out.setOutput(fos, "utf-8");
3224 out.startDocument(null, true);
3225 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3226 out.startTag(null, NODE_SUBTYPES);
3227 for (String imiId : allSubtypes.keySet()) {
3228 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3229 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3230 continue;
3231 }
3232 out.startTag(null, NODE_IMI);
3233 out.attribute(null, ATTR_ID, imiId);
3234 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3235 final int N = subtypesList.size();
3236 for (int i = 0; i < N; ++i) {
3237 final InputMethodSubtype subtype = subtypesList.get(i);
3238 out.startTag(null, NODE_SUBTYPE);
3239 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3240 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3241 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3242 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3243 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3244 out.attribute(null, ATTR_IS_AUXILIARY,
3245 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3246 out.endTag(null, NODE_SUBTYPE);
3247 }
3248 out.endTag(null, NODE_IMI);
3249 }
3250 out.endTag(null, NODE_SUBTYPES);
3251 out.endDocument();
3252 subtypesFile.finishWrite(fos);
3253 } catch (java.io.IOException e) {
3254 Slog.w(TAG, "Error writing subtypes", e);
3255 if (fos != null) {
3256 subtypesFile.failWrite(fos);
3257 }
3258 }
3259 }
3260
3261 private static void readAdditionalInputMethodSubtypes(
3262 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3263 if (allSubtypes == null || subtypesFile == null) return;
3264 allSubtypes.clear();
3265 FileInputStream fis = null;
3266 try {
3267 fis = subtypesFile.openRead();
3268 final XmlPullParser parser = Xml.newPullParser();
3269 parser.setInput(fis, null);
3270 int type = parser.getEventType();
3271 // Skip parsing until START_TAG
3272 while ((type = parser.next()) != XmlPullParser.START_TAG
3273 && type != XmlPullParser.END_DOCUMENT) {}
3274 String firstNodeName = parser.getName();
3275 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3276 throw new XmlPullParserException("Xml doesn't start with subtypes");
3277 }
3278 final int depth =parser.getDepth();
3279 String currentImiId = null;
3280 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3281 while (((type = parser.next()) != XmlPullParser.END_TAG
3282 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3283 if (type != XmlPullParser.START_TAG)
3284 continue;
3285 final String nodeName = parser.getName();
3286 if (NODE_IMI.equals(nodeName)) {
3287 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3288 if (TextUtils.isEmpty(currentImiId)) {
3289 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3290 continue;
3291 }
3292 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3293 allSubtypes.put(currentImiId, tempSubtypesArray);
3294 } else if (NODE_SUBTYPE.equals(nodeName)) {
3295 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3296 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3297 continue;
3298 }
3299 final int icon = Integer.valueOf(
3300 parser.getAttributeValue(null, ATTR_ICON));
3301 final int label = Integer.valueOf(
3302 parser.getAttributeValue(null, ATTR_LABEL));
3303 final String imeSubtypeLocale =
3304 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3305 final String imeSubtypeMode =
3306 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3307 final String imeSubtypeExtraValue =
3308 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003309 final boolean isAuxiliary = "1".equals(String.valueOf(
3310 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003311 final InputMethodSubtype subtype =
3312 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3313 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3314 tempSubtypesArray.add(subtype);
3315 }
3316 }
3317 } catch (XmlPullParserException e) {
3318 Slog.w(TAG, "Error reading subtypes: " + e);
3319 return;
3320 } catch (java.io.IOException e) {
3321 Slog.w(TAG, "Error reading subtypes: " + e);
3322 return;
3323 } catch (NumberFormatException e) {
3324 Slog.w(TAG, "Error reading subtypes: " + e);
3325 return;
3326 } finally {
3327 if (fis != null) {
3328 try {
3329 fis.close();
3330 } catch (java.io.IOException e1) {
3331 Slog.w(TAG, "Failed to close.");
3332 }
3333 }
3334 }
3335 }
3336 }
3337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 @Override
3341 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3342 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3343 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3346 + Binder.getCallingPid()
3347 + ", uid=" + Binder.getCallingUid());
3348 return;
3349 }
3350
3351 IInputMethod method;
3352 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 synchronized (mMethodMap) {
3357 p.println("Current Input Method Manager state:");
3358 int N = mMethodList.size();
3359 p.println(" Input Methods:");
3360 for (int i=0; i<N; i++) {
3361 InputMethodInfo info = mMethodList.get(i);
3362 p.println(" InputMethod #" + i + ":");
3363 info.dump(p, " ");
3364 }
3365 p.println(" Clients:");
3366 for (ClientState ci : mClients.values()) {
3367 p.println(" Client " + ci + ":");
3368 p.println(" client=" + ci.client);
3369 p.println(" inputContext=" + ci.inputContext);
3370 p.println(" sessionRequested=" + ci.sessionRequested);
3371 p.println(" curSession=" + ci.curSession);
3372 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003373 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003375 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3376 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3378 + " mBoundToMethod=" + mBoundToMethod);
3379 p.println(" mCurToken=" + mCurToken);
3380 p.println(" mCurIntent=" + mCurIntent);
3381 method = mCurMethod;
3382 p.println(" mCurMethod=" + mCurMethod);
3383 p.println(" mEnabledSession=" + mEnabledSession);
3384 p.println(" mShowRequested=" + mShowRequested
3385 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3386 + " mShowForced=" + mShowForced
3387 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003388 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003390
Jeff Brownb88102f2010-09-08 11:49:43 -07003391 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 pw.flush();
3394 try {
3395 client.client.asBinder().dump(fd, args);
3396 } catch (RemoteException e) {
3397 p.println("Input method client dead: " + e);
3398 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003399 } else {
3400 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003402
Jeff Brownb88102f2010-09-08 11:49:43 -07003403 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 pw.flush();
3406 try {
3407 method.asBinder().dump(fd, args);
3408 } catch (RemoteException e) {
3409 p.println("Input method service dead: " + e);
3410 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003411 } else {
3412 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414 }
3415}