blob: f5c4ed46c4117a2a01397cd763548897a1c0a15c [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;
satok3afd6c02011-11-18 08:38:19 +0900385 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
387 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900388 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700389 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
390 hideInputMethodMenu();
391 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800393 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395
396 // Inform the current client of the change in active status
397 try {
398 if (mCurClient != null && mCurClient.client != null) {
399 mCurClient.client.setActive(mScreenOn);
400 }
401 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800402 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 + mCurClient.pid + " uid " + mCurClient.uid);
404 }
405 }
406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800408 class MyPackageMonitor extends PackageMonitor {
409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800411 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800413 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
415 final int N = mMethodList.size();
416 if (curInputMethodId != null) {
417 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800418 InputMethodInfo imi = mMethodList.get(i);
419 if (imi.getId().equals(curInputMethodId)) {
420 for (String pkg : packages) {
421 if (imi.getPackageName().equals(pkg)) {
422 if (!doit) {
423 return true;
424 }
satok723a27e2010-11-11 14:58:11 +0900425 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800426 chooseNewDefaultIMELocked();
427 return true;
428 }
429 }
430 }
431 }
432 }
433 }
434 return false;
435 }
436
437 @Override
438 public void onSomePackagesChanged() {
439 synchronized (mMethodMap) {
440 InputMethodInfo curIm = null;
441 String curInputMethodId = Settings.Secure.getString(mContext
442 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
443 final int N = mMethodList.size();
444 if (curInputMethodId != null) {
445 for (int i=0; i<N; i++) {
446 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900447 final String imiId = imi.getId();
448 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800449 curIm = imi;
450 }
satoke7c6998e2011-06-03 17:57:59 +0900451
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900453 if (isPackageModified(imi.getPackageName())) {
454 mFileManager.deleteAllInputMethodSubtypes(imiId);
455 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 if (change == PACKAGE_TEMPORARY_CHANGE
457 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800458 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800459 + imi.getComponent());
460 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 buildInputMethodListLocked(mMethodList, mMethodMap);
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800469 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 int change = isPackageDisappearing(curIm.getPackageName());
471 if (change == PACKAGE_TEMPORARY_CHANGE
472 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800473 ServiceInfo si = null;
474 try {
475 si = mContext.getPackageManager().getServiceInfo(
476 curIm.getComponent(), 0);
477 } catch (PackageManager.NameNotFoundException ex) {
478 }
479 if (si == null) {
480 // Uh oh, current input method is no longer around!
481 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800482 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900483 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800485 changed = true;
486 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800487 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900488 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800489 }
490 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800491 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800492 }
satokab751aa2010-09-14 19:17:36 +0900493
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 if (curIm == null) {
495 // We currently don't have a default input method... is
496 // one now available?
497 changed = chooseNewDefaultIMELocked();
498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800500 if (changed) {
501 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 }
504 }
505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800506
Jean Chalarde0d32a62011-10-20 20:36:07 +0900507 private static class MethodCallback extends IInputMethodCallback.Stub {
508 private final IInputMethod mMethod;
509 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800510
Jean Chalarde0d32a62011-10-20 20:36:07 +0900511 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900513 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
satoke7c6998e2011-06-03 17:57:59 +0900516 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public void finishedEvent(int seq, boolean handled) throws RemoteException {
518 }
519
satoke7c6998e2011-06-03 17:57:59 +0900520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900522 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800525
Dianne Hackborn661cd522011-08-22 00:26:20 -0700526 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800528 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 mHandler = new Handler(this);
530 mIWindowManager = IWindowManager.Stub.asInterface(
531 ServiceManager.getService(Context.WINDOW_SERVICE));
532 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900533 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 public void executeMessage(Message msg) {
535 handleMessage(msg);
536 }
537 });
satok7cfc0ed2011-06-20 21:29:36 +0900538
satok7cfc0ed2011-06-20 21:29:36 +0900539 mImeSwitcherNotification = new Notification();
540 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
541 mImeSwitcherNotification.when = 0;
542 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
543 mImeSwitcherNotification.tickerText = null;
544 mImeSwitcherNotification.defaults = 0; // please be quiet
545 mImeSwitcherNotification.sound = null;
546 mImeSwitcherNotification.vibrate = null;
547 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900548 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900549
550 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900551
satoke7c6998e2011-06-03 17:57:59 +0900552 synchronized (mMethodMap) {
553 mFileManager = new InputMethodFileManager(mMethodMap);
554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800556 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 IntentFilter screenOnOffFilt = new IntentFilter();
559 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
560 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700561 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
satok7cfc0ed2011-06-20 21:29:36 +0900564 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900565
satokd87c2592010-09-29 11:52:06 +0900566 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900567 mSettings = new InputMethodSettings(
568 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900570 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571
satokd87c2592010-09-29 11:52:06 +0900572 if (TextUtils.isEmpty(Settings.Secure.getString(
573 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900575 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
577 try {
satokd87c2592010-09-29 11:52:06 +0900578 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 imi.getPackageName(), 0).getResources();
580 if (res.getBoolean(imi.getIsDefaultResourceId())) {
581 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800582 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
584 } catch (PackageManager.NameNotFoundException ex) {
585 } catch (Resources.NotFoundException ex) {
586 }
587 }
588 }
satokd87c2592010-09-29 11:52:06 +0900589 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900590 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800591 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900594 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mSettingsObserver = new SettingsObserver(mHandler);
599 updateFromSettingsLocked();
600 }
601
602 @Override
603 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
604 throws RemoteException {
605 try {
606 return super.onTransact(code, data, reply, flags);
607 } catch (RuntimeException e) {
608 // The input method manager only throws security exceptions, so let's
609 // log all others.
610 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800611 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613 throw e;
614 }
615 }
616
Dianne Hackborn661cd522011-08-22 00:26:20 -0700617 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700618 synchronized (mMethodMap) {
619 if (!mSystemReady) {
620 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700621 mKeyguardManager = (KeyguardManager)
622 mContext.getSystemService(Context.KEYGUARD_SERVICE);
623 mNotificationManager = (NotificationManager)
624 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
625 mStatusBar = statusBar;
626 statusBar.setIconVisibility("ime", false);
627 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900628 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
629 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700630 try {
631 startInputInnerLocked();
632 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800633 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700634 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700635 }
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
satok15452a42011-10-28 17:58:28 +0900639 private void setImeWindowVisibilityStatusHiddenLocked() {
640 mImeWindowVis = 0;
641 updateImeWindowStatusLocked();
642 }
643
satok3afd6c02011-11-18 08:38:19 +0900644 private void refreshImeWindowVisibilityLocked() {
645 final Configuration conf = mRes.getConfiguration();
646 final boolean haveHardKeyboard = conf.keyboard
647 != Configuration.KEYBOARD_NOKEYS;
648 final boolean hardKeyShown = haveHardKeyboard
649 && conf.hardKeyboardHidden
650 != Configuration.HARDKEYBOARDHIDDEN_YES;
651 final boolean isScreenLocked = mKeyguardManager != null
652 && mKeyguardManager.isKeyguardLocked()
653 && mKeyguardManager.isKeyguardSecure();
654 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
655 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
656 updateImeWindowStatusLocked();
657 }
658
satok15452a42011-10-28 17:58:28 +0900659 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900660 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700661 }
662
satoke7c6998e2011-06-03 17:57:59 +0900663 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 public List<InputMethodInfo> getInputMethodList() {
665 synchronized (mMethodMap) {
666 return new ArrayList<InputMethodInfo>(mMethodList);
667 }
668 }
669
satoke7c6998e2011-06-03 17:57:59 +0900670 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public List<InputMethodInfo> getEnabledInputMethodList() {
672 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900673 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 }
675 }
676
satokbb4aa062011-01-19 21:40:27 +0900677 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
678 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
679 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
680 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
681 for (InputMethodInfo imi: getEnabledInputMethodList()) {
682 enabledInputMethodAndSubtypes.put(
683 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
684 }
685 return enabledInputMethodAndSubtypes;
686 }
687
688 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
689 boolean allowsImplicitlySelectedSubtypes) {
690 if (imi == null && mCurMethodId != null) {
691 imi = mMethodMap.get(mCurMethodId);
692 }
satok7265d9b2011-02-14 15:47:30 +0900693 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900694 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900695 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900696 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900697 }
satok7265d9b2011-02-14 15:47:30 +0900698 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900699 }
700
satoke7c6998e2011-06-03 17:57:59 +0900701 @Override
satok16331c82010-12-20 23:48:46 +0900702 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
703 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900704 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900705 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900706 }
707 }
708
satoke7c6998e2011-06-03 17:57:59 +0900709 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 public void addClient(IInputMethodClient client,
711 IInputContext inputContext, int uid, int pid) {
712 synchronized (mMethodMap) {
713 mClients.put(client.asBinder(), new ClientState(client,
714 inputContext, uid, pid));
715 }
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
satoke7c6998e2011-06-03 17:57:59 +0900718 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 public void removeClient(IInputMethodClient client) {
720 synchronized (mMethodMap) {
721 mClients.remove(client.asBinder());
722 }
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 void executeOrSendMessage(IInterface target, Message msg) {
726 if (target.asBinder() instanceof Binder) {
727 mCaller.sendMessage(msg);
728 } else {
729 handleMessage(msg);
730 msg.recycle();
731 }
732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800733
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700734 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 + mCurClient.client.asBinder());
738 if (mBoundToMethod) {
739 mBoundToMethod = false;
740 if (mCurMethod != null) {
741 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
742 MSG_UNBIND_INPUT, mCurMethod));
743 }
744 }
745 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
746 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
747 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 // Call setActive(false) on the old client
750 try {
751 mCurClient.client.setActive(false);
752 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800753 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 + mCurClient.pid + " uid " + mCurClient.uid);
755 }
756 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800757
The Android Open Source Project10592532009-03-18 17:39:46 -0700758 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 private int getImeShowFlags() {
763 int flags = 0;
764 if (mShowForced) {
765 flags |= InputMethod.SHOW_FORCED
766 | InputMethod.SHOW_EXPLICIT;
767 } else if (mShowExplicitlyRequested) {
768 flags |= InputMethod.SHOW_EXPLICIT;
769 }
770 return flags;
771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 private int getAppShowFlags() {
774 int flags = 0;
775 if (mShowForced) {
776 flags |= InputMethodManager.SHOW_FORCED;
777 } else if (!mShowExplicitlyRequested) {
778 flags |= InputMethodManager.SHOW_IMPLICIT;
779 }
780 return flags;
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
784 if (!mBoundToMethod) {
785 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
786 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
787 mBoundToMethod = true;
788 }
789 final SessionState session = mCurClient.curSession;
790 if (initial) {
791 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
792 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
793 } else {
794 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
795 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
796 }
797 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800798 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800799 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
801 return needResult
802 ? new InputBindResult(session.session, mCurId, mCurSeq)
803 : null;
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 InputBindResult startInputLocked(IInputMethodClient client,
807 IInputContext inputContext, EditorInfo attribute,
808 boolean initial, boolean needResult) {
809 // If no method is currently selected, do nothing.
810 if (mCurMethodId == null) {
811 return mNoBinding;
812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 ClientState cs = mClients.get(client.asBinder());
815 if (cs == null) {
816 throw new IllegalArgumentException("unknown client "
817 + client.asBinder());
818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 try {
821 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
822 // Check with the window manager to make sure this client actually
823 // has a window with focus. If not, reject. This is thread safe
824 // because if the focus changes some time before or after, the
825 // next client receiving focus that has any interest in input will
826 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
829 return null;
830 }
831 } catch (RemoteException e) {
832 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 if (mCurClient != cs) {
835 // If the client is changing, we need to switch over to the new
836 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700837 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800838 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 + cs.client.asBinder());
840
841 // If the screen is on, inform the new client it is active
842 if (mScreenOn) {
843 try {
844 cs.client.setActive(mScreenOn);
845 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800846 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 + cs.pid + " uid " + cs.uid);
848 }
849 }
850 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 // Bump up the sequence for this client and attach it.
853 mCurSeq++;
854 if (mCurSeq <= 0) mCurSeq = 1;
855 mCurClient = cs;
856 mCurInputContext = inputContext;
857 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 // Check if the input method is changing.
860 if (mCurId != null && mCurId.equals(mCurMethodId)) {
861 if (cs.curSession != null) {
862 // Fast case: if we are already connected to the input method,
863 // then just return it.
864 return attachNewInputLocked(initial, needResult);
865 }
866 if (mHaveConnection) {
867 if (mCurMethod != null) {
868 if (!cs.sessionRequested) {
869 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800870 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
872 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900873 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
875 // Return to client, and we will get back with it when
876 // we have had a session made for it.
877 return new InputBindResult(null, mCurId, mCurSeq);
878 } else if (SystemClock.uptimeMillis()
879 < (mLastBindTime+TIME_TO_RECONNECT)) {
880 // In this case we have connected to the service, but
881 // don't yet have its interface. If it hasn't been too
882 // long since we did the connection, we'll return to
883 // the client and wait to get the service interface so
884 // we can report back. If it has been too long, we want
885 // to fall through so we can try a disconnect/reconnect
886 // to see if we can get back in touch with the service.
887 return new InputBindResult(null, mCurId, mCurSeq);
888 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
890 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892 }
893 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800894
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700895 return startInputInnerLocked();
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700898 InputBindResult startInputInnerLocked() {
899 if (mCurMethodId == null) {
900 return mNoBinding;
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700903 if (!mSystemReady) {
904 // If the system is not yet ready, we shouldn't be running third
905 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700906 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 InputMethodInfo info = mMethodMap.get(mCurMethodId);
910 if (info == null) {
911 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700914 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
917 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700918 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
919 com.android.internal.R.string.input_method_binding_label);
920 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
921 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700922 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
923 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 mLastBindTime = SystemClock.uptimeMillis();
925 mHaveConnection = true;
926 mCurId = info.getId();
927 mCurToken = new Binder();
928 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 mIWindowManager.addWindowToken(mCurToken,
931 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
932 } catch (RemoteException e) {
933 }
934 return new InputBindResult(null, mCurId, mCurSeq);
935 } else {
936 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800937 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 + mCurIntent);
939 }
940 return null;
941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
satoke7c6998e2011-06-03 17:57:59 +0900943 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 public InputBindResult startInput(IInputMethodClient client,
945 IInputContext inputContext, EditorInfo attribute,
946 boolean initial, boolean needResult) {
947 synchronized (mMethodMap) {
948 final long ident = Binder.clearCallingIdentity();
949 try {
950 return startInputLocked(client, inputContext, attribute,
951 initial, needResult);
952 } finally {
953 Binder.restoreCallingIdentity(ident);
954 }
955 }
956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800957
satoke7c6998e2011-06-03 17:57:59 +0900958 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 public void finishInput(IInputMethodClient client) {
960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
satoke7c6998e2011-06-03 17:57:59 +0900962 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 public void onServiceConnected(ComponentName name, IBinder service) {
964 synchronized (mMethodMap) {
965 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
966 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700967 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800968 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700969 unbindCurrentMethodLocked(false);
970 return;
971 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800972 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700973 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
974 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800976 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700977 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700979 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900980 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
983 }
984 }
985
986 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
987 synchronized (mMethodMap) {
988 if (mCurMethod != null && method != null
989 && mCurMethod.asBinder() == method.asBinder()) {
990 if (mCurClient != null) {
991 mCurClient.curSession = new SessionState(mCurClient,
992 method, session);
993 mCurClient.sessionRequested = false;
994 InputBindResult res = attachNewInputLocked(true, true);
995 if (res.method != null) {
996 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
997 MSG_BIND_METHOD, mCurClient.client, res));
998 }
999 }
1000 }
1001 }
1002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001003
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001004 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001005 if (mVisibleBound) {
1006 mContext.unbindService(mVisibleConnection);
1007 mVisibleBound = false;
1008 }
1009
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001010 if (mHaveConnection) {
1011 mContext.unbindService(this);
1012 mHaveConnection = false;
1013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001015 if (mCurToken != null) {
1016 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001017 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001018 mIWindowManager.removeWindowToken(mCurToken);
1019 } catch (RemoteException e) {
1020 }
1021 mCurToken = null;
1022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
The Android Open Source Project10592532009-03-18 17:39:46 -07001024 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001025 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001027 if (reportToClient && mCurClient != null) {
1028 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1029 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1030 }
1031 }
1032
Devin Taylor0c33ed22010-02-23 13:26:46 -06001033 private void finishSession(SessionState sessionState) {
1034 if (sessionState != null && sessionState.session != null) {
1035 try {
1036 sessionState.session.finishSession();
1037 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001038 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001039 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001040 }
1041 }
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001044 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (mCurMethod != null) {
1046 for (ClientState cs : mClients.values()) {
1047 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001048 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 cs.curSession = null;
1050 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001051
1052 finishSession(mEnabledSession);
1053 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 mCurMethod = null;
1055 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001056 if (mStatusBar != null) {
1057 mStatusBar.setIconVisibility("ime", false);
1058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001060
satoke7c6998e2011-06-03 17:57:59 +09001061 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 public void onServiceDisconnected(ComponentName name) {
1063 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 + " mCurIntent=" + mCurIntent);
1066 if (mCurMethod != null && mCurIntent != null
1067 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001068 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 // We consider this to be a new bind attempt, since the system
1070 // should now try to restart the service for us.
1071 mLastBindTime = SystemClock.uptimeMillis();
1072 mShowRequested = mInputShown;
1073 mInputShown = false;
1074 if (mCurClient != null) {
1075 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1076 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1077 }
1078 }
1079 }
1080 }
1081
satokf9f01002011-05-19 21:31:50 +09001082 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001084 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 long ident = Binder.clearCallingIdentity();
1086 try {
1087 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001088 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 return;
1090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 synchronized (mMethodMap) {
1093 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001094 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001095 if (mStatusBar != null) {
1096 mStatusBar.setIconVisibility("ime", false);
1097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001100 CharSequence contentDescription = null;
1101 try {
1102 PackageManager packageManager = mContext.getPackageManager();
1103 contentDescription = packageManager.getApplicationLabel(
1104 packageManager.getApplicationInfo(packageName, 0));
1105 } catch (NameNotFoundException nnfe) {
1106 /* ignore */
1107 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001108 if (mStatusBar != null) {
1109 mStatusBar.setIcon("ime", packageName, iconId, 0,
1110 contentDescription != null
1111 ? contentDescription.toString() : null);
1112 mStatusBar.setIconVisibility("ime", true);
1113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
1116 } finally {
1117 Binder.restoreCallingIdentity(ident);
1118 }
1119 }
1120
satok7cfc0ed2011-06-20 21:29:36 +09001121 private boolean needsToShowImeSwitchOngoingNotification() {
1122 if (!mShowOngoingImeSwitcherForPhones) return false;
1123 synchronized (mMethodMap) {
1124 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1125 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001126 if (N > 2) return true;
1127 if (N < 1) return false;
1128 int nonAuxCount = 0;
1129 int auxCount = 0;
1130 InputMethodSubtype nonAuxSubtype = null;
1131 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001132 for(int i = 0; i < N; ++i) {
1133 final InputMethodInfo imi = imis.get(i);
1134 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1135 imi, true);
1136 final int subtypeCount = subtypes.size();
1137 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001138 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001139 } else {
1140 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001141 final InputMethodSubtype subtype = subtypes.get(j);
1142 if (!subtype.isAuxiliary()) {
1143 ++nonAuxCount;
1144 nonAuxSubtype = subtype;
1145 } else {
1146 ++auxCount;
1147 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001148 }
1149 }
1150 }
satok7cfc0ed2011-06-20 21:29:36 +09001151 }
satokb6359412011-06-28 17:47:41 +09001152 if (nonAuxCount > 1 || auxCount > 1) {
1153 return true;
1154 } else if (nonAuxCount == 1 && auxCount == 1) {
1155 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001156 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1157 || auxSubtype.overridesImplicitlyEnabledSubtype()
1158 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001159 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1160 return false;
1161 }
1162 return true;
1163 }
1164 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001165 }
satok7cfc0ed2011-06-20 21:29:36 +09001166 }
1167
satokdbf29502011-08-25 15:28:23 +09001168 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001169 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001170 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001171 int uid = Binder.getCallingUid();
1172 long ident = Binder.clearCallingIdentity();
1173 try {
1174 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001175 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001176 return;
1177 }
1178
1179 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001180 mImeWindowVis = vis;
1181 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001182 if (mStatusBar != null) {
1183 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1184 }
satok7cfc0ed2011-06-20 21:29:36 +09001185 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001186 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1187 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001188 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001189 final CharSequence title = mRes.getText(
1190 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001191 final CharSequence imiLabel = imi.loadLabel(pm);
1192 final CharSequence summary = mCurrentSubtype != null
1193 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1194 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1195 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001196 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001197 : imiLabel;
1198
satok7cfc0ed2011-06-20 21:29:36 +09001199 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001200 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001201 if (mNotificationManager != null) {
1202 mNotificationManager.notify(
1203 com.android.internal.R.string.select_input_method,
1204 mImeSwitcherNotification);
1205 mNotificationShown = true;
1206 }
satok7cfc0ed2011-06-20 21:29:36 +09001207 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001208 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001209 mNotificationManager.cancel(
1210 com.android.internal.R.string.select_input_method);
1211 mNotificationShown = false;
1212 }
1213 }
satok06487a52010-10-29 11:37:18 +09001214 }
1215 } finally {
1216 Binder.restoreCallingIdentity(ident);
1217 }
1218 }
1219
satoke7c6998e2011-06-03 17:57:59 +09001220 @Override
satokf9f01002011-05-19 21:31:50 +09001221 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1222 synchronized (mMethodMap) {
1223 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1224 for (int i = 0; i < spans.length; ++i) {
1225 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001226 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001227 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001228 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001229 }
1230 }
1231 }
1232 }
1233
satoke7c6998e2011-06-03 17:57:59 +09001234 @Override
satokf9f01002011-05-19 21:31:50 +09001235 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1236 synchronized (mMethodMap) {
1237 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1238 // TODO: Do not send the intent if the process of the targetImi is already dead.
1239 if (targetImi != null) {
1240 final String[] suggestions = span.getSuggestions();
1241 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001242 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001243 final Intent intent = new Intent();
1244 // Ensures that only a class in the original IME package will receive the
1245 // notification.
satok42c5a162011-05-26 16:46:14 +09001246 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001247 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1248 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1249 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1250 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1251 mContext.sendBroadcast(intent);
1252 return true;
1253 }
1254 }
1255 return false;
1256 }
1257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001259 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1260 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1261 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1262 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001264 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001265 // There is no input method selected, try to choose new applicable input method.
1266 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1267 id = Settings.Secure.getString(mContext.getContentResolver(),
1268 Settings.Secure.DEFAULT_INPUT_METHOD);
1269 }
1270 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 try {
satokab751aa2010-09-14 19:17:36 +09001272 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001274 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001275 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001276 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
satokf3db1af2010-11-23 13:34:33 +09001278 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001279 } else {
1280 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001281 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001282 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 }
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
satokab751aa2010-09-14 19:17:36 +09001286 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 InputMethodInfo info = mMethodMap.get(id);
1288 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001289 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001293 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001294 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1295 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001296 }
1297 if (subtype != mCurrentSubtype) {
1298 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001299 if (subtype != null) {
1300 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1301 }
satokcd7cd292010-11-20 15:46:23 +09001302 if (mCurMethod != null) {
1303 try {
satok3afd6c02011-11-18 08:38:19 +09001304 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001305 // If subtype is null, try to find the most applicable one from
1306 // getCurrentInputMethodSubtype.
1307 if (subtype == null) {
1308 subtype = getCurrentInputMethodSubtype();
1309 }
1310 mCurMethod.changeInputMethodSubtype(subtype);
1311 } catch (RemoteException e) {
1312 return;
satokab751aa2010-09-14 19:17:36 +09001313 }
1314 }
1315 }
1316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 return;
1318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 final long ident = Binder.clearCallingIdentity();
1321 try {
satokab751aa2010-09-14 19:17:36 +09001322 // Set a subtype to this input method.
1323 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001324 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1325 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1326 // because mCurMethodId is stored as a history in
1327 // setSelectedInputMethodAndSubtypeLocked().
1328 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329
1330 if (ActivityManagerNative.isSystemReady()) {
1331 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001332 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 intent.putExtra("input_method_id", id);
1334 mContext.sendBroadcast(intent);
1335 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001336 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 } finally {
1338 Binder.restoreCallingIdentity(ident);
1339 }
1340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
satok42c5a162011-05-26 16:46:14 +09001342 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001343 public boolean showSoftInput(IInputMethodClient client, int flags,
1344 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001345 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 long ident = Binder.clearCallingIdentity();
1347 try {
1348 synchronized (mMethodMap) {
1349 if (mCurClient == null || client == null
1350 || mCurClient.client.asBinder() != client.asBinder()) {
1351 try {
1352 // We need to check if this is the current client with
1353 // focus in the window manager, to allow this call to
1354 // be made before input is started in it.
1355 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001356 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001360 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
1362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001365 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
1367 } finally {
1368 Binder.restoreCallingIdentity(ident);
1369 }
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
The Android Open Source Project4df24232009-03-05 14:34:35 -08001372 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 mShowRequested = true;
1374 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1375 mShowExplicitlyRequested = true;
1376 }
1377 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1378 mShowExplicitlyRequested = true;
1379 mShowForced = true;
1380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
Dianne Hackborncc278702009-09-02 23:07:23 -07001382 if (!mSystemReady) {
1383 return false;
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1389 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1390 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001392 if (mHaveConnection && !mVisibleBound) {
1393 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1394 mVisibleBound = true;
1395 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001396 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001398 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 // The client has asked to have the input method shown, but
1400 // we have been sitting here too long with a connection to the
1401 // service and no interface received, so let's disconnect/connect
1402 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001405 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001407 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1408 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
The Android Open Source Project4df24232009-03-05 14:34:35 -08001411 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
satok42c5a162011-05-26 16:46:14 +09001414 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001415 public boolean hideSoftInput(IInputMethodClient client, int flags,
1416 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001417 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 long ident = Binder.clearCallingIdentity();
1419 try {
1420 synchronized (mMethodMap) {
1421 if (mCurClient == null || client == null
1422 || mCurClient.client.asBinder() != client.asBinder()) {
1423 try {
1424 // We need to check if this is the current client with
1425 // focus in the window manager, to allow this call to
1426 // be made before input is started in it.
1427 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001428 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1429 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001430 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001434 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001435 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001440 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 } finally {
1443 Binder.restoreCallingIdentity(ident);
1444 }
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
The Android Open Source Project4df24232009-03-05 14:34:35 -08001447 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1449 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001450 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001452 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001457 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001459 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001461 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1462 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1463 res = true;
1464 } else {
1465 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001467 if (mHaveConnection && mVisibleBound) {
1468 mContext.unbindService(mVisibleConnection);
1469 mVisibleBound = false;
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mInputShown = false;
1472 mShowRequested = false;
1473 mShowExplicitlyRequested = false;
1474 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001475 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
satok42c5a162011-05-26 16:46:14 +09001478 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001479 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1481 boolean first, int windowFlags) {
1482 long ident = Binder.clearCallingIdentity();
1483 try {
1484 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001485 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 + " viewHasFocus=" + viewHasFocus
1487 + " isTextEditor=" + isTextEditor
1488 + " softInputMode=#" + Integer.toHexString(softInputMode)
1489 + " first=" + first + " flags=#"
1490 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (mCurClient == null || client == null
1493 || mCurClient.client.asBinder() != client.asBinder()) {
1494 try {
1495 // We need to check if this is the current client with
1496 // focus in the window manager, to allow this call to
1497 // be made before input is started in it.
1498 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 return;
1501 }
1502 } catch (RemoteException e) {
1503 }
1504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001506 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001508 return;
1509 }
1510 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001511
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001512 // Should we auto-show the IME even if the caller has not
1513 // specified what should be done with it?
1514 // We only do this automatically if the window can resize
1515 // to accommodate the IME (so what the user sees will give
1516 // them good context without input information being obscured
1517 // by the IME) or if running on a large screen where there
1518 // is more room for the target window + IME.
1519 final boolean doAutoShow =
1520 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1521 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1522 || mRes.getConfiguration().isLayoutSizeAtLeast(
1523 Configuration.SCREENLAYOUT_SIZE_LARGE);
1524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1526 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001527 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1529 // There is no focus view, and this window will
1530 // be behind any soft input window, so hide the
1531 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001532 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001533 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001535 } else if (isTextEditor && doAutoShow && (softInputMode &
1536 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 // There is a focus view, and we are navigating forward
1538 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001539 // We only do this automatically if the window an resize
1540 // to accomodate the IME (so what the user sees will give
1541 // them good context without input information being obscured
1542 // by the IME) or if running on a large screen where there
1543 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001545 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547 break;
1548 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1549 // Do nothing.
1550 break;
1551 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1552 if ((softInputMode &
1553 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001554 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001555 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
1557 break;
1558 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001559 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001560 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 break;
1562 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1563 if ((softInputMode &
1564 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001565 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001566 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 break;
1569 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001570 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001571 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 break;
1573 }
1574 }
1575 } finally {
1576 Binder.restoreCallingIdentity(ident);
1577 }
1578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001579
satok42c5a162011-05-26 16:46:14 +09001580 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1582 synchronized (mMethodMap) {
1583 if (mCurClient == null || client == null
1584 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001585 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001586 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588
satok440aab52010-11-25 09:43:11 +09001589 // Always call subtype picker, because subtype picker is a superset of input method
1590 // picker.
satokab751aa2010-09-14 19:17:36 +09001591 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1592 }
1593 }
1594
satok42c5a162011-05-26 16:46:14 +09001595 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001597 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1598 }
1599
satok42c5a162011-05-26 16:46:14 +09001600 @Override
satok28203512010-11-24 11:06:49 +09001601 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1602 synchronized (mMethodMap) {
1603 if (subtype != null) {
1604 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1605 mMethodMap.get(id), subtype.hashCode()));
1606 } else {
1607 setInputMethod(token, id);
1608 }
1609 }
satokab751aa2010-09-14 19:17:36 +09001610 }
1611
satok42c5a162011-05-26 16:46:14 +09001612 @Override
satokb416a71e2010-11-25 20:42:14 +09001613 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001614 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001615 synchronized (mMethodMap) {
1616 if (mCurClient == null || client == null
1617 || mCurClient.client.asBinder() != client.asBinder()) {
1618 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1619 }
satok7fee71f2010-12-17 18:54:26 +09001620 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1621 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001622 }
1623 }
1624
satok4fc87d62011-05-20 16:13:43 +09001625 @Override
satok735cf382010-11-11 20:40:09 +09001626 public boolean switchToLastInputMethod(IBinder token) {
1627 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001628 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001629 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001630 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001631 lastImi = mMethodMap.get(lastIme.first);
1632 } else {
1633 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001634 }
satok4fc87d62011-05-20 16:13:43 +09001635 String targetLastImiId = null;
1636 int subtypeId = NOT_A_SUBTYPE_ID;
1637 if (lastIme != null && lastImi != null) {
1638 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1639 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1640 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1641 : mCurrentSubtype.hashCode();
1642 // If the last IME is the same as the current IME and the last subtype is not
1643 // defined, there is no need to switch to the last IME.
1644 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1645 targetLastImiId = lastIme.first;
1646 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1647 }
1648 }
1649
1650 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1651 // This is a safety net. If the currentSubtype can't be added to the history
1652 // and the framework couldn't find the last ime, we will make the last ime be
1653 // the most applicable enabled keyboard subtype of the system imes.
1654 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1655 if (enabled != null) {
1656 final int N = enabled.size();
1657 final String locale = mCurrentSubtype == null
1658 ? mRes.getConfiguration().locale.toString()
1659 : mCurrentSubtype.getLocale();
1660 for (int i = 0; i < N; ++i) {
1661 final InputMethodInfo imi = enabled.get(i);
1662 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1663 InputMethodSubtype keyboardSubtype =
1664 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1665 SUBTYPE_MODE_KEYBOARD, locale, true);
1666 if (keyboardSubtype != null) {
1667 targetLastImiId = imi.getId();
1668 subtypeId = getSubtypeIdFromHashCode(
1669 imi, keyboardSubtype.hashCode());
1670 if(keyboardSubtype.getLocale().equals(locale)) {
1671 break;
1672 }
1673 }
1674 }
1675 }
1676 }
1677 }
1678
1679 if (!TextUtils.isEmpty(targetLastImiId)) {
1680 if (DEBUG) {
1681 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1682 + ", from: " + mCurMethodId + ", " + subtypeId);
1683 }
1684 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1685 return true;
1686 } else {
1687 return false;
1688 }
satok735cf382010-11-11 20:40:09 +09001689 }
1690 }
1691
satoke7c6998e2011-06-03 17:57:59 +09001692 @Override
satok68f1b782011-04-11 14:26:04 +09001693 public InputMethodSubtype getLastInputMethodSubtype() {
1694 synchronized (mMethodMap) {
1695 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1696 // TODO: Handle the case of the last IME with no subtypes
1697 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1698 || TextUtils.isEmpty(lastIme.second)) return null;
1699 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1700 if (lastImi == null) return null;
1701 try {
1702 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001703 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1704 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1705 return null;
1706 }
1707 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001708 } catch (NumberFormatException e) {
1709 return null;
1710 }
1711 }
1712 }
1713
satoke7c6998e2011-06-03 17:57:59 +09001714 @Override
satokee5e77c2011-09-02 18:50:15 +09001715 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001716 // By this IPC call, only a process which shares the same uid with the IME can add
1717 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001718 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001719 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001720 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001721 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001722 final PackageManager pm = mContext.getPackageManager();
1723 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1724 if (packageInfos != null) {
1725 final int packageNum = packageInfos.length;
1726 for (int i = 0; i < packageNum; ++i) {
1727 if (packageInfos[i].equals(imi.getPackageName())) {
1728 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001729 final long ident = Binder.clearCallingIdentity();
1730 try {
1731 buildInputMethodListLocked(mMethodList, mMethodMap);
1732 } finally {
1733 Binder.restoreCallingIdentity(ident);
1734 }
satokee5e77c2011-09-02 18:50:15 +09001735 return;
satok91e88122011-07-18 11:11:42 +09001736 }
1737 }
1738 }
satoke7c6998e2011-06-03 17:57:59 +09001739 }
satokee5e77c2011-09-02 18:50:15 +09001740 return;
satoke7c6998e2011-06-03 17:57:59 +09001741 }
1742
satok28203512010-11-24 11:06:49 +09001743 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 synchronized (mMethodMap) {
1745 if (token == null) {
1746 if (mContext.checkCallingOrSelfPermission(
1747 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1748 != PackageManager.PERMISSION_GRANTED) {
1749 throw new SecurityException(
1750 "Using null token requires permission "
1751 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1752 }
1753 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001754 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1755 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 return;
1757 }
1758
satokc5933802011-08-31 21:26:04 +09001759 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 try {
satokab751aa2010-09-14 19:17:36 +09001761 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 } finally {
1763 Binder.restoreCallingIdentity(ident);
1764 }
1765 }
1766 }
1767
satok42c5a162011-05-26 16:46:14 +09001768 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 public void hideMySoftInput(IBinder token, int flags) {
1770 synchronized (mMethodMap) {
1771 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001772 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1773 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 return;
1775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 long ident = Binder.clearCallingIdentity();
1777 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001778 hideCurrentInputLocked(flags, null);
1779 } finally {
1780 Binder.restoreCallingIdentity(ident);
1781 }
1782 }
1783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001784
satok42c5a162011-05-26 16:46:14 +09001785 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001786 public void showMySoftInput(IBinder token, int flags) {
1787 synchronized (mMethodMap) {
1788 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001789 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1790 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001791 return;
1792 }
1793 long ident = Binder.clearCallingIdentity();
1794 try {
1795 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 } finally {
1797 Binder.restoreCallingIdentity(ident);
1798 }
1799 }
1800 }
1801
1802 void setEnabledSessionInMainThread(SessionState session) {
1803 if (mEnabledSession != session) {
1804 if (mEnabledSession != null) {
1805 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001806 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 mEnabledSession.method.setSessionEnabled(
1808 mEnabledSession.session, false);
1809 } catch (RemoteException e) {
1810 }
1811 }
1812 mEnabledSession = session;
1813 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001814 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 session.method.setSessionEnabled(
1816 session.session, true);
1817 } catch (RemoteException e) {
1818 }
1819 }
1820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821
satok42c5a162011-05-26 16:46:14 +09001822 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 public boolean handleMessage(Message msg) {
1824 HandlerCaller.SomeArgs args;
1825 switch (msg.what) {
1826 case MSG_SHOW_IM_PICKER:
1827 showInputMethodMenu();
1828 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829
satokab751aa2010-09-14 19:17:36 +09001830 case MSG_SHOW_IM_SUBTYPE_PICKER:
1831 showInputMethodSubtypeMenu();
1832 return true;
1833
satok47a44912010-10-06 16:03:58 +09001834 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001835 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001836 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001837 return true;
1838
1839 case MSG_SHOW_IM_CONFIG:
1840 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001841 return true;
1842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 case MSG_UNBIND_INPUT:
1846 try {
1847 ((IInputMethod)msg.obj).unbindInput();
1848 } catch (RemoteException e) {
1849 // There is nothing interesting about the method dying.
1850 }
1851 return true;
1852 case MSG_BIND_INPUT:
1853 args = (HandlerCaller.SomeArgs)msg.obj;
1854 try {
1855 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1856 } catch (RemoteException e) {
1857 }
1858 return true;
1859 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001862 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1863 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } catch (RemoteException e) {
1865 }
1866 return true;
1867 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001868 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001870 ((IInputMethod)args.arg1).hideSoftInput(0,
1871 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 } catch (RemoteException e) {
1873 }
1874 return true;
1875 case MSG_ATTACH_TOKEN:
1876 args = (HandlerCaller.SomeArgs)msg.obj;
1877 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001878 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1880 } catch (RemoteException e) {
1881 }
1882 return true;
1883 case MSG_CREATE_SESSION:
1884 args = (HandlerCaller.SomeArgs)msg.obj;
1885 try {
1886 ((IInputMethod)args.arg1).createSession(
1887 (IInputMethodCallback)args.arg2);
1888 } catch (RemoteException e) {
1889 }
1890 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 case MSG_START_INPUT:
1894 args = (HandlerCaller.SomeArgs)msg.obj;
1895 try {
1896 SessionState session = (SessionState)args.arg1;
1897 setEnabledSessionInMainThread(session);
1898 session.method.startInput((IInputContext)args.arg2,
1899 (EditorInfo)args.arg3);
1900 } catch (RemoteException e) {
1901 }
1902 return true;
1903 case MSG_RESTART_INPUT:
1904 args = (HandlerCaller.SomeArgs)msg.obj;
1905 try {
1906 SessionState session = (SessionState)args.arg1;
1907 setEnabledSessionInMainThread(session);
1908 session.method.restartInput((IInputContext)args.arg2,
1909 (EditorInfo)args.arg3);
1910 } catch (RemoteException e) {
1911 }
1912 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 case MSG_UNBIND_METHOD:
1917 try {
1918 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1919 } catch (RemoteException e) {
1920 // There is nothing interesting about the last client dying.
1921 }
1922 return true;
1923 case MSG_BIND_METHOD:
1924 args = (HandlerCaller.SomeArgs)msg.obj;
1925 try {
1926 ((IInputMethodClient)args.arg1).onBindMethod(
1927 (InputBindResult)args.arg2);
1928 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 return true;
1932 }
1933 return false;
1934 }
1935
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001936 private boolean isSystemIme(InputMethodInfo inputMethod) {
1937 return (inputMethod.getServiceInfo().applicationInfo.flags
1938 & ApplicationInfo.FLAG_SYSTEM) != 0;
1939 }
1940
Ken Wakasa586f0512011-01-20 22:31:01 +09001941 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1942 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1943 final int subtypeCount = imi.getSubtypeCount();
1944 for (int i = 0; i < subtypeCount; ++i) {
1945 subtypes.add(imi.getSubtypeAt(i));
1946 }
1947 return subtypes;
1948 }
1949
satoka86f5e42011-09-02 17:12:42 +09001950
1951 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1952 InputMethodInfo imi, String mode) {
1953 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1954 final int subtypeCount = imi.getSubtypeCount();
1955 for (int i = 0; i < subtypeCount; ++i) {
1956 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1957 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1958 subtypes.add(subtype);
1959 }
1960 }
1961 return subtypes;
1962 }
1963
satokdc9ddae2011-10-06 12:22:36 +09001964 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001965 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001966 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001967 // We'd prefer to fall back on a system IME, since that is safer.
1968 int i=enabled.size();
1969 while (i > 0) {
1970 i--;
satokdc9ddae2011-10-06 12:22:36 +09001971 final InputMethodInfo imi = enabled.get(i);
1972 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001973 break;
1974 }
1975 }
satokdc9ddae2011-10-06 12:22:36 +09001976 return enabled.get(i);
1977 }
1978 return null;
1979 }
1980
1981 private boolean chooseNewDefaultIMELocked() {
1982 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1983 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001984 if (DEBUG) {
1985 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1986 }
satok723a27e2010-11-11 14:58:11 +09001987 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001988 return true;
1989 }
1990
1991 return false;
1992 }
1993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1995 HashMap<String, InputMethodInfo> map) {
1996 list.clear();
1997 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002000 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002001 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2002 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2003 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2004 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005
2006 List<ResolveInfo> services = pm.queryIntentServices(
2007 new Intent(InputMethod.SERVICE_INTERFACE),
2008 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
satoke7c6998e2011-06-03 17:57:59 +09002010 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2011 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 for (int i = 0; i < services.size(); ++i) {
2013 ResolveInfo ri = services.get(i);
2014 ServiceInfo si = ri.serviceInfo;
2015 ComponentName compName = new ComponentName(si.packageName, si.name);
2016 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2017 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002018 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 + ": it does not require the permission "
2020 + android.Manifest.permission.BIND_INPUT_METHOD);
2021 continue;
2022 }
2023
Joe Onorato8a9b2202010-02-26 18:56:32 -08002024 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025
2026 try {
satoke7c6998e2011-06-03 17:57:59 +09002027 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002029 final String id = p.getId();
2030 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031
Amith Yamasanie861ec12010-03-24 21:39:27 -07002032 // System IMEs are enabled by default, unless there's a hard keyboard
2033 // and the system IME was explicitly disabled
2034 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2035 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002036 }
2037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002039 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002043 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002045 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
2047 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002048
2049 String defaultIme = Settings.Secure.getString(mContext
2050 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002051 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002052 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002053 updateFromSettingsLocked();
2054 }
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002059
satokab751aa2010-09-14 19:17:36 +09002060 private void showInputMethodMenu() {
2061 showInputMethodMenuInternal(false);
2062 }
2063
2064 private void showInputMethodSubtypeMenu() {
2065 showInputMethodMenuInternal(true);
2066 }
2067
satok217f5482010-12-15 05:19:19 +09002068 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002069 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002070 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002071 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2072 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002073 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002074 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002075 }
satok217f5482010-12-15 05:19:19 +09002076 mContext.startActivity(intent);
2077 }
2078
2079 private void showConfigureInputMethods() {
2080 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2081 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2082 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2083 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002084 mContext.startActivity(intent);
2085 }
2086
satokab751aa2010-09-14 19:17:36 +09002087 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002088 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002092 final boolean isScreenLocked = mKeyguardManager != null
2093 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 String lastInputMethodId = Settings.Secure.getString(context
2096 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002097 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002098 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002099
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002100 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002101 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2102 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002103 if (immis == null || immis.size() == 0) {
2104 return;
2105 }
2106
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002107 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108
Ken Wakasa761eb372011-03-04 19:06:18 +09002109 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2110 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2111 new Comparator<InputMethodInfo>() {
2112 @Override
2113 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2114 if (imi2 == null) return 0;
2115 if (imi1 == null) return 1;
2116 if (pm == null) {
2117 return imi1.getId().compareTo(imi2.getId());
2118 }
2119 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2120 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2121 return imiId1.toString().compareTo(imiId2.toString());
2122 }
2123 });
satok913a8922010-08-26 21:53:41 +09002124
Ken Wakasa761eb372011-03-04 19:06:18 +09002125 sortedImmis.putAll(immis);
2126
Ken Wakasa05dbb652011-08-22 15:22:43 +09002127 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002128
2129 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002130 if (imi == null) continue;
2131 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002132 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002133 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2134 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002135 }
satokbb4aa062011-01-19 21:40:27 +09002136 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002137 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002138 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002139 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002140 if (DEBUG) {
2141 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2142 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002143 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002144 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2145 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2146 // We show all enabled IMEs and subtypes when an IME is shown.
2147 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002148 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002149 final CharSequence subtypeLabel =
2150 subtype.overridesImplicitlyEnabledSubtype() ? null
2151 : subtype.getDisplayName(context, imi.getPackageName(),
2152 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002153 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2154
satok4a28bde2011-06-29 21:03:40 +09002155 // Removing this subtype from enabledSubtypeSet because we no longer
2156 // need to add an entry of this subtype to imList to avoid duplicated
2157 // entries.
2158 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002159 }
satokab751aa2010-09-14 19:17:36 +09002160 }
2161 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002162 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002163 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002164 }
satok913a8922010-08-26 21:53:41 +09002165
Ken Wakasa761eb372011-03-04 19:06:18 +09002166 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002167 mIms = new InputMethodInfo[N];
2168 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002169 int checkedItem = 0;
2170 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002171 final ImeSubtypeListItem item = imList.get(i);
2172 mIms[i] = item.mImi;
2173 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002174 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002175 int subtypeId = mSubtypeIds[i];
2176 if ((subtypeId == NOT_A_SUBTYPE_ID)
2177 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2178 || (subtypeId == lastInputMethodSubtypeId)) {
2179 checkedItem = i;
2180 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
satokab751aa2010-09-14 19:17:36 +09002183
Ken Wakasa05dbb652011-08-22 15:22:43 +09002184 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002185 com.android.internal.R.styleable.DialogPreference,
2186 com.android.internal.R.attr.alertDialogStyle, 0);
2187 mDialogBuilder = new AlertDialog.Builder(context)
2188 .setTitle(com.android.internal.R.string.select_input_method)
2189 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002190 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002191 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002193 }
2194 })
2195 .setIcon(a.getDrawable(
2196 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2197 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002198
Ken Wakasa05dbb652011-08-22 15:22:43 +09002199 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2200 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2201 checkedItem);
2202
2203 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002204 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002205 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002206 public void onClick(DialogInterface dialog, int which) {
2207 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002208 if (mIms == null || mIms.length <= which
2209 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002210 return;
2211 }
2212 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002213 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002214 hideInputMethodMenu();
2215 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002216 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002217 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002218 subtypeId = NOT_A_SUBTYPE_ID;
2219 }
2220 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002221 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002224 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225
satokbc81b692011-08-26 16:22:22 +09002226 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002227 mDialogBuilder.setPositiveButton(
2228 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002229 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002230 @Override
satok7f35c8c2010-10-07 21:13:11 +09002231 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002232 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002233 }
2234 });
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002237 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 mSwitchingDialog.getWindow().setType(
2239 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002240 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 mSwitchingDialog.show();
2242 }
2243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002244
Ken Wakasa05dbb652011-08-22 15:22:43 +09002245 private static class ImeSubtypeListItem {
2246 public final CharSequence mImeName;
2247 public final CharSequence mSubtypeName;
2248 public final InputMethodInfo mImi;
2249 public final int mSubtypeId;
2250 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2251 InputMethodInfo imi, int subtypeId) {
2252 mImeName = imeName;
2253 mSubtypeName = subtypeName;
2254 mImi = imi;
2255 mSubtypeId = subtypeId;
2256 }
2257 }
2258
2259 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2260 private final LayoutInflater mInflater;
2261 private final int mTextViewResourceId;
2262 private final List<ImeSubtypeListItem> mItemsList;
2263 private final int mCheckedItem;
2264 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2265 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2266 super(context, textViewResourceId, itemsList);
2267 mTextViewResourceId = textViewResourceId;
2268 mItemsList = itemsList;
2269 mCheckedItem = checkedItem;
2270 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2271 }
2272
2273 @Override
2274 public View getView(int position, View convertView, ViewGroup parent) {
2275 final View view = convertView != null ? convertView
2276 : mInflater.inflate(mTextViewResourceId, null);
2277 if (position < 0 || position >= mItemsList.size()) return view;
2278 final ImeSubtypeListItem item = mItemsList.get(position);
2279 final CharSequence imeName = item.mImeName;
2280 final CharSequence subtypeName = item.mSubtypeName;
2281 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2282 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2283 if (TextUtils.isEmpty(subtypeName)) {
2284 firstTextView.setText(imeName);
2285 secondTextView.setVisibility(View.GONE);
2286 } else {
2287 firstTextView.setText(subtypeName);
2288 secondTextView.setText(imeName);
2289 secondTextView.setVisibility(View.VISIBLE);
2290 }
2291 final RadioButton radioButton =
2292 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2293 radioButton.setChecked(position == mCheckedItem);
2294 return view;
2295 }
2296 }
2297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002299 synchronized (mMethodMap) {
2300 hideInputMethodMenuLocked();
2301 }
2302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002303
The Android Open Source Project10592532009-03-18 17:39:46 -07002304 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002305 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306
The Android Open Source Project10592532009-03-18 17:39:46 -07002307 if (mSwitchingDialog != null) {
2308 mSwitchingDialog.dismiss();
2309 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002311
The Android Open Source Project10592532009-03-18 17:39:46 -07002312 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002313 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002317
satok42c5a162011-05-26 16:46:14 +09002318 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 public boolean setInputMethodEnabled(String id, boolean enabled) {
2320 synchronized (mMethodMap) {
2321 if (mContext.checkCallingOrSelfPermission(
2322 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2323 != PackageManager.PERMISSION_GRANTED) {
2324 throw new SecurityException(
2325 "Requires permission "
2326 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2327 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 long ident = Binder.clearCallingIdentity();
2330 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002331 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 } finally {
2333 Binder.restoreCallingIdentity(ident);
2334 }
2335 }
2336 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002337
2338 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2339 // Make sure this is a valid input method.
2340 InputMethodInfo imm = mMethodMap.get(id);
2341 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002342 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002343 }
2344
satokd87c2592010-09-29 11:52:06 +09002345 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2346 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002347
satokd87c2592010-09-29 11:52:06 +09002348 if (enabled) {
2349 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2350 if (pair.first.equals(id)) {
2351 // We are enabling this input method, but it is already enabled.
2352 // Nothing to do. The previous state was enabled.
2353 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002354 }
2355 }
satokd87c2592010-09-29 11:52:06 +09002356 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2357 // Previous state was disabled.
2358 return false;
2359 } else {
2360 StringBuilder builder = new StringBuilder();
2361 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2362 builder, enabledInputMethodsList, id)) {
2363 // Disabled input method is currently selected, switch to another one.
2364 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2365 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002366 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2367 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2368 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002369 }
2370 // Previous state was enabled.
2371 return true;
2372 } else {
2373 // We are disabling the input method but it is already disabled.
2374 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002375 return false;
2376 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002377 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002378 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002379
satok57ffc002011-01-25 00:11:47 +09002380 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2381 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002382 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002383 }
2384
satok723a27e2010-11-11 14:58:11 +09002385 private void saveCurrentInputMethodAndSubtypeToHistory() {
2386 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2387 if (mCurrentSubtype != null) {
2388 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2389 }
satok57ffc002011-01-25 00:11:47 +09002390 if (canAddToLastInputMethod(mCurrentSubtype)) {
2391 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2392 }
satokab751aa2010-09-14 19:17:36 +09002393 }
2394
satok723a27e2010-11-11 14:58:11 +09002395 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2396 boolean setSubtypeOnly) {
2397 // Update the history of InputMethod and Subtype
2398 saveCurrentInputMethodAndSubtypeToHistory();
2399
2400 // Set Subtype here
2401 if (imi == null || subtypeId < 0) {
2402 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002403 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002404 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002405 if (subtypeId < imi.getSubtypeCount()) {
2406 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2407 mSettings.putSelectedSubtype(subtype.hashCode());
2408 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002409 } else {
2410 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2411 mCurrentSubtype = null;
2412 }
satokab751aa2010-09-14 19:17:36 +09002413 }
satok723a27e2010-11-11 14:58:11 +09002414
2415 if (!setSubtypeOnly) {
2416 // Set InputMethod here
2417 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2418 }
2419 }
2420
2421 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2422 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2423 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2424 // newDefaultIme is empty when there is no candidate for the selected IME.
2425 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2426 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2427 if (subtypeHashCode != null) {
2428 try {
2429 lastSubtypeId = getSubtypeIdFromHashCode(
2430 imi, Integer.valueOf(subtypeHashCode));
2431 } catch (NumberFormatException e) {
2432 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2433 }
2434 }
2435 }
2436 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002437 }
2438
2439 private int getSelectedInputMethodSubtypeId(String id) {
2440 InputMethodInfo imi = mMethodMap.get(id);
2441 if (imi == null) {
2442 return NOT_A_SUBTYPE_ID;
2443 }
satokab751aa2010-09-14 19:17:36 +09002444 int subtypeId;
2445 try {
2446 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2447 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2448 } catch (SettingNotFoundException e) {
2449 return NOT_A_SUBTYPE_ID;
2450 }
satok723a27e2010-11-11 14:58:11 +09002451 return getSubtypeIdFromHashCode(imi, subtypeId);
2452 }
2453
2454 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002455 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002456 final int subtypeCount = imi.getSubtypeCount();
2457 for (int i = 0; i < subtypeCount; ++i) {
2458 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002459 if (subtypeHashCode == ims.hashCode()) {
2460 return i;
2461 }
satokab751aa2010-09-14 19:17:36 +09002462 }
2463 }
2464 return NOT_A_SUBTYPE_ID;
2465 }
2466
satoka86f5e42011-09-02 17:12:42 +09002467 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2468 Resources res, InputMethodInfo imi) {
2469 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002470 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002471 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002472 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002473 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002474 final int N = subtypes.size();
2475 boolean containsKeyboardSubtype = false;
2476 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002477 // scan overriding implicitly enabled subtypes.
2478 InputMethodSubtype subtype = subtypes.get(i);
2479 if (subtype.overridesImplicitlyEnabledSubtype()) {
2480 final String mode = subtype.getMode();
2481 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2482 applicableModeAndSubtypesMap.put(mode, subtype);
2483 }
2484 }
2485 }
2486 if (applicableModeAndSubtypesMap.size() > 0) {
2487 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2488 }
2489 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002490 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002491 final String locale = subtype.getLocale();
2492 final String mode = subtype.getMode();
2493 // When system locale starts with subtype's locale, that subtype will be applicable
2494 // for system locale
2495 // For instance, it's clearly applicable for cases like system locale = en_US and
2496 // subtype = en, but it is not necessarily considered applicable for cases like system
2497 // locale = en and subtype = en_US.
2498 // We just call systemLocale.startsWith(locale) in this function because there is no
2499 // need to find applicable subtypes aggressively unlike
2500 // findLastResortApplicableSubtypeLocked.
2501 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002502 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002503 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002504 if (applicableSubtype != null) {
2505 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2506 if (!systemLocale.equals(locale)) continue;
2507 }
satok3da92232011-01-11 22:46:30 +09002508 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002509 if (!containsKeyboardSubtype
2510 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2511 containsKeyboardSubtype = true;
2512 }
2513 }
2514 }
satok4a28bde2011-06-29 21:03:40 +09002515 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002516 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002517 if (!containsKeyboardSubtype) {
2518 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002519 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002520 if (lastResortKeyboardSubtype != null) {
2521 applicableSubtypes.add(lastResortKeyboardSubtype);
2522 }
2523 }
2524 return applicableSubtypes;
2525 }
2526
satok4e4569d2010-11-19 18:45:53 +09002527 /**
2528 * If there are no selected subtypes, tries finding the most applicable one according to the
2529 * given locale.
2530 * @param subtypes this function will search the most applicable subtype in subtypes
2531 * @param mode subtypes will be filtered by mode
2532 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002533 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2534 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002535 * @return the most applicable subtypeId
2536 */
satokdf31ae62011-01-15 06:19:44 +09002537 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2538 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002539 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002540 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002541 return null;
satok8fbb1e82010-11-02 23:15:58 +09002542 }
satok4e4569d2010-11-19 18:45:53 +09002543 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002544 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002545 }
satok8fbb1e82010-11-02 23:15:58 +09002546 final String language = locale.substring(0, 2);
2547 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002548 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002549 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002550 final int N = subtypes.size();
2551 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002552 InputMethodSubtype subtype = subtypes.get(i);
2553 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002554 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2555 // and all subtypes with all modes can be candidates.
2556 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002557 if (firstMatchedModeSubtype == null) {
2558 firstMatchedModeSubtype = subtype;
2559 }
satok9ef02832010-11-04 21:17:48 +09002560 if (locale.equals(subtypeLocale)) {
2561 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002562 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002563 break;
2564 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2565 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002566 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002567 partialMatchFound = true;
2568 }
satok8fbb1e82010-11-02 23:15:58 +09002569 }
2570 }
2571
satok7599a7fb2010-12-22 13:45:23 +09002572 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2573 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002574 }
2575
satok8fbb1e82010-11-02 23:15:58 +09002576 // The first subtype applicable to the system locale will be defined as the most applicable
2577 // subtype.
2578 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002579 if (applicableSubtype != null) {
2580 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2581 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2582 }
satok8fbb1e82010-11-02 23:15:58 +09002583 }
satokcd7cd292010-11-20 15:46:23 +09002584 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002585 }
2586
satok4e4569d2010-11-19 18:45:53 +09002587 // If there are no selected shortcuts, tries finding the most applicable ones.
2588 private Pair<InputMethodInfo, InputMethodSubtype>
2589 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2590 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2591 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002592 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002593 boolean foundInSystemIME = false;
2594
2595 // Search applicable subtype for each InputMethodInfo
2596 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002597 final String imiId = imi.getId();
2598 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2599 continue;
2600 }
satokcd7cd292010-11-20 15:46:23 +09002601 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002602 final List<InputMethodSubtype> enabledSubtypes =
2603 getEnabledInputMethodSubtypeList(imi, true);
2604 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002605 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002606 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002607 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002608 }
satokdf31ae62011-01-15 06:19:44 +09002609 // 2. Search by the system locale from enabledSubtypes.
2610 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002611 if (subtype == null) {
2612 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002613 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002614 }
satoka86f5e42011-09-02 17:12:42 +09002615 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2616 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2617 final ArrayList<InputMethodSubtype> subtypesForSearch =
2618 overridingImplicitlyEnabledSubtypes.isEmpty()
2619 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002620 // 4. Search by the current subtype's locale from all subtypes.
2621 if (subtype == null && mCurrentSubtype != null) {
2622 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002623 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002624 }
2625 // 5. Search by the system locale from all subtypes.
2626 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002627 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002628 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002629 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002630 }
satokcd7cd292010-11-20 15:46:23 +09002631 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002632 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002633 // The current input method is the most applicable IME.
2634 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002635 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002636 break;
satok7599a7fb2010-12-22 13:45:23 +09002637 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002638 // The system input method is 2nd applicable IME.
2639 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002640 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002641 if ((imi.getServiceInfo().applicationInfo.flags
2642 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2643 foundInSystemIME = true;
2644 }
satok4e4569d2010-11-19 18:45:53 +09002645 }
2646 }
2647 }
2648 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002649 if (mostApplicableIMI != null) {
2650 Slog.w(TAG, "Most applicable shortcut input method was:"
2651 + mostApplicableIMI.getId());
2652 if (mostApplicableSubtype != null) {
2653 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2654 + "," + mostApplicableSubtype.getMode() + ","
2655 + mostApplicableSubtype.getLocale());
2656 }
2657 }
satok4e4569d2010-11-19 18:45:53 +09002658 }
satokcd7cd292010-11-20 15:46:23 +09002659 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002660 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002661 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002662 } else {
2663 return null;
2664 }
2665 }
2666
satokab751aa2010-09-14 19:17:36 +09002667 /**
2668 * @return Return the current subtype of this input method.
2669 */
satok42c5a162011-05-26 16:46:14 +09002670 @Override
satokab751aa2010-09-14 19:17:36 +09002671 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002672 boolean subtypeIsSelected = false;
2673 try {
2674 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2675 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2676 } catch (SettingNotFoundException e) {
2677 }
satok3ef8b292010-11-23 06:06:29 +09002678 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002679 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002680 String lastInputMethodId = Settings.Secure.getString(
2681 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002682 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2683 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002684 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2685 if (imi != null) {
2686 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002687 // the most applicable subtype from explicitly or implicitly enabled
2688 // subtypes.
2689 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2690 getEnabledInputMethodSubtypeList(imi, true);
2691 // If there is only one explicitly or implicitly enabled subtype,
2692 // just returns it.
2693 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2694 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2695 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2696 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2697 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2698 SUBTYPE_MODE_KEYBOARD, null, true);
2699 if (mCurrentSubtype == null) {
2700 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2701 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2702 true);
2703 }
2704 }
satok4e4569d2010-11-19 18:45:53 +09002705 }
satokcd7cd292010-11-20 15:46:23 +09002706 } else {
satok3ef8b292010-11-23 06:06:29 +09002707 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002708 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002709 }
satok8fbb1e82010-11-02 23:15:58 +09002710 }
satok3ef8b292010-11-23 06:06:29 +09002711 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002712 }
satokab751aa2010-09-14 19:17:36 +09002713 }
2714
satokf3db1af2010-11-23 13:34:33 +09002715 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2716 InputMethodSubtype subtype) {
2717 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2718 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2719 } else {
2720 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2721 subtypes.add(subtype);
2722 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2723 }
2724 }
2725
satok4e4569d2010-11-19 18:45:53 +09002726 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002727 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002728 @Override
satok4e4569d2010-11-19 18:45:53 +09002729 public List getShortcutInputMethodsAndSubtypes() {
2730 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002731 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002732 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002733 // If there are no selected shortcut subtypes, the framework will try to find
2734 // the most applicable subtype from all subtypes whose mode is
2735 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002736 Pair<InputMethodInfo, InputMethodSubtype> info =
2737 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2738 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002739 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002740 ret.add(info.first);
2741 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002742 }
satok3da92232011-01-11 22:46:30 +09002743 return ret;
satokf3db1af2010-11-23 13:34:33 +09002744 }
satokf3db1af2010-11-23 13:34:33 +09002745 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2746 ret.add(imi);
2747 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2748 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002749 }
2750 }
satokf3db1af2010-11-23 13:34:33 +09002751 return ret;
satok4e4569d2010-11-19 18:45:53 +09002752 }
2753 }
2754
satok42c5a162011-05-26 16:46:14 +09002755 @Override
satokb66d2872010-11-10 01:04:04 +09002756 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2757 synchronized (mMethodMap) {
2758 if (subtype != null && mCurMethodId != null) {
2759 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2760 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2761 if (subtypeId != NOT_A_SUBTYPE_ID) {
2762 setInputMethodLocked(mCurMethodId, subtypeId);
2763 return true;
2764 }
2765 }
2766 return false;
2767 }
2768 }
2769
satokd87c2592010-09-29 11:52:06 +09002770 /**
2771 * Utility class for putting and getting settings for InputMethod
2772 * TODO: Move all putters and getters of settings to this class.
2773 */
2774 private static class InputMethodSettings {
2775 // The string for enabled input method is saved as follows:
2776 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2777 private static final char INPUT_METHOD_SEPARATER = ':';
2778 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002779 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002780 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2781
satok723a27e2010-11-11 14:58:11 +09002782 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002783 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2784
satokdf31ae62011-01-15 06:19:44 +09002785 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002786 private final ContentResolver mResolver;
2787 private final HashMap<String, InputMethodInfo> mMethodMap;
2788 private final ArrayList<InputMethodInfo> mMethodList;
2789
2790 private String mEnabledInputMethodsStrCache;
2791
2792 private static void buildEnabledInputMethodsSettingString(
2793 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2794 String id = pair.first;
2795 ArrayList<String> subtypes = pair.second;
2796 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002797 // Inputmethod and subtypes are saved in the settings as follows:
2798 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2799 for (String subtypeId: subtypes) {
2800 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002801 }
2802 }
2803
2804 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002805 Resources res, ContentResolver resolver,
2806 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2807 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002808 mResolver = resolver;
2809 mMethodMap = methodMap;
2810 mMethodList = methodList;
2811 }
2812
2813 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2814 return createEnabledInputMethodListLocked(
2815 getEnabledInputMethodsAndSubtypeListLocked());
2816 }
2817
satok7f35c8c2010-10-07 21:13:11 +09002818 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002819 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2820 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002821 getEnabledInputMethodsAndSubtypeListLocked());
2822 }
2823
satok67ddf9c2010-11-17 09:45:54 +09002824 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2825 InputMethodInfo imi) {
2826 List<Pair<String, ArrayList<String>>> imsList =
2827 getEnabledInputMethodsAndSubtypeListLocked();
2828 ArrayList<InputMethodSubtype> enabledSubtypes =
2829 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002830 if (imi != null) {
2831 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2832 InputMethodInfo info = mMethodMap.get(imsPair.first);
2833 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002834 final int subtypeCount = info.getSubtypeCount();
2835 for (int i = 0; i < subtypeCount; ++i) {
2836 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002837 for (String s: imsPair.second) {
2838 if (String.valueOf(ims.hashCode()).equals(s)) {
2839 enabledSubtypes.add(ims);
2840 }
satok67ddf9c2010-11-17 09:45:54 +09002841 }
2842 }
satok884ef9a2010-11-18 10:39:46 +09002843 break;
satok67ddf9c2010-11-17 09:45:54 +09002844 }
satok67ddf9c2010-11-17 09:45:54 +09002845 }
2846 }
2847 return enabledSubtypes;
2848 }
2849
satokd87c2592010-09-29 11:52:06 +09002850 // At the initial boot, the settings for input methods are not set,
2851 // so we need to enable IME in that case.
2852 public void enableAllIMEsIfThereIsNoEnabledIME() {
2853 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2854 StringBuilder sb = new StringBuilder();
2855 final int N = mMethodList.size();
2856 for (int i = 0; i < N; i++) {
2857 InputMethodInfo imi = mMethodList.get(i);
2858 Slog.i(TAG, "Adding: " + imi.getId());
2859 if (i > 0) sb.append(':');
2860 sb.append(imi.getId());
2861 }
2862 putEnabledInputMethodsStr(sb.toString());
2863 }
2864 }
2865
satokbb4aa062011-01-19 21:40:27 +09002866 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002867 ArrayList<Pair<String, ArrayList<String>>> imsList
2868 = new ArrayList<Pair<String, ArrayList<String>>>();
2869 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2870 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2871 return imsList;
2872 }
satok723a27e2010-11-11 14:58:11 +09002873 mInputMethodSplitter.setString(enabledInputMethodsStr);
2874 while (mInputMethodSplitter.hasNext()) {
2875 String nextImsStr = mInputMethodSplitter.next();
2876 mSubtypeSplitter.setString(nextImsStr);
2877 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002878 ArrayList<String> subtypeHashes = new ArrayList<String>();
2879 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002880 String imeId = mSubtypeSplitter.next();
2881 while (mSubtypeSplitter.hasNext()) {
2882 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002883 }
2884 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2885 }
2886 }
2887 return imsList;
2888 }
2889
2890 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2891 if (reloadInputMethodStr) {
2892 getEnabledInputMethodsStr();
2893 }
2894 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2895 // Add in the newly enabled input method.
2896 putEnabledInputMethodsStr(id);
2897 } else {
2898 putEnabledInputMethodsStr(
2899 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2900 }
2901 }
2902
2903 /**
2904 * Build and put a string of EnabledInputMethods with removing specified Id.
2905 * @return the specified id was removed or not.
2906 */
2907 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2908 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2909 boolean isRemoved = false;
2910 boolean needsAppendSeparator = false;
2911 for (Pair<String, ArrayList<String>> ims: imsList) {
2912 String curId = ims.first;
2913 if (curId.equals(id)) {
2914 // We are disabling this input method, and it is
2915 // currently enabled. Skip it to remove from the
2916 // new list.
2917 isRemoved = true;
2918 } else {
2919 if (needsAppendSeparator) {
2920 builder.append(INPUT_METHOD_SEPARATER);
2921 } else {
2922 needsAppendSeparator = true;
2923 }
2924 buildEnabledInputMethodsSettingString(builder, ims);
2925 }
2926 }
2927 if (isRemoved) {
2928 // Update the setting with the new list of input methods.
2929 putEnabledInputMethodsStr(builder.toString());
2930 }
2931 return isRemoved;
2932 }
2933
2934 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2935 List<Pair<String, ArrayList<String>>> imsList) {
2936 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2937 for (Pair<String, ArrayList<String>> ims: imsList) {
2938 InputMethodInfo info = mMethodMap.get(ims.first);
2939 if (info != null) {
2940 res.add(info);
2941 }
2942 }
2943 return res;
2944 }
2945
satok7f35c8c2010-10-07 21:13:11 +09002946 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002947 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002948 List<Pair<String, ArrayList<String>>> imsList) {
2949 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2950 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2951 for (Pair<String, ArrayList<String>> ims : imsList) {
2952 InputMethodInfo info = mMethodMap.get(ims.first);
2953 if (info != null) {
2954 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2955 }
2956 }
2957 return res;
2958 }
2959
satokd87c2592010-09-29 11:52:06 +09002960 private void putEnabledInputMethodsStr(String str) {
2961 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2962 mEnabledInputMethodsStrCache = str;
2963 }
2964
2965 private String getEnabledInputMethodsStr() {
2966 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2967 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002968 if (DEBUG) {
2969 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2970 }
satokd87c2592010-09-29 11:52:06 +09002971 return mEnabledInputMethodsStrCache;
2972 }
satok723a27e2010-11-11 14:58:11 +09002973
2974 private void saveSubtypeHistory(
2975 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2976 StringBuilder builder = new StringBuilder();
2977 boolean isImeAdded = false;
2978 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2979 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2980 newSubtypeId);
2981 isImeAdded = true;
2982 }
2983 for (Pair<String, String> ime: savedImes) {
2984 String imeId = ime.first;
2985 String subtypeId = ime.second;
2986 if (TextUtils.isEmpty(subtypeId)) {
2987 subtypeId = NOT_A_SUBTYPE_ID_STR;
2988 }
2989 if (isImeAdded) {
2990 builder.append(INPUT_METHOD_SEPARATER);
2991 } else {
2992 isImeAdded = true;
2993 }
2994 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2995 subtypeId);
2996 }
2997 // Remove the last INPUT_METHOD_SEPARATER
2998 putSubtypeHistoryStr(builder.toString());
2999 }
3000
3001 public void addSubtypeToHistory(String imeId, String subtypeId) {
3002 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3003 for (Pair<String, String> ime: subtypeHistory) {
3004 if (ime.first.equals(imeId)) {
3005 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003006 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003007 + ime.second);
3008 }
3009 // We should break here
3010 subtypeHistory.remove(ime);
3011 break;
3012 }
3013 }
satokbb4aa062011-01-19 21:40:27 +09003014 if (DEBUG) {
3015 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3016 }
satok723a27e2010-11-11 14:58:11 +09003017 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3018 }
3019
3020 private void putSubtypeHistoryStr(String str) {
3021 if (DEBUG) {
3022 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3023 }
3024 Settings.Secure.putString(
3025 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3026 }
3027
3028 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3029 // Gets the first one from the history
3030 return getLastSubtypeForInputMethodLockedInternal(null);
3031 }
3032
3033 public String getLastSubtypeForInputMethodLocked(String imeId) {
3034 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3035 if (ime != null) {
3036 return ime.second;
3037 } else {
3038 return null;
3039 }
3040 }
3041
3042 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3043 List<Pair<String, ArrayList<String>>> enabledImes =
3044 getEnabledInputMethodsAndSubtypeListLocked();
3045 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003046 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003047 final String imeInTheHistory = imeAndSubtype.first;
3048 // If imeId is empty, returns the first IME and subtype in the history
3049 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3050 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003051 final String subtypeHashCode =
3052 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3053 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003054 if (!TextUtils.isEmpty(subtypeHashCode)) {
3055 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003056 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003057 }
3058 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3059 }
3060 }
3061 }
3062 if (DEBUG) {
3063 Slog.d(TAG, "No enabled IME found in the history");
3064 }
3065 return null;
3066 }
3067
satokdf31ae62011-01-15 06:19:44 +09003068 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003069 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3070 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3071 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003072 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3073 if (explicitlyEnabledSubtypes.size() == 0) {
3074 // If there are no explicitly enabled subtypes, applicable subtypes are
3075 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003076 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003077 // If IME is enabled and no subtypes are enabled, applicable subtypes
3078 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003079 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003080 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003081 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003082 if (implicitlySelectedSubtypes != null) {
3083 final int N = implicitlySelectedSubtypes.size();
3084 for (int i = 0; i < N; ++i) {
3085 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3086 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3087 return subtypeHashCode;
3088 }
3089 }
3090 }
3091 }
3092 } else {
satokf6cafb62011-01-17 16:29:02 +09003093 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003094 if (s.equals(subtypeHashCode)) {
3095 // If both imeId and subtypeId are enabled, return subtypeId.
3096 return s;
3097 }
satok723a27e2010-11-11 14:58:11 +09003098 }
3099 }
3100 // If imeId was enabled but subtypeId was disabled.
3101 return NOT_A_SUBTYPE_ID_STR;
3102 }
3103 }
3104 // If both imeId and subtypeId are disabled, return null
3105 return null;
3106 }
3107
3108 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3109 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3110 final String subtypeHistoryStr = getSubtypeHistoryStr();
3111 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3112 return imsList;
3113 }
3114 mInputMethodSplitter.setString(subtypeHistoryStr);
3115 while (mInputMethodSplitter.hasNext()) {
3116 String nextImsStr = mInputMethodSplitter.next();
3117 mSubtypeSplitter.setString(nextImsStr);
3118 if (mSubtypeSplitter.hasNext()) {
3119 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3120 // The first element is ime id.
3121 String imeId = mSubtypeSplitter.next();
3122 while (mSubtypeSplitter.hasNext()) {
3123 subtypeId = mSubtypeSplitter.next();
3124 break;
3125 }
3126 imsList.add(new Pair<String, String>(imeId, subtypeId));
3127 }
3128 }
3129 return imsList;
3130 }
3131
3132 private String getSubtypeHistoryStr() {
3133 if (DEBUG) {
3134 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3135 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3136 }
3137 return Settings.Secure.getString(
3138 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3139 }
3140
3141 public void putSelectedInputMethod(String imeId) {
3142 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3143 }
3144
3145 public void putSelectedSubtype(int subtypeId) {
3146 Settings.Secure.putInt(
3147 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3148 }
satokd87c2592010-09-29 11:52:06 +09003149 }
3150
satoke7c6998e2011-06-03 17:57:59 +09003151 private static class InputMethodFileManager {
3152 private static final String SYSTEM_PATH = "system";
3153 private static final String INPUT_METHOD_PATH = "inputmethod";
3154 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3155 private static final String NODE_SUBTYPES = "subtypes";
3156 private static final String NODE_SUBTYPE = "subtype";
3157 private static final String NODE_IMI = "imi";
3158 private static final String ATTR_ID = "id";
3159 private static final String ATTR_LABEL = "label";
3160 private static final String ATTR_ICON = "icon";
3161 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3162 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3163 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3164 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3165 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3166 private final HashMap<String, InputMethodInfo> mMethodMap;
3167 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3168 new HashMap<String, List<InputMethodSubtype>>();
3169 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3170 if (methodMap == null) {
3171 throw new NullPointerException("methodMap is null");
3172 }
3173 mMethodMap = methodMap;
3174 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3175 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3176 if (!inputMethodDir.mkdirs()) {
3177 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3178 }
3179 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3180 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3181 if (!subtypeFile.exists()) {
3182 // If "subtypes.xml" doesn't exist, create a blank file.
3183 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3184 methodMap);
3185 } else {
3186 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3187 }
3188 }
3189
3190 private void deleteAllInputMethodSubtypes(String imiId) {
3191 synchronized (mMethodMap) {
3192 mSubtypesMap.remove(imiId);
3193 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3194 mMethodMap);
3195 }
3196 }
3197
3198 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003199 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003200 synchronized (mMethodMap) {
3201 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3202 final int N = additionalSubtypes.length;
3203 for (int i = 0; i < N; ++i) {
3204 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003205 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003206 subtypes.add(subtype);
3207 }
3208 }
satok4a28bde2011-06-29 21:03:40 +09003209 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003210 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3211 mMethodMap);
3212 }
3213 }
3214
3215 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3216 synchronized (mMethodMap) {
3217 return mSubtypesMap;
3218 }
3219 }
3220
3221 private static void writeAdditionalInputMethodSubtypes(
3222 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3223 HashMap<String, InputMethodInfo> methodMap) {
3224 // Safety net for the case that this function is called before methodMap is set.
3225 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3226 FileOutputStream fos = null;
3227 try {
3228 fos = subtypesFile.startWrite();
3229 final XmlSerializer out = new FastXmlSerializer();
3230 out.setOutput(fos, "utf-8");
3231 out.startDocument(null, true);
3232 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3233 out.startTag(null, NODE_SUBTYPES);
3234 for (String imiId : allSubtypes.keySet()) {
3235 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3236 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3237 continue;
3238 }
3239 out.startTag(null, NODE_IMI);
3240 out.attribute(null, ATTR_ID, imiId);
3241 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3242 final int N = subtypesList.size();
3243 for (int i = 0; i < N; ++i) {
3244 final InputMethodSubtype subtype = subtypesList.get(i);
3245 out.startTag(null, NODE_SUBTYPE);
3246 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3247 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3248 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3249 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3250 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3251 out.attribute(null, ATTR_IS_AUXILIARY,
3252 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3253 out.endTag(null, NODE_SUBTYPE);
3254 }
3255 out.endTag(null, NODE_IMI);
3256 }
3257 out.endTag(null, NODE_SUBTYPES);
3258 out.endDocument();
3259 subtypesFile.finishWrite(fos);
3260 } catch (java.io.IOException e) {
3261 Slog.w(TAG, "Error writing subtypes", e);
3262 if (fos != null) {
3263 subtypesFile.failWrite(fos);
3264 }
3265 }
3266 }
3267
3268 private static void readAdditionalInputMethodSubtypes(
3269 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3270 if (allSubtypes == null || subtypesFile == null) return;
3271 allSubtypes.clear();
3272 FileInputStream fis = null;
3273 try {
3274 fis = subtypesFile.openRead();
3275 final XmlPullParser parser = Xml.newPullParser();
3276 parser.setInput(fis, null);
3277 int type = parser.getEventType();
3278 // Skip parsing until START_TAG
3279 while ((type = parser.next()) != XmlPullParser.START_TAG
3280 && type != XmlPullParser.END_DOCUMENT) {}
3281 String firstNodeName = parser.getName();
3282 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3283 throw new XmlPullParserException("Xml doesn't start with subtypes");
3284 }
3285 final int depth =parser.getDepth();
3286 String currentImiId = null;
3287 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3288 while (((type = parser.next()) != XmlPullParser.END_TAG
3289 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3290 if (type != XmlPullParser.START_TAG)
3291 continue;
3292 final String nodeName = parser.getName();
3293 if (NODE_IMI.equals(nodeName)) {
3294 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3295 if (TextUtils.isEmpty(currentImiId)) {
3296 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3297 continue;
3298 }
3299 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3300 allSubtypes.put(currentImiId, tempSubtypesArray);
3301 } else if (NODE_SUBTYPE.equals(nodeName)) {
3302 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3303 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3304 continue;
3305 }
3306 final int icon = Integer.valueOf(
3307 parser.getAttributeValue(null, ATTR_ICON));
3308 final int label = Integer.valueOf(
3309 parser.getAttributeValue(null, ATTR_LABEL));
3310 final String imeSubtypeLocale =
3311 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3312 final String imeSubtypeMode =
3313 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3314 final String imeSubtypeExtraValue =
3315 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003316 final boolean isAuxiliary = "1".equals(String.valueOf(
3317 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003318 final InputMethodSubtype subtype =
3319 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3320 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3321 tempSubtypesArray.add(subtype);
3322 }
3323 }
3324 } catch (XmlPullParserException e) {
3325 Slog.w(TAG, "Error reading subtypes: " + e);
3326 return;
3327 } catch (java.io.IOException e) {
3328 Slog.w(TAG, "Error reading subtypes: " + e);
3329 return;
3330 } catch (NumberFormatException e) {
3331 Slog.w(TAG, "Error reading subtypes: " + e);
3332 return;
3333 } finally {
3334 if (fis != null) {
3335 try {
3336 fis.close();
3337 } catch (java.io.IOException e1) {
3338 Slog.w(TAG, "Failed to close.");
3339 }
3340 }
3341 }
3342 }
3343 }
3344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 @Override
3348 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3349 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3350 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3353 + Binder.getCallingPid()
3354 + ", uid=" + Binder.getCallingUid());
3355 return;
3356 }
3357
3358 IInputMethod method;
3359 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 synchronized (mMethodMap) {
3364 p.println("Current Input Method Manager state:");
3365 int N = mMethodList.size();
3366 p.println(" Input Methods:");
3367 for (int i=0; i<N; i++) {
3368 InputMethodInfo info = mMethodList.get(i);
3369 p.println(" InputMethod #" + i + ":");
3370 info.dump(p, " ");
3371 }
3372 p.println(" Clients:");
3373 for (ClientState ci : mClients.values()) {
3374 p.println(" Client " + ci + ":");
3375 p.println(" client=" + ci.client);
3376 p.println(" inputContext=" + ci.inputContext);
3377 p.println(" sessionRequested=" + ci.sessionRequested);
3378 p.println(" curSession=" + ci.curSession);
3379 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003380 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003382 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3383 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3385 + " mBoundToMethod=" + mBoundToMethod);
3386 p.println(" mCurToken=" + mCurToken);
3387 p.println(" mCurIntent=" + mCurIntent);
3388 method = mCurMethod;
3389 p.println(" mCurMethod=" + mCurMethod);
3390 p.println(" mEnabledSession=" + mEnabledSession);
3391 p.println(" mShowRequested=" + mShowRequested
3392 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3393 + " mShowForced=" + mShowForced
3394 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003395 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003397
Jeff Brownb88102f2010-09-08 11:49:43 -07003398 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 pw.flush();
3401 try {
3402 client.client.asBinder().dump(fd, args);
3403 } catch (RemoteException e) {
3404 p.println("Input method client dead: " + e);
3405 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003406 } else {
3407 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003409
Jeff Brownb88102f2010-09-08 11:49:43 -07003410 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 pw.flush();
3413 try {
3414 method.asBinder().dump(fd, args);
3415 } catch (RemoteException e) {
3416 p.println("Input method service dead: " + e);
3417 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003418 } else {
3419 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 }
3421 }
3422}