blob: 7d4faea20388d30af6a1beb2d9d8ac4554eaac57 [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
Dianne Hackborn7663d802012-02-24 13:08:49 -0800783 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 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 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800801 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800805 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 // If no method is currently selected, do nothing.
807 if (mCurMethodId == null) {
808 return mNoBinding;
809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 ClientState cs = mClients.get(client.asBinder());
812 if (cs == null) {
813 throw new IllegalArgumentException("unknown client "
814 + client.asBinder());
815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 try {
818 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
819 // Check with the window manager to make sure this client actually
820 // has a window with focus. If not, reject. This is thread safe
821 // because if the focus changes some time before or after, the
822 // next client receiving focus that has any interest in input will
823 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800824 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
826 return null;
827 }
828 } catch (RemoteException e) {
829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800830
Dianne Hackborn7663d802012-02-24 13:08:49 -0800831 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
832 }
833
834 InputBindResult startInputUncheckedLocked(ClientState cs,
835 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
836 // If no method is currently selected, do nothing.
837 if (mCurMethodId == null) {
838 return mNoBinding;
839 }
840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 if (mCurClient != cs) {
842 // If the client is changing, we need to switch over to the new
843 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700844 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 + cs.client.asBinder());
847
848 // If the screen is on, inform the new client it is active
849 if (mScreenOn) {
850 try {
851 cs.client.setActive(mScreenOn);
852 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800853 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 + cs.pid + " uid " + cs.uid);
855 }
856 }
857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 // Bump up the sequence for this client and attach it.
860 mCurSeq++;
861 if (mCurSeq <= 0) mCurSeq = 1;
862 mCurClient = cs;
863 mCurInputContext = inputContext;
864 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 // Check if the input method is changing.
867 if (mCurId != null && mCurId.equals(mCurMethodId)) {
868 if (cs.curSession != null) {
869 // Fast case: if we are already connected to the input method,
870 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -0800871 return attachNewInputLocked(
872 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874 if (mHaveConnection) {
875 if (mCurMethod != null) {
876 if (!cs.sessionRequested) {
877 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800878 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
880 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900881 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883 // Return to client, and we will get back with it when
884 // we have had a session made for it.
885 return new InputBindResult(null, mCurId, mCurSeq);
886 } else if (SystemClock.uptimeMillis()
887 < (mLastBindTime+TIME_TO_RECONNECT)) {
888 // In this case we have connected to the service, but
889 // don't yet have its interface. If it hasn't been too
890 // long since we did the connection, we'll return to
891 // the client and wait to get the service interface so
892 // we can report back. If it has been too long, we want
893 // to fall through so we can try a disconnect/reconnect
894 // to see if we can get back in touch with the service.
895 return new InputBindResult(null, mCurId, mCurSeq);
896 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
898 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900 }
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700903 return startInputInnerLocked();
904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700906 InputBindResult startInputInnerLocked() {
907 if (mCurMethodId == null) {
908 return mNoBinding;
909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700911 if (!mSystemReady) {
912 // If the system is not yet ready, we shouldn't be running third
913 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700914 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 InputMethodInfo info = mMethodMap.get(mCurMethodId);
918 if (info == null) {
919 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700922 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
925 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700926 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
927 com.android.internal.R.string.input_method_binding_label);
928 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
929 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700930 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
931 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 mLastBindTime = SystemClock.uptimeMillis();
933 mHaveConnection = true;
934 mCurId = info.getId();
935 mCurToken = new Binder();
936 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800937 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 mIWindowManager.addWindowToken(mCurToken,
939 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
940 } catch (RemoteException e) {
941 }
942 return new InputBindResult(null, mCurId, mCurSeq);
943 } else {
944 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 + mCurIntent);
947 }
948 return null;
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
satoke7c6998e2011-06-03 17:57:59 +0900951 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800953 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 synchronized (mMethodMap) {
955 final long ident = Binder.clearCallingIdentity();
956 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -0800957 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 } finally {
959 Binder.restoreCallingIdentity(ident);
960 }
961 }
962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963
satoke7c6998e2011-06-03 17:57:59 +0900964 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 public void finishInput(IInputMethodClient client) {
966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800967
satoke7c6998e2011-06-03 17:57:59 +0900968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 public void onServiceConnected(ComponentName name, IBinder service) {
970 synchronized (mMethodMap) {
971 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
972 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700973 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800974 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700975 unbindCurrentMethodLocked(false);
976 return;
977 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800978 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700979 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
980 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700983 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700985 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900986 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 }
989 }
990 }
991
992 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
993 synchronized (mMethodMap) {
994 if (mCurMethod != null && method != null
995 && mCurMethod.asBinder() == method.asBinder()) {
996 if (mCurClient != null) {
997 mCurClient.curSession = new SessionState(mCurClient,
998 method, session);
999 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001000 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (res.method != null) {
1002 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1003 MSG_BIND_METHOD, mCurClient.client, res));
1004 }
1005 }
1006 }
1007 }
1008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001010 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001011 if (mVisibleBound) {
1012 mContext.unbindService(mVisibleConnection);
1013 mVisibleBound = false;
1014 }
1015
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001016 if (mHaveConnection) {
1017 mContext.unbindService(this);
1018 mHaveConnection = false;
1019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001021 if (mCurToken != null) {
1022 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001023 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001024 mIWindowManager.removeWindowToken(mCurToken);
1025 } catch (RemoteException e) {
1026 }
1027 mCurToken = null;
1028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001029
The Android Open Source Project10592532009-03-18 17:39:46 -07001030 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001031 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001032
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001033 if (reportToClient && mCurClient != null) {
1034 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1035 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1036 }
1037 }
1038
Devin Taylor0c33ed22010-02-23 13:26:46 -06001039 private void finishSession(SessionState sessionState) {
1040 if (sessionState != null && sessionState.session != null) {
1041 try {
1042 sessionState.session.finishSession();
1043 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001044 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001045 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001046 }
1047 }
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001050 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 if (mCurMethod != null) {
1052 for (ClientState cs : mClients.values()) {
1053 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001054 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 cs.curSession = null;
1056 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001057
1058 finishSession(mEnabledSession);
1059 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mCurMethod = null;
1061 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001062 if (mStatusBar != null) {
1063 mStatusBar.setIconVisibility("ime", false);
1064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001066
satoke7c6998e2011-06-03 17:57:59 +09001067 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 public void onServiceDisconnected(ComponentName name) {
1069 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001070 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 + " mCurIntent=" + mCurIntent);
1072 if (mCurMethod != null && mCurIntent != null
1073 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001074 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 // We consider this to be a new bind attempt, since the system
1076 // should now try to restart the service for us.
1077 mLastBindTime = SystemClock.uptimeMillis();
1078 mShowRequested = mInputShown;
1079 mInputShown = false;
1080 if (mCurClient != null) {
1081 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1082 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1083 }
1084 }
1085 }
1086 }
1087
satokf9f01002011-05-19 21:31:50 +09001088 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001090 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 long ident = Binder.clearCallingIdentity();
1092 try {
1093 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001094 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 return;
1096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 synchronized (mMethodMap) {
1099 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001100 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001101 if (mStatusBar != null) {
1102 mStatusBar.setIconVisibility("ime", false);
1103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001105 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001106 CharSequence contentDescription = null;
1107 try {
1108 PackageManager packageManager = mContext.getPackageManager();
1109 contentDescription = packageManager.getApplicationLabel(
1110 packageManager.getApplicationInfo(packageName, 0));
1111 } catch (NameNotFoundException nnfe) {
1112 /* ignore */
1113 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001114 if (mStatusBar != null) {
1115 mStatusBar.setIcon("ime", packageName, iconId, 0,
1116 contentDescription != null
1117 ? contentDescription.toString() : null);
1118 mStatusBar.setIconVisibility("ime", true);
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
1122 } finally {
1123 Binder.restoreCallingIdentity(ident);
1124 }
1125 }
1126
satok7cfc0ed2011-06-20 21:29:36 +09001127 private boolean needsToShowImeSwitchOngoingNotification() {
1128 if (!mShowOngoingImeSwitcherForPhones) return false;
1129 synchronized (mMethodMap) {
1130 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1131 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001132 if (N > 2) return true;
1133 if (N < 1) return false;
1134 int nonAuxCount = 0;
1135 int auxCount = 0;
1136 InputMethodSubtype nonAuxSubtype = null;
1137 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001138 for(int i = 0; i < N; ++i) {
1139 final InputMethodInfo imi = imis.get(i);
1140 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1141 imi, true);
1142 final int subtypeCount = subtypes.size();
1143 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001144 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001145 } else {
1146 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001147 final InputMethodSubtype subtype = subtypes.get(j);
1148 if (!subtype.isAuxiliary()) {
1149 ++nonAuxCount;
1150 nonAuxSubtype = subtype;
1151 } else {
1152 ++auxCount;
1153 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001154 }
1155 }
1156 }
satok7cfc0ed2011-06-20 21:29:36 +09001157 }
satokb6359412011-06-28 17:47:41 +09001158 if (nonAuxCount > 1 || auxCount > 1) {
1159 return true;
1160 } else if (nonAuxCount == 1 && auxCount == 1) {
1161 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001162 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1163 || auxSubtype.overridesImplicitlyEnabledSubtype()
1164 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001165 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1166 return false;
1167 }
1168 return true;
1169 }
1170 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001171 }
satok7cfc0ed2011-06-20 21:29:36 +09001172 }
1173
satokdbf29502011-08-25 15:28:23 +09001174 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001175 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001176 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001177 int uid = Binder.getCallingUid();
1178 long ident = Binder.clearCallingIdentity();
1179 try {
1180 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001181 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001182 return;
1183 }
1184
1185 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001186 mImeWindowVis = vis;
1187 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001188 if (mStatusBar != null) {
1189 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1190 }
satok7cfc0ed2011-06-20 21:29:36 +09001191 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001192 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1193 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001194 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001195 final CharSequence title = mRes.getText(
1196 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001197 final CharSequence imiLabel = imi.loadLabel(pm);
1198 final CharSequence summary = mCurrentSubtype != null
1199 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1200 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1201 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001202 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001203 : imiLabel;
1204
satok7cfc0ed2011-06-20 21:29:36 +09001205 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001206 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001207 if (mNotificationManager != null) {
1208 mNotificationManager.notify(
1209 com.android.internal.R.string.select_input_method,
1210 mImeSwitcherNotification);
1211 mNotificationShown = true;
1212 }
satok7cfc0ed2011-06-20 21:29:36 +09001213 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001214 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001215 mNotificationManager.cancel(
1216 com.android.internal.R.string.select_input_method);
1217 mNotificationShown = false;
1218 }
1219 }
satok06487a52010-10-29 11:37:18 +09001220 }
1221 } finally {
1222 Binder.restoreCallingIdentity(ident);
1223 }
1224 }
1225
satoke7c6998e2011-06-03 17:57:59 +09001226 @Override
satokf9f01002011-05-19 21:31:50 +09001227 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1228 synchronized (mMethodMap) {
1229 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1230 for (int i = 0; i < spans.length; ++i) {
1231 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001232 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001233 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001234 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001235 }
1236 }
1237 }
1238 }
1239
satoke7c6998e2011-06-03 17:57:59 +09001240 @Override
satokf9f01002011-05-19 21:31:50 +09001241 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1242 synchronized (mMethodMap) {
1243 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1244 // TODO: Do not send the intent if the process of the targetImi is already dead.
1245 if (targetImi != null) {
1246 final String[] suggestions = span.getSuggestions();
1247 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001248 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001249 final Intent intent = new Intent();
1250 // Ensures that only a class in the original IME package will receive the
1251 // notification.
satok42c5a162011-05-26 16:46:14 +09001252 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001253 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1254 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1255 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1256 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1257 mContext.sendBroadcast(intent);
1258 return true;
1259 }
1260 }
1261 return false;
1262 }
1263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001265 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1266 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1267 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1268 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001270 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001271 // There is no input method selected, try to choose new applicable input method.
1272 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1273 id = Settings.Secure.getString(mContext.getContentResolver(),
1274 Settings.Secure.DEFAULT_INPUT_METHOD);
1275 }
1276 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 try {
satokab751aa2010-09-14 19:17:36 +09001278 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001280 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
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 }
satokf3db1af2010-11-23 13:34:33 +09001284 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001285 } else {
1286 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001287 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001288 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
satokab751aa2010-09-14 19:17:36 +09001292 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 InputMethodInfo info = mMethodMap.get(id);
1294 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001295 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001299 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001300 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1301 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001302 }
1303 if (subtype != mCurrentSubtype) {
1304 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001305 if (subtype != null) {
1306 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1307 }
satokcd7cd292010-11-20 15:46:23 +09001308 if (mCurMethod != null) {
1309 try {
satok3afd6c02011-11-18 08:38:19 +09001310 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001311 // If subtype is null, try to find the most applicable one from
1312 // getCurrentInputMethodSubtype.
1313 if (subtype == null) {
1314 subtype = getCurrentInputMethodSubtype();
1315 }
1316 mCurMethod.changeInputMethodSubtype(subtype);
1317 } catch (RemoteException e) {
1318 return;
satokab751aa2010-09-14 19:17:36 +09001319 }
1320 }
1321 }
1322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 return;
1324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 final long ident = Binder.clearCallingIdentity();
1327 try {
satokab751aa2010-09-14 19:17:36 +09001328 // Set a subtype to this input method.
1329 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001330 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1331 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1332 // because mCurMethodId is stored as a history in
1333 // setSelectedInputMethodAndSubtypeLocked().
1334 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335
1336 if (ActivityManagerNative.isSystemReady()) {
1337 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001338 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 intent.putExtra("input_method_id", id);
1340 mContext.sendBroadcast(intent);
1341 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001342 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 } finally {
1344 Binder.restoreCallingIdentity(ident);
1345 }
1346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001347
satok42c5a162011-05-26 16:46:14 +09001348 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001349 public boolean showSoftInput(IInputMethodClient client, int flags,
1350 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001351 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 long ident = Binder.clearCallingIdentity();
1353 try {
1354 synchronized (mMethodMap) {
1355 if (mCurClient == null || client == null
1356 || mCurClient.client.asBinder() != client.asBinder()) {
1357 try {
1358 // We need to check if this is the current client with
1359 // focus in the window manager, to allow this call to
1360 // be made before input is started in it.
1361 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001362 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001366 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001371 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373 } finally {
1374 Binder.restoreCallingIdentity(ident);
1375 }
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project4df24232009-03-05 14:34:35 -08001378 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 mShowRequested = true;
1380 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1381 mShowExplicitlyRequested = true;
1382 }
1383 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1384 mShowExplicitlyRequested = true;
1385 mShowForced = true;
1386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
Dianne Hackborncc278702009-09-02 23:07:23 -07001388 if (!mSystemReady) {
1389 return false;
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
The Android Open Source Project4df24232009-03-05 14:34:35 -08001392 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001394 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1395 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1396 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001398 if (mHaveConnection && !mVisibleBound) {
1399 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1400 mVisibleBound = true;
1401 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001402 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001404 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 // The client has asked to have the input method shown, but
1406 // we have been sitting here too long with a connection to the
1407 // service and no interface received, so let's disconnect/connect
1408 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001411 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001413 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1414 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
The Android Open Source Project4df24232009-03-05 14:34:35 -08001417 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
satok42c5a162011-05-26 16:46:14 +09001420 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001421 public boolean hideSoftInput(IInputMethodClient client, int flags,
1422 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001423 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 long ident = Binder.clearCallingIdentity();
1425 try {
1426 synchronized (mMethodMap) {
1427 if (mCurClient == null || client == null
1428 || mCurClient.client.asBinder() != client.asBinder()) {
1429 try {
1430 // We need to check if this is the current client with
1431 // focus in the window manager, to allow this call to
1432 // be made before input is started in it.
1433 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001434 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1435 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001436 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001437 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
1439 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001440 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001441 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
Joe Onorato8a9b2202010-02-26 18:56:32 -08001445 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001446 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 } finally {
1449 Binder.restoreCallingIdentity(ident);
1450 }
1451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
The Android Open Source Project4df24232009-03-05 14:34:35 -08001453 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1455 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001458 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
1460 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001461 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001463 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001465 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001467 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1468 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1469 res = true;
1470 } else {
1471 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001473 if (mHaveConnection && mVisibleBound) {
1474 mContext.unbindService(mVisibleConnection);
1475 mVisibleBound = false;
1476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mInputShown = false;
1478 mShowRequested = false;
1479 mShowExplicitlyRequested = false;
1480 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001481 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
satok42c5a162011-05-26 16:46:14 +09001484 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001485 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1486 int controlFlags, int softInputMode, int windowFlags,
1487 EditorInfo attribute, IInputContext inputContext) {
1488 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 long ident = Binder.clearCallingIdentity();
1490 try {
1491 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001492 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001493 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001495 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
Dianne Hackborn7663d802012-02-24 13:08:49 -08001497 ClientState cs = mClients.get(client.asBinder());
1498 if (cs == null) {
1499 throw new IllegalArgumentException("unknown client "
1500 + client.asBinder());
1501 }
1502
1503 try {
1504 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1505 // Check with the window manager to make sure this client actually
1506 // has a window with focus. If not, reject. This is thread safe
1507 // because if the focus changes some time before or after, the
1508 // next client receiving focus that has any interest in input will
1509 // be calling through here after that change happens.
1510 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1511 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1512 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001514 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001517 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001518 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001519 if (attribute != null) {
1520 return startInputUncheckedLocked(cs, inputContext, attribute,
1521 controlFlags);
1522 }
1523 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001524 }
1525 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001527 // Should we auto-show the IME even if the caller has not
1528 // specified what should be done with it?
1529 // We only do this automatically if the window can resize
1530 // to accommodate the IME (so what the user sees will give
1531 // them good context without input information being obscured
1532 // by the IME) or if running on a large screen where there
1533 // is more room for the target window + IME.
1534 final boolean doAutoShow =
1535 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1536 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1537 || mRes.getConfiguration().isLayoutSizeAtLeast(
1538 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001539 final boolean isTextEditor =
1540 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1541
1542 // We want to start input before showing the IME, but after closing
1543 // it. We want to do this after closing it to help the IME disappear
1544 // more quickly (not get stuck behind it initializing itself for the
1545 // new focused input, even if its window wants to hide the IME).
1546 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1549 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001550 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1552 // There is no focus view, and this window will
1553 // be behind any soft input window, so hide the
1554 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001555 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001556 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001558 } else if (isTextEditor && doAutoShow && (softInputMode &
1559 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 // There is a focus view, and we are navigating forward
1561 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001562 // We only do this automatically if the window can resize
1563 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001564 // them good context without input information being obscured
1565 // by the IME) or if running on a large screen where there
1566 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001568 if (attribute != null) {
1569 res = startInputUncheckedLocked(cs, inputContext, attribute,
1570 controlFlags);
1571 didStart = true;
1572 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001573 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 break;
1576 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1577 // Do nothing.
1578 break;
1579 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1580 if ((softInputMode &
1581 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001583 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 }
1585 break;
1586 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001587 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001588 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 break;
1590 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1591 if ((softInputMode &
1592 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001593 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001594 if (attribute != null) {
1595 res = startInputUncheckedLocked(cs, inputContext, attribute,
1596 controlFlags);
1597 didStart = true;
1598 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001599 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601 break;
1602 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001604 if (attribute != null) {
1605 res = startInputUncheckedLocked(cs, inputContext, attribute,
1606 controlFlags);
1607 didStart = true;
1608 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001609 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 break;
1611 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001612
1613 if (!didStart && attribute != null) {
1614 res = startInputUncheckedLocked(cs, inputContext, attribute,
1615 controlFlags);
1616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618 } finally {
1619 Binder.restoreCallingIdentity(ident);
1620 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001621
1622 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
satok42c5a162011-05-26 16:46:14 +09001625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1627 synchronized (mMethodMap) {
1628 if (mCurClient == null || client == null
1629 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001630 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001631 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633
satok440aab52010-11-25 09:43:11 +09001634 // Always call subtype picker, because subtype picker is a superset of input method
1635 // picker.
satokab751aa2010-09-14 19:17:36 +09001636 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1637 }
1638 }
1639
satok42c5a162011-05-26 16:46:14 +09001640 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001642 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1643 }
1644
satok42c5a162011-05-26 16:46:14 +09001645 @Override
satok28203512010-11-24 11:06:49 +09001646 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1647 synchronized (mMethodMap) {
1648 if (subtype != null) {
1649 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1650 mMethodMap.get(id), subtype.hashCode()));
1651 } else {
1652 setInputMethod(token, id);
1653 }
1654 }
satokab751aa2010-09-14 19:17:36 +09001655 }
1656
satok42c5a162011-05-26 16:46:14 +09001657 @Override
satokb416a712010-11-25 20:42:14 +09001658 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001659 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001660 synchronized (mMethodMap) {
1661 if (mCurClient == null || client == null
1662 || mCurClient.client.asBinder() != client.asBinder()) {
1663 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1664 }
satok7fee71f2010-12-17 18:54:26 +09001665 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1666 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001667 }
1668 }
1669
satok4fc87d62011-05-20 16:13:43 +09001670 @Override
satok735cf382010-11-11 20:40:09 +09001671 public boolean switchToLastInputMethod(IBinder token) {
1672 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001673 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001674 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001675 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001676 lastImi = mMethodMap.get(lastIme.first);
1677 } else {
1678 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001679 }
satok4fc87d62011-05-20 16:13:43 +09001680 String targetLastImiId = null;
1681 int subtypeId = NOT_A_SUBTYPE_ID;
1682 if (lastIme != null && lastImi != null) {
1683 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1684 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1685 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1686 : mCurrentSubtype.hashCode();
1687 // If the last IME is the same as the current IME and the last subtype is not
1688 // defined, there is no need to switch to the last IME.
1689 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1690 targetLastImiId = lastIme.first;
1691 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1692 }
1693 }
1694
1695 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1696 // This is a safety net. If the currentSubtype can't be added to the history
1697 // and the framework couldn't find the last ime, we will make the last ime be
1698 // the most applicable enabled keyboard subtype of the system imes.
1699 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1700 if (enabled != null) {
1701 final int N = enabled.size();
1702 final String locale = mCurrentSubtype == null
1703 ? mRes.getConfiguration().locale.toString()
1704 : mCurrentSubtype.getLocale();
1705 for (int i = 0; i < N; ++i) {
1706 final InputMethodInfo imi = enabled.get(i);
1707 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1708 InputMethodSubtype keyboardSubtype =
1709 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1710 SUBTYPE_MODE_KEYBOARD, locale, true);
1711 if (keyboardSubtype != null) {
1712 targetLastImiId = imi.getId();
1713 subtypeId = getSubtypeIdFromHashCode(
1714 imi, keyboardSubtype.hashCode());
1715 if(keyboardSubtype.getLocale().equals(locale)) {
1716 break;
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723
1724 if (!TextUtils.isEmpty(targetLastImiId)) {
1725 if (DEBUG) {
1726 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1727 + ", from: " + mCurMethodId + ", " + subtypeId);
1728 }
1729 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1730 return true;
1731 } else {
1732 return false;
1733 }
satok735cf382010-11-11 20:40:09 +09001734 }
1735 }
1736
satoke7c6998e2011-06-03 17:57:59 +09001737 @Override
satok68f1b782011-04-11 14:26:04 +09001738 public InputMethodSubtype getLastInputMethodSubtype() {
1739 synchronized (mMethodMap) {
1740 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1741 // TODO: Handle the case of the last IME with no subtypes
1742 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1743 || TextUtils.isEmpty(lastIme.second)) return null;
1744 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1745 if (lastImi == null) return null;
1746 try {
1747 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001748 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1749 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1750 return null;
1751 }
1752 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001753 } catch (NumberFormatException e) {
1754 return null;
1755 }
1756 }
1757 }
1758
satoke7c6998e2011-06-03 17:57:59 +09001759 @Override
satokee5e77c2011-09-02 18:50:15 +09001760 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001761 // By this IPC call, only a process which shares the same uid with the IME can add
1762 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001763 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001764 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001765 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001766 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001767 final PackageManager pm = mContext.getPackageManager();
1768 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1769 if (packageInfos != null) {
1770 final int packageNum = packageInfos.length;
1771 for (int i = 0; i < packageNum; ++i) {
1772 if (packageInfos[i].equals(imi.getPackageName())) {
1773 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001774 final long ident = Binder.clearCallingIdentity();
1775 try {
1776 buildInputMethodListLocked(mMethodList, mMethodMap);
1777 } finally {
1778 Binder.restoreCallingIdentity(ident);
1779 }
satokee5e77c2011-09-02 18:50:15 +09001780 return;
satok91e88122011-07-18 11:11:42 +09001781 }
1782 }
1783 }
satoke7c6998e2011-06-03 17:57:59 +09001784 }
satokee5e77c2011-09-02 18:50:15 +09001785 return;
satoke7c6998e2011-06-03 17:57:59 +09001786 }
1787
satok28203512010-11-24 11:06:49 +09001788 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 synchronized (mMethodMap) {
1790 if (token == null) {
1791 if (mContext.checkCallingOrSelfPermission(
1792 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1793 != PackageManager.PERMISSION_GRANTED) {
1794 throw new SecurityException(
1795 "Using null token requires permission "
1796 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1797 }
1798 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001799 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1800 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 return;
1802 }
1803
satokc5933802011-08-31 21:26:04 +09001804 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 try {
satokab751aa2010-09-14 19:17:36 +09001806 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 } finally {
1808 Binder.restoreCallingIdentity(ident);
1809 }
1810 }
1811 }
1812
satok42c5a162011-05-26 16:46:14 +09001813 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 public void hideMySoftInput(IBinder token, int flags) {
1815 synchronized (mMethodMap) {
1816 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001817 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1818 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 return;
1820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 long ident = Binder.clearCallingIdentity();
1822 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001823 hideCurrentInputLocked(flags, null);
1824 } finally {
1825 Binder.restoreCallingIdentity(ident);
1826 }
1827 }
1828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829
satok42c5a162011-05-26 16:46:14 +09001830 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001831 public void showMySoftInput(IBinder token, int flags) {
1832 synchronized (mMethodMap) {
1833 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001834 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1835 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 return;
1837 }
1838 long ident = Binder.clearCallingIdentity();
1839 try {
1840 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 } finally {
1842 Binder.restoreCallingIdentity(ident);
1843 }
1844 }
1845 }
1846
1847 void setEnabledSessionInMainThread(SessionState session) {
1848 if (mEnabledSession != session) {
1849 if (mEnabledSession != null) {
1850 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001851 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 mEnabledSession.method.setSessionEnabled(
1853 mEnabledSession.session, false);
1854 } catch (RemoteException e) {
1855 }
1856 }
1857 mEnabledSession = session;
1858 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001859 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 session.method.setSessionEnabled(
1861 session.session, true);
1862 } catch (RemoteException e) {
1863 }
1864 }
1865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001866
satok42c5a162011-05-26 16:46:14 +09001867 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 public boolean handleMessage(Message msg) {
1869 HandlerCaller.SomeArgs args;
1870 switch (msg.what) {
1871 case MSG_SHOW_IM_PICKER:
1872 showInputMethodMenu();
1873 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
satokab751aa2010-09-14 19:17:36 +09001875 case MSG_SHOW_IM_SUBTYPE_PICKER:
1876 showInputMethodSubtypeMenu();
1877 return true;
1878
satok47a44912010-10-06 16:03:58 +09001879 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001880 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001881 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001882 return true;
1883
1884 case MSG_SHOW_IM_CONFIG:
1885 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001886 return true;
1887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 case MSG_UNBIND_INPUT:
1891 try {
1892 ((IInputMethod)msg.obj).unbindInput();
1893 } catch (RemoteException e) {
1894 // There is nothing interesting about the method dying.
1895 }
1896 return true;
1897 case MSG_BIND_INPUT:
1898 args = (HandlerCaller.SomeArgs)msg.obj;
1899 try {
1900 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1901 } catch (RemoteException e) {
1902 }
1903 return true;
1904 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001905 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001907 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1908 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 } catch (RemoteException e) {
1910 }
1911 return true;
1912 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001913 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001915 ((IInputMethod)args.arg1).hideSoftInput(0,
1916 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 } catch (RemoteException e) {
1918 }
1919 return true;
1920 case MSG_ATTACH_TOKEN:
1921 args = (HandlerCaller.SomeArgs)msg.obj;
1922 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1925 } catch (RemoteException e) {
1926 }
1927 return true;
1928 case MSG_CREATE_SESSION:
1929 args = (HandlerCaller.SomeArgs)msg.obj;
1930 try {
1931 ((IInputMethod)args.arg1).createSession(
1932 (IInputMethodCallback)args.arg2);
1933 } catch (RemoteException e) {
1934 }
1935 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 case MSG_START_INPUT:
1939 args = (HandlerCaller.SomeArgs)msg.obj;
1940 try {
1941 SessionState session = (SessionState)args.arg1;
1942 setEnabledSessionInMainThread(session);
1943 session.method.startInput((IInputContext)args.arg2,
1944 (EditorInfo)args.arg3);
1945 } catch (RemoteException e) {
1946 }
1947 return true;
1948 case MSG_RESTART_INPUT:
1949 args = (HandlerCaller.SomeArgs)msg.obj;
1950 try {
1951 SessionState session = (SessionState)args.arg1;
1952 setEnabledSessionInMainThread(session);
1953 session.method.restartInput((IInputContext)args.arg2,
1954 (EditorInfo)args.arg3);
1955 } catch (RemoteException e) {
1956 }
1957 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 case MSG_UNBIND_METHOD:
1962 try {
1963 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1964 } catch (RemoteException e) {
1965 // There is nothing interesting about the last client dying.
1966 }
1967 return true;
1968 case MSG_BIND_METHOD:
1969 args = (HandlerCaller.SomeArgs)msg.obj;
1970 try {
1971 ((IInputMethodClient)args.arg1).onBindMethod(
1972 (InputBindResult)args.arg2);
1973 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001974 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
1976 return true;
1977 }
1978 return false;
1979 }
1980
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001981 private boolean isSystemIme(InputMethodInfo inputMethod) {
1982 return (inputMethod.getServiceInfo().applicationInfo.flags
1983 & ApplicationInfo.FLAG_SYSTEM) != 0;
1984 }
1985
Ken Wakasa586f0512011-01-20 22:31:01 +09001986 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1987 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1988 final int subtypeCount = imi.getSubtypeCount();
1989 for (int i = 0; i < subtypeCount; ++i) {
1990 subtypes.add(imi.getSubtypeAt(i));
1991 }
1992 return subtypes;
1993 }
1994
satoka86f5e42011-09-02 17:12:42 +09001995
1996 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1997 InputMethodInfo imi, String mode) {
1998 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1999 final int subtypeCount = imi.getSubtypeCount();
2000 for (int i = 0; i < subtypeCount; ++i) {
2001 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2002 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2003 subtypes.add(subtype);
2004 }
2005 }
2006 return subtypes;
2007 }
2008
satokdc9ddae2011-10-06 12:22:36 +09002009 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002010 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002011 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002012 // We'd prefer to fall back on a system IME, since that is safer.
2013 int i=enabled.size();
2014 while (i > 0) {
2015 i--;
satokdc9ddae2011-10-06 12:22:36 +09002016 final InputMethodInfo imi = enabled.get(i);
2017 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002018 break;
2019 }
2020 }
satokdc9ddae2011-10-06 12:22:36 +09002021 return enabled.get(i);
2022 }
2023 return null;
2024 }
2025
2026 private boolean chooseNewDefaultIMELocked() {
2027 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2028 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002029 if (DEBUG) {
2030 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2031 }
satok723a27e2010-11-11 14:58:11 +09002032 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002033 return true;
2034 }
2035
2036 return false;
2037 }
2038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2040 HashMap<String, InputMethodInfo> map) {
2041 list.clear();
2042 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002045 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002046 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2047 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2048 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2049 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050
2051 List<ResolveInfo> services = pm.queryIntentServices(
2052 new Intent(InputMethod.SERVICE_INTERFACE),
2053 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002054
satoke7c6998e2011-06-03 17:57:59 +09002055 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2056 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 for (int i = 0; i < services.size(); ++i) {
2058 ResolveInfo ri = services.get(i);
2059 ServiceInfo si = ri.serviceInfo;
2060 ComponentName compName = new ComponentName(si.packageName, si.name);
2061 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2062 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002063 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 + ": it does not require the permission "
2065 + android.Manifest.permission.BIND_INPUT_METHOD);
2066 continue;
2067 }
2068
Joe Onorato8a9b2202010-02-26 18:56:32 -08002069 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070
2071 try {
satoke7c6998e2011-06-03 17:57:59 +09002072 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002074 final String id = p.getId();
2075 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076
Amith Yamasanie861ec12010-03-24 21:39:27 -07002077 // System IMEs are enabled by default, unless there's a hard keyboard
2078 // and the system IME was explicitly disabled
2079 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2080 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002081 }
2082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002084 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002088 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002090 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002093
2094 String defaultIme = Settings.Secure.getString(mContext
2095 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002096 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002097 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002098 updateFromSettingsLocked();
2099 }
2100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002104
satokab751aa2010-09-14 19:17:36 +09002105 private void showInputMethodMenu() {
2106 showInputMethodMenuInternal(false);
2107 }
2108
2109 private void showInputMethodSubtypeMenu() {
2110 showInputMethodMenuInternal(true);
2111 }
2112
satok217f5482010-12-15 05:19:19 +09002113 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002114 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002115 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002116 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2117 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002118 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002119 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002120 }
satok217f5482010-12-15 05:19:19 +09002121 mContext.startActivity(intent);
2122 }
2123
2124 private void showConfigureInputMethods() {
2125 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2126 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2127 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2128 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002129 mContext.startActivity(intent);
2130 }
2131
satokab751aa2010-09-14 19:17:36 +09002132 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002133 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002137 final boolean isScreenLocked = mKeyguardManager != null
2138 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 String lastInputMethodId = Settings.Secure.getString(context
2141 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002142 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002143 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002145 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002146 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2147 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002148 if (immis == null || immis.size() == 0) {
2149 return;
2150 }
2151
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002152 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
Ken Wakasa761eb372011-03-04 19:06:18 +09002154 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2155 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2156 new Comparator<InputMethodInfo>() {
2157 @Override
2158 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2159 if (imi2 == null) return 0;
2160 if (imi1 == null) return 1;
2161 if (pm == null) {
2162 return imi1.getId().compareTo(imi2.getId());
2163 }
2164 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2165 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2166 return imiId1.toString().compareTo(imiId2.toString());
2167 }
2168 });
satok913a8922010-08-26 21:53:41 +09002169
Ken Wakasa761eb372011-03-04 19:06:18 +09002170 sortedImmis.putAll(immis);
2171
Ken Wakasa05dbb652011-08-22 15:22:43 +09002172 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002173
2174 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002175 if (imi == null) continue;
2176 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002177 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002178 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2179 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002180 }
satokbb4aa062011-01-19 21:40:27 +09002181 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002182 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002183 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002184 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002185 if (DEBUG) {
2186 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2187 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002188 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002189 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2190 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2191 // We show all enabled IMEs and subtypes when an IME is shown.
2192 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002193 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002194 final CharSequence subtypeLabel =
2195 subtype.overridesImplicitlyEnabledSubtype() ? null
2196 : subtype.getDisplayName(context, imi.getPackageName(),
2197 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002198 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2199
satok4a28bde2011-06-29 21:03:40 +09002200 // Removing this subtype from enabledSubtypeSet because we no longer
2201 // need to add an entry of this subtype to imList to avoid duplicated
2202 // entries.
2203 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002204 }
satokab751aa2010-09-14 19:17:36 +09002205 }
2206 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002207 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002208 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002209 }
satok913a8922010-08-26 21:53:41 +09002210
Ken Wakasa761eb372011-03-04 19:06:18 +09002211 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002212 mIms = new InputMethodInfo[N];
2213 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002214 int checkedItem = 0;
2215 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002216 final ImeSubtypeListItem item = imList.get(i);
2217 mIms[i] = item.mImi;
2218 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002219 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002220 int subtypeId = mSubtypeIds[i];
2221 if ((subtypeId == NOT_A_SUBTYPE_ID)
2222 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2223 || (subtypeId == lastInputMethodSubtypeId)) {
2224 checkedItem = i;
2225 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
satokab751aa2010-09-14 19:17:36 +09002228
Ken Wakasa05dbb652011-08-22 15:22:43 +09002229 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002230 com.android.internal.R.styleable.DialogPreference,
2231 com.android.internal.R.attr.alertDialogStyle, 0);
2232 mDialogBuilder = new AlertDialog.Builder(context)
2233 .setTitle(com.android.internal.R.string.select_input_method)
2234 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002235 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002236 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002238 }
2239 })
2240 .setIcon(a.getDrawable(
2241 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2242 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002243
Ken Wakasa05dbb652011-08-22 15:22:43 +09002244 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2245 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2246 checkedItem);
2247
2248 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002249 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002250 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002251 public void onClick(DialogInterface dialog, int which) {
2252 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002253 if (mIms == null || mIms.length <= which
2254 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002255 return;
2256 }
2257 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002258 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002259 hideInputMethodMenu();
2260 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002261 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002262 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002263 subtypeId = NOT_A_SUBTYPE_ID;
2264 }
2265 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002266 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002269 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270
satokbc81b692011-08-26 16:22:22 +09002271 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002272 mDialogBuilder.setPositiveButton(
2273 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002274 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002275 @Override
satok7f35c8c2010-10-07 21:13:11 +09002276 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002277 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002278 }
2279 });
2280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002282 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 mSwitchingDialog.getWindow().setType(
2284 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002285 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 mSwitchingDialog.show();
2287 }
2288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
Ken Wakasa05dbb652011-08-22 15:22:43 +09002290 private static class ImeSubtypeListItem {
2291 public final CharSequence mImeName;
2292 public final CharSequence mSubtypeName;
2293 public final InputMethodInfo mImi;
2294 public final int mSubtypeId;
2295 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2296 InputMethodInfo imi, int subtypeId) {
2297 mImeName = imeName;
2298 mSubtypeName = subtypeName;
2299 mImi = imi;
2300 mSubtypeId = subtypeId;
2301 }
2302 }
2303
2304 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2305 private final LayoutInflater mInflater;
2306 private final int mTextViewResourceId;
2307 private final List<ImeSubtypeListItem> mItemsList;
2308 private final int mCheckedItem;
2309 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2310 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2311 super(context, textViewResourceId, itemsList);
2312 mTextViewResourceId = textViewResourceId;
2313 mItemsList = itemsList;
2314 mCheckedItem = checkedItem;
2315 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2316 }
2317
2318 @Override
2319 public View getView(int position, View convertView, ViewGroup parent) {
2320 final View view = convertView != null ? convertView
2321 : mInflater.inflate(mTextViewResourceId, null);
2322 if (position < 0 || position >= mItemsList.size()) return view;
2323 final ImeSubtypeListItem item = mItemsList.get(position);
2324 final CharSequence imeName = item.mImeName;
2325 final CharSequence subtypeName = item.mSubtypeName;
2326 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2327 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2328 if (TextUtils.isEmpty(subtypeName)) {
2329 firstTextView.setText(imeName);
2330 secondTextView.setVisibility(View.GONE);
2331 } else {
2332 firstTextView.setText(subtypeName);
2333 secondTextView.setText(imeName);
2334 secondTextView.setVisibility(View.VISIBLE);
2335 }
2336 final RadioButton radioButton =
2337 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2338 radioButton.setChecked(position == mCheckedItem);
2339 return view;
2340 }
2341 }
2342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002344 synchronized (mMethodMap) {
2345 hideInputMethodMenuLocked();
2346 }
2347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002348
The Android Open Source Project10592532009-03-18 17:39:46 -07002349 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002350 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351
The Android Open Source Project10592532009-03-18 17:39:46 -07002352 if (mSwitchingDialog != null) {
2353 mSwitchingDialog.dismiss();
2354 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002356
The Android Open Source Project10592532009-03-18 17:39:46 -07002357 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002358 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002362
satok42c5a162011-05-26 16:46:14 +09002363 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 public boolean setInputMethodEnabled(String id, boolean enabled) {
2365 synchronized (mMethodMap) {
2366 if (mContext.checkCallingOrSelfPermission(
2367 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2368 != PackageManager.PERMISSION_GRANTED) {
2369 throw new SecurityException(
2370 "Requires permission "
2371 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2372 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 long ident = Binder.clearCallingIdentity();
2375 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002376 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 } finally {
2378 Binder.restoreCallingIdentity(ident);
2379 }
2380 }
2381 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002382
2383 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2384 // Make sure this is a valid input method.
2385 InputMethodInfo imm = mMethodMap.get(id);
2386 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002387 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002388 }
2389
satokd87c2592010-09-29 11:52:06 +09002390 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2391 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002392
satokd87c2592010-09-29 11:52:06 +09002393 if (enabled) {
2394 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2395 if (pair.first.equals(id)) {
2396 // We are enabling this input method, but it is already enabled.
2397 // Nothing to do. The previous state was enabled.
2398 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002399 }
2400 }
satokd87c2592010-09-29 11:52:06 +09002401 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2402 // Previous state was disabled.
2403 return false;
2404 } else {
2405 StringBuilder builder = new StringBuilder();
2406 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2407 builder, enabledInputMethodsList, id)) {
2408 // Disabled input method is currently selected, switch to another one.
2409 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2410 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002411 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2412 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2413 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002414 }
2415 // Previous state was enabled.
2416 return true;
2417 } else {
2418 // We are disabling the input method but it is already disabled.
2419 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002420 return false;
2421 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002422 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002423 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002424
satok57ffc002011-01-25 00:11:47 +09002425 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2426 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002427 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002428 }
2429
satok723a27e2010-11-11 14:58:11 +09002430 private void saveCurrentInputMethodAndSubtypeToHistory() {
2431 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2432 if (mCurrentSubtype != null) {
2433 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2434 }
satok57ffc002011-01-25 00:11:47 +09002435 if (canAddToLastInputMethod(mCurrentSubtype)) {
2436 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2437 }
satokab751aa2010-09-14 19:17:36 +09002438 }
2439
satok723a27e2010-11-11 14:58:11 +09002440 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2441 boolean setSubtypeOnly) {
2442 // Update the history of InputMethod and Subtype
2443 saveCurrentInputMethodAndSubtypeToHistory();
2444
2445 // Set Subtype here
2446 if (imi == null || subtypeId < 0) {
2447 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002448 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002449 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002450 if (subtypeId < imi.getSubtypeCount()) {
2451 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2452 mSettings.putSelectedSubtype(subtype.hashCode());
2453 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002454 } else {
2455 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2456 mCurrentSubtype = null;
2457 }
satokab751aa2010-09-14 19:17:36 +09002458 }
satok723a27e2010-11-11 14:58:11 +09002459
2460 if (!setSubtypeOnly) {
2461 // Set InputMethod here
2462 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2463 }
2464 }
2465
2466 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2467 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2468 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2469 // newDefaultIme is empty when there is no candidate for the selected IME.
2470 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2471 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2472 if (subtypeHashCode != null) {
2473 try {
2474 lastSubtypeId = getSubtypeIdFromHashCode(
2475 imi, Integer.valueOf(subtypeHashCode));
2476 } catch (NumberFormatException e) {
2477 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2478 }
2479 }
2480 }
2481 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002482 }
2483
2484 private int getSelectedInputMethodSubtypeId(String id) {
2485 InputMethodInfo imi = mMethodMap.get(id);
2486 if (imi == null) {
2487 return NOT_A_SUBTYPE_ID;
2488 }
satokab751aa2010-09-14 19:17:36 +09002489 int subtypeId;
2490 try {
2491 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2492 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2493 } catch (SettingNotFoundException e) {
2494 return NOT_A_SUBTYPE_ID;
2495 }
satok723a27e2010-11-11 14:58:11 +09002496 return getSubtypeIdFromHashCode(imi, subtypeId);
2497 }
2498
2499 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002500 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002501 final int subtypeCount = imi.getSubtypeCount();
2502 for (int i = 0; i < subtypeCount; ++i) {
2503 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002504 if (subtypeHashCode == ims.hashCode()) {
2505 return i;
2506 }
satokab751aa2010-09-14 19:17:36 +09002507 }
2508 }
2509 return NOT_A_SUBTYPE_ID;
2510 }
2511
satoka86f5e42011-09-02 17:12:42 +09002512 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2513 Resources res, InputMethodInfo imi) {
2514 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002515 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002516 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002517 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002518 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002519 final int N = subtypes.size();
2520 boolean containsKeyboardSubtype = false;
2521 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002522 // scan overriding implicitly enabled subtypes.
2523 InputMethodSubtype subtype = subtypes.get(i);
2524 if (subtype.overridesImplicitlyEnabledSubtype()) {
2525 final String mode = subtype.getMode();
2526 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2527 applicableModeAndSubtypesMap.put(mode, subtype);
2528 }
2529 }
2530 }
2531 if (applicableModeAndSubtypesMap.size() > 0) {
2532 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2533 }
2534 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002535 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002536 final String locale = subtype.getLocale();
2537 final String mode = subtype.getMode();
2538 // When system locale starts with subtype's locale, that subtype will be applicable
2539 // for system locale
2540 // For instance, it's clearly applicable for cases like system locale = en_US and
2541 // subtype = en, but it is not necessarily considered applicable for cases like system
2542 // locale = en and subtype = en_US.
2543 // We just call systemLocale.startsWith(locale) in this function because there is no
2544 // need to find applicable subtypes aggressively unlike
2545 // findLastResortApplicableSubtypeLocked.
2546 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002547 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002548 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002549 if (applicableSubtype != null) {
2550 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2551 if (!systemLocale.equals(locale)) continue;
2552 }
satok3da92232011-01-11 22:46:30 +09002553 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002554 if (!containsKeyboardSubtype
2555 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2556 containsKeyboardSubtype = true;
2557 }
2558 }
2559 }
satok4a28bde2011-06-29 21:03:40 +09002560 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002561 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002562 if (!containsKeyboardSubtype) {
2563 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002564 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002565 if (lastResortKeyboardSubtype != null) {
2566 applicableSubtypes.add(lastResortKeyboardSubtype);
2567 }
2568 }
2569 return applicableSubtypes;
2570 }
2571
satok4e4569d2010-11-19 18:45:53 +09002572 /**
2573 * If there are no selected subtypes, tries finding the most applicable one according to the
2574 * given locale.
2575 * @param subtypes this function will search the most applicable subtype in subtypes
2576 * @param mode subtypes will be filtered by mode
2577 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002578 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2579 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002580 * @return the most applicable subtypeId
2581 */
satokdf31ae62011-01-15 06:19:44 +09002582 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2583 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002584 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002585 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002586 return null;
satok8fbb1e82010-11-02 23:15:58 +09002587 }
satok4e4569d2010-11-19 18:45:53 +09002588 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002589 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002590 }
satok8fbb1e82010-11-02 23:15:58 +09002591 final String language = locale.substring(0, 2);
2592 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002593 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002594 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002595 final int N = subtypes.size();
2596 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002597 InputMethodSubtype subtype = subtypes.get(i);
2598 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002599 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2600 // and all subtypes with all modes can be candidates.
2601 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002602 if (firstMatchedModeSubtype == null) {
2603 firstMatchedModeSubtype = subtype;
2604 }
satok9ef02832010-11-04 21:17:48 +09002605 if (locale.equals(subtypeLocale)) {
2606 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002607 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002608 break;
2609 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2610 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002611 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002612 partialMatchFound = true;
2613 }
satok8fbb1e82010-11-02 23:15:58 +09002614 }
2615 }
2616
satok7599a7f2010-12-22 13:45:23 +09002617 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2618 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002619 }
2620
satok8fbb1e82010-11-02 23:15:58 +09002621 // The first subtype applicable to the system locale will be defined as the most applicable
2622 // subtype.
2623 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002624 if (applicableSubtype != null) {
2625 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2626 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2627 }
satok8fbb1e82010-11-02 23:15:58 +09002628 }
satokcd7cd292010-11-20 15:46:23 +09002629 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002630 }
2631
satok4e4569d2010-11-19 18:45:53 +09002632 // If there are no selected shortcuts, tries finding the most applicable ones.
2633 private Pair<InputMethodInfo, InputMethodSubtype>
2634 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2635 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2636 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002637 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002638 boolean foundInSystemIME = false;
2639
2640 // Search applicable subtype for each InputMethodInfo
2641 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002642 final String imiId = imi.getId();
2643 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2644 continue;
2645 }
satokcd7cd292010-11-20 15:46:23 +09002646 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002647 final List<InputMethodSubtype> enabledSubtypes =
2648 getEnabledInputMethodSubtypeList(imi, true);
2649 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002650 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002651 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002652 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002653 }
satokdf31ae62011-01-15 06:19:44 +09002654 // 2. Search by the system locale from enabledSubtypes.
2655 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002656 if (subtype == null) {
2657 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002658 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002659 }
satoka86f5e42011-09-02 17:12:42 +09002660 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2661 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2662 final ArrayList<InputMethodSubtype> subtypesForSearch =
2663 overridingImplicitlyEnabledSubtypes.isEmpty()
2664 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002665 // 4. Search by the current subtype's locale from all subtypes.
2666 if (subtype == null && mCurrentSubtype != null) {
2667 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002668 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002669 }
2670 // 5. Search by the system locale from all subtypes.
2671 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002672 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002673 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002674 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002675 }
satokcd7cd292010-11-20 15:46:23 +09002676 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002677 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002678 // The current input method is the most applicable IME.
2679 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002680 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002681 break;
satok7599a7f2010-12-22 13:45:23 +09002682 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002683 // The system input method is 2nd applicable IME.
2684 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002685 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002686 if ((imi.getServiceInfo().applicationInfo.flags
2687 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2688 foundInSystemIME = true;
2689 }
satok4e4569d2010-11-19 18:45:53 +09002690 }
2691 }
2692 }
2693 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002694 if (mostApplicableIMI != null) {
2695 Slog.w(TAG, "Most applicable shortcut input method was:"
2696 + mostApplicableIMI.getId());
2697 if (mostApplicableSubtype != null) {
2698 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2699 + "," + mostApplicableSubtype.getMode() + ","
2700 + mostApplicableSubtype.getLocale());
2701 }
2702 }
satok4e4569d2010-11-19 18:45:53 +09002703 }
satokcd7cd292010-11-20 15:46:23 +09002704 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002705 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002706 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002707 } else {
2708 return null;
2709 }
2710 }
2711
satokab751aa2010-09-14 19:17:36 +09002712 /**
2713 * @return Return the current subtype of this input method.
2714 */
satok42c5a162011-05-26 16:46:14 +09002715 @Override
satokab751aa2010-09-14 19:17:36 +09002716 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002717 boolean subtypeIsSelected = false;
2718 try {
2719 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2720 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2721 } catch (SettingNotFoundException e) {
2722 }
satok3ef8b292010-11-23 06:06:29 +09002723 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002724 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002725 String lastInputMethodId = Settings.Secure.getString(
2726 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002727 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2728 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002729 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2730 if (imi != null) {
2731 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002732 // the most applicable subtype from explicitly or implicitly enabled
2733 // subtypes.
2734 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2735 getEnabledInputMethodSubtypeList(imi, true);
2736 // If there is only one explicitly or implicitly enabled subtype,
2737 // just returns it.
2738 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2739 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2740 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2741 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2742 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2743 SUBTYPE_MODE_KEYBOARD, null, true);
2744 if (mCurrentSubtype == null) {
2745 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2746 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2747 true);
2748 }
2749 }
satok4e4569d2010-11-19 18:45:53 +09002750 }
satokcd7cd292010-11-20 15:46:23 +09002751 } else {
satok3ef8b292010-11-23 06:06:29 +09002752 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002753 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002754 }
satok8fbb1e82010-11-02 23:15:58 +09002755 }
satok3ef8b292010-11-23 06:06:29 +09002756 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002757 }
satokab751aa2010-09-14 19:17:36 +09002758 }
2759
satokf3db1af2010-11-23 13:34:33 +09002760 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2761 InputMethodSubtype subtype) {
2762 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2763 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2764 } else {
2765 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2766 subtypes.add(subtype);
2767 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2768 }
2769 }
2770
satok4e4569d2010-11-19 18:45:53 +09002771 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002772 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002773 @Override
satok4e4569d2010-11-19 18:45:53 +09002774 public List getShortcutInputMethodsAndSubtypes() {
2775 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002776 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002777 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002778 // If there are no selected shortcut subtypes, the framework will try to find
2779 // the most applicable subtype from all subtypes whose mode is
2780 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002781 Pair<InputMethodInfo, InputMethodSubtype> info =
2782 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2783 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002784 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002785 ret.add(info.first);
2786 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002787 }
satok3da92232011-01-11 22:46:30 +09002788 return ret;
satokf3db1af2010-11-23 13:34:33 +09002789 }
satokf3db1af2010-11-23 13:34:33 +09002790 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2791 ret.add(imi);
2792 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2793 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002794 }
2795 }
satokf3db1af2010-11-23 13:34:33 +09002796 return ret;
satok4e4569d2010-11-19 18:45:53 +09002797 }
2798 }
2799
satok42c5a162011-05-26 16:46:14 +09002800 @Override
satokb66d2872010-11-10 01:04:04 +09002801 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2802 synchronized (mMethodMap) {
2803 if (subtype != null && mCurMethodId != null) {
2804 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2805 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2806 if (subtypeId != NOT_A_SUBTYPE_ID) {
2807 setInputMethodLocked(mCurMethodId, subtypeId);
2808 return true;
2809 }
2810 }
2811 return false;
2812 }
2813 }
2814
satokd87c2592010-09-29 11:52:06 +09002815 /**
2816 * Utility class for putting and getting settings for InputMethod
2817 * TODO: Move all putters and getters of settings to this class.
2818 */
2819 private static class InputMethodSettings {
2820 // The string for enabled input method is saved as follows:
2821 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2822 private static final char INPUT_METHOD_SEPARATER = ':';
2823 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002824 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002825 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2826
satok723a27e2010-11-11 14:58:11 +09002827 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002828 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2829
satokdf31ae62011-01-15 06:19:44 +09002830 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002831 private final ContentResolver mResolver;
2832 private final HashMap<String, InputMethodInfo> mMethodMap;
2833 private final ArrayList<InputMethodInfo> mMethodList;
2834
2835 private String mEnabledInputMethodsStrCache;
2836
2837 private static void buildEnabledInputMethodsSettingString(
2838 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2839 String id = pair.first;
2840 ArrayList<String> subtypes = pair.second;
2841 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002842 // Inputmethod and subtypes are saved in the settings as follows:
2843 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2844 for (String subtypeId: subtypes) {
2845 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002846 }
2847 }
2848
2849 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002850 Resources res, ContentResolver resolver,
2851 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2852 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002853 mResolver = resolver;
2854 mMethodMap = methodMap;
2855 mMethodList = methodList;
2856 }
2857
2858 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2859 return createEnabledInputMethodListLocked(
2860 getEnabledInputMethodsAndSubtypeListLocked());
2861 }
2862
satok7f35c8c2010-10-07 21:13:11 +09002863 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002864 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2865 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002866 getEnabledInputMethodsAndSubtypeListLocked());
2867 }
2868
satok67ddf9c2010-11-17 09:45:54 +09002869 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2870 InputMethodInfo imi) {
2871 List<Pair<String, ArrayList<String>>> imsList =
2872 getEnabledInputMethodsAndSubtypeListLocked();
2873 ArrayList<InputMethodSubtype> enabledSubtypes =
2874 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002875 if (imi != null) {
2876 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2877 InputMethodInfo info = mMethodMap.get(imsPair.first);
2878 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002879 final int subtypeCount = info.getSubtypeCount();
2880 for (int i = 0; i < subtypeCount; ++i) {
2881 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002882 for (String s: imsPair.second) {
2883 if (String.valueOf(ims.hashCode()).equals(s)) {
2884 enabledSubtypes.add(ims);
2885 }
satok67ddf9c2010-11-17 09:45:54 +09002886 }
2887 }
satok884ef9a2010-11-18 10:39:46 +09002888 break;
satok67ddf9c2010-11-17 09:45:54 +09002889 }
satok67ddf9c2010-11-17 09:45:54 +09002890 }
2891 }
2892 return enabledSubtypes;
2893 }
2894
satokd87c2592010-09-29 11:52:06 +09002895 // At the initial boot, the settings for input methods are not set,
2896 // so we need to enable IME in that case.
2897 public void enableAllIMEsIfThereIsNoEnabledIME() {
2898 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2899 StringBuilder sb = new StringBuilder();
2900 final int N = mMethodList.size();
2901 for (int i = 0; i < N; i++) {
2902 InputMethodInfo imi = mMethodList.get(i);
2903 Slog.i(TAG, "Adding: " + imi.getId());
2904 if (i > 0) sb.append(':');
2905 sb.append(imi.getId());
2906 }
2907 putEnabledInputMethodsStr(sb.toString());
2908 }
2909 }
2910
satokbb4aa062011-01-19 21:40:27 +09002911 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002912 ArrayList<Pair<String, ArrayList<String>>> imsList
2913 = new ArrayList<Pair<String, ArrayList<String>>>();
2914 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2915 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2916 return imsList;
2917 }
satok723a27e2010-11-11 14:58:11 +09002918 mInputMethodSplitter.setString(enabledInputMethodsStr);
2919 while (mInputMethodSplitter.hasNext()) {
2920 String nextImsStr = mInputMethodSplitter.next();
2921 mSubtypeSplitter.setString(nextImsStr);
2922 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002923 ArrayList<String> subtypeHashes = new ArrayList<String>();
2924 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002925 String imeId = mSubtypeSplitter.next();
2926 while (mSubtypeSplitter.hasNext()) {
2927 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002928 }
2929 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2930 }
2931 }
2932 return imsList;
2933 }
2934
2935 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2936 if (reloadInputMethodStr) {
2937 getEnabledInputMethodsStr();
2938 }
2939 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2940 // Add in the newly enabled input method.
2941 putEnabledInputMethodsStr(id);
2942 } else {
2943 putEnabledInputMethodsStr(
2944 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2945 }
2946 }
2947
2948 /**
2949 * Build and put a string of EnabledInputMethods with removing specified Id.
2950 * @return the specified id was removed or not.
2951 */
2952 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2953 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2954 boolean isRemoved = false;
2955 boolean needsAppendSeparator = false;
2956 for (Pair<String, ArrayList<String>> ims: imsList) {
2957 String curId = ims.first;
2958 if (curId.equals(id)) {
2959 // We are disabling this input method, and it is
2960 // currently enabled. Skip it to remove from the
2961 // new list.
2962 isRemoved = true;
2963 } else {
2964 if (needsAppendSeparator) {
2965 builder.append(INPUT_METHOD_SEPARATER);
2966 } else {
2967 needsAppendSeparator = true;
2968 }
2969 buildEnabledInputMethodsSettingString(builder, ims);
2970 }
2971 }
2972 if (isRemoved) {
2973 // Update the setting with the new list of input methods.
2974 putEnabledInputMethodsStr(builder.toString());
2975 }
2976 return isRemoved;
2977 }
2978
2979 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2980 List<Pair<String, ArrayList<String>>> imsList) {
2981 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2982 for (Pair<String, ArrayList<String>> ims: imsList) {
2983 InputMethodInfo info = mMethodMap.get(ims.first);
2984 if (info != null) {
2985 res.add(info);
2986 }
2987 }
2988 return res;
2989 }
2990
satok7f35c8c2010-10-07 21:13:11 +09002991 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002992 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002993 List<Pair<String, ArrayList<String>>> imsList) {
2994 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2995 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2996 for (Pair<String, ArrayList<String>> ims : imsList) {
2997 InputMethodInfo info = mMethodMap.get(ims.first);
2998 if (info != null) {
2999 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3000 }
3001 }
3002 return res;
3003 }
3004
satokd87c2592010-09-29 11:52:06 +09003005 private void putEnabledInputMethodsStr(String str) {
3006 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3007 mEnabledInputMethodsStrCache = str;
3008 }
3009
3010 private String getEnabledInputMethodsStr() {
3011 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3012 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003013 if (DEBUG) {
3014 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3015 }
satokd87c2592010-09-29 11:52:06 +09003016 return mEnabledInputMethodsStrCache;
3017 }
satok723a27e2010-11-11 14:58:11 +09003018
3019 private void saveSubtypeHistory(
3020 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3021 StringBuilder builder = new StringBuilder();
3022 boolean isImeAdded = false;
3023 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3024 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3025 newSubtypeId);
3026 isImeAdded = true;
3027 }
3028 for (Pair<String, String> ime: savedImes) {
3029 String imeId = ime.first;
3030 String subtypeId = ime.second;
3031 if (TextUtils.isEmpty(subtypeId)) {
3032 subtypeId = NOT_A_SUBTYPE_ID_STR;
3033 }
3034 if (isImeAdded) {
3035 builder.append(INPUT_METHOD_SEPARATER);
3036 } else {
3037 isImeAdded = true;
3038 }
3039 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3040 subtypeId);
3041 }
3042 // Remove the last INPUT_METHOD_SEPARATER
3043 putSubtypeHistoryStr(builder.toString());
3044 }
3045
3046 public void addSubtypeToHistory(String imeId, String subtypeId) {
3047 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3048 for (Pair<String, String> ime: subtypeHistory) {
3049 if (ime.first.equals(imeId)) {
3050 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003051 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003052 + ime.second);
3053 }
3054 // We should break here
3055 subtypeHistory.remove(ime);
3056 break;
3057 }
3058 }
satokbb4aa062011-01-19 21:40:27 +09003059 if (DEBUG) {
3060 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3061 }
satok723a27e2010-11-11 14:58:11 +09003062 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3063 }
3064
3065 private void putSubtypeHistoryStr(String str) {
3066 if (DEBUG) {
3067 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3068 }
3069 Settings.Secure.putString(
3070 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3071 }
3072
3073 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3074 // Gets the first one from the history
3075 return getLastSubtypeForInputMethodLockedInternal(null);
3076 }
3077
3078 public String getLastSubtypeForInputMethodLocked(String imeId) {
3079 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3080 if (ime != null) {
3081 return ime.second;
3082 } else {
3083 return null;
3084 }
3085 }
3086
3087 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3088 List<Pair<String, ArrayList<String>>> enabledImes =
3089 getEnabledInputMethodsAndSubtypeListLocked();
3090 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003091 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003092 final String imeInTheHistory = imeAndSubtype.first;
3093 // If imeId is empty, returns the first IME and subtype in the history
3094 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3095 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003096 final String subtypeHashCode =
3097 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3098 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003099 if (!TextUtils.isEmpty(subtypeHashCode)) {
3100 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003101 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003102 }
3103 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3104 }
3105 }
3106 }
3107 if (DEBUG) {
3108 Slog.d(TAG, "No enabled IME found in the history");
3109 }
3110 return null;
3111 }
3112
satokdf31ae62011-01-15 06:19:44 +09003113 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003114 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3115 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3116 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003117 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3118 if (explicitlyEnabledSubtypes.size() == 0) {
3119 // If there are no explicitly enabled subtypes, applicable subtypes are
3120 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003121 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003122 // If IME is enabled and no subtypes are enabled, applicable subtypes
3123 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003124 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003125 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003126 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003127 if (implicitlySelectedSubtypes != null) {
3128 final int N = implicitlySelectedSubtypes.size();
3129 for (int i = 0; i < N; ++i) {
3130 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3131 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3132 return subtypeHashCode;
3133 }
3134 }
3135 }
3136 }
3137 } else {
satokf6cafb62011-01-17 16:29:02 +09003138 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003139 if (s.equals(subtypeHashCode)) {
3140 // If both imeId and subtypeId are enabled, return subtypeId.
3141 return s;
3142 }
satok723a27e2010-11-11 14:58:11 +09003143 }
3144 }
3145 // If imeId was enabled but subtypeId was disabled.
3146 return NOT_A_SUBTYPE_ID_STR;
3147 }
3148 }
3149 // If both imeId and subtypeId are disabled, return null
3150 return null;
3151 }
3152
3153 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3154 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3155 final String subtypeHistoryStr = getSubtypeHistoryStr();
3156 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3157 return imsList;
3158 }
3159 mInputMethodSplitter.setString(subtypeHistoryStr);
3160 while (mInputMethodSplitter.hasNext()) {
3161 String nextImsStr = mInputMethodSplitter.next();
3162 mSubtypeSplitter.setString(nextImsStr);
3163 if (mSubtypeSplitter.hasNext()) {
3164 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3165 // The first element is ime id.
3166 String imeId = mSubtypeSplitter.next();
3167 while (mSubtypeSplitter.hasNext()) {
3168 subtypeId = mSubtypeSplitter.next();
3169 break;
3170 }
3171 imsList.add(new Pair<String, String>(imeId, subtypeId));
3172 }
3173 }
3174 return imsList;
3175 }
3176
3177 private String getSubtypeHistoryStr() {
3178 if (DEBUG) {
3179 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3180 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3181 }
3182 return Settings.Secure.getString(
3183 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3184 }
3185
3186 public void putSelectedInputMethod(String imeId) {
3187 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3188 }
3189
3190 public void putSelectedSubtype(int subtypeId) {
3191 Settings.Secure.putInt(
3192 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3193 }
satokd87c2592010-09-29 11:52:06 +09003194 }
3195
satoke7c6998e2011-06-03 17:57:59 +09003196 private static class InputMethodFileManager {
3197 private static final String SYSTEM_PATH = "system";
3198 private static final String INPUT_METHOD_PATH = "inputmethod";
3199 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3200 private static final String NODE_SUBTYPES = "subtypes";
3201 private static final String NODE_SUBTYPE = "subtype";
3202 private static final String NODE_IMI = "imi";
3203 private static final String ATTR_ID = "id";
3204 private static final String ATTR_LABEL = "label";
3205 private static final String ATTR_ICON = "icon";
3206 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3207 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3208 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3209 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3210 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3211 private final HashMap<String, InputMethodInfo> mMethodMap;
3212 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3213 new HashMap<String, List<InputMethodSubtype>>();
3214 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3215 if (methodMap == null) {
3216 throw new NullPointerException("methodMap is null");
3217 }
3218 mMethodMap = methodMap;
3219 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3220 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3221 if (!inputMethodDir.mkdirs()) {
3222 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3223 }
3224 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3225 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3226 if (!subtypeFile.exists()) {
3227 // If "subtypes.xml" doesn't exist, create a blank file.
3228 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3229 methodMap);
3230 } else {
3231 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3232 }
3233 }
3234
3235 private void deleteAllInputMethodSubtypes(String imiId) {
3236 synchronized (mMethodMap) {
3237 mSubtypesMap.remove(imiId);
3238 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3239 mMethodMap);
3240 }
3241 }
3242
3243 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003244 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003245 synchronized (mMethodMap) {
3246 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3247 final int N = additionalSubtypes.length;
3248 for (int i = 0; i < N; ++i) {
3249 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003250 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003251 subtypes.add(subtype);
3252 }
3253 }
satok4a28bde2011-06-29 21:03:40 +09003254 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003255 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3256 mMethodMap);
3257 }
3258 }
3259
3260 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3261 synchronized (mMethodMap) {
3262 return mSubtypesMap;
3263 }
3264 }
3265
3266 private static void writeAdditionalInputMethodSubtypes(
3267 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3268 HashMap<String, InputMethodInfo> methodMap) {
3269 // Safety net for the case that this function is called before methodMap is set.
3270 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3271 FileOutputStream fos = null;
3272 try {
3273 fos = subtypesFile.startWrite();
3274 final XmlSerializer out = new FastXmlSerializer();
3275 out.setOutput(fos, "utf-8");
3276 out.startDocument(null, true);
3277 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3278 out.startTag(null, NODE_SUBTYPES);
3279 for (String imiId : allSubtypes.keySet()) {
3280 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3281 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3282 continue;
3283 }
3284 out.startTag(null, NODE_IMI);
3285 out.attribute(null, ATTR_ID, imiId);
3286 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3287 final int N = subtypesList.size();
3288 for (int i = 0; i < N; ++i) {
3289 final InputMethodSubtype subtype = subtypesList.get(i);
3290 out.startTag(null, NODE_SUBTYPE);
3291 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3292 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3293 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3294 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3295 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3296 out.attribute(null, ATTR_IS_AUXILIARY,
3297 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3298 out.endTag(null, NODE_SUBTYPE);
3299 }
3300 out.endTag(null, NODE_IMI);
3301 }
3302 out.endTag(null, NODE_SUBTYPES);
3303 out.endDocument();
3304 subtypesFile.finishWrite(fos);
3305 } catch (java.io.IOException e) {
3306 Slog.w(TAG, "Error writing subtypes", e);
3307 if (fos != null) {
3308 subtypesFile.failWrite(fos);
3309 }
3310 }
3311 }
3312
3313 private static void readAdditionalInputMethodSubtypes(
3314 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3315 if (allSubtypes == null || subtypesFile == null) return;
3316 allSubtypes.clear();
3317 FileInputStream fis = null;
3318 try {
3319 fis = subtypesFile.openRead();
3320 final XmlPullParser parser = Xml.newPullParser();
3321 parser.setInput(fis, null);
3322 int type = parser.getEventType();
3323 // Skip parsing until START_TAG
3324 while ((type = parser.next()) != XmlPullParser.START_TAG
3325 && type != XmlPullParser.END_DOCUMENT) {}
3326 String firstNodeName = parser.getName();
3327 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3328 throw new XmlPullParserException("Xml doesn't start with subtypes");
3329 }
3330 final int depth =parser.getDepth();
3331 String currentImiId = null;
3332 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3333 while (((type = parser.next()) != XmlPullParser.END_TAG
3334 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3335 if (type != XmlPullParser.START_TAG)
3336 continue;
3337 final String nodeName = parser.getName();
3338 if (NODE_IMI.equals(nodeName)) {
3339 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3340 if (TextUtils.isEmpty(currentImiId)) {
3341 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3342 continue;
3343 }
3344 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3345 allSubtypes.put(currentImiId, tempSubtypesArray);
3346 } else if (NODE_SUBTYPE.equals(nodeName)) {
3347 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3348 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3349 continue;
3350 }
3351 final int icon = Integer.valueOf(
3352 parser.getAttributeValue(null, ATTR_ICON));
3353 final int label = Integer.valueOf(
3354 parser.getAttributeValue(null, ATTR_LABEL));
3355 final String imeSubtypeLocale =
3356 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3357 final String imeSubtypeMode =
3358 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3359 final String imeSubtypeExtraValue =
3360 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003361 final boolean isAuxiliary = "1".equals(String.valueOf(
3362 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003363 final InputMethodSubtype subtype =
3364 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3365 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3366 tempSubtypesArray.add(subtype);
3367 }
3368 }
3369 } catch (XmlPullParserException e) {
3370 Slog.w(TAG, "Error reading subtypes: " + e);
3371 return;
3372 } catch (java.io.IOException e) {
3373 Slog.w(TAG, "Error reading subtypes: " + e);
3374 return;
3375 } catch (NumberFormatException e) {
3376 Slog.w(TAG, "Error reading subtypes: " + e);
3377 return;
3378 } finally {
3379 if (fis != null) {
3380 try {
3381 fis.close();
3382 } catch (java.io.IOException e1) {
3383 Slog.w(TAG, "Failed to close.");
3384 }
3385 }
3386 }
3387 }
3388 }
3389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 @Override
3393 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3394 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3395 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3398 + Binder.getCallingPid()
3399 + ", uid=" + Binder.getCallingUid());
3400 return;
3401 }
3402
3403 IInputMethod method;
3404 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 synchronized (mMethodMap) {
3409 p.println("Current Input Method Manager state:");
3410 int N = mMethodList.size();
3411 p.println(" Input Methods:");
3412 for (int i=0; i<N; i++) {
3413 InputMethodInfo info = mMethodList.get(i);
3414 p.println(" InputMethod #" + i + ":");
3415 info.dump(p, " ");
3416 }
3417 p.println(" Clients:");
3418 for (ClientState ci : mClients.values()) {
3419 p.println(" Client " + ci + ":");
3420 p.println(" client=" + ci.client);
3421 p.println(" inputContext=" + ci.inputContext);
3422 p.println(" sessionRequested=" + ci.sessionRequested);
3423 p.println(" curSession=" + ci.curSession);
3424 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003425 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003427 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3428 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3430 + " mBoundToMethod=" + mBoundToMethod);
3431 p.println(" mCurToken=" + mCurToken);
3432 p.println(" mCurIntent=" + mCurIntent);
3433 method = mCurMethod;
3434 p.println(" mCurMethod=" + mCurMethod);
3435 p.println(" mEnabledSession=" + mEnabledSession);
3436 p.println(" mShowRequested=" + mShowRequested
3437 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3438 + " mShowForced=" + mShowForced
3439 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003440 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003442
Jeff Brownb88102f2010-09-08 11:49:43 -07003443 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 pw.flush();
3446 try {
3447 client.client.asBinder().dump(fd, args);
3448 } catch (RemoteException e) {
3449 p.println("Input method client dead: " + e);
3450 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003451 } else {
3452 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003454
Jeff Brownb88102f2010-09-08 11:49:43 -07003455 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 pw.flush();
3458 try {
3459 method.asBinder().dump(fd, args);
3460 } catch (RemoteException e) {
3461 p.println("Input method service dead: " + e);
3462 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003463 } else {
3464 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 }
3466 }
3467}