blob: 8d25d50a657c988f1e5eb291ce54be11ad3d58a4 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 static final int MSG_UNBIND_INPUT = 1000;
104 static final int MSG_BIND_INPUT = 1010;
105 static final int MSG_SHOW_SOFT_INPUT = 1020;
106 static final int MSG_HIDE_SOFT_INPUT = 1030;
107 static final int MSG_ATTACH_TOKEN = 1040;
108 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 static final int MSG_START_INPUT = 2000;
111 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static final int MSG_UNBIND_METHOD = 3000;
114 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
satokab751aa2010-09-14 19:17:36 +0900118 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900119 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900120 // If IME doesn't support the system locale, the default subtype will be the first defined one.
121 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 final Context mContext;
124 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900125 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700127 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final IWindowManager mIWindowManager;
129 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 // All known input methods. mMethodMap also serves as the global
134 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900135 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
136 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 class SessionState {
139 final ClientState client;
140 final IInputMethod method;
141 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 @Override
144 public String toString() {
145 return "SessionState{uid " + client.uid + " pid " + client.pid
146 + " method " + Integer.toHexString(
147 System.identityHashCode(method))
148 + " session " + Integer.toHexString(
149 System.identityHashCode(session))
150 + "}";
151 }
152
153 SessionState(ClientState _client, IInputMethod _method,
154 IInputMethodSession _session) {
155 client = _client;
156 method = _method;
157 session = _session;
158 }
159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 class ClientState {
162 final IInputMethodClient client;
163 final IInputContext inputContext;
164 final int uid;
165 final int pid;
166 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 boolean sessionRequested;
169 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 @Override
172 public String toString() {
173 return "ClientState{" + Integer.toHexString(
174 System.identityHashCode(this)) + " uid " + uid
175 + " pid " + pid + "}";
176 }
177
178 ClientState(IInputMethodClient _client, IInputContext _inputContext,
179 int _uid, int _pid) {
180 client = _client;
181 inputContext = _inputContext;
182 uid = _uid;
183 pid = _pid;
184 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
185 }
186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HashMap<IBinder, ClientState> mClients
189 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700192 * Set once the system is ready to run third party code.
193 */
194 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Id of the currently selected input method.
198 */
199 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * The current binding sequence number, incremented every time there is
203 * a new bind performed.
204 */
205 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
208 * The client that is currently bound to an input method.
209 */
210 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700213 * The last window token that gained focus.
214 */
215 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * The input context last provided by the current client.
219 */
220 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
223 * The attributes last provided by the current client.
224 */
225 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 /**
228 * The input method ID of the input method service that we are currently
229 * connected to or in the process of connecting to.
230 */
231 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
satokab751aa2010-09-14 19:17:36 +0900234 * The current subtype of the current input method.
235 */
236 private InputMethodSubtype mCurrentSubtype;
237
238
239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * Set to true if our ServiceConnection is currently actively bound to
241 * a service (whether or not we have gotten its IBinder back yet).
242 */
243 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Set if the client has asked for the input method to be shown.
247 */
248 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Set if we were explicitly told to show the input method.
252 */
253 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * Set if we were forced to be shown.
257 */
258 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Set if we last told the input method to show itself.
262 */
263 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * The Intent used to connect to the current input method.
267 */
268 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The token we have made for the currently active input method, to
272 * identify it in the future.
273 */
274 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * If non-null, this is the input method service we are currently connected
278 * to.
279 */
280 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Time that we last initiated a bind to the input method, to determine
284 * if we should try to disconnect and reconnect to it.
285 */
286 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Have we called mCurMethod.bindInput()?
290 */
291 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Currently enabled session. Only touched by service thread, not
295 * protected by a lock.
296 */
297 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * True if the screen is on. The value is true initially.
301 */
302 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 AlertDialog.Builder mDialogBuilder;
305 AlertDialog mSwitchingDialog;
306 InputMethodInfo[] mIms;
307 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900308 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 class SettingsObserver extends ContentObserver {
311 SettingsObserver(Handler handler) {
312 super(handler);
313 ContentResolver resolver = mContext.getContentResolver();
314 resolver.registerContentObserver(Settings.Secure.getUriFor(
315 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900316 resolver.registerContentObserver(Settings.Secure.getUriFor(
317 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 @Override public void onChange(boolean selfChange) {
321 synchronized (mMethodMap) {
322 updateFromSettingsLocked();
323 }
324 }
325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
328 @Override
329 public void onReceive(Context context, Intent intent) {
330 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
331 mScreenOn = true;
332 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
333 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700334 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
335 hideInputMethodMenu();
336 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800338 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340
341 // Inform the current client of the change in active status
342 try {
343 if (mCurClient != null && mCurClient.client != null) {
344 mCurClient.client.setActive(mScreenOn);
345 }
346 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800347 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 + mCurClient.pid + " uid " + mCurClient.uid);
349 }
350 }
351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800353 class MyPackageMonitor extends PackageMonitor {
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800356 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800358 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
360 final int N = mMethodList.size();
361 if (curInputMethodId != null) {
362 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 InputMethodInfo imi = mMethodList.get(i);
364 if (imi.getId().equals(curInputMethodId)) {
365 for (String pkg : packages) {
366 if (imi.getPackageName().equals(pkg)) {
367 if (!doit) {
368 return true;
369 }
satok723a27e2010-11-11 14:58:11 +0900370 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 chooseNewDefaultIMELocked();
372 return true;
373 }
374 }
375 }
376 }
377 }
378 }
379 return false;
380 }
381
382 @Override
383 public void onSomePackagesChanged() {
384 synchronized (mMethodMap) {
385 InputMethodInfo curIm = null;
386 String curInputMethodId = Settings.Secure.getString(mContext
387 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
388 final int N = mMethodList.size();
389 if (curInputMethodId != null) {
390 for (int i=0; i<N; i++) {
391 InputMethodInfo imi = mMethodList.get(i);
392 if (imi.getId().equals(curInputMethodId)) {
393 curIm = imi;
394 }
395 int change = isPackageDisappearing(imi.getPackageName());
396 if (change == PACKAGE_TEMPORARY_CHANGE
397 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800398 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 + imi.getComponent());
400 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 }
403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800405 buildInputMethodListLocked(mMethodList, mMethodMap);
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800409 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 int change = isPackageDisappearing(curIm.getPackageName());
411 if (change == PACKAGE_TEMPORARY_CHANGE
412 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800413 ServiceInfo si = null;
414 try {
415 si = mContext.getPackageManager().getServiceInfo(
416 curIm.getComponent(), 0);
417 } catch (PackageManager.NameNotFoundException ex) {
418 }
419 if (si == null) {
420 // Uh oh, current input method is no longer around!
421 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800422 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800424 changed = true;
425 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800426 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900427 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800428 }
429 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800430 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 }
satokab751aa2010-09-14 19:17:36 +0900432
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 if (curIm == null) {
434 // We currently don't have a default input method... is
435 // one now available?
436 changed = chooseNewDefaultIMELocked();
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 if (changed) {
440 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
443 }
444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 class MethodCallback extends IInputMethodCallback.Stub {
447 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 MethodCallback(IInputMethod method) {
450 mMethod = method;
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public void finishedEvent(int seq, boolean handled) throws RemoteException {
454 }
455
456 public void sessionCreated(IInputMethodSession session) throws RemoteException {
457 onSessionCreated(mMethod, session);
458 }
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Joe Onorato089de882010-04-12 08:18:45 -0700461 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 mContext = context;
463 mHandler = new Handler(this);
464 mIWindowManager = IWindowManager.Stub.asInterface(
465 ServiceManager.getService(Context.WINDOW_SERVICE));
466 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
467 public void executeMessage(Message msg) {
468 handleMessage(msg);
469 }
470 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 IntentFilter screenOnOffFilt = new IntentFilter();
475 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
476 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700477 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
satok913a8922010-08-26 21:53:41 +0900480 mStatusBar = statusBar;
481 statusBar.setIconVisibility("ime", false);
482
satokd87c2592010-09-29 11:52:06 +0900483 // mSettings should be created before buildInputMethodListLocked
484 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900486 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
satokd87c2592010-09-29 11:52:06 +0900488 if (TextUtils.isEmpty(Settings.Secure.getString(
489 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900491 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
493 try {
satokd87c2592010-09-29 11:52:06 +0900494 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 imi.getPackageName(), 0).getResources();
496 if (res.getBoolean(imi.getIsDefaultResourceId())) {
497 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800498 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500 } catch (PackageManager.NameNotFoundException ex) {
501 } catch (Resources.NotFoundException ex) {
502 }
503 }
504 }
satokd87c2592010-09-29 11:52:06 +0900505 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900510 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mSettingsObserver = new SettingsObserver(mHandler);
515 updateFromSettingsLocked();
516 }
517
518 @Override
519 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
520 throws RemoteException {
521 try {
522 return super.onTransact(code, data, reply, flags);
523 } catch (RuntimeException e) {
524 // The input method manager only throws security exceptions, so let's
525 // log all others.
526 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 throw e;
530 }
531 }
532
533 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700534 synchronized (mMethodMap) {
535 if (!mSystemReady) {
536 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700537 try {
538 startInputInnerLocked();
539 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800540 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700541 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700542 }
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public List<InputMethodInfo> getInputMethodList() {
547 synchronized (mMethodMap) {
548 return new ArrayList<InputMethodInfo>(mMethodList);
549 }
550 }
551
552 public List<InputMethodInfo> getEnabledInputMethodList() {
553 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900554 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557
satok67ddf9c2010-11-17 09:45:54 +0900558 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
559 synchronized (mMethodMap) {
560 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
561 }
562 }
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 public void addClient(IInputMethodClient client,
565 IInputContext inputContext, int uid, int pid) {
566 synchronized (mMethodMap) {
567 mClients.put(client.asBinder(), new ClientState(client,
568 inputContext, uid, pid));
569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 public void removeClient(IInputMethodClient client) {
573 synchronized (mMethodMap) {
574 mClients.remove(client.asBinder());
575 }
576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 void executeOrSendMessage(IInterface target, Message msg) {
579 if (target.asBinder() instanceof Binder) {
580 mCaller.sendMessage(msg);
581 } else {
582 handleMessage(msg);
583 msg.recycle();
584 }
585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800586
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700587 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 + mCurClient.client.asBinder());
591 if (mBoundToMethod) {
592 mBoundToMethod = false;
593 if (mCurMethod != null) {
594 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
595 MSG_UNBIND_INPUT, mCurMethod));
596 }
597 }
598 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
599 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
600 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 // Call setActive(false) on the old client
603 try {
604 mCurClient.client.setActive(false);
605 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800606 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 + mCurClient.pid + " uid " + mCurClient.uid);
608 }
609 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800610
The Android Open Source Project10592532009-03-18 17:39:46 -0700611 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 private int getImeShowFlags() {
616 int flags = 0;
617 if (mShowForced) {
618 flags |= InputMethod.SHOW_FORCED
619 | InputMethod.SHOW_EXPLICIT;
620 } else if (mShowExplicitlyRequested) {
621 flags |= InputMethod.SHOW_EXPLICIT;
622 }
623 return flags;
624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 private int getAppShowFlags() {
627 int flags = 0;
628 if (mShowForced) {
629 flags |= InputMethodManager.SHOW_FORCED;
630 } else if (!mShowExplicitlyRequested) {
631 flags |= InputMethodManager.SHOW_IMPLICIT;
632 }
633 return flags;
634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
637 if (!mBoundToMethod) {
638 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
639 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
640 mBoundToMethod = true;
641 }
642 final SessionState session = mCurClient.curSession;
643 if (initial) {
644 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
645 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
646 } else {
647 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
648 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
649 }
650 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800651 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800652 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654 return needResult
655 ? new InputBindResult(session.session, mCurId, mCurSeq)
656 : null;
657 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 InputBindResult startInputLocked(IInputMethodClient client,
660 IInputContext inputContext, EditorInfo attribute,
661 boolean initial, boolean needResult) {
662 // If no method is currently selected, do nothing.
663 if (mCurMethodId == null) {
664 return mNoBinding;
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 ClientState cs = mClients.get(client.asBinder());
668 if (cs == null) {
669 throw new IllegalArgumentException("unknown client "
670 + client.asBinder());
671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 try {
674 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
675 // Check with the window manager to make sure this client actually
676 // has a window with focus. If not, reject. This is thread safe
677 // because if the focus changes some time before or after, the
678 // next client receiving focus that has any interest in input will
679 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800680 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
682 return null;
683 }
684 } catch (RemoteException e) {
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 if (mCurClient != cs) {
688 // If the client is changing, we need to switch over to the new
689 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700690 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 + cs.client.asBinder());
693
694 // If the screen is on, inform the new client it is active
695 if (mScreenOn) {
696 try {
697 cs.client.setActive(mScreenOn);
698 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 + cs.pid + " uid " + cs.uid);
701 }
702 }
703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 // Bump up the sequence for this client and attach it.
706 mCurSeq++;
707 if (mCurSeq <= 0) mCurSeq = 1;
708 mCurClient = cs;
709 mCurInputContext = inputContext;
710 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 // Check if the input method is changing.
713 if (mCurId != null && mCurId.equals(mCurMethodId)) {
714 if (cs.curSession != null) {
715 // Fast case: if we are already connected to the input method,
716 // then just return it.
717 return attachNewInputLocked(initial, needResult);
718 }
719 if (mHaveConnection) {
720 if (mCurMethod != null) {
721 if (!cs.sessionRequested) {
722 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800723 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
725 MSG_CREATE_SESSION, mCurMethod,
726 new MethodCallback(mCurMethod)));
727 }
728 // Return to client, and we will get back with it when
729 // we have had a session made for it.
730 return new InputBindResult(null, mCurId, mCurSeq);
731 } else if (SystemClock.uptimeMillis()
732 < (mLastBindTime+TIME_TO_RECONNECT)) {
733 // In this case we have connected to the service, but
734 // don't yet have its interface. If it hasn't been too
735 // long since we did the connection, we'll return to
736 // the client and wait to get the service interface so
737 // we can report back. If it has been too long, we want
738 // to fall through so we can try a disconnect/reconnect
739 // to see if we can get back in touch with the service.
740 return new InputBindResult(null, mCurId, mCurSeq);
741 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
743 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700748 return startInputInnerLocked();
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700751 InputBindResult startInputInnerLocked() {
752 if (mCurMethodId == null) {
753 return mNoBinding;
754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700756 if (!mSystemReady) {
757 // If the system is not yet ready, we shouldn't be running third
758 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700759 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 InputMethodInfo info = mMethodMap.get(mCurMethodId);
763 if (info == null) {
764 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700767 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
770 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700771 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
772 com.android.internal.R.string.input_method_binding_label);
773 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
774 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
776 mLastBindTime = SystemClock.uptimeMillis();
777 mHaveConnection = true;
778 mCurId = info.getId();
779 mCurToken = new Binder();
780 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800781 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 mIWindowManager.addWindowToken(mCurToken,
783 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
784 } catch (RemoteException e) {
785 }
786 return new InputBindResult(null, mCurId, mCurSeq);
787 } else {
788 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800789 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 + mCurIntent);
791 }
792 return null;
793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 public InputBindResult startInput(IInputMethodClient client,
796 IInputContext inputContext, EditorInfo attribute,
797 boolean initial, boolean needResult) {
798 synchronized (mMethodMap) {
799 final long ident = Binder.clearCallingIdentity();
800 try {
801 return startInputLocked(client, inputContext, attribute,
802 initial, needResult);
803 } finally {
804 Binder.restoreCallingIdentity(ident);
805 }
806 }
807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 public void finishInput(IInputMethodClient client) {
810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public void onServiceConnected(ComponentName name, IBinder service) {
813 synchronized (mMethodMap) {
814 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
815 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700816 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800817 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700818 unbindCurrentMethodLocked(false);
819 return;
820 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800821 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700822 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
823 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800825 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700826 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700828 MSG_CREATE_SESSION, mCurMethod,
829 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831 }
832 }
833 }
834
835 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
836 synchronized (mMethodMap) {
837 if (mCurMethod != null && method != null
838 && mCurMethod.asBinder() == method.asBinder()) {
839 if (mCurClient != null) {
840 mCurClient.curSession = new SessionState(mCurClient,
841 method, session);
842 mCurClient.sessionRequested = false;
843 InputBindResult res = attachNewInputLocked(true, true);
844 if (res.method != null) {
845 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
846 MSG_BIND_METHOD, mCurClient.client, res));
847 }
848 }
849 }
850 }
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700853 void unbindCurrentMethodLocked(boolean reportToClient) {
854 if (mHaveConnection) {
855 mContext.unbindService(this);
856 mHaveConnection = false;
857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700859 if (mCurToken != null) {
860 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800861 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700862 mIWindowManager.removeWindowToken(mCurToken);
863 } catch (RemoteException e) {
864 }
865 mCurToken = null;
866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800867
The Android Open Source Project10592532009-03-18 17:39:46 -0700868 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700869 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700871 if (reportToClient && mCurClient != null) {
872 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
873 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
874 }
875 }
876
Devin Taylor0c33ed22010-02-23 13:26:46 -0600877 private void finishSession(SessionState sessionState) {
878 if (sessionState != null && sessionState.session != null) {
879 try {
880 sessionState.session.finishSession();
881 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700882 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600883 }
884 }
885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700887 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 if (mCurMethod != null) {
889 for (ClientState cs : mClients.values()) {
890 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600891 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 cs.curSession = null;
893 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600894
895 finishSession(mEnabledSession);
896 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 mCurMethod = null;
898 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700899 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 public void onServiceDisconnected(ComponentName name) {
903 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800904 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 + " mCurIntent=" + mCurIntent);
906 if (mCurMethod != null && mCurIntent != null
907 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700908 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 // We consider this to be a new bind attempt, since the system
910 // should now try to restart the service for us.
911 mLastBindTime = SystemClock.uptimeMillis();
912 mShowRequested = mInputShown;
913 mInputShown = false;
914 if (mCurClient != null) {
915 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
916 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
917 }
918 }
919 }
920 }
921
922 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700923 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 long ident = Binder.clearCallingIdentity();
925 try {
926 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700927 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 return;
929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 synchronized (mMethodMap) {
932 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800933 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700934 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800936 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700937 mStatusBar.setIcon("ime", packageName, iconId, 0);
938 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 }
941 } finally {
942 Binder.restoreCallingIdentity(ident);
943 }
944 }
945
satok06487a52010-10-29 11:37:18 +0900946 public void setIMEButtonVisible(IBinder token, boolean visible) {
947 int uid = Binder.getCallingUid();
948 long ident = Binder.clearCallingIdentity();
949 try {
950 if (token == null || mCurToken != token) {
951 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
952 return;
953 }
954
955 synchronized (mMethodMap) {
956 mStatusBar.setIMEButtonVisible(visible);
957 }
958 } finally {
959 Binder.restoreCallingIdentity(ident);
960 }
961 }
962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700964 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
965 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
966 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
967 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900969 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900970 // There is no input method selected, try to choose new applicable input method.
971 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
972 id = Settings.Secure.getString(mContext.getContentResolver(),
973 Settings.Secure.DEFAULT_INPUT_METHOD);
974 }
975 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 try {
satokab751aa2010-09-14 19:17:36 +0900977 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800979 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700980 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700981 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 } else {
984 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700985 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
satokab751aa2010-09-14 19:17:36 +0900990 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 InputMethodInfo info = mMethodMap.get(id);
992 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900993 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +0900997 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
998 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
999 InputMethodSubtype subtype = subtypes.get(subtypeId);
satokab751aa2010-09-14 19:17:36 +09001000 if (subtype != mCurrentSubtype) {
1001 synchronized (mMethodMap) {
1002 if (mCurMethod != null) {
1003 try {
satok723a27e2010-11-11 14:58:11 +09001004 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +09001005 if (mInputShown) {
1006 // If mInputShown is false, there is no IME button on the
1007 // system bar.
1008 // Thus there is no need to make it invisible explicitly.
1009 mStatusBar.setIMEButtonVisible(true);
1010 }
satokab751aa2010-09-14 19:17:36 +09001011 mCurMethod.changeInputMethodSubtype(subtype);
1012 } catch (RemoteException e) {
1013 return;
1014 }
1015 }
1016 }
1017 }
1018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 return;
1020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 final long ident = Binder.clearCallingIdentity();
1023 try {
satokab751aa2010-09-14 19:17:36 +09001024 // Set a subtype to this input method.
1025 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001026 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1027 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1028 // because mCurMethodId is stored as a history in
1029 // setSelectedInputMethodAndSubtypeLocked().
1030 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031
1032 if (ActivityManagerNative.isSystemReady()) {
1033 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001034 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 intent.putExtra("input_method_id", id);
1036 mContext.sendBroadcast(intent);
1037 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001038 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 } finally {
1040 Binder.restoreCallingIdentity(ident);
1041 }
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project4df24232009-03-05 14:34:35 -08001044 public boolean showSoftInput(IInputMethodClient client, int flags,
1045 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001046 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 long ident = Binder.clearCallingIdentity();
1048 try {
1049 synchronized (mMethodMap) {
1050 if (mCurClient == null || client == null
1051 || mCurClient.client.asBinder() != client.asBinder()) {
1052 try {
1053 // We need to check if this is the current client with
1054 // focus in the window manager, to allow this call to
1055 // be made before input is started in it.
1056 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001057 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001058 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001061 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
Joe Onorato8a9b2202010-02-26 18:56:32 -08001065 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001066 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068 } finally {
1069 Binder.restoreCallingIdentity(ident);
1070 }
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project4df24232009-03-05 14:34:35 -08001073 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 mShowRequested = true;
1075 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1076 mShowExplicitlyRequested = true;
1077 }
1078 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1079 mShowExplicitlyRequested = true;
1080 mShowForced = true;
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
Dianne Hackborncc278702009-09-02 23:07:23 -07001083 if (!mSystemReady) {
1084 return false;
1085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001086
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001089 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1090 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1091 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001093 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 } else if (mHaveConnection && SystemClock.uptimeMillis()
1095 < (mLastBindTime+TIME_TO_RECONNECT)) {
1096 // The client has asked to have the input method shown, but
1097 // we have been sitting here too long with a connection to the
1098 // service and no interface received, so let's disconnect/connect
1099 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 SystemClock.uptimeMillis()-mLastBindTime,1);
1102 mContext.unbindService(this);
1103 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project4df24232009-03-05 14:34:35 -08001106 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
The Android Open Source Project4df24232009-03-05 14:34:35 -08001109 public boolean hideSoftInput(IInputMethodClient client, int flags,
1110 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001111 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 long ident = Binder.clearCallingIdentity();
1113 try {
1114 synchronized (mMethodMap) {
1115 if (mCurClient == null || client == null
1116 || mCurClient.client.asBinder() != client.asBinder()) {
1117 try {
1118 // We need to check if this is the current client with
1119 // focus in the window manager, to allow this call to
1120 // be made before input is started in it.
1121 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001122 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1123 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001124 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001127 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
Joe Onorato8a9b2202010-02-26 18:56:32 -08001131 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 } finally {
1135 Binder.restoreCallingIdentity(ident);
1136 }
1137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001138
The Android Open Source Project4df24232009-03-05 14:34:35 -08001139 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1141 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001142 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001144 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001147 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001149 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1154 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1155 res = true;
1156 } else {
1157 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 mInputShown = false;
1160 mShowRequested = false;
1161 mShowExplicitlyRequested = false;
1162 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001166 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1168 boolean first, int windowFlags) {
1169 long ident = Binder.clearCallingIdentity();
1170 try {
1171 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 + " viewHasFocus=" + viewHasFocus
1174 + " isTextEditor=" + isTextEditor
1175 + " softInputMode=#" + Integer.toHexString(softInputMode)
1176 + " first=" + first + " flags=#"
1177 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 if (mCurClient == null || client == null
1180 || mCurClient.client.asBinder() != client.asBinder()) {
1181 try {
1182 // We need to check if this is the current client with
1183 // focus in the window manager, to allow this call to
1184 // be made before input is started in it.
1185 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001186 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 return;
1188 }
1189 } catch (RemoteException e) {
1190 }
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001193 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001194 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001195 return;
1196 }
1197 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1200 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1201 if (!isTextEditor || (softInputMode &
1202 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1203 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1204 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1205 // There is no focus view, and this window will
1206 // be behind any soft input window, so hide the
1207 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001209 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 } else if (isTextEditor && (softInputMode &
1212 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1213 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1214 && (softInputMode &
1215 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1216 // There is a focus view, and we are navigating forward
1217 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001218 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001219 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 break;
1222 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1223 // Do nothing.
1224 break;
1225 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1226 if ((softInputMode &
1227 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001229 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231 break;
1232 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001233 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001234 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 break;
1236 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1237 if ((softInputMode &
1238 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001240 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 break;
1243 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001244 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001245 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 break;
1247 }
1248 }
1249 } finally {
1250 Binder.restoreCallingIdentity(ident);
1251 }
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1255 synchronized (mMethodMap) {
1256 if (mCurClient == null || client == null
1257 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001258 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001259 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261
1262 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1263 }
1264 }
1265
satokab751aa2010-09-14 19:17:36 +09001266 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1267 synchronized (mMethodMap) {
1268 if (mCurClient == null || client == null
1269 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001270 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001271 }
1272
1273 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1274 }
1275 }
1276
satok47a44912010-10-06 16:03:58 +09001277 public void showInputMethodAndSubtypeEnablerFromClient(
1278 IInputMethodClient client, String topId) {
1279 // TODO: Handle topId for setting the top position of the list activity
1280 synchronized (mMethodMap) {
1281 if (mCurClient == null || client == null
1282 || mCurClient.client.asBinder() != client.asBinder()) {
1283 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1284 }
1285
1286 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1287 }
1288 }
1289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001291 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1292 }
1293
satok735cf382010-11-11 20:40:09 +09001294 public boolean switchToLastInputMethod(IBinder token) {
1295 synchronized (mMethodMap) {
1296 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1297 if (lastIme != null) {
1298 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1299 if (imi != null) {
1300 setInputMethodWithSubtype(token, lastIme.first, getSubtypeIdFromHashCode(
1301 imi, Integer.valueOf(lastIme.second)));
1302 return true;
1303 }
1304 }
1305 return false;
1306 }
1307 }
1308
satokab751aa2010-09-14 19:17:36 +09001309 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 synchronized (mMethodMap) {
1311 if (token == null) {
1312 if (mContext.checkCallingOrSelfPermission(
1313 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1314 != PackageManager.PERMISSION_GRANTED) {
1315 throw new SecurityException(
1316 "Using null token requires permission "
1317 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1318 }
1319 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001320 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1321 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 return;
1323 }
1324
1325 long ident = Binder.clearCallingIdentity();
1326 try {
satokab751aa2010-09-14 19:17:36 +09001327 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 } finally {
1329 Binder.restoreCallingIdentity(ident);
1330 }
1331 }
1332 }
1333
1334 public void hideMySoftInput(IBinder token, int flags) {
1335 synchronized (mMethodMap) {
1336 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001337 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1338 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 return;
1340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 long ident = Binder.clearCallingIdentity();
1342 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001343 hideCurrentInputLocked(flags, null);
1344 } finally {
1345 Binder.restoreCallingIdentity(ident);
1346 }
1347 }
1348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
The Android Open Source Project4df24232009-03-05 14:34:35 -08001350 public void showMySoftInput(IBinder token, int flags) {
1351 synchronized (mMethodMap) {
1352 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001353 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1354 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001355 return;
1356 }
1357 long ident = Binder.clearCallingIdentity();
1358 try {
1359 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } finally {
1361 Binder.restoreCallingIdentity(ident);
1362 }
1363 }
1364 }
1365
1366 void setEnabledSessionInMainThread(SessionState session) {
1367 if (mEnabledSession != session) {
1368 if (mEnabledSession != null) {
1369 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 mEnabledSession.method.setSessionEnabled(
1372 mEnabledSession.session, false);
1373 } catch (RemoteException e) {
1374 }
1375 }
1376 mEnabledSession = session;
1377 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 session.method.setSessionEnabled(
1380 session.session, true);
1381 } catch (RemoteException e) {
1382 }
1383 }
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 public boolean handleMessage(Message msg) {
1387 HandlerCaller.SomeArgs args;
1388 switch (msg.what) {
1389 case MSG_SHOW_IM_PICKER:
1390 showInputMethodMenu();
1391 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
satokab751aa2010-09-14 19:17:36 +09001393 case MSG_SHOW_IM_SUBTYPE_PICKER:
1394 showInputMethodSubtypeMenu();
1395 return true;
1396
satok47a44912010-10-06 16:03:58 +09001397 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1398 showInputMethodAndSubtypeEnabler();
1399 return true;
1400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 case MSG_UNBIND_INPUT:
1404 try {
1405 ((IInputMethod)msg.obj).unbindInput();
1406 } catch (RemoteException e) {
1407 // There is nothing interesting about the method dying.
1408 }
1409 return true;
1410 case MSG_BIND_INPUT:
1411 args = (HandlerCaller.SomeArgs)msg.obj;
1412 try {
1413 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1414 } catch (RemoteException e) {
1415 }
1416 return true;
1417 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001418 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001420 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1421 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 } catch (RemoteException e) {
1423 }
1424 return true;
1425 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001426 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001428 ((IInputMethod)args.arg1).hideSoftInput(0,
1429 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 } catch (RemoteException e) {
1431 }
1432 return true;
1433 case MSG_ATTACH_TOKEN:
1434 args = (HandlerCaller.SomeArgs)msg.obj;
1435 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001436 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1438 } catch (RemoteException e) {
1439 }
1440 return true;
1441 case MSG_CREATE_SESSION:
1442 args = (HandlerCaller.SomeArgs)msg.obj;
1443 try {
1444 ((IInputMethod)args.arg1).createSession(
1445 (IInputMethodCallback)args.arg2);
1446 } catch (RemoteException e) {
1447 }
1448 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 case MSG_START_INPUT:
1452 args = (HandlerCaller.SomeArgs)msg.obj;
1453 try {
1454 SessionState session = (SessionState)args.arg1;
1455 setEnabledSessionInMainThread(session);
1456 session.method.startInput((IInputContext)args.arg2,
1457 (EditorInfo)args.arg3);
1458 } catch (RemoteException e) {
1459 }
1460 return true;
1461 case MSG_RESTART_INPUT:
1462 args = (HandlerCaller.SomeArgs)msg.obj;
1463 try {
1464 SessionState session = (SessionState)args.arg1;
1465 setEnabledSessionInMainThread(session);
1466 session.method.restartInput((IInputContext)args.arg2,
1467 (EditorInfo)args.arg3);
1468 } catch (RemoteException e) {
1469 }
1470 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 case MSG_UNBIND_METHOD:
1475 try {
1476 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1477 } catch (RemoteException e) {
1478 // There is nothing interesting about the last client dying.
1479 }
1480 return true;
1481 case MSG_BIND_METHOD:
1482 args = (HandlerCaller.SomeArgs)msg.obj;
1483 try {
1484 ((IInputMethodClient)args.arg1).onBindMethod(
1485 (InputBindResult)args.arg2);
1486 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001487 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489 return true;
1490 }
1491 return false;
1492 }
1493
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001494 private boolean isSystemIme(InputMethodInfo inputMethod) {
1495 return (inputMethod.getServiceInfo().applicationInfo.flags
1496 & ApplicationInfo.FLAG_SYSTEM) != 0;
1497 }
1498
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001499 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001500 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001501 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001502 // We'd prefer to fall back on a system IME, since that is safer.
1503 int i=enabled.size();
1504 while (i > 0) {
1505 i--;
1506 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1507 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1508 break;
1509 }
1510 }
satokab751aa2010-09-14 19:17:36 +09001511 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001512 if (DEBUG) {
1513 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1514 }
satok723a27e2010-11-11 14:58:11 +09001515 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001516 return true;
1517 }
1518
1519 return false;
1520 }
1521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1523 HashMap<String, InputMethodInfo> map) {
1524 list.clear();
1525 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001528 final Configuration config = mContext.getResources().getConfiguration();
1529 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1530 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1531 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1532 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 List<ResolveInfo> services = pm.queryIntentServices(
1535 new Intent(InputMethod.SERVICE_INTERFACE),
1536 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 for (int i = 0; i < services.size(); ++i) {
1539 ResolveInfo ri = services.get(i);
1540 ServiceInfo si = ri.serviceInfo;
1541 ComponentName compName = new ComponentName(si.packageName, si.name);
1542 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1543 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 + ": it does not require the permission "
1546 + android.Manifest.permission.BIND_INPUT_METHOD);
1547 continue;
1548 }
1549
Joe Onorato8a9b2202010-02-26 18:56:32 -08001550 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551
1552 try {
1553 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1554 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001555 final String id = p.getId();
1556 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557
Amith Yamasanie861ec12010-03-24 21:39:27 -07001558 // System IMEs are enabled by default, unless there's a hard keyboard
1559 // and the system IME was explicitly disabled
1560 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1561 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001562 }
1563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001565 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001569 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001571 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001574
1575 String defaultIme = Settings.Secure.getString(mContext
1576 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001577 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001578 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001579 updateFromSettingsLocked();
1580 }
1581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
satokab751aa2010-09-14 19:17:36 +09001586 private void showInputMethodMenu() {
1587 showInputMethodMenuInternal(false);
1588 }
1589
1590 private void showInputMethodSubtypeMenu() {
1591 showInputMethodMenuInternal(true);
1592 }
1593
satok47a44912010-10-06 16:03:58 +09001594 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001595 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001596 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001597 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1598 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001599 mContext.startActivity(intent);
1600 }
1601
satokab751aa2010-09-14 19:17:36 +09001602 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 String lastInputMethodId = Settings.Secure.getString(context
1610 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001611 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001612 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001613
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001614 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001615 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001616 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001617 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1618
1619 if (immis == null || immis.size() == 0) {
1620 return;
1621 }
1622
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001623 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001625 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001626
satokab751aa2010-09-14 19:17:36 +09001627 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1628 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001629
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001630 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001631 InputMethodInfo property = immis.get(i).first;
1632 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1633 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1634 for (String s : enabledSubtypeIds) {
1635 enabledSubtypeSet.add(s);
1636 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001637 if (property == null) {
1638 continue;
1639 }
satokab751aa2010-09-14 19:17:36 +09001640 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1641 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001642 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001643 for (int j = 0; j < subtypes.size(); ++j) {
1644 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001645 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1646 CharSequence title;
1647 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001648 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001649 if (nameResId != 0) {
1650 title = pm.getText(property.getPackageName(), nameResId,
1651 property.getServiceInfo().applicationInfo);
1652 } else {
1653 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001654 // TODO: Use more friendly Title and UI
1655 title = label + "," + (mode == null ? "" : mode) + ","
1656 + (language == null ? "" : language);
1657 }
1658 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001659 }
satokab751aa2010-09-14 19:17:36 +09001660 }
1661 } else {
1662 imMap.put(label,
1663 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1664 subtypeIds.add(0);
1665 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001666 }
satok913a8922010-08-26 21:53:41 +09001667
1668 N = imMap.size();
1669 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001670 mIms = new InputMethodInfo[N];
1671 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001672 int checkedItem = 0;
1673 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001674 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1675 mIms[i] = value.first;
1676 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001677 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001678 int subtypeId = mSubtypeIds[i];
1679 if ((subtypeId == NOT_A_SUBTYPE_ID)
1680 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1681 || (subtypeId == lastInputMethodSubtypeId)) {
1682 checkedItem = i;
1683 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
satokab751aa2010-09-14 19:17:36 +09001686
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001687 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1688 public void onClick(DialogInterface dialog, int which) {
1689 hideInputMethodMenu();
1690 }
1691 };
satokd87c2592010-09-29 11:52:06 +09001692
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001693 TypedArray a = context.obtainStyledAttributes(null,
1694 com.android.internal.R.styleable.DialogPreference,
1695 com.android.internal.R.attr.alertDialogStyle, 0);
1696 mDialogBuilder = new AlertDialog.Builder(context)
1697 .setTitle(com.android.internal.R.string.select_input_method)
1698 .setOnCancelListener(new OnCancelListener() {
1699 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001701 }
1702 })
1703 .setIcon(a.getDrawable(
1704 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1705 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001706
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001707 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1708 new AlertDialog.OnClickListener() {
1709 public void onClick(DialogInterface dialog, int which) {
1710 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001711 if (mIms == null || mIms.length <= which
1712 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001713 return;
1714 }
1715 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001716 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001717 hideInputMethodMenu();
1718 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001719 if ((subtypeId < 0)
1720 || (subtypeId >= im.getSubtypes().size())) {
1721 subtypeId = NOT_A_SUBTYPE_ID;
1722 }
1723 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001724 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001727 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728
satok7f35c8c2010-10-07 21:13:11 +09001729 if (showSubtypes) {
1730 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1731 new DialogInterface.OnClickListener() {
1732 public void onClick(DialogInterface dialog, int whichButton) {
1733 showInputMethodAndSubtypeEnabler();
1734 }
1735 });
1736 }
satok0ff647b2010-10-08 13:49:28 +09001737 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1738 new DialogInterface.OnClickListener() {
1739 public void onClick(DialogInterface dialog, int whichButton) {
1740 hideInputMethodMenu();
1741 }
1742 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mSwitchingDialog = mDialogBuilder.create();
1744 mSwitchingDialog.getWindow().setType(
1745 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1746 mSwitchingDialog.show();
1747 }
1748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001751 synchronized (mMethodMap) {
1752 hideInputMethodMenuLocked();
1753 }
1754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project10592532009-03-18 17:39:46 -07001756 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001757 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 if (mSwitchingDialog != null) {
1760 mSwitchingDialog.dismiss();
1761 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
The Android Open Source Project10592532009-03-18 17:39:46 -07001764 mDialogBuilder = null;
1765 mItems = null;
1766 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 public boolean setInputMethodEnabled(String id, boolean enabled) {
1772 synchronized (mMethodMap) {
1773 if (mContext.checkCallingOrSelfPermission(
1774 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1775 != PackageManager.PERMISSION_GRANTED) {
1776 throw new SecurityException(
1777 "Requires permission "
1778 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1779 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 long ident = Binder.clearCallingIdentity();
1782 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001783 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 } finally {
1785 Binder.restoreCallingIdentity(ident);
1786 }
1787 }
1788 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001789
1790 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1791 // Make sure this is a valid input method.
1792 InputMethodInfo imm = mMethodMap.get(id);
1793 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001794 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001795 }
1796
satokd87c2592010-09-29 11:52:06 +09001797 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1798 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001799
satokd87c2592010-09-29 11:52:06 +09001800 if (enabled) {
1801 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1802 if (pair.first.equals(id)) {
1803 // We are enabling this input method, but it is already enabled.
1804 // Nothing to do. The previous state was enabled.
1805 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001806 }
1807 }
satokd87c2592010-09-29 11:52:06 +09001808 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1809 // Previous state was disabled.
1810 return false;
1811 } else {
1812 StringBuilder builder = new StringBuilder();
1813 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1814 builder, enabledInputMethodsList, id)) {
1815 // Disabled input method is currently selected, switch to another one.
1816 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1817 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001818 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1819 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1820 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001821 }
1822 // Previous state was enabled.
1823 return true;
1824 } else {
1825 // We are disabling the input method but it is already disabled.
1826 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001827 return false;
1828 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001829 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001830 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001831
satok723a27e2010-11-11 14:58:11 +09001832 private void saveCurrentInputMethodAndSubtypeToHistory() {
1833 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1834 if (mCurrentSubtype != null) {
1835 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1836 }
1837 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001838 }
1839
satok723a27e2010-11-11 14:58:11 +09001840 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1841 boolean setSubtypeOnly) {
1842 // Update the history of InputMethod and Subtype
1843 saveCurrentInputMethodAndSubtypeToHistory();
1844
1845 // Set Subtype here
1846 if (imi == null || subtypeId < 0) {
1847 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001848 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001849 } else {
1850 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1851 if (subtypeId < subtypes.size()) {
1852 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1853 mCurrentSubtype = subtypes.get(subtypeId);
1854 } else {
1855 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1856 mCurrentSubtype = null;
1857 }
satokab751aa2010-09-14 19:17:36 +09001858 }
satok723a27e2010-11-11 14:58:11 +09001859
1860 if (!setSubtypeOnly) {
1861 // Set InputMethod here
1862 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1863 }
1864 }
1865
1866 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1867 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1868 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1869 // newDefaultIme is empty when there is no candidate for the selected IME.
1870 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1871 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1872 if (subtypeHashCode != null) {
1873 try {
1874 lastSubtypeId = getSubtypeIdFromHashCode(
1875 imi, Integer.valueOf(subtypeHashCode));
1876 } catch (NumberFormatException e) {
1877 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1878 }
1879 }
1880 }
1881 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001882 }
1883
1884 private int getSelectedInputMethodSubtypeId(String id) {
1885 InputMethodInfo imi = mMethodMap.get(id);
1886 if (imi == null) {
1887 return NOT_A_SUBTYPE_ID;
1888 }
satokab751aa2010-09-14 19:17:36 +09001889 int subtypeId;
1890 try {
1891 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1892 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1893 } catch (SettingNotFoundException e) {
1894 return NOT_A_SUBTYPE_ID;
1895 }
satok723a27e2010-11-11 14:58:11 +09001896 return getSubtypeIdFromHashCode(imi, subtypeId);
1897 }
1898
1899 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
1900 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001901 for (int i = 0; i < subtypes.size(); ++i) {
1902 InputMethodSubtype ims = subtypes.get(i);
satok723a27e2010-11-11 14:58:11 +09001903 if (subtypeHashCode == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001904 return i;
1905 }
1906 }
1907 return NOT_A_SUBTYPE_ID;
1908 }
1909
satok8fbb1e82010-11-02 23:15:58 +09001910 // If there are no selected subtypes, tries finding the most applicable one according to the
1911 // current system locale
1912 private int findApplicableSubtype(String id) {
1913 InputMethodInfo imi = mMethodMap.get(id);
1914 if (imi == null) {
1915 return NOT_A_SUBTYPE_ID;
1916 }
1917 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1918 if (subtypes == null || subtypes.size() == 0) {
1919 return NOT_A_SUBTYPE_ID;
1920 }
1921 final String locale = mContext.getResources().getConfiguration().locale.toString();
1922 final String language = locale.substring(0, 2);
1923 boolean partialMatchFound = false;
1924 int applicableSubtypeId = DEFAULT_SUBTYPE_ID;
1925 for (int i = 0; i < subtypes.size(); ++i) {
1926 final String subtypeLocale = subtypes.get(i).getLocale();
satok9ef02832010-11-04 21:17:48 +09001927 // An applicable subtype should be a keyboard subtype
1928 if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) {
1929 if (locale.equals(subtypeLocale)) {
1930 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1931 applicableSubtypeId = i;
1932 break;
1933 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1934 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1935 applicableSubtypeId = i;
1936 partialMatchFound = true;
1937 }
satok8fbb1e82010-11-02 23:15:58 +09001938 }
1939 }
1940
1941 // The first subtype applicable to the system locale will be defined as the most applicable
1942 // subtype.
1943 if (DEBUG) {
satok03eb319a2010-11-11 18:17:42 +09001944 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId + ","
satok8fbb1e82010-11-02 23:15:58 +09001945 + subtypes.get(applicableSubtypeId).getLocale());
1946 }
1947 return applicableSubtypeId;
1948 }
1949
satokab751aa2010-09-14 19:17:36 +09001950 /**
1951 * @return Return the current subtype of this input method.
1952 */
1953 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok8fbb1e82010-11-02 23:15:58 +09001954 boolean subtypeIsSelected = false;
1955 try {
1956 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
1957 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
1958 } catch (SettingNotFoundException e) {
1959 }
1960 if (!subtypeIsSelected || mCurrentSubtype == null) {
1961 String lastInputMethodId = Settings.Secure.getString(mContext
1962 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1963 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
1964 if (subtypeId == NOT_A_SUBTYPE_ID) {
1965 subtypeId = findApplicableSubtype(lastInputMethodId);
1966 }
1967 if (subtypeId != NOT_A_SUBTYPE_ID) {
1968 mCurrentSubtype = mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
1969 }
1970 }
satokab751aa2010-09-14 19:17:36 +09001971 return mCurrentSubtype;
1972 }
1973
satokb66d2872010-11-10 01:04:04 +09001974 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
1975 synchronized (mMethodMap) {
1976 if (subtype != null && mCurMethodId != null) {
1977 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1978 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
1979 if (subtypeId != NOT_A_SUBTYPE_ID) {
1980 setInputMethodLocked(mCurMethodId, subtypeId);
1981 return true;
1982 }
1983 }
1984 return false;
1985 }
1986 }
1987
satokd87c2592010-09-29 11:52:06 +09001988 /**
1989 * Utility class for putting and getting settings for InputMethod
1990 * TODO: Move all putters and getters of settings to this class.
1991 */
1992 private static class InputMethodSettings {
1993 // The string for enabled input method is saved as follows:
1994 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
1995 private static final char INPUT_METHOD_SEPARATER = ':';
1996 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09001997 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09001998 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
1999
satok723a27e2010-11-11 14:58:11 +09002000 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002001 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2002
2003 private final ContentResolver mResolver;
2004 private final HashMap<String, InputMethodInfo> mMethodMap;
2005 private final ArrayList<InputMethodInfo> mMethodList;
2006
2007 private String mEnabledInputMethodsStrCache;
2008
2009 private static void buildEnabledInputMethodsSettingString(
2010 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2011 String id = pair.first;
2012 ArrayList<String> subtypes = pair.second;
2013 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002014 // Inputmethod and subtypes are saved in the settings as follows:
2015 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2016 for (String subtypeId: subtypes) {
2017 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002018 }
2019 }
2020
2021 public InputMethodSettings(
2022 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2023 ArrayList<InputMethodInfo> methodList) {
2024 mResolver = resolver;
2025 mMethodMap = methodMap;
2026 mMethodList = methodList;
2027 }
2028
2029 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2030 return createEnabledInputMethodListLocked(
2031 getEnabledInputMethodsAndSubtypeListLocked());
2032 }
2033
satok7f35c8c2010-10-07 21:13:11 +09002034 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002035 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2036 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002037 getEnabledInputMethodsAndSubtypeListLocked());
2038 }
2039
satok67ddf9c2010-11-17 09:45:54 +09002040 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2041 InputMethodInfo imi) {
2042 List<Pair<String, ArrayList<String>>> imsList =
2043 getEnabledInputMethodsAndSubtypeListLocked();
2044 ArrayList<InputMethodSubtype> enabledSubtypes =
2045 new ArrayList<InputMethodSubtype>();
2046 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2047 InputMethodInfo info = mMethodMap.get(imsPair.first);
2048 if (info != null && info.getId().equals(imi.getId())) {
2049 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2050 for (InputMethodSubtype ims: subtypes) {
2051 for (String s: imsPair.second) {
2052 if (String.valueOf(ims.hashCode()).equals(s)) {
2053 enabledSubtypes.add(ims);
2054 }
2055 }
2056 }
2057 break;
2058 }
2059 }
2060 return enabledSubtypes;
2061 }
2062
satokd87c2592010-09-29 11:52:06 +09002063 // At the initial boot, the settings for input methods are not set,
2064 // so we need to enable IME in that case.
2065 public void enableAllIMEsIfThereIsNoEnabledIME() {
2066 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2067 StringBuilder sb = new StringBuilder();
2068 final int N = mMethodList.size();
2069 for (int i = 0; i < N; i++) {
2070 InputMethodInfo imi = mMethodList.get(i);
2071 Slog.i(TAG, "Adding: " + imi.getId());
2072 if (i > 0) sb.append(':');
2073 sb.append(imi.getId());
2074 }
2075 putEnabledInputMethodsStr(sb.toString());
2076 }
2077 }
2078
2079 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2080 ArrayList<Pair<String, ArrayList<String>>> imsList
2081 = new ArrayList<Pair<String, ArrayList<String>>>();
2082 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2083 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2084 return imsList;
2085 }
satok723a27e2010-11-11 14:58:11 +09002086 mInputMethodSplitter.setString(enabledInputMethodsStr);
2087 while (mInputMethodSplitter.hasNext()) {
2088 String nextImsStr = mInputMethodSplitter.next();
2089 mSubtypeSplitter.setString(nextImsStr);
2090 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002091 ArrayList<String> subtypeHashes = new ArrayList<String>();
2092 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002093 String imeId = mSubtypeSplitter.next();
2094 while (mSubtypeSplitter.hasNext()) {
2095 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002096 }
2097 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2098 }
2099 }
2100 return imsList;
2101 }
2102
2103 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2104 if (reloadInputMethodStr) {
2105 getEnabledInputMethodsStr();
2106 }
2107 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2108 // Add in the newly enabled input method.
2109 putEnabledInputMethodsStr(id);
2110 } else {
2111 putEnabledInputMethodsStr(
2112 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2113 }
2114 }
2115
2116 /**
2117 * Build and put a string of EnabledInputMethods with removing specified Id.
2118 * @return the specified id was removed or not.
2119 */
2120 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2121 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2122 boolean isRemoved = false;
2123 boolean needsAppendSeparator = false;
2124 for (Pair<String, ArrayList<String>> ims: imsList) {
2125 String curId = ims.first;
2126 if (curId.equals(id)) {
2127 // We are disabling this input method, and it is
2128 // currently enabled. Skip it to remove from the
2129 // new list.
2130 isRemoved = true;
2131 } else {
2132 if (needsAppendSeparator) {
2133 builder.append(INPUT_METHOD_SEPARATER);
2134 } else {
2135 needsAppendSeparator = true;
2136 }
2137 buildEnabledInputMethodsSettingString(builder, ims);
2138 }
2139 }
2140 if (isRemoved) {
2141 // Update the setting with the new list of input methods.
2142 putEnabledInputMethodsStr(builder.toString());
2143 }
2144 return isRemoved;
2145 }
2146
2147 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2148 List<Pair<String, ArrayList<String>>> imsList) {
2149 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2150 for (Pair<String, ArrayList<String>> ims: imsList) {
2151 InputMethodInfo info = mMethodMap.get(ims.first);
2152 if (info != null) {
2153 res.add(info);
2154 }
2155 }
2156 return res;
2157 }
2158
satok7f35c8c2010-10-07 21:13:11 +09002159 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002160 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002161 List<Pair<String, ArrayList<String>>> imsList) {
2162 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2163 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2164 for (Pair<String, ArrayList<String>> ims : imsList) {
2165 InputMethodInfo info = mMethodMap.get(ims.first);
2166 if (info != null) {
2167 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2168 }
2169 }
2170 return res;
2171 }
2172
satokd87c2592010-09-29 11:52:06 +09002173 private void putEnabledInputMethodsStr(String str) {
2174 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2175 mEnabledInputMethodsStrCache = str;
2176 }
2177
2178 private String getEnabledInputMethodsStr() {
2179 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2180 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002181 if (DEBUG) {
2182 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2183 }
satokd87c2592010-09-29 11:52:06 +09002184 return mEnabledInputMethodsStrCache;
2185 }
satok723a27e2010-11-11 14:58:11 +09002186
2187 private void saveSubtypeHistory(
2188 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2189 StringBuilder builder = new StringBuilder();
2190 boolean isImeAdded = false;
2191 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2192 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2193 newSubtypeId);
2194 isImeAdded = true;
2195 }
2196 for (Pair<String, String> ime: savedImes) {
2197 String imeId = ime.first;
2198 String subtypeId = ime.second;
2199 if (TextUtils.isEmpty(subtypeId)) {
2200 subtypeId = NOT_A_SUBTYPE_ID_STR;
2201 }
2202 if (isImeAdded) {
2203 builder.append(INPUT_METHOD_SEPARATER);
2204 } else {
2205 isImeAdded = true;
2206 }
2207 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2208 subtypeId);
2209 }
2210 // Remove the last INPUT_METHOD_SEPARATER
2211 putSubtypeHistoryStr(builder.toString());
2212 }
2213
2214 public void addSubtypeToHistory(String imeId, String subtypeId) {
2215 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2216 for (Pair<String, String> ime: subtypeHistory) {
2217 if (ime.first.equals(imeId)) {
2218 if (DEBUG) {
2219 Slog.v(TAG, "Subtype found in the history: " + imeId
2220 + ime.second);
2221 }
2222 // We should break here
2223 subtypeHistory.remove(ime);
2224 break;
2225 }
2226 }
2227 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2228 }
2229
2230 private void putSubtypeHistoryStr(String str) {
2231 if (DEBUG) {
2232 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2233 }
2234 Settings.Secure.putString(
2235 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2236 }
2237
2238 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2239 // Gets the first one from the history
2240 return getLastSubtypeForInputMethodLockedInternal(null);
2241 }
2242
2243 public String getLastSubtypeForInputMethodLocked(String imeId) {
2244 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2245 if (ime != null) {
2246 return ime.second;
2247 } else {
2248 return null;
2249 }
2250 }
2251
2252 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2253 List<Pair<String, ArrayList<String>>> enabledImes =
2254 getEnabledInputMethodsAndSubtypeListLocked();
2255 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2256 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2257 final String imeInTheHistory = imeAndSubtype.first;
2258 // If imeId is empty, returns the first IME and subtype in the history
2259 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2260 final String subtypeInTheHistory = imeAndSubtype.second;
2261 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2262 enabledImes, imeInTheHistory, subtypeInTheHistory);
2263 if (!TextUtils.isEmpty(subtypeHashCode)) {
2264 if (DEBUG) {
2265 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2266 }
2267 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2268 }
2269 }
2270 }
2271 if (DEBUG) {
2272 Slog.d(TAG, "No enabled IME found in the history");
2273 }
2274 return null;
2275 }
2276
2277 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2278 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2279 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2280 if (enabledIme.first.equals(imeId)) {
2281 for (String s: enabledIme.second) {
2282 if (s.equals(subtypeHashCode)) {
2283 // If both imeId and subtypeId are enabled, return subtypeId.
2284 return s;
2285 }
2286 }
2287 // If imeId was enabled but subtypeId was disabled.
2288 return NOT_A_SUBTYPE_ID_STR;
2289 }
2290 }
2291 // If both imeId and subtypeId are disabled, return null
2292 return null;
2293 }
2294
2295 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2296 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2297 final String subtypeHistoryStr = getSubtypeHistoryStr();
2298 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2299 return imsList;
2300 }
2301 mInputMethodSplitter.setString(subtypeHistoryStr);
2302 while (mInputMethodSplitter.hasNext()) {
2303 String nextImsStr = mInputMethodSplitter.next();
2304 mSubtypeSplitter.setString(nextImsStr);
2305 if (mSubtypeSplitter.hasNext()) {
2306 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2307 // The first element is ime id.
2308 String imeId = mSubtypeSplitter.next();
2309 while (mSubtypeSplitter.hasNext()) {
2310 subtypeId = mSubtypeSplitter.next();
2311 break;
2312 }
2313 imsList.add(new Pair<String, String>(imeId, subtypeId));
2314 }
2315 }
2316 return imsList;
2317 }
2318
2319 private String getSubtypeHistoryStr() {
2320 if (DEBUG) {
2321 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2322 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2323 }
2324 return Settings.Secure.getString(
2325 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2326 }
2327
2328 public void putSelectedInputMethod(String imeId) {
2329 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2330 }
2331
2332 public void putSelectedSubtype(int subtypeId) {
2333 Settings.Secure.putInt(
2334 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2335 }
satokd87c2592010-09-29 11:52:06 +09002336 }
2337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 @Override
2341 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2342 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2343 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2346 + Binder.getCallingPid()
2347 + ", uid=" + Binder.getCallingUid());
2348 return;
2349 }
2350
2351 IInputMethod method;
2352 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 synchronized (mMethodMap) {
2357 p.println("Current Input Method Manager state:");
2358 int N = mMethodList.size();
2359 p.println(" Input Methods:");
2360 for (int i=0; i<N; i++) {
2361 InputMethodInfo info = mMethodList.get(i);
2362 p.println(" InputMethod #" + i + ":");
2363 info.dump(p, " ");
2364 }
2365 p.println(" Clients:");
2366 for (ClientState ci : mClients.values()) {
2367 p.println(" Client " + ci + ":");
2368 p.println(" client=" + ci.client);
2369 p.println(" inputContext=" + ci.inputContext);
2370 p.println(" sessionRequested=" + ci.sessionRequested);
2371 p.println(" curSession=" + ci.curSession);
2372 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002373 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002375 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2376 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2378 + " mBoundToMethod=" + mBoundToMethod);
2379 p.println(" mCurToken=" + mCurToken);
2380 p.println(" mCurIntent=" + mCurIntent);
2381 method = mCurMethod;
2382 p.println(" mCurMethod=" + mCurMethod);
2383 p.println(" mEnabledSession=" + mEnabledSession);
2384 p.println(" mShowRequested=" + mShowRequested
2385 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2386 + " mShowForced=" + mShowForced
2387 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002388 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002390
Jeff Brownb88102f2010-09-08 11:49:43 -07002391 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 pw.flush();
2394 try {
2395 client.client.asBinder().dump(fd, args);
2396 } catch (RemoteException e) {
2397 p.println("Input method client dead: " + e);
2398 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002399 } else {
2400 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002402
Jeff Brownb88102f2010-09-08 11:49:43 -07002403 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 pw.flush();
2406 try {
2407 method.asBinder().dump(fd, args);
2408 } catch (RemoteException e) {
2409 p.println("Input method service dead: " + e);
2410 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002411 } else {
2412 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
2414 }
2415}