blob: 73d790a073c02e6b322afe89e398c4ae2d7197e6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090020import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090022import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.view.IInputContext;
24import com.android.internal.view.IInputMethod;
25import com.android.internal.view.IInputMethodCallback;
26import com.android.internal.view.IInputMethodClient;
27import com.android.internal.view.IInputMethodManager;
28import com.android.internal.view.IInputMethodSession;
29import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090038import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090039import android.app.Notification;
40import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070041import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.ComponentName;
43import android.content.ContentResolver;
44import android.content.Context;
45import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.DialogInterface.OnCancelListener;
47import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090048import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070050import android.content.pm.ApplicationInfo;
satok91e88122011-07-18 11:11:42 +090051import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070056import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.res.Resources;
58import android.content.res.TypedArray;
59import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090060import android.graphics.BitmapFactory;
Joe Onorato857fd9b2011-01-27 15:08:35 -080061import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090063import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Handler;
65import android.os.IBinder;
66import android.os.IInterface;
67import android.os.Message;
68import android.os.Parcel;
69import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080070import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.ServiceManager;
72import android.os.SystemClock;
73import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070074import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090075import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090077import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090079import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090080import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.PrintWriterPrinter;
82import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090083import android.util.Slog;
84import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.IWindowManager;
86import 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
satoke7c6998e2011-06-03 17:57:59 +090094import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090096import java.io.FileInputStream;
97import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.IOException;
99import java.io.PrintWriter;
100import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +0900101import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900102import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900104import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900106import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900107import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109/**
110 * This class provides a system service that manages input methods.
111 */
112public class InputMethodManagerService extends IInputMethodManager.Stub
113 implements ServiceConnection, Handler.Callback {
114 static final boolean DEBUG = false;
115 static final String TAG = "InputManagerService";
116
117 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900118 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900119 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900120 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 static final int MSG_UNBIND_INPUT = 1000;
123 static final int MSG_BIND_INPUT = 1010;
124 static final int MSG_SHOW_SOFT_INPUT = 1020;
125 static final int MSG_HIDE_SOFT_INPUT = 1030;
126 static final int MSG_ATTACH_TOKEN = 1040;
127 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 static final int MSG_START_INPUT = 2000;
130 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final int MSG_UNBIND_METHOD = 3000;
133 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
satokf9f01002011-05-19 21:31:50 +0900137 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
138
satokab751aa2010-09-14 19:17:36 +0900139 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900140 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900141 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
142 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900143 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800146 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900148 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700150 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final IWindowManager mIWindowManager;
152 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900153 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // All known input methods. mMethodMap also serves as the global
158 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900159 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
160 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900161 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
162 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
satok7cfc0ed2011-06-20 21:29:36 +0900164 // Ongoing notification
165 private final NotificationManager mNotificationManager;
satokf90a33e2011-07-19 11:55:52 +0900166 private final KeyguardManager mKeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +0900167 private final Notification mImeSwitcherNotification;
168 private final PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900169 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900170 private boolean mNotificationShown;
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 class SessionState {
173 final ClientState client;
174 final IInputMethod method;
175 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 @Override
178 public String toString() {
179 return "SessionState{uid " + client.uid + " pid " + client.pid
180 + " method " + Integer.toHexString(
181 System.identityHashCode(method))
182 + " session " + Integer.toHexString(
183 System.identityHashCode(session))
184 + "}";
185 }
186
187 SessionState(ClientState _client, IInputMethod _method,
188 IInputMethodSession _session) {
189 client = _client;
190 method = _method;
191 session = _session;
192 }
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 class ClientState {
196 final IInputMethodClient client;
197 final IInputContext inputContext;
198 final int uid;
199 final int pid;
200 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 boolean sessionRequested;
203 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 @Override
206 public String toString() {
207 return "ClientState{" + Integer.toHexString(
208 System.identityHashCode(this)) + " uid " + uid
209 + " pid " + pid + "}";
210 }
211
212 ClientState(IInputMethodClient _client, IInputContext _inputContext,
213 int _uid, int _pid) {
214 client = _client;
215 inputContext = _inputContext;
216 uid = _uid;
217 pid = _pid;
218 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
219 }
220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 final HashMap<IBinder, ClientState> mClients
223 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700226 * Set once the system is ready to run third party code.
227 */
228 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * Id of the currently selected input method.
232 */
233 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * The current binding sequence number, incremented every time there is
237 * a new bind performed.
238 */
239 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * The client that is currently bound to an input method.
243 */
244 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700247 * The last window token that gained focus.
248 */
249 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * The input context last provided by the current client.
253 */
254 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * The attributes last provided by the current client.
258 */
259 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * The input method ID of the input method service that we are currently
263 * connected to or in the process of connecting to.
264 */
265 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
satokab751aa2010-09-14 19:17:36 +0900268 * The current subtype of the current input method.
269 */
270 private InputMethodSubtype mCurrentSubtype;
271
satok4e4569d2010-11-19 18:45:53 +0900272 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900273 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
274 mShortcutInputMethodsAndSubtypes =
275 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900276
277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 * Set to true if our ServiceConnection is currently actively bound to
279 * a service (whether or not we have gotten its IBinder back yet).
280 */
281 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Set if the client has asked for the input method to be shown.
285 */
286 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Set if we were explicitly told to show the input method.
290 */
291 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Set if we were forced to be shown.
295 */
296 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * Set if we last told the input method to show itself.
300 */
301 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * The Intent used to connect to the current input method.
305 */
306 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The token we have made for the currently active input method, to
310 * identify it in the future.
311 */
312 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * If non-null, this is the input method service we are currently connected
316 * to.
317 */
318 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * Time that we last initiated a bind to the input method, to determine
322 * if we should try to disconnect and reconnect to it.
323 */
324 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Have we called mCurMethod.bindInput()?
328 */
329 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * Currently enabled session. Only touched by service thread, not
333 * protected by a lock.
334 */
335 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * True if the screen is on. The value is true initially.
339 */
340 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
Joe Onorato857fd9b2011-01-27 15:08:35 -0800342 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
343 int mImeWindowVis;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 AlertDialog.Builder mDialogBuilder;
346 AlertDialog mSwitchingDialog;
347 InputMethodInfo[] mIms;
348 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900349 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 class SettingsObserver extends ContentObserver {
352 SettingsObserver(Handler handler) {
353 super(handler);
354 ContentResolver resolver = mContext.getContentResolver();
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
356 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900358 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
359 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900360 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override public void onChange(boolean selfChange) {
364 synchronized (mMethodMap) {
365 updateFromSettingsLocked();
366 }
367 }
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
371 @Override
372 public void onReceive(Context context, Intent intent) {
373 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
374 mScreenOn = true;
375 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
376 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
378 hideInputMethodMenu();
379 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 // Inform the current client of the change in active status
385 try {
386 if (mCurClient != null && mCurClient.client != null) {
387 mCurClient.client.setActive(mScreenOn);
388 }
389 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800390 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + mCurClient.pid + " uid " + mCurClient.uid);
392 }
393 }
394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 class MyPackageMonitor extends PackageMonitor {
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
403 final int N = mMethodList.size();
404 if (curInputMethodId != null) {
405 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 InputMethodInfo imi = mMethodList.get(i);
407 if (imi.getId().equals(curInputMethodId)) {
408 for (String pkg : packages) {
409 if (imi.getPackageName().equals(pkg)) {
410 if (!doit) {
411 return true;
412 }
satok723a27e2010-11-11 14:58:11 +0900413 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 chooseNewDefaultIMELocked();
415 return true;
416 }
417 }
418 }
419 }
420 }
421 }
422 return false;
423 }
424
425 @Override
426 public void onSomePackagesChanged() {
427 synchronized (mMethodMap) {
428 InputMethodInfo curIm = null;
429 String curInputMethodId = Settings.Secure.getString(mContext
430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
434 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900435 final String imiId = imi.getId();
436 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 curIm = imi;
438 }
satoke7c6998e2011-06-03 17:57:59 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900441 if (isPackageModified(imi.getPackageName())) {
442 mFileManager.deleteAllInputMethodSubtypes(imiId);
443 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800446 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 + imi.getComponent());
448 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 buildInputMethodListLocked(mMethodList, mMethodMap);
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 int change = isPackageDisappearing(curIm.getPackageName());
459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 ServiceInfo si = null;
462 try {
463 si = mContext.getPackageManager().getServiceInfo(
464 curIm.getComponent(), 0);
465 } catch (PackageManager.NameNotFoundException ex) {
466 }
467 if (si == null) {
468 // Uh oh, current input method is no longer around!
469 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 mImeWindowVis = 0;
472 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
473 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 changed = true;
476 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900478 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800479 }
480 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800481 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800482 }
satokab751aa2010-09-14 19:17:36 +0900483
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 if (curIm == null) {
485 // We currently don't have a default input method... is
486 // one now available?
487 changed = chooseNewDefaultIMELocked();
488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 if (changed) {
491 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 }
494 }
495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 class MethodCallback extends IInputMethodCallback.Stub {
498 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 MethodCallback(IInputMethod method) {
501 mMethod = method;
502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
satoke7c6998e2011-06-03 17:57:59 +0900504 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public void finishedEvent(int seq, boolean handled) throws RemoteException {
506 }
507
satoke7c6998e2011-06-03 17:57:59 +0900508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public void sessionCreated(IInputMethodSession session) throws RemoteException {
510 onSessionCreated(mMethod, session);
511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Joe Onorato089de882010-04-12 08:18:45 -0700514 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800516 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 mHandler = new Handler(this);
518 mIWindowManager = IWindowManager.Stub.asInterface(
519 ServiceManager.getService(Context.WINDOW_SERVICE));
520 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900521 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public void executeMessage(Message msg) {
523 handleMessage(msg);
524 }
525 });
satok7cfc0ed2011-06-20 21:29:36 +0900526
satokf90a33e2011-07-19 11:55:52 +0900527 mKeyguardManager = (KeyguardManager)
528 mContext.getSystemService(Context.KEYGUARD_SERVICE);
satok7cfc0ed2011-06-20 21:29:36 +0900529 mNotificationManager = (NotificationManager)
530 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
531 mImeSwitcherNotification = new Notification();
532 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
533 mImeSwitcherNotification.when = 0;
534 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
535 mImeSwitcherNotification.tickerText = null;
536 mImeSwitcherNotification.defaults = 0; // please be quiet
537 mImeSwitcherNotification.sound = null;
538 mImeSwitcherNotification.vibrate = null;
539 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900540 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900541
542 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900543
satoke7c6998e2011-06-03 17:57:59 +0900544 synchronized (mMethodMap) {
545 mFileManager = new InputMethodFileManager(mMethodMap);
546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 IntentFilter screenOnOffFilt = new IntentFilter();
551 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
552 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700553 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
satok913a8922010-08-26 21:53:41 +0900556 mStatusBar = statusBar;
557 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900558 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900559
satokd87c2592010-09-29 11:52:06 +0900560 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900561 mSettings = new InputMethodSettings(
562 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900564 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
satokd87c2592010-09-29 11:52:06 +0900566 if (TextUtils.isEmpty(Settings.Secure.getString(
567 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900569 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
571 try {
satokd87c2592010-09-29 11:52:06 +0900572 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 imi.getPackageName(), 0).getResources();
574 if (res.getBoolean(imi.getIsDefaultResourceId())) {
575 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800576 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 } catch (PackageManager.NameNotFoundException ex) {
579 } catch (Resources.NotFoundException ex) {
580 }
581 }
582 }
satokd87c2592010-09-29 11:52:06 +0900583 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800585 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900588 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mSettingsObserver = new SettingsObserver(mHandler);
593 updateFromSettingsLocked();
594 }
595
596 @Override
597 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
598 throws RemoteException {
599 try {
600 return super.onTransact(code, data, reply, flags);
601 } catch (RuntimeException e) {
602 // The input method manager only throws security exceptions, so let's
603 // log all others.
604 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800605 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 throw e;
608 }
609 }
610
611 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700612 synchronized (mMethodMap) {
613 if (!mSystemReady) {
614 mSystemReady = true;
satokb858c732011-07-22 19:54:34 +0900615 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
616 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700617 try {
618 startInputInnerLocked();
619 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700621 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700622 }
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
satoke7c6998e2011-06-03 17:57:59 +0900626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public List<InputMethodInfo> getInputMethodList() {
628 synchronized (mMethodMap) {
629 return new ArrayList<InputMethodInfo>(mMethodList);
630 }
631 }
632
satoke7c6998e2011-06-03 17:57:59 +0900633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public List<InputMethodInfo> getEnabledInputMethodList() {
635 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900636 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638 }
639
satokbb4aa062011-01-19 21:40:27 +0900640 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
641 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
642 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
643 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
644 for (InputMethodInfo imi: getEnabledInputMethodList()) {
645 enabledInputMethodAndSubtypes.put(
646 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
647 }
648 return enabledInputMethodAndSubtypes;
649 }
650
651 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
652 boolean allowsImplicitlySelectedSubtypes) {
653 if (imi == null && mCurMethodId != null) {
654 imi = mMethodMap.get(mCurMethodId);
655 }
satok7265d9b2011-02-14 15:47:30 +0900656 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900657 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900658 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
659 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900660 }
satok7265d9b2011-02-14 15:47:30 +0900661 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900662 }
663
satoke7c6998e2011-06-03 17:57:59 +0900664 @Override
satok16331c82010-12-20 23:48:46 +0900665 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
666 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900667 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900668 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900669 }
670 }
671
satoke7c6998e2011-06-03 17:57:59 +0900672 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 public void addClient(IInputMethodClient client,
674 IInputContext inputContext, int uid, int pid) {
675 synchronized (mMethodMap) {
676 mClients.put(client.asBinder(), new ClientState(client,
677 inputContext, uid, pid));
678 }
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
satoke7c6998e2011-06-03 17:57:59 +0900681 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public void removeClient(IInputMethodClient client) {
683 synchronized (mMethodMap) {
684 mClients.remove(client.asBinder());
685 }
686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 void executeOrSendMessage(IInterface target, Message msg) {
689 if (target.asBinder() instanceof Binder) {
690 mCaller.sendMessage(msg);
691 } else {
692 handleMessage(msg);
693 msg.recycle();
694 }
695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800696
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700697 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 + mCurClient.client.asBinder());
701 if (mBoundToMethod) {
702 mBoundToMethod = false;
703 if (mCurMethod != null) {
704 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
705 MSG_UNBIND_INPUT, mCurMethod));
706 }
707 }
708 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
709 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
710 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 // Call setActive(false) on the old client
713 try {
714 mCurClient.client.setActive(false);
715 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800716 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 + mCurClient.pid + " uid " + mCurClient.uid);
718 }
719 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
The Android Open Source Project10592532009-03-18 17:39:46 -0700721 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 private int getImeShowFlags() {
726 int flags = 0;
727 if (mShowForced) {
728 flags |= InputMethod.SHOW_FORCED
729 | InputMethod.SHOW_EXPLICIT;
730 } else if (mShowExplicitlyRequested) {
731 flags |= InputMethod.SHOW_EXPLICIT;
732 }
733 return flags;
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 private int getAppShowFlags() {
737 int flags = 0;
738 if (mShowForced) {
739 flags |= InputMethodManager.SHOW_FORCED;
740 } else if (!mShowExplicitlyRequested) {
741 flags |= InputMethodManager.SHOW_IMPLICIT;
742 }
743 return flags;
744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
747 if (!mBoundToMethod) {
748 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
749 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
750 mBoundToMethod = true;
751 }
752 final SessionState session = mCurClient.curSession;
753 if (initial) {
754 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
755 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
756 } else {
757 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
758 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
759 }
760 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800762 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 return needResult
765 ? new InputBindResult(session.session, mCurId, mCurSeq)
766 : null;
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 InputBindResult startInputLocked(IInputMethodClient client,
770 IInputContext inputContext, EditorInfo attribute,
771 boolean initial, boolean needResult) {
772 // If no method is currently selected, do nothing.
773 if (mCurMethodId == null) {
774 return mNoBinding;
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 ClientState cs = mClients.get(client.asBinder());
778 if (cs == null) {
779 throw new IllegalArgumentException("unknown client "
780 + client.asBinder());
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 try {
784 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
785 // Check with the window manager to make sure this client actually
786 // has a window with focus. If not, reject. This is thread safe
787 // because if the focus changes some time before or after, the
788 // next client receiving focus that has any interest in input will
789 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800790 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
792 return null;
793 }
794 } catch (RemoteException e) {
795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 if (mCurClient != cs) {
798 // If the client is changing, we need to switch over to the new
799 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700800 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + cs.client.asBinder());
803
804 // If the screen is on, inform the new client it is active
805 if (mScreenOn) {
806 try {
807 cs.client.setActive(mScreenOn);
808 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 + cs.pid + " uid " + cs.uid);
811 }
812 }
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 // Bump up the sequence for this client and attach it.
816 mCurSeq++;
817 if (mCurSeq <= 0) mCurSeq = 1;
818 mCurClient = cs;
819 mCurInputContext = inputContext;
820 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 // Check if the input method is changing.
823 if (mCurId != null && mCurId.equals(mCurMethodId)) {
824 if (cs.curSession != null) {
825 // Fast case: if we are already connected to the input method,
826 // then just return it.
827 return attachNewInputLocked(initial, needResult);
828 }
829 if (mHaveConnection) {
830 if (mCurMethod != null) {
831 if (!cs.sessionRequested) {
832 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
835 MSG_CREATE_SESSION, mCurMethod,
836 new MethodCallback(mCurMethod)));
837 }
838 // Return to client, and we will get back with it when
839 // we have had a session made for it.
840 return new InputBindResult(null, mCurId, mCurSeq);
841 } else if (SystemClock.uptimeMillis()
842 < (mLastBindTime+TIME_TO_RECONNECT)) {
843 // In this case we have connected to the service, but
844 // don't yet have its interface. If it hasn't been too
845 // long since we did the connection, we'll return to
846 // the client and wait to get the service interface so
847 // we can report back. If it has been too long, we want
848 // to fall through so we can try a disconnect/reconnect
849 // to see if we can get back in touch with the service.
850 return new InputBindResult(null, mCurId, mCurSeq);
851 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
853 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 return startInputInnerLocked();
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 InputBindResult startInputInnerLocked() {
862 if (mCurMethodId == null) {
863 return mNoBinding;
864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700866 if (!mSystemReady) {
867 // If the system is not yet ready, we shouldn't be running third
868 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700869 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 InputMethodInfo info = mMethodMap.get(mCurMethodId);
873 if (info == null) {
874 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700877 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
880 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700881 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
882 com.android.internal.R.string.input_method_binding_label);
883 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
884 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
886 mLastBindTime = SystemClock.uptimeMillis();
887 mHaveConnection = true;
888 mCurId = info.getId();
889 mCurToken = new Binder();
890 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800891 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 mIWindowManager.addWindowToken(mCurToken,
893 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
894 } catch (RemoteException e) {
895 }
896 return new InputBindResult(null, mCurId, mCurSeq);
897 } else {
898 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800899 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 + mCurIntent);
901 }
902 return null;
903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
satoke7c6998e2011-06-03 17:57:59 +0900905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public InputBindResult startInput(IInputMethodClient client,
907 IInputContext inputContext, EditorInfo attribute,
908 boolean initial, boolean needResult) {
909 synchronized (mMethodMap) {
910 final long ident = Binder.clearCallingIdentity();
911 try {
912 return startInputLocked(client, inputContext, attribute,
913 initial, needResult);
914 } finally {
915 Binder.restoreCallingIdentity(ident);
916 }
917 }
918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
satoke7c6998e2011-06-03 17:57:59 +0900920 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void finishInput(IInputMethodClient client) {
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public void onServiceConnected(ComponentName name, IBinder service) {
926 synchronized (mMethodMap) {
927 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
928 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700929 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700931 unbindCurrentMethodLocked(false);
932 return;
933 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
936 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700939 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700941 MSG_CREATE_SESSION, mCurMethod,
942 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944 }
945 }
946 }
947
948 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
949 synchronized (mMethodMap) {
950 if (mCurMethod != null && method != null
951 && mCurMethod.asBinder() == method.asBinder()) {
952 if (mCurClient != null) {
953 mCurClient.curSession = new SessionState(mCurClient,
954 method, session);
955 mCurClient.sessionRequested = false;
956 InputBindResult res = attachNewInputLocked(true, true);
957 if (res.method != null) {
958 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
959 MSG_BIND_METHOD, mCurClient.client, res));
960 }
961 }
962 }
963 }
964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700966 void unbindCurrentMethodLocked(boolean reportToClient) {
967 if (mHaveConnection) {
968 mContext.unbindService(this);
969 mHaveConnection = false;
970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800971
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700972 if (mCurToken != null) {
973 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800974 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 mIWindowManager.removeWindowToken(mCurToken);
976 } catch (RemoteException e) {
977 }
978 mCurToken = null;
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Project10592532009-03-18 17:39:46 -0700981 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700982 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 if (reportToClient && mCurClient != null) {
985 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
986 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
987 }
988 }
989
Devin Taylor0c33ed22010-02-23 13:26:46 -0600990 private void finishSession(SessionState sessionState) {
991 if (sessionState != null && sessionState.session != null) {
992 try {
993 sessionState.session.finishSession();
994 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700995 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600996 }
997 }
998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800999
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001000 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (mCurMethod != null) {
1002 for (ClientState cs : mClients.values()) {
1003 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001004 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 cs.curSession = null;
1006 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001007
1008 finishSession(mEnabledSession);
1009 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 mCurMethod = null;
1011 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001012 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
satoke7c6998e2011-06-03 17:57:59 +09001015 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 public void onServiceDisconnected(ComponentName name) {
1017 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 + " mCurIntent=" + mCurIntent);
1020 if (mCurMethod != null && mCurIntent != null
1021 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001022 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 // We consider this to be a new bind attempt, since the system
1024 // should now try to restart the service for us.
1025 mLastBindTime = SystemClock.uptimeMillis();
1026 mShowRequested = mInputShown;
1027 mInputShown = false;
1028 if (mCurClient != null) {
1029 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1030 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1031 }
1032 }
1033 }
1034 }
1035
satokf9f01002011-05-19 21:31:50 +09001036 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001038 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 long ident = Binder.clearCallingIdentity();
1040 try {
1041 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001042 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 return;
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 synchronized (mMethodMap) {
1047 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001049 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001052 CharSequence contentDescription = null;
1053 try {
1054 PackageManager packageManager = mContext.getPackageManager();
1055 contentDescription = packageManager.getApplicationLabel(
1056 packageManager.getApplicationInfo(packageName, 0));
1057 } catch (NameNotFoundException nnfe) {
1058 /* ignore */
1059 }
1060 mStatusBar.setIcon("ime", packageName, iconId, 0,
1061 contentDescription != null ? contentDescription.toString() : null);
Joe Onorato0cbda992010-05-02 16:28:15 -07001062 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
1065 } finally {
1066 Binder.restoreCallingIdentity(ident);
1067 }
1068 }
1069
satok7cfc0ed2011-06-20 21:29:36 +09001070 private boolean needsToShowImeSwitchOngoingNotification() {
1071 if (!mShowOngoingImeSwitcherForPhones) return false;
1072 synchronized (mMethodMap) {
1073 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1074 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001075 if (N > 2) return true;
1076 if (N < 1) return false;
1077 int nonAuxCount = 0;
1078 int auxCount = 0;
1079 InputMethodSubtype nonAuxSubtype = null;
1080 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001081 for(int i = 0; i < N; ++i) {
1082 final InputMethodInfo imi = imis.get(i);
1083 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1084 imi, true);
1085 final int subtypeCount = subtypes.size();
1086 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001087 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001088 } else {
1089 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001090 final InputMethodSubtype subtype = subtypes.get(j);
1091 if (!subtype.isAuxiliary()) {
1092 ++nonAuxCount;
1093 nonAuxSubtype = subtype;
1094 } else {
1095 ++auxCount;
1096 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001097 }
1098 }
1099 }
satok7cfc0ed2011-06-20 21:29:36 +09001100 }
satokb6359412011-06-28 17:47:41 +09001101 if (nonAuxCount > 1 || auxCount > 1) {
1102 return true;
1103 } else if (nonAuxCount == 1 && auxCount == 1) {
1104 if (nonAuxSubtype != null && auxSubtype != null
1105 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1106 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1107 return false;
1108 }
1109 return true;
1110 }
1111 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001112 }
satok7cfc0ed2011-06-20 21:29:36 +09001113 }
1114
satokf9f01002011-05-19 21:31:50 +09001115 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001116 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001117 int uid = Binder.getCallingUid();
1118 long ident = Binder.clearCallingIdentity();
1119 try {
1120 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001121 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001122 return;
1123 }
1124
1125 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001126 mImeWindowVis = vis;
1127 mBackDisposition = backDisposition;
1128 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001129 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001130 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1131 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001132 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001133 final CharSequence title = mRes.getText(
1134 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001135 final CharSequence imiLabel = imi.loadLabel(pm);
1136 final CharSequence summary = mCurrentSubtype != null
1137 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1138 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1139 (TextUtils.isEmpty(imiLabel) ?
1140 "" : " (" + imiLabel + ")"))
1141 : imiLabel;
1142
satok7cfc0ed2011-06-20 21:29:36 +09001143 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001144 mContext, title, summary, mImeSwitchPendingIntent);
satok7cfc0ed2011-06-20 21:29:36 +09001145 mNotificationManager.notify(
1146 com.android.internal.R.string.select_input_method,
1147 mImeSwitcherNotification);
1148 mNotificationShown = true;
1149 } else {
1150 if (mNotificationShown) {
1151 mNotificationManager.cancel(
1152 com.android.internal.R.string.select_input_method);
1153 mNotificationShown = false;
1154 }
1155 }
satok06487a52010-10-29 11:37:18 +09001156 }
1157 } finally {
1158 Binder.restoreCallingIdentity(ident);
1159 }
1160 }
1161
satoke7c6998e2011-06-03 17:57:59 +09001162 @Override
satokf9f01002011-05-19 21:31:50 +09001163 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1164 synchronized (mMethodMap) {
1165 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1166 for (int i = 0; i < spans.length; ++i) {
1167 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001168 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001169 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001170 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001171 }
1172 }
1173 }
1174 }
1175
satoke7c6998e2011-06-03 17:57:59 +09001176 @Override
satokf9f01002011-05-19 21:31:50 +09001177 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1178 synchronized (mMethodMap) {
1179 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1180 // TODO: Do not send the intent if the process of the targetImi is already dead.
1181 if (targetImi != null) {
1182 final String[] suggestions = span.getSuggestions();
1183 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001184 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001185 final Intent intent = new Intent();
1186 // Ensures that only a class in the original IME package will receive the
1187 // notification.
satok42c5a162011-05-26 16:46:14 +09001188 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001189 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1190 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1191 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1192 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1193 mContext.sendBroadcast(intent);
1194 return true;
1195 }
1196 }
1197 return false;
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001201 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1202 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1203 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1204 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001206 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001207 // There is no input method selected, try to choose new applicable input method.
1208 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1209 id = Settings.Secure.getString(mContext.getContentResolver(),
1210 Settings.Secure.DEFAULT_INPUT_METHOD);
1211 }
1212 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 try {
satokab751aa2010-09-14 19:17:36 +09001214 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001216 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001217 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001218 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
satokf3db1af2010-11-23 13:34:33 +09001220 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001221 } else {
1222 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001223 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001224 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227
satokab751aa2010-09-14 19:17:36 +09001228 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 InputMethodInfo info = mMethodMap.get(id);
1230 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001231 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001235 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001236 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1237 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001238 }
1239 if (subtype != mCurrentSubtype) {
1240 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001241 if (subtype != null) {
1242 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1243 }
satokcd7cd292010-11-20 15:46:23 +09001244 if (mCurMethod != null) {
1245 try {
satoke40dea02011-01-30 01:14:02 +09001246 final Configuration conf = mRes.getConfiguration();
1247 final boolean haveHardKeyboard = conf.keyboard
1248 != Configuration.KEYBOARD_NOKEYS;
1249 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001250 && conf.hardKeyboardHidden
1251 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001252 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1253 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1254 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001255 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1256 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001257 // If subtype is null, try to find the most applicable one from
1258 // getCurrentInputMethodSubtype.
1259 if (subtype == null) {
1260 subtype = getCurrentInputMethodSubtype();
1261 }
1262 mCurMethod.changeInputMethodSubtype(subtype);
1263 } catch (RemoteException e) {
1264 return;
satokab751aa2010-09-14 19:17:36 +09001265 }
1266 }
1267 }
1268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 return;
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 final long ident = Binder.clearCallingIdentity();
1273 try {
satokab751aa2010-09-14 19:17:36 +09001274 // Set a subtype to this input method.
1275 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001276 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1277 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1278 // because mCurMethodId is stored as a history in
1279 // setSelectedInputMethodAndSubtypeLocked().
1280 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281
1282 if (ActivityManagerNative.isSystemReady()) {
1283 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001284 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 intent.putExtra("input_method_id", id);
1286 mContext.sendBroadcast(intent);
1287 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001288 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 } finally {
1290 Binder.restoreCallingIdentity(ident);
1291 }
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
satok42c5a162011-05-26 16:46:14 +09001294 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001295 public boolean showSoftInput(IInputMethodClient client, int flags,
1296 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001297 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 long ident = Binder.clearCallingIdentity();
1299 try {
1300 synchronized (mMethodMap) {
1301 if (mCurClient == null || client == null
1302 || mCurClient.client.asBinder() != client.asBinder()) {
1303 try {
1304 // We need to check if this is the current client with
1305 // focus in the window manager, to allow this call to
1306 // be made before input is started in it.
1307 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001308 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001309 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001312 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
Joe Onorato8a9b2202010-02-26 18:56:32 -08001316 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001317 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319 } finally {
1320 Binder.restoreCallingIdentity(ident);
1321 }
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 mShowRequested = true;
1326 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1327 mShowExplicitlyRequested = true;
1328 }
1329 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1330 mShowExplicitlyRequested = true;
1331 mShowForced = true;
1332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
Dianne Hackborncc278702009-09-02 23:07:23 -07001334 if (!mSystemReady) {
1335 return false;
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
The Android Open Source Project4df24232009-03-05 14:34:35 -08001338 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001340 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1341 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1342 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001344 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 } else if (mHaveConnection && SystemClock.uptimeMillis()
1346 < (mLastBindTime+TIME_TO_RECONNECT)) {
1347 // The client has asked to have the input method shown, but
1348 // we have been sitting here too long with a connection to the
1349 // service and no interface received, so let's disconnect/connect
1350 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 SystemClock.uptimeMillis()-mLastBindTime,1);
1353 mContext.unbindService(this);
1354 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001359
satok42c5a162011-05-26 16:46:14 +09001360 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001361 public boolean hideSoftInput(IInputMethodClient client, int flags,
1362 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001363 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 long ident = Binder.clearCallingIdentity();
1365 try {
1366 synchronized (mMethodMap) {
1367 if (mCurClient == null || client == null
1368 || mCurClient.client.asBinder() != client.asBinder()) {
1369 try {
1370 // We need to check if this is the current client with
1371 // focus in the window manager, to allow this call to
1372 // be made before input is started in it.
1373 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001374 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1375 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001376 mImeWindowVis = 0;
1377 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1378 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001382 mImeWindowVis = 0;
1383 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001384 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001389 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391 } finally {
1392 Binder.restoreCallingIdentity(ident);
1393 }
1394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001395
The Android Open Source Project4df24232009-03-05 14:34:35 -08001396 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1398 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001399 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001401 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001406 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001408 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001410 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1411 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1412 res = true;
1413 } else {
1414 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 mInputShown = false;
1417 mShowRequested = false;
1418 mShowExplicitlyRequested = false;
1419 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001420 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
satok42c5a162011-05-26 16:46:14 +09001423 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001424 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1426 boolean first, int windowFlags) {
1427 long ident = Binder.clearCallingIdentity();
1428 try {
1429 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001430 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 + " viewHasFocus=" + viewHasFocus
1432 + " isTextEditor=" + isTextEditor
1433 + " softInputMode=#" + Integer.toHexString(softInputMode)
1434 + " first=" + first + " flags=#"
1435 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (mCurClient == null || client == null
1438 || mCurClient.client.asBinder() != client.asBinder()) {
1439 try {
1440 // We need to check if this is the current client with
1441 // focus in the window manager, to allow this call to
1442 // be made before input is started in it.
1443 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001444 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 return;
1446 }
1447 } catch (RemoteException e) {
1448 }
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001451 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001453 return;
1454 }
1455 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001457 // Should we auto-show the IME even if the caller has not
1458 // specified what should be done with it?
1459 // We only do this automatically if the window can resize
1460 // to accommodate the IME (so what the user sees will give
1461 // them good context without input information being obscured
1462 // by the IME) or if running on a large screen where there
1463 // is more room for the target window + IME.
1464 final boolean doAutoShow =
1465 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1466 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1467 || mRes.getConfiguration().isLayoutSizeAtLeast(
1468 Configuration.SCREENLAYOUT_SIZE_LARGE);
1469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1471 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001472 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1474 // There is no focus view, and this window will
1475 // be behind any soft input window, so hide the
1476 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001477 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001478 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001480 } else if (isTextEditor && doAutoShow && (softInputMode &
1481 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 // There is a focus view, and we are navigating forward
1483 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001484 // We only do this automatically if the window an resize
1485 // to accomodate the IME (so what the user sees will give
1486 // them good context without input information being obscured
1487 // by the IME) or if running on a large screen where there
1488 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001490 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 break;
1493 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1494 // Do nothing.
1495 break;
1496 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1497 if ((softInputMode &
1498 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001500 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502 break;
1503 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001504 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001505 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 break;
1507 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1508 if ((softInputMode &
1509 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001511 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513 break;
1514 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001515 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001516 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 break;
1518 }
1519 }
1520 } finally {
1521 Binder.restoreCallingIdentity(ident);
1522 }
1523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001524
satok42c5a162011-05-26 16:46:14 +09001525 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1527 synchronized (mMethodMap) {
1528 if (mCurClient == null || client == null
1529 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001530 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001531 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
1533
satok440aab52010-11-25 09:43:11 +09001534 // Always call subtype picker, because subtype picker is a superset of input method
1535 // picker.
satokab751aa2010-09-14 19:17:36 +09001536 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1537 }
1538 }
1539
satok42c5a162011-05-26 16:46:14 +09001540 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001542 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1543 }
1544
satok42c5a162011-05-26 16:46:14 +09001545 @Override
satok28203512010-11-24 11:06:49 +09001546 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1547 synchronized (mMethodMap) {
1548 if (subtype != null) {
1549 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1550 mMethodMap.get(id), subtype.hashCode()));
1551 } else {
1552 setInputMethod(token, id);
1553 }
1554 }
satokab751aa2010-09-14 19:17:36 +09001555 }
1556
satok42c5a162011-05-26 16:46:14 +09001557 @Override
satokb416a71e2010-11-25 20:42:14 +09001558 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001559 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001560 synchronized (mMethodMap) {
1561 if (mCurClient == null || client == null
1562 || mCurClient.client.asBinder() != client.asBinder()) {
1563 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1564 }
satok7fee71f2010-12-17 18:54:26 +09001565 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1566 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001567 }
1568 }
1569
satok4fc87d62011-05-20 16:13:43 +09001570 @Override
satok735cf382010-11-11 20:40:09 +09001571 public boolean switchToLastInputMethod(IBinder token) {
1572 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001573 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001574 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001575 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001576 lastImi = mMethodMap.get(lastIme.first);
1577 } else {
1578 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001579 }
satok4fc87d62011-05-20 16:13:43 +09001580 String targetLastImiId = null;
1581 int subtypeId = NOT_A_SUBTYPE_ID;
1582 if (lastIme != null && lastImi != null) {
1583 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1584 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1585 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1586 : mCurrentSubtype.hashCode();
1587 // If the last IME is the same as the current IME and the last subtype is not
1588 // defined, there is no need to switch to the last IME.
1589 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1590 targetLastImiId = lastIme.first;
1591 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1592 }
1593 }
1594
1595 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1596 // This is a safety net. If the currentSubtype can't be added to the history
1597 // and the framework couldn't find the last ime, we will make the last ime be
1598 // the most applicable enabled keyboard subtype of the system imes.
1599 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1600 if (enabled != null) {
1601 final int N = enabled.size();
1602 final String locale = mCurrentSubtype == null
1603 ? mRes.getConfiguration().locale.toString()
1604 : mCurrentSubtype.getLocale();
1605 for (int i = 0; i < N; ++i) {
1606 final InputMethodInfo imi = enabled.get(i);
1607 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1608 InputMethodSubtype keyboardSubtype =
1609 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1610 SUBTYPE_MODE_KEYBOARD, locale, true);
1611 if (keyboardSubtype != null) {
1612 targetLastImiId = imi.getId();
1613 subtypeId = getSubtypeIdFromHashCode(
1614 imi, keyboardSubtype.hashCode());
1615 if(keyboardSubtype.getLocale().equals(locale)) {
1616 break;
1617 }
1618 }
1619 }
1620 }
1621 }
1622 }
1623
1624 if (!TextUtils.isEmpty(targetLastImiId)) {
1625 if (DEBUG) {
1626 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1627 + ", from: " + mCurMethodId + ", " + subtypeId);
1628 }
1629 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1630 return true;
1631 } else {
1632 return false;
1633 }
satok735cf382010-11-11 20:40:09 +09001634 }
1635 }
1636
satoke7c6998e2011-06-03 17:57:59 +09001637 @Override
satok68f1b782011-04-11 14:26:04 +09001638 public InputMethodSubtype getLastInputMethodSubtype() {
1639 synchronized (mMethodMap) {
1640 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1641 // TODO: Handle the case of the last IME with no subtypes
1642 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1643 || TextUtils.isEmpty(lastIme.second)) return null;
1644 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1645 if (lastImi == null) return null;
1646 try {
1647 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001648 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1649 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1650 return null;
1651 }
1652 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001653 } catch (NumberFormatException e) {
1654 return null;
1655 }
1656 }
1657 }
1658
satoke7c6998e2011-06-03 17:57:59 +09001659 @Override
satok91e88122011-07-18 11:11:42 +09001660 public boolean setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
1661 // By this IPC call, only a process which shares the same uid with the IME can add
1662 // additional input method subtypes to the IME.
1663 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return false;
satoke7c6998e2011-06-03 17:57:59 +09001664 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001665 final InputMethodInfo imi = mMethodMap.get(imiId);
satoke7c6998e2011-06-03 17:57:59 +09001666 if (imi == null) return false;
satok91e88122011-07-18 11:11:42 +09001667 final PackageManager pm = mContext.getPackageManager();
1668 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1669 if (packageInfos != null) {
1670 final int packageNum = packageInfos.length;
1671 for (int i = 0; i < packageNum; ++i) {
1672 if (packageInfos[i].equals(imi.getPackageName())) {
1673 mFileManager.addInputMethodSubtypes(imi, subtypes);
1674 buildInputMethodListLocked(mMethodList, mMethodMap);
1675 return true;
1676 }
1677 }
1678 }
satoke7c6998e2011-06-03 17:57:59 +09001679 }
satok91e88122011-07-18 11:11:42 +09001680 return false;
satoke7c6998e2011-06-03 17:57:59 +09001681 }
1682
satok28203512010-11-24 11:06:49 +09001683 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 synchronized (mMethodMap) {
1685 if (token == null) {
1686 if (mContext.checkCallingOrSelfPermission(
1687 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1688 != PackageManager.PERMISSION_GRANTED) {
1689 throw new SecurityException(
1690 "Using null token requires permission "
1691 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1692 }
1693 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001694 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1695 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 return;
1697 }
1698
1699 long ident = Binder.clearCallingIdentity();
1700 try {
satokab751aa2010-09-14 19:17:36 +09001701 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 } finally {
1703 Binder.restoreCallingIdentity(ident);
1704 }
1705 }
1706 }
1707
satok42c5a162011-05-26 16:46:14 +09001708 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 public void hideMySoftInput(IBinder token, int flags) {
1710 synchronized (mMethodMap) {
1711 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001712 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1713 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 return;
1715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long ident = Binder.clearCallingIdentity();
1717 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001718 hideCurrentInputLocked(flags, null);
1719 } finally {
1720 Binder.restoreCallingIdentity(ident);
1721 }
1722 }
1723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001724
satok42c5a162011-05-26 16:46:14 +09001725 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001726 public void showMySoftInput(IBinder token, int flags) {
1727 synchronized (mMethodMap) {
1728 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001729 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1730 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 return;
1732 }
1733 long ident = Binder.clearCallingIdentity();
1734 try {
1735 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } finally {
1737 Binder.restoreCallingIdentity(ident);
1738 }
1739 }
1740 }
1741
1742 void setEnabledSessionInMainThread(SessionState session) {
1743 if (mEnabledSession != session) {
1744 if (mEnabledSession != null) {
1745 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001746 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 mEnabledSession.method.setSessionEnabled(
1748 mEnabledSession.session, false);
1749 } catch (RemoteException e) {
1750 }
1751 }
1752 mEnabledSession = session;
1753 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001754 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 session.method.setSessionEnabled(
1756 session.session, true);
1757 } catch (RemoteException e) {
1758 }
1759 }
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
satok42c5a162011-05-26 16:46:14 +09001762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 public boolean handleMessage(Message msg) {
1764 HandlerCaller.SomeArgs args;
1765 switch (msg.what) {
1766 case MSG_SHOW_IM_PICKER:
1767 showInputMethodMenu();
1768 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001769
satokab751aa2010-09-14 19:17:36 +09001770 case MSG_SHOW_IM_SUBTYPE_PICKER:
1771 showInputMethodSubtypeMenu();
1772 return true;
1773
satok47a44912010-10-06 16:03:58 +09001774 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001775 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001776 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001777 return true;
1778
1779 case MSG_SHOW_IM_CONFIG:
1780 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001781 return true;
1782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 case MSG_UNBIND_INPUT:
1786 try {
1787 ((IInputMethod)msg.obj).unbindInput();
1788 } catch (RemoteException e) {
1789 // There is nothing interesting about the method dying.
1790 }
1791 return true;
1792 case MSG_BIND_INPUT:
1793 args = (HandlerCaller.SomeArgs)msg.obj;
1794 try {
1795 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1796 } catch (RemoteException e) {
1797 }
1798 return true;
1799 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001800 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1803 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 } catch (RemoteException e) {
1805 }
1806 return true;
1807 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001808 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 ((IInputMethod)args.arg1).hideSoftInput(0,
1811 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 } catch (RemoteException e) {
1813 }
1814 return true;
1815 case MSG_ATTACH_TOKEN:
1816 args = (HandlerCaller.SomeArgs)msg.obj;
1817 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001818 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1820 } catch (RemoteException e) {
1821 }
1822 return true;
1823 case MSG_CREATE_SESSION:
1824 args = (HandlerCaller.SomeArgs)msg.obj;
1825 try {
1826 ((IInputMethod)args.arg1).createSession(
1827 (IInputMethodCallback)args.arg2);
1828 } catch (RemoteException e) {
1829 }
1830 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 case MSG_START_INPUT:
1834 args = (HandlerCaller.SomeArgs)msg.obj;
1835 try {
1836 SessionState session = (SessionState)args.arg1;
1837 setEnabledSessionInMainThread(session);
1838 session.method.startInput((IInputContext)args.arg2,
1839 (EditorInfo)args.arg3);
1840 } catch (RemoteException e) {
1841 }
1842 return true;
1843 case MSG_RESTART_INPUT:
1844 args = (HandlerCaller.SomeArgs)msg.obj;
1845 try {
1846 SessionState session = (SessionState)args.arg1;
1847 setEnabledSessionInMainThread(session);
1848 session.method.restartInput((IInputContext)args.arg2,
1849 (EditorInfo)args.arg3);
1850 } catch (RemoteException e) {
1851 }
1852 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 case MSG_UNBIND_METHOD:
1857 try {
1858 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1859 } catch (RemoteException e) {
1860 // There is nothing interesting about the last client dying.
1861 }
1862 return true;
1863 case MSG_BIND_METHOD:
1864 args = (HandlerCaller.SomeArgs)msg.obj;
1865 try {
1866 ((IInputMethodClient)args.arg1).onBindMethod(
1867 (InputBindResult)args.arg2);
1868 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001869 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871 return true;
1872 }
1873 return false;
1874 }
1875
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001876 private boolean isSystemIme(InputMethodInfo inputMethod) {
1877 return (inputMethod.getServiceInfo().applicationInfo.flags
1878 & ApplicationInfo.FLAG_SYSTEM) != 0;
1879 }
1880
Ken Wakasa586f0512011-01-20 22:31:01 +09001881 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1882 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1883 final int subtypeCount = imi.getSubtypeCount();
1884 for (int i = 0; i < subtypeCount; ++i) {
1885 subtypes.add(imi.getSubtypeAt(i));
1886 }
1887 return subtypes;
1888 }
1889
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001890 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001891 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001892 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001893 // We'd prefer to fall back on a system IME, since that is safer.
1894 int i=enabled.size();
1895 while (i > 0) {
1896 i--;
1897 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1898 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1899 break;
1900 }
1901 }
satokab751aa2010-09-14 19:17:36 +09001902 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001903 if (DEBUG) {
1904 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1905 }
satok723a27e2010-11-11 14:58:11 +09001906 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001907 return true;
1908 }
1909
1910 return false;
1911 }
1912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1914 HashMap<String, InputMethodInfo> map) {
1915 list.clear();
1916 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001919 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001920 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1921 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1922 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1923 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924
1925 List<ResolveInfo> services = pm.queryIntentServices(
1926 new Intent(InputMethod.SERVICE_INTERFACE),
1927 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001928
satoke7c6998e2011-06-03 17:57:59 +09001929 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1930 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 for (int i = 0; i < services.size(); ++i) {
1932 ResolveInfo ri = services.get(i);
1933 ServiceInfo si = ri.serviceInfo;
1934 ComponentName compName = new ComponentName(si.packageName, si.name);
1935 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1936 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001937 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 + ": it does not require the permission "
1939 + android.Manifest.permission.BIND_INPUT_METHOD);
1940 continue;
1941 }
1942
Joe Onorato8a9b2202010-02-26 18:56:32 -08001943 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944
1945 try {
satoke7c6998e2011-06-03 17:57:59 +09001946 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001948 final String id = p.getId();
1949 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950
Amith Yamasanie861ec12010-03-24 21:39:27 -07001951 // System IMEs are enabled by default, unless there's a hard keyboard
1952 // and the system IME was explicitly disabled
1953 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1954 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001955 }
1956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001958 Slog.d(TAG, "Found a third-party input method " + p);
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 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001962 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001967
1968 String defaultIme = Settings.Secure.getString(mContext
1969 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001970 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001971 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001972 updateFromSettingsLocked();
1973 }
1974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001978
satokab751aa2010-09-14 19:17:36 +09001979 private void showInputMethodMenu() {
1980 showInputMethodMenuInternal(false);
1981 }
1982
1983 private void showInputMethodSubtypeMenu() {
1984 showInputMethodMenuInternal(true);
1985 }
1986
satok217f5482010-12-15 05:19:19 +09001987 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001988 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001989 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001990 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1991 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001992 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001993 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001994 }
satok217f5482010-12-15 05:19:19 +09001995 mContext.startActivity(intent);
1996 }
1997
1998 private void showConfigureInputMethods() {
1999 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2000 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2001 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2002 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002003 mContext.startActivity(intent);
2004 }
2005
satokab751aa2010-09-14 19:17:36 +09002006 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002007 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 String lastInputMethodId = Settings.Secure.getString(context
2014 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002015 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002016 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002017
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002018 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002019 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2020 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002021 if (immis == null || immis.size() == 0) {
2022 return;
2023 }
2024
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002025 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026
Ken Wakasa761eb372011-03-04 19:06:18 +09002027 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2028 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2029 new Comparator<InputMethodInfo>() {
2030 @Override
2031 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2032 if (imi2 == null) return 0;
2033 if (imi1 == null) return 1;
2034 if (pm == null) {
2035 return imi1.getId().compareTo(imi2.getId());
2036 }
2037 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2038 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2039 return imiId1.toString().compareTo(imiId2.toString());
2040 }
2041 });
satok913a8922010-08-26 21:53:41 +09002042
Ken Wakasa761eb372011-03-04 19:06:18 +09002043 sortedImmis.putAll(immis);
2044
2045 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2046 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2047
2048 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002049 if (imi == null) continue;
2050 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002051 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002052 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2053 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002054 }
satokbb4aa062011-01-19 21:40:27 +09002055 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002056 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002057 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002058 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002059 if (DEBUG) {
2060 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2061 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002062 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002063 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2064 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2065 // We show all enabled IMEs and subtypes when an IME is shown.
2066 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002067 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002068 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002069 final String mode = subtype.getMode();
2070 title = TextUtils.concat(subtype.getDisplayName(context,
2071 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2072 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002073 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2074 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002075 // Removing this subtype from enabledSubtypeSet because we no longer
2076 // need to add an entry of this subtype to imList to avoid duplicated
2077 // entries.
2078 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002079 }
satokab751aa2010-09-14 19:17:36 +09002080 }
2081 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002082 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2083 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002084 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002085 }
satok913a8922010-08-26 21:53:41 +09002086
Ken Wakasa761eb372011-03-04 19:06:18 +09002087 final int N = imList.size();
2088 mItems = new CharSequence[N];
2089 for (int i = 0; i < N; ++i) {
2090 mItems[i] = imList.get(i).first;
2091 }
satokab751aa2010-09-14 19:17:36 +09002092 mIms = new InputMethodInfo[N];
2093 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002094 int checkedItem = 0;
2095 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002096 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002097 mIms[i] = value.first;
2098 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002099 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002100 int subtypeId = mSubtypeIds[i];
2101 if ((subtypeId == NOT_A_SUBTYPE_ID)
2102 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2103 || (subtypeId == lastInputMethodSubtypeId)) {
2104 checkedItem = i;
2105 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
satokab751aa2010-09-14 19:17:36 +09002108
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002109 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002110 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002111 public void onClick(DialogInterface dialog, int which) {
2112 hideInputMethodMenu();
2113 }
2114 };
satokd87c2592010-09-29 11:52:06 +09002115
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002116 TypedArray a = context.obtainStyledAttributes(null,
2117 com.android.internal.R.styleable.DialogPreference,
2118 com.android.internal.R.attr.alertDialogStyle, 0);
2119 mDialogBuilder = new AlertDialog.Builder(context)
2120 .setTitle(com.android.internal.R.string.select_input_method)
2121 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002122 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002123 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002125 }
2126 })
2127 .setIcon(a.getDrawable(
2128 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2129 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002130
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002131 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2132 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002133 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002134 public void onClick(DialogInterface dialog, int which) {
2135 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002136 if (mIms == null || mIms.length <= which
2137 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002138 return;
2139 }
2140 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002141 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002142 hideInputMethodMenu();
2143 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002144 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002145 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002146 subtypeId = NOT_A_SUBTYPE_ID;
2147 }
2148 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002149 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002152 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
satokf90a33e2011-07-19 11:55:52 +09002154 if (showSubtypes && !(mKeyguardManager.isKeyguardLocked()
2155 && mKeyguardManager.isKeyguardSecure())) {
satok82beadf2010-12-27 19:03:06 +09002156 mDialogBuilder.setPositiveButton(
2157 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002158 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002159 @Override
satok7f35c8c2010-10-07 21:13:11 +09002160 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002161 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002162 }
2163 });
2164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002166 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 mSwitchingDialog.getWindow().setType(
2168 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002169 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 mSwitchingDialog.show();
2171 }
2172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002175 synchronized (mMethodMap) {
2176 hideInputMethodMenuLocked();
2177 }
2178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002179
The Android Open Source Project10592532009-03-18 17:39:46 -07002180 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002181 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182
The Android Open Source Project10592532009-03-18 17:39:46 -07002183 if (mSwitchingDialog != null) {
2184 mSwitchingDialog.dismiss();
2185 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002187
The Android Open Source Project10592532009-03-18 17:39:46 -07002188 mDialogBuilder = null;
2189 mItems = null;
2190 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002194
satok42c5a162011-05-26 16:46:14 +09002195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 public boolean setInputMethodEnabled(String id, boolean enabled) {
2197 synchronized (mMethodMap) {
2198 if (mContext.checkCallingOrSelfPermission(
2199 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2200 != PackageManager.PERMISSION_GRANTED) {
2201 throw new SecurityException(
2202 "Requires permission "
2203 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2204 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 long ident = Binder.clearCallingIdentity();
2207 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002208 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 } finally {
2210 Binder.restoreCallingIdentity(ident);
2211 }
2212 }
2213 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002214
2215 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2216 // Make sure this is a valid input method.
2217 InputMethodInfo imm = mMethodMap.get(id);
2218 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002219 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002220 }
2221
satokd87c2592010-09-29 11:52:06 +09002222 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2223 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002224
satokd87c2592010-09-29 11:52:06 +09002225 if (enabled) {
2226 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2227 if (pair.first.equals(id)) {
2228 // We are enabling this input method, but it is already enabled.
2229 // Nothing to do. The previous state was enabled.
2230 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002231 }
2232 }
satokd87c2592010-09-29 11:52:06 +09002233 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2234 // Previous state was disabled.
2235 return false;
2236 } else {
2237 StringBuilder builder = new StringBuilder();
2238 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2239 builder, enabledInputMethodsList, id)) {
2240 // Disabled input method is currently selected, switch to another one.
2241 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2242 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002243 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2244 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2245 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002246 }
2247 // Previous state was enabled.
2248 return true;
2249 } else {
2250 // We are disabling the input method but it is already disabled.
2251 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002252 return false;
2253 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002254 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002255 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002256
satok57ffc002011-01-25 00:11:47 +09002257 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2258 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002259 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002260 }
2261
satok723a27e2010-11-11 14:58:11 +09002262 private void saveCurrentInputMethodAndSubtypeToHistory() {
2263 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2264 if (mCurrentSubtype != null) {
2265 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2266 }
satok57ffc002011-01-25 00:11:47 +09002267 if (canAddToLastInputMethod(mCurrentSubtype)) {
2268 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2269 }
satokab751aa2010-09-14 19:17:36 +09002270 }
2271
satok723a27e2010-11-11 14:58:11 +09002272 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2273 boolean setSubtypeOnly) {
2274 // Update the history of InputMethod and Subtype
2275 saveCurrentInputMethodAndSubtypeToHistory();
2276
2277 // Set Subtype here
2278 if (imi == null || subtypeId < 0) {
2279 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002280 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002281 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002282 if (subtypeId < imi.getSubtypeCount()) {
2283 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2284 mSettings.putSelectedSubtype(subtype.hashCode());
2285 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002286 } else {
2287 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2288 mCurrentSubtype = null;
2289 }
satokab751aa2010-09-14 19:17:36 +09002290 }
satok723a27e2010-11-11 14:58:11 +09002291
2292 if (!setSubtypeOnly) {
2293 // Set InputMethod here
2294 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2295 }
2296 }
2297
2298 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2299 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2300 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2301 // newDefaultIme is empty when there is no candidate for the selected IME.
2302 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2303 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2304 if (subtypeHashCode != null) {
2305 try {
2306 lastSubtypeId = getSubtypeIdFromHashCode(
2307 imi, Integer.valueOf(subtypeHashCode));
2308 } catch (NumberFormatException e) {
2309 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2310 }
2311 }
2312 }
2313 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002314 }
2315
2316 private int getSelectedInputMethodSubtypeId(String id) {
2317 InputMethodInfo imi = mMethodMap.get(id);
2318 if (imi == null) {
2319 return NOT_A_SUBTYPE_ID;
2320 }
satokab751aa2010-09-14 19:17:36 +09002321 int subtypeId;
2322 try {
2323 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2324 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2325 } catch (SettingNotFoundException e) {
2326 return NOT_A_SUBTYPE_ID;
2327 }
satok723a27e2010-11-11 14:58:11 +09002328 return getSubtypeIdFromHashCode(imi, subtypeId);
2329 }
2330
2331 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002332 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002333 final int subtypeCount = imi.getSubtypeCount();
2334 for (int i = 0; i < subtypeCount; ++i) {
2335 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002336 if (subtypeHashCode == ims.hashCode()) {
2337 return i;
2338 }
satokab751aa2010-09-14 19:17:36 +09002339 }
2340 }
2341 return NOT_A_SUBTYPE_ID;
2342 }
2343
satokdf31ae62011-01-15 06:19:44 +09002344 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2345 Resources res, List<InputMethodSubtype> subtypes) {
2346 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002347 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2348 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2349 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002350 final int N = subtypes.size();
2351 boolean containsKeyboardSubtype = false;
2352 for (int i = 0; i < N; ++i) {
2353 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002354 final String locale = subtype.getLocale();
2355 final String mode = subtype.getMode();
2356 // When system locale starts with subtype's locale, that subtype will be applicable
2357 // for system locale
2358 // For instance, it's clearly applicable for cases like system locale = en_US and
2359 // subtype = en, but it is not necessarily considered applicable for cases like system
2360 // locale = en and subtype = en_US.
2361 // We just call systemLocale.startsWith(locale) in this function because there is no
2362 // need to find applicable subtypes aggressively unlike
2363 // findLastResortApplicableSubtypeLocked.
2364 if (systemLocale.startsWith(locale)) {
2365 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2366 // If more applicable subtypes are contained, skip.
2367 if (applicableSubtype != null
2368 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2369 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002370 if (!containsKeyboardSubtype
2371 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2372 containsKeyboardSubtype = true;
2373 }
2374 }
2375 }
satok4a28bde2011-06-29 21:03:40 +09002376 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002377 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002378 if (!containsKeyboardSubtype) {
2379 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002380 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002381 if (lastResortKeyboardSubtype != null) {
2382 applicableSubtypes.add(lastResortKeyboardSubtype);
2383 }
2384 }
2385 return applicableSubtypes;
2386 }
2387
satok4e4569d2010-11-19 18:45:53 +09002388 /**
2389 * If there are no selected subtypes, tries finding the most applicable one according to the
2390 * given locale.
2391 * @param subtypes this function will search the most applicable subtype in subtypes
2392 * @param mode subtypes will be filtered by mode
2393 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002394 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2395 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002396 * @return the most applicable subtypeId
2397 */
satokdf31ae62011-01-15 06:19:44 +09002398 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2399 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002400 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002401 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002402 return null;
satok8fbb1e82010-11-02 23:15:58 +09002403 }
satok4e4569d2010-11-19 18:45:53 +09002404 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002405 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002406 }
satok8fbb1e82010-11-02 23:15:58 +09002407 final String language = locale.substring(0, 2);
2408 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002409 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002410 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002411 final int N = subtypes.size();
2412 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002413 InputMethodSubtype subtype = subtypes.get(i);
2414 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002415 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2416 // and all subtypes with all modes can be candidates.
2417 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002418 if (firstMatchedModeSubtype == null) {
2419 firstMatchedModeSubtype = subtype;
2420 }
satok9ef02832010-11-04 21:17:48 +09002421 if (locale.equals(subtypeLocale)) {
2422 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002423 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002424 break;
2425 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2426 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002427 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002428 partialMatchFound = true;
2429 }
satok8fbb1e82010-11-02 23:15:58 +09002430 }
2431 }
2432
satok7599a7fb2010-12-22 13:45:23 +09002433 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2434 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002435 }
2436
satok8fbb1e82010-11-02 23:15:58 +09002437 // The first subtype applicable to the system locale will be defined as the most applicable
2438 // subtype.
2439 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002440 if (applicableSubtype != null) {
2441 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2442 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2443 }
satok8fbb1e82010-11-02 23:15:58 +09002444 }
satokcd7cd292010-11-20 15:46:23 +09002445 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002446 }
2447
satok4e4569d2010-11-19 18:45:53 +09002448 // If there are no selected shortcuts, tries finding the most applicable ones.
2449 private Pair<InputMethodInfo, InputMethodSubtype>
2450 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2451 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2452 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002453 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002454 boolean foundInSystemIME = false;
2455
2456 // Search applicable subtype for each InputMethodInfo
2457 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002458 final String imiId = imi.getId();
2459 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2460 continue;
2461 }
satokcd7cd292010-11-20 15:46:23 +09002462 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002463 final List<InputMethodSubtype> enabledSubtypes =
2464 getEnabledInputMethodSubtypeList(imi, true);
2465 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002466 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002467 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002468 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002469 }
satokdf31ae62011-01-15 06:19:44 +09002470 // 2. Search by the system locale from enabledSubtypes.
2471 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002472 if (subtype == null) {
2473 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002474 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002475 }
satok7599a7fb2010-12-22 13:45:23 +09002476 // 4. Search by the current subtype's locale from all subtypes.
2477 if (subtype == null && mCurrentSubtype != null) {
2478 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002479 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002480 }
2481 // 5. Search by the system locale from all subtypes.
2482 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002483 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002484 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002485 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002486 }
satokcd7cd292010-11-20 15:46:23 +09002487 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002488 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002489 // The current input method is the most applicable IME.
2490 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002491 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002492 break;
satok7599a7fb2010-12-22 13:45:23 +09002493 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002494 // The system input method is 2nd applicable IME.
2495 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002496 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002497 if ((imi.getServiceInfo().applicationInfo.flags
2498 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2499 foundInSystemIME = true;
2500 }
satok4e4569d2010-11-19 18:45:53 +09002501 }
2502 }
2503 }
2504 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002505 if (mostApplicableIMI != null) {
2506 Slog.w(TAG, "Most applicable shortcut input method was:"
2507 + mostApplicableIMI.getId());
2508 if (mostApplicableSubtype != null) {
2509 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2510 + "," + mostApplicableSubtype.getMode() + ","
2511 + mostApplicableSubtype.getLocale());
2512 }
2513 }
satok4e4569d2010-11-19 18:45:53 +09002514 }
satokcd7cd292010-11-20 15:46:23 +09002515 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002516 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002517 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002518 } else {
2519 return null;
2520 }
2521 }
2522
satokab751aa2010-09-14 19:17:36 +09002523 /**
2524 * @return Return the current subtype of this input method.
2525 */
satok42c5a162011-05-26 16:46:14 +09002526 @Override
satokab751aa2010-09-14 19:17:36 +09002527 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002528 boolean subtypeIsSelected = false;
2529 try {
2530 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2531 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2532 } catch (SettingNotFoundException e) {
2533 }
satok3ef8b292010-11-23 06:06:29 +09002534 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002535 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002536 String lastInputMethodId = Settings.Secure.getString(
2537 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002538 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2539 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002540 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2541 if (imi != null) {
2542 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002543 // the most applicable subtype from explicitly or implicitly enabled
2544 // subtypes.
2545 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2546 getEnabledInputMethodSubtypeList(imi, true);
2547 // If there is only one explicitly or implicitly enabled subtype,
2548 // just returns it.
2549 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2550 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2551 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2552 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2553 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2554 SUBTYPE_MODE_KEYBOARD, null, true);
2555 if (mCurrentSubtype == null) {
2556 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2557 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2558 true);
2559 }
2560 }
satok4e4569d2010-11-19 18:45:53 +09002561 }
satokcd7cd292010-11-20 15:46:23 +09002562 } else {
satok3ef8b292010-11-23 06:06:29 +09002563 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002564 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002565 }
satok8fbb1e82010-11-02 23:15:58 +09002566 }
satok3ef8b292010-11-23 06:06:29 +09002567 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002568 }
satokab751aa2010-09-14 19:17:36 +09002569 }
2570
satokf3db1af2010-11-23 13:34:33 +09002571 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2572 InputMethodSubtype subtype) {
2573 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2574 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2575 } else {
2576 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2577 subtypes.add(subtype);
2578 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2579 }
2580 }
2581
satok4e4569d2010-11-19 18:45:53 +09002582 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002583 @Override
satok4e4569d2010-11-19 18:45:53 +09002584 public List getShortcutInputMethodsAndSubtypes() {
2585 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002586 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002587 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002588 // If there are no selected shortcut subtypes, the framework will try to find
2589 // the most applicable subtype from all subtypes whose mode is
2590 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002591 Pair<InputMethodInfo, InputMethodSubtype> info =
2592 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2593 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002594 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002595 ret.add(info.first);
2596 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002597 }
satok3da92232011-01-11 22:46:30 +09002598 return ret;
satokf3db1af2010-11-23 13:34:33 +09002599 }
satokf3db1af2010-11-23 13:34:33 +09002600 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2601 ret.add(imi);
2602 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2603 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002604 }
2605 }
satokf3db1af2010-11-23 13:34:33 +09002606 return ret;
satok4e4569d2010-11-19 18:45:53 +09002607 }
2608 }
2609
satok42c5a162011-05-26 16:46:14 +09002610 @Override
satokb66d2872010-11-10 01:04:04 +09002611 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2612 synchronized (mMethodMap) {
2613 if (subtype != null && mCurMethodId != null) {
2614 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2615 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2616 if (subtypeId != NOT_A_SUBTYPE_ID) {
2617 setInputMethodLocked(mCurMethodId, subtypeId);
2618 return true;
2619 }
2620 }
2621 return false;
2622 }
2623 }
2624
satokd87c2592010-09-29 11:52:06 +09002625 /**
2626 * Utility class for putting and getting settings for InputMethod
2627 * TODO: Move all putters and getters of settings to this class.
2628 */
2629 private static class InputMethodSettings {
2630 // The string for enabled input method is saved as follows:
2631 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2632 private static final char INPUT_METHOD_SEPARATER = ':';
2633 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002634 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002635 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2636
satok723a27e2010-11-11 14:58:11 +09002637 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002638 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2639
satokdf31ae62011-01-15 06:19:44 +09002640 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002641 private final ContentResolver mResolver;
2642 private final HashMap<String, InputMethodInfo> mMethodMap;
2643 private final ArrayList<InputMethodInfo> mMethodList;
2644
2645 private String mEnabledInputMethodsStrCache;
2646
2647 private static void buildEnabledInputMethodsSettingString(
2648 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2649 String id = pair.first;
2650 ArrayList<String> subtypes = pair.second;
2651 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002652 // Inputmethod and subtypes are saved in the settings as follows:
2653 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2654 for (String subtypeId: subtypes) {
2655 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002656 }
2657 }
2658
2659 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002660 Resources res, ContentResolver resolver,
2661 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2662 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002663 mResolver = resolver;
2664 mMethodMap = methodMap;
2665 mMethodList = methodList;
2666 }
2667
2668 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2669 return createEnabledInputMethodListLocked(
2670 getEnabledInputMethodsAndSubtypeListLocked());
2671 }
2672
satok7f35c8c2010-10-07 21:13:11 +09002673 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002674 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2675 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002676 getEnabledInputMethodsAndSubtypeListLocked());
2677 }
2678
satok67ddf9c2010-11-17 09:45:54 +09002679 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2680 InputMethodInfo imi) {
2681 List<Pair<String, ArrayList<String>>> imsList =
2682 getEnabledInputMethodsAndSubtypeListLocked();
2683 ArrayList<InputMethodSubtype> enabledSubtypes =
2684 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002685 if (imi != null) {
2686 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2687 InputMethodInfo info = mMethodMap.get(imsPair.first);
2688 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002689 final int subtypeCount = info.getSubtypeCount();
2690 for (int i = 0; i < subtypeCount; ++i) {
2691 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002692 for (String s: imsPair.second) {
2693 if (String.valueOf(ims.hashCode()).equals(s)) {
2694 enabledSubtypes.add(ims);
2695 }
satok67ddf9c2010-11-17 09:45:54 +09002696 }
2697 }
satok884ef9a2010-11-18 10:39:46 +09002698 break;
satok67ddf9c2010-11-17 09:45:54 +09002699 }
satok67ddf9c2010-11-17 09:45:54 +09002700 }
2701 }
2702 return enabledSubtypes;
2703 }
2704
satokd87c2592010-09-29 11:52:06 +09002705 // At the initial boot, the settings for input methods are not set,
2706 // so we need to enable IME in that case.
2707 public void enableAllIMEsIfThereIsNoEnabledIME() {
2708 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2709 StringBuilder sb = new StringBuilder();
2710 final int N = mMethodList.size();
2711 for (int i = 0; i < N; i++) {
2712 InputMethodInfo imi = mMethodList.get(i);
2713 Slog.i(TAG, "Adding: " + imi.getId());
2714 if (i > 0) sb.append(':');
2715 sb.append(imi.getId());
2716 }
2717 putEnabledInputMethodsStr(sb.toString());
2718 }
2719 }
2720
satokbb4aa062011-01-19 21:40:27 +09002721 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002722 ArrayList<Pair<String, ArrayList<String>>> imsList
2723 = new ArrayList<Pair<String, ArrayList<String>>>();
2724 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2725 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2726 return imsList;
2727 }
satok723a27e2010-11-11 14:58:11 +09002728 mInputMethodSplitter.setString(enabledInputMethodsStr);
2729 while (mInputMethodSplitter.hasNext()) {
2730 String nextImsStr = mInputMethodSplitter.next();
2731 mSubtypeSplitter.setString(nextImsStr);
2732 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002733 ArrayList<String> subtypeHashes = new ArrayList<String>();
2734 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002735 String imeId = mSubtypeSplitter.next();
2736 while (mSubtypeSplitter.hasNext()) {
2737 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002738 }
2739 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2740 }
2741 }
2742 return imsList;
2743 }
2744
2745 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2746 if (reloadInputMethodStr) {
2747 getEnabledInputMethodsStr();
2748 }
2749 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2750 // Add in the newly enabled input method.
2751 putEnabledInputMethodsStr(id);
2752 } else {
2753 putEnabledInputMethodsStr(
2754 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2755 }
2756 }
2757
2758 /**
2759 * Build and put a string of EnabledInputMethods with removing specified Id.
2760 * @return the specified id was removed or not.
2761 */
2762 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2763 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2764 boolean isRemoved = false;
2765 boolean needsAppendSeparator = false;
2766 for (Pair<String, ArrayList<String>> ims: imsList) {
2767 String curId = ims.first;
2768 if (curId.equals(id)) {
2769 // We are disabling this input method, and it is
2770 // currently enabled. Skip it to remove from the
2771 // new list.
2772 isRemoved = true;
2773 } else {
2774 if (needsAppendSeparator) {
2775 builder.append(INPUT_METHOD_SEPARATER);
2776 } else {
2777 needsAppendSeparator = true;
2778 }
2779 buildEnabledInputMethodsSettingString(builder, ims);
2780 }
2781 }
2782 if (isRemoved) {
2783 // Update the setting with the new list of input methods.
2784 putEnabledInputMethodsStr(builder.toString());
2785 }
2786 return isRemoved;
2787 }
2788
2789 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2790 List<Pair<String, ArrayList<String>>> imsList) {
2791 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2792 for (Pair<String, ArrayList<String>> ims: imsList) {
2793 InputMethodInfo info = mMethodMap.get(ims.first);
2794 if (info != null) {
2795 res.add(info);
2796 }
2797 }
2798 return res;
2799 }
2800
satok7f35c8c2010-10-07 21:13:11 +09002801 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002802 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002803 List<Pair<String, ArrayList<String>>> imsList) {
2804 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2805 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2806 for (Pair<String, ArrayList<String>> ims : imsList) {
2807 InputMethodInfo info = mMethodMap.get(ims.first);
2808 if (info != null) {
2809 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2810 }
2811 }
2812 return res;
2813 }
2814
satokd87c2592010-09-29 11:52:06 +09002815 private void putEnabledInputMethodsStr(String str) {
2816 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2817 mEnabledInputMethodsStrCache = str;
2818 }
2819
2820 private String getEnabledInputMethodsStr() {
2821 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2822 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002823 if (DEBUG) {
2824 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2825 }
satokd87c2592010-09-29 11:52:06 +09002826 return mEnabledInputMethodsStrCache;
2827 }
satok723a27e2010-11-11 14:58:11 +09002828
2829 private void saveSubtypeHistory(
2830 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2831 StringBuilder builder = new StringBuilder();
2832 boolean isImeAdded = false;
2833 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2834 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2835 newSubtypeId);
2836 isImeAdded = true;
2837 }
2838 for (Pair<String, String> ime: savedImes) {
2839 String imeId = ime.first;
2840 String subtypeId = ime.second;
2841 if (TextUtils.isEmpty(subtypeId)) {
2842 subtypeId = NOT_A_SUBTYPE_ID_STR;
2843 }
2844 if (isImeAdded) {
2845 builder.append(INPUT_METHOD_SEPARATER);
2846 } else {
2847 isImeAdded = true;
2848 }
2849 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2850 subtypeId);
2851 }
2852 // Remove the last INPUT_METHOD_SEPARATER
2853 putSubtypeHistoryStr(builder.toString());
2854 }
2855
2856 public void addSubtypeToHistory(String imeId, String subtypeId) {
2857 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2858 for (Pair<String, String> ime: subtypeHistory) {
2859 if (ime.first.equals(imeId)) {
2860 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002861 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002862 + ime.second);
2863 }
2864 // We should break here
2865 subtypeHistory.remove(ime);
2866 break;
2867 }
2868 }
satokbb4aa062011-01-19 21:40:27 +09002869 if (DEBUG) {
2870 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2871 }
satok723a27e2010-11-11 14:58:11 +09002872 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2873 }
2874
2875 private void putSubtypeHistoryStr(String str) {
2876 if (DEBUG) {
2877 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2878 }
2879 Settings.Secure.putString(
2880 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2881 }
2882
2883 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2884 // Gets the first one from the history
2885 return getLastSubtypeForInputMethodLockedInternal(null);
2886 }
2887
2888 public String getLastSubtypeForInputMethodLocked(String imeId) {
2889 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2890 if (ime != null) {
2891 return ime.second;
2892 } else {
2893 return null;
2894 }
2895 }
2896
2897 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2898 List<Pair<String, ArrayList<String>>> enabledImes =
2899 getEnabledInputMethodsAndSubtypeListLocked();
2900 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002901 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002902 final String imeInTheHistory = imeAndSubtype.first;
2903 // If imeId is empty, returns the first IME and subtype in the history
2904 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2905 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002906 final String subtypeHashCode =
2907 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2908 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002909 if (!TextUtils.isEmpty(subtypeHashCode)) {
2910 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002911 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002912 }
2913 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2914 }
2915 }
2916 }
2917 if (DEBUG) {
2918 Slog.d(TAG, "No enabled IME found in the history");
2919 }
2920 return null;
2921 }
2922
satokdf31ae62011-01-15 06:19:44 +09002923 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002924 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2925 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2926 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002927 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2928 if (explicitlyEnabledSubtypes.size() == 0) {
2929 // If there are no explicitly enabled subtypes, applicable subtypes are
2930 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002931 InputMethodInfo ime = mMethodMap.get(imeId);
2932 // If IME is enabled and no subtypes are enabled, applicable subtypes
2933 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002934 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002935 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002936 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002937 if (implicitlySelectedSubtypes != null) {
2938 final int N = implicitlySelectedSubtypes.size();
2939 for (int i = 0; i < N; ++i) {
2940 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2941 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2942 return subtypeHashCode;
2943 }
2944 }
2945 }
2946 }
2947 } else {
satokf6cafb62011-01-17 16:29:02 +09002948 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002949 if (s.equals(subtypeHashCode)) {
2950 // If both imeId and subtypeId are enabled, return subtypeId.
2951 return s;
2952 }
satok723a27e2010-11-11 14:58:11 +09002953 }
2954 }
2955 // If imeId was enabled but subtypeId was disabled.
2956 return NOT_A_SUBTYPE_ID_STR;
2957 }
2958 }
2959 // If both imeId and subtypeId are disabled, return null
2960 return null;
2961 }
2962
2963 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2964 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2965 final String subtypeHistoryStr = getSubtypeHistoryStr();
2966 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2967 return imsList;
2968 }
2969 mInputMethodSplitter.setString(subtypeHistoryStr);
2970 while (mInputMethodSplitter.hasNext()) {
2971 String nextImsStr = mInputMethodSplitter.next();
2972 mSubtypeSplitter.setString(nextImsStr);
2973 if (mSubtypeSplitter.hasNext()) {
2974 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2975 // The first element is ime id.
2976 String imeId = mSubtypeSplitter.next();
2977 while (mSubtypeSplitter.hasNext()) {
2978 subtypeId = mSubtypeSplitter.next();
2979 break;
2980 }
2981 imsList.add(new Pair<String, String>(imeId, subtypeId));
2982 }
2983 }
2984 return imsList;
2985 }
2986
2987 private String getSubtypeHistoryStr() {
2988 if (DEBUG) {
2989 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2990 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2991 }
2992 return Settings.Secure.getString(
2993 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2994 }
2995
2996 public void putSelectedInputMethod(String imeId) {
2997 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2998 }
2999
3000 public void putSelectedSubtype(int subtypeId) {
3001 Settings.Secure.putInt(
3002 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3003 }
satokd87c2592010-09-29 11:52:06 +09003004 }
3005
satoke7c6998e2011-06-03 17:57:59 +09003006 private static class InputMethodFileManager {
3007 private static final String SYSTEM_PATH = "system";
3008 private static final String INPUT_METHOD_PATH = "inputmethod";
3009 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3010 private static final String NODE_SUBTYPES = "subtypes";
3011 private static final String NODE_SUBTYPE = "subtype";
3012 private static final String NODE_IMI = "imi";
3013 private static final String ATTR_ID = "id";
3014 private static final String ATTR_LABEL = "label";
3015 private static final String ATTR_ICON = "icon";
3016 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3017 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3018 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3019 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3020 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3021 private final HashMap<String, InputMethodInfo> mMethodMap;
3022 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3023 new HashMap<String, List<InputMethodSubtype>>();
3024 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3025 if (methodMap == null) {
3026 throw new NullPointerException("methodMap is null");
3027 }
3028 mMethodMap = methodMap;
3029 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3030 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3031 if (!inputMethodDir.mkdirs()) {
3032 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3033 }
3034 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3035 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3036 if (!subtypeFile.exists()) {
3037 // If "subtypes.xml" doesn't exist, create a blank file.
3038 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3039 methodMap);
3040 } else {
3041 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3042 }
3043 }
3044
3045 private void deleteAllInputMethodSubtypes(String imiId) {
3046 synchronized (mMethodMap) {
3047 mSubtypesMap.remove(imiId);
3048 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3049 mMethodMap);
3050 }
3051 }
3052
3053 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003054 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003055 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003056 final HashSet<InputMethodSubtype> existingSubtypes =
3057 new HashSet<InputMethodSubtype>();
3058 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3059 existingSubtypes.add(imi.getSubtypeAt(i));
3060 }
3061
satoke7c6998e2011-06-03 17:57:59 +09003062 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3063 final int N = additionalSubtypes.length;
3064 for (int i = 0; i < N; ++i) {
3065 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003066 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003067 subtypes.add(subtype);
3068 }
3069 }
satok4a28bde2011-06-29 21:03:40 +09003070 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003071 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3072 mMethodMap);
3073 }
3074 }
3075
3076 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3077 synchronized (mMethodMap) {
3078 return mSubtypesMap;
3079 }
3080 }
3081
3082 private static void writeAdditionalInputMethodSubtypes(
3083 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3084 HashMap<String, InputMethodInfo> methodMap) {
3085 // Safety net for the case that this function is called before methodMap is set.
3086 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3087 FileOutputStream fos = null;
3088 try {
3089 fos = subtypesFile.startWrite();
3090 final XmlSerializer out = new FastXmlSerializer();
3091 out.setOutput(fos, "utf-8");
3092 out.startDocument(null, true);
3093 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3094 out.startTag(null, NODE_SUBTYPES);
3095 for (String imiId : allSubtypes.keySet()) {
3096 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3097 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3098 continue;
3099 }
3100 out.startTag(null, NODE_IMI);
3101 out.attribute(null, ATTR_ID, imiId);
3102 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3103 final int N = subtypesList.size();
3104 for (int i = 0; i < N; ++i) {
3105 final InputMethodSubtype subtype = subtypesList.get(i);
3106 out.startTag(null, NODE_SUBTYPE);
3107 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3108 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3109 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3110 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3111 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3112 out.attribute(null, ATTR_IS_AUXILIARY,
3113 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3114 out.endTag(null, NODE_SUBTYPE);
3115 }
3116 out.endTag(null, NODE_IMI);
3117 }
3118 out.endTag(null, NODE_SUBTYPES);
3119 out.endDocument();
3120 subtypesFile.finishWrite(fos);
3121 } catch (java.io.IOException e) {
3122 Slog.w(TAG, "Error writing subtypes", e);
3123 if (fos != null) {
3124 subtypesFile.failWrite(fos);
3125 }
3126 }
3127 }
3128
3129 private static void readAdditionalInputMethodSubtypes(
3130 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3131 if (allSubtypes == null || subtypesFile == null) return;
3132 allSubtypes.clear();
3133 FileInputStream fis = null;
3134 try {
3135 fis = subtypesFile.openRead();
3136 final XmlPullParser parser = Xml.newPullParser();
3137 parser.setInput(fis, null);
3138 int type = parser.getEventType();
3139 // Skip parsing until START_TAG
3140 while ((type = parser.next()) != XmlPullParser.START_TAG
3141 && type != XmlPullParser.END_DOCUMENT) {}
3142 String firstNodeName = parser.getName();
3143 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3144 throw new XmlPullParserException("Xml doesn't start with subtypes");
3145 }
3146 final int depth =parser.getDepth();
3147 String currentImiId = null;
3148 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3149 while (((type = parser.next()) != XmlPullParser.END_TAG
3150 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3151 if (type != XmlPullParser.START_TAG)
3152 continue;
3153 final String nodeName = parser.getName();
3154 if (NODE_IMI.equals(nodeName)) {
3155 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3156 if (TextUtils.isEmpty(currentImiId)) {
3157 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3158 continue;
3159 }
3160 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3161 allSubtypes.put(currentImiId, tempSubtypesArray);
3162 } else if (NODE_SUBTYPE.equals(nodeName)) {
3163 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3164 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3165 continue;
3166 }
3167 final int icon = Integer.valueOf(
3168 parser.getAttributeValue(null, ATTR_ICON));
3169 final int label = Integer.valueOf(
3170 parser.getAttributeValue(null, ATTR_LABEL));
3171 final String imeSubtypeLocale =
3172 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3173 final String imeSubtypeMode =
3174 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3175 final String imeSubtypeExtraValue =
3176 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003177 final boolean isAuxiliary = "1".equals(String.valueOf(
3178 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003179 final InputMethodSubtype subtype =
3180 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3181 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3182 tempSubtypesArray.add(subtype);
3183 }
3184 }
3185 } catch (XmlPullParserException e) {
3186 Slog.w(TAG, "Error reading subtypes: " + e);
3187 return;
3188 } catch (java.io.IOException e) {
3189 Slog.w(TAG, "Error reading subtypes: " + e);
3190 return;
3191 } catch (NumberFormatException e) {
3192 Slog.w(TAG, "Error reading subtypes: " + e);
3193 return;
3194 } finally {
3195 if (fis != null) {
3196 try {
3197 fis.close();
3198 } catch (java.io.IOException e1) {
3199 Slog.w(TAG, "Failed to close.");
3200 }
3201 }
3202 }
3203 }
3204 }
3205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 @Override
3209 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3210 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3211 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3214 + Binder.getCallingPid()
3215 + ", uid=" + Binder.getCallingUid());
3216 return;
3217 }
3218
3219 IInputMethod method;
3220 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 synchronized (mMethodMap) {
3225 p.println("Current Input Method Manager state:");
3226 int N = mMethodList.size();
3227 p.println(" Input Methods:");
3228 for (int i=0; i<N; i++) {
3229 InputMethodInfo info = mMethodList.get(i);
3230 p.println(" InputMethod #" + i + ":");
3231 info.dump(p, " ");
3232 }
3233 p.println(" Clients:");
3234 for (ClientState ci : mClients.values()) {
3235 p.println(" Client " + ci + ":");
3236 p.println(" client=" + ci.client);
3237 p.println(" inputContext=" + ci.inputContext);
3238 p.println(" sessionRequested=" + ci.sessionRequested);
3239 p.println(" curSession=" + ci.curSession);
3240 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003241 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003243 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3244 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3246 + " mBoundToMethod=" + mBoundToMethod);
3247 p.println(" mCurToken=" + mCurToken);
3248 p.println(" mCurIntent=" + mCurIntent);
3249 method = mCurMethod;
3250 p.println(" mCurMethod=" + mCurMethod);
3251 p.println(" mEnabledSession=" + mEnabledSession);
3252 p.println(" mShowRequested=" + mShowRequested
3253 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3254 + " mShowForced=" + mShowForced
3255 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003256 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003258
Jeff Brownb88102f2010-09-08 11:49:43 -07003259 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 pw.flush();
3262 try {
3263 client.client.asBinder().dump(fd, args);
3264 } catch (RemoteException e) {
3265 p.println("Input method client dead: " + e);
3266 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003267 } else {
3268 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003270
Jeff Brownb88102f2010-09-08 11:49:43 -07003271 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 pw.flush();
3274 try {
3275 method.asBinder().dump(fd, args);
3276 } catch (RemoteException e) {
3277 p.println("Input method service dead: " + e);
3278 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003279 } else {
3280 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282 }
3283}