blob: 4d35bec1211dbe0b6a1462b1ff339838ea4ee934 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.text.TextUtils;
65import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080066import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.PrintWriterPrinter;
68import android.util.Printer;
69import android.view.IWindowManager;
70import android.view.WindowManager;
71import android.view.inputmethod.InputBinding;
72import android.view.inputmethod.InputMethod;
73import android.view.inputmethod.InputMethodInfo;
74import android.view.inputmethod.InputMethodManager;
75import android.view.inputmethod.EditorInfo;
76
77import java.io.FileDescriptor;
78import java.io.IOException;
79import java.io.PrintWriter;
80import java.util.ArrayList;
81import java.util.HashMap;
82import java.util.List;
83
84/**
85 * This class provides a system service that manages input methods.
86 */
87public class InputMethodManagerService extends IInputMethodManager.Stub
88 implements ServiceConnection, Handler.Callback {
89 static final boolean DEBUG = false;
90 static final String TAG = "InputManagerService";
91
92 static final int MSG_SHOW_IM_PICKER = 1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 static final int MSG_UNBIND_INPUT = 1000;
95 static final int MSG_BIND_INPUT = 1010;
96 static final int MSG_SHOW_SOFT_INPUT = 1020;
97 static final int MSG_HIDE_SOFT_INPUT = 1030;
98 static final int MSG_ATTACH_TOKEN = 1040;
99 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 static final int MSG_START_INPUT = 2000;
102 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_METHOD = 3000;
105 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 final Context mContext;
110 final Handler mHandler;
111 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700112 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 final IWindowManager mIWindowManager;
114 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 // All known input methods. mMethodMap also serves as the global
119 // lock for this class.
120 final ArrayList<InputMethodInfo> mMethodList
121 = new ArrayList<InputMethodInfo>();
122 final HashMap<String, InputMethodInfo> mMethodMap
123 = new HashMap<String, InputMethodInfo>();
124
125 final TextUtils.SimpleStringSplitter mStringColonSplitter
126 = new TextUtils.SimpleStringSplitter(':');
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 class SessionState {
129 final ClientState client;
130 final IInputMethod method;
131 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 @Override
134 public String toString() {
135 return "SessionState{uid " + client.uid + " pid " + client.pid
136 + " method " + Integer.toHexString(
137 System.identityHashCode(method))
138 + " session " + Integer.toHexString(
139 System.identityHashCode(session))
140 + "}";
141 }
142
143 SessionState(ClientState _client, IInputMethod _method,
144 IInputMethodSession _session) {
145 client = _client;
146 method = _method;
147 session = _session;
148 }
149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 class ClientState {
152 final IInputMethodClient client;
153 final IInputContext inputContext;
154 final int uid;
155 final int pid;
156 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 boolean sessionRequested;
159 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 @Override
162 public String toString() {
163 return "ClientState{" + Integer.toHexString(
164 System.identityHashCode(this)) + " uid " + uid
165 + " pid " + pid + "}";
166 }
167
168 ClientState(IInputMethodClient _client, IInputContext _inputContext,
169 int _uid, int _pid) {
170 client = _client;
171 inputContext = _inputContext;
172 uid = _uid;
173 pid = _pid;
174 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
175 }
176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final HashMap<IBinder, ClientState> mClients
179 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700182 * Set once the system is ready to run third party code.
183 */
184 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * Id of the currently selected input method.
188 */
189 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
192 * The current binding sequence number, incremented every time there is
193 * a new bind performed.
194 */
195 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
198 * The client that is currently bound to an input method.
199 */
200 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700203 * The last window token that gained focus.
204 */
205 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * The input context last provided by the current client.
209 */
210 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 /**
213 * The attributes last provided by the current client.
214 */
215 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 /**
218 * The input method ID of the input method service that we are currently
219 * connected to or in the process of connecting to.
220 */
221 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
224 * Set to true if our ServiceConnection is currently actively bound to
225 * a service (whether or not we have gotten its IBinder back yet).
226 */
227 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
230 * Set if the client has asked for the input method to be shown.
231 */
232 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 /**
235 * Set if we were explicitly told to show the input method.
236 */
237 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /**
240 * Set if we were forced to be shown.
241 */
242 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
245 * Set if we last told the input method to show itself.
246 */
247 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 /**
250 * The Intent used to connect to the current input method.
251 */
252 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * The token we have made for the currently active input method, to
256 * identify it in the future.
257 */
258 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * If non-null, this is the input method service we are currently connected
262 * to.
263 */
264 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Time that we last initiated a bind to the input method, to determine
268 * if we should try to disconnect and reconnect to it.
269 */
270 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * Have we called mCurMethod.bindInput()?
274 */
275 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * Currently enabled session. Only touched by service thread, not
279 * protected by a lock.
280 */
281 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * True if the screen is on. The value is true initially.
285 */
286 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 AlertDialog.Builder mDialogBuilder;
289 AlertDialog mSwitchingDialog;
290 InputMethodInfo[] mIms;
291 CharSequence[] mItems;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 class SettingsObserver extends ContentObserver {
294 SettingsObserver(Handler handler) {
295 super(handler);
296 ContentResolver resolver = mContext.getContentResolver();
297 resolver.registerContentObserver(Settings.Secure.getUriFor(
298 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 @Override public void onChange(boolean selfChange) {
302 synchronized (mMethodMap) {
303 updateFromSettingsLocked();
304 }
305 }
306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
309 @Override
310 public void onReceive(Context context, Intent intent) {
311 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
312 mScreenOn = true;
313 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
314 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700315 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
316 hideInputMethodMenu();
317 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800319 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321
322 // Inform the current client of the change in active status
323 try {
324 if (mCurClient != null && mCurClient.client != null) {
325 mCurClient.client.setActive(mScreenOn);
326 }
327 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800328 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 + mCurClient.pid + " uid " + mCurClient.uid);
330 }
331 }
332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800334 class MyPackageMonitor extends PackageMonitor {
335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800337 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800339 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
341 final int N = mMethodList.size();
342 if (curInputMethodId != null) {
343 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800344 InputMethodInfo imi = mMethodList.get(i);
345 if (imi.getId().equals(curInputMethodId)) {
346 for (String pkg : packages) {
347 if (imi.getPackageName().equals(pkg)) {
348 if (!doit) {
349 return true;
350 }
351
352 Settings.Secure.putString(mContext.getContentResolver(),
353 Settings.Secure.DEFAULT_INPUT_METHOD, "");
354 chooseNewDefaultIMELocked();
355 return true;
356 }
357 }
358 }
359 }
360 }
361 }
362 return false;
363 }
364
365 @Override
366 public void onSomePackagesChanged() {
367 synchronized (mMethodMap) {
368 InputMethodInfo curIm = null;
369 String curInputMethodId = Settings.Secure.getString(mContext
370 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
371 final int N = mMethodList.size();
372 if (curInputMethodId != null) {
373 for (int i=0; i<N; i++) {
374 InputMethodInfo imi = mMethodList.get(i);
375 if (imi.getId().equals(curInputMethodId)) {
376 curIm = imi;
377 }
378 int change = isPackageDisappearing(imi.getPackageName());
379 if (change == PACKAGE_TEMPORARY_CHANGE
380 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800382 + imi.getComponent());
383 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 }
385 }
386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800388 buildInputMethodListLocked(mMethodList, mMethodMap);
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800392 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800393 int change = isPackageDisappearing(curIm.getPackageName());
394 if (change == PACKAGE_TEMPORARY_CHANGE
395 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800396 ServiceInfo si = null;
397 try {
398 si = mContext.getPackageManager().getServiceInfo(
399 curIm.getComponent(), 0);
400 } catch (PackageManager.NameNotFoundException ex) {
401 }
402 if (si == null) {
403 // Uh oh, current input method is no longer around!
404 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800405 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800407 changed = true;
408 curIm = null;
409 curInputMethodId = "";
Joe Onorato8a9b2202010-02-26 18:56:32 -0800410 Slog.i(TAG, "Unsetting current input method");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800411 Settings.Secure.putString(mContext.getContentResolver(),
412 Settings.Secure.DEFAULT_INPUT_METHOD,
413 curInputMethodId);
414 }
415 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800416 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 }
418
419 if (curIm == null) {
420 // We currently don't have a default input method... is
421 // one now available?
422 changed = chooseNewDefaultIMELocked();
423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800425 if (changed) {
426 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428 }
429 }
430 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 class MethodCallback extends IInputMethodCallback.Stub {
433 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 MethodCallback(IInputMethod method) {
436 mMethod = method;
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 public void finishedEvent(int seq, boolean handled) throws RemoteException {
440 }
441
442 public void sessionCreated(IInputMethodSession session) throws RemoteException {
443 onSessionCreated(mMethod, session);
444 }
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Joe Onorato089de882010-04-12 08:18:45 -0700447 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 mContext = context;
449 mHandler = new Handler(this);
450 mIWindowManager = IWindowManager.Stub.asInterface(
451 ServiceManager.getService(Context.WINDOW_SERVICE));
452 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
453 public void executeMessage(Message msg) {
454 handleMessage(msg);
455 }
456 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 IntentFilter screenOnOffFilt = new IntentFilter();
461 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
462 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700463 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 buildInputMethodListLocked(mMethodList, mMethodMap);
467
468 final String enabledStr = Settings.Secure.getString(
469 mContext.getContentResolver(),
470 Settings.Secure.ENABLED_INPUT_METHODS);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800471 Slog.i(TAG, "Enabled input methods: " + enabledStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 if (enabledStr == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 Slog.i(TAG, "Enabled input methods has not been set, enabling all");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 InputMethodInfo defIm = null;
475 StringBuilder sb = new StringBuilder(256);
476 final int N = mMethodList.size();
477 for (int i=0; i<N; i++) {
478 InputMethodInfo imi = mMethodList.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800479 Slog.i(TAG, "Adding: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 if (i > 0) sb.append(':');
481 sb.append(imi.getId());
482 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
483 try {
484 Resources res = mContext.createPackageContext(
485 imi.getPackageName(), 0).getResources();
486 if (res.getBoolean(imi.getIsDefaultResourceId())) {
487 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800488 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 } catch (PackageManager.NameNotFoundException ex) {
491 } catch (Resources.NotFoundException ex) {
492 }
493 }
494 }
495 if (defIm == null && N > 0) {
496 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800497 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499 Settings.Secure.putString(mContext.getContentResolver(),
500 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
501 if (defIm != null) {
502 Settings.Secure.putString(mContext.getContentResolver(),
503 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
504 }
505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 mStatusBar = statusBar;
Joe Onorato0cbda992010-05-02 16:28:15 -0700508 statusBar.setIconVisibility("ime", false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 mSettingsObserver = new SettingsObserver(mHandler);
511 updateFromSettingsLocked();
512 }
513
514 @Override
515 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
516 throws RemoteException {
517 try {
518 return super.onTransact(code, data, reply, flags);
519 } catch (RuntimeException e) {
520 // The input method manager only throws security exceptions, so let's
521 // log all others.
522 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 throw e;
526 }
527 }
528
529 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700530 synchronized (mMethodMap) {
531 if (!mSystemReady) {
532 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700533 try {
534 startInputInnerLocked();
535 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700537 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700538 }
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 public List<InputMethodInfo> getInputMethodList() {
543 synchronized (mMethodMap) {
544 return new ArrayList<InputMethodInfo>(mMethodList);
545 }
546 }
547
548 public List<InputMethodInfo> getEnabledInputMethodList() {
549 synchronized (mMethodMap) {
550 return getEnabledInputMethodListLocked();
551 }
552 }
553
554 List<InputMethodInfo> getEnabledInputMethodListLocked() {
555 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 final String enabledStr = Settings.Secure.getString(
558 mContext.getContentResolver(),
559 Settings.Secure.ENABLED_INPUT_METHODS);
560 if (enabledStr != null) {
561 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
562 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 while (splitter.hasNext()) {
565 InputMethodInfo info = mMethodMap.get(splitter.next());
566 if (info != null) {
567 res.add(info);
568 }
569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 return res;
573 }
574
575 public void addClient(IInputMethodClient client,
576 IInputContext inputContext, int uid, int pid) {
577 synchronized (mMethodMap) {
578 mClients.put(client.asBinder(), new ClientState(client,
579 inputContext, uid, pid));
580 }
581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public void removeClient(IInputMethodClient client) {
584 synchronized (mMethodMap) {
585 mClients.remove(client.asBinder());
586 }
587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 void executeOrSendMessage(IInterface target, Message msg) {
590 if (target.asBinder() instanceof Binder) {
591 mCaller.sendMessage(msg);
592 } else {
593 handleMessage(msg);
594 msg.recycle();
595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700598 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 + mCurClient.client.asBinder());
602 if (mBoundToMethod) {
603 mBoundToMethod = false;
604 if (mCurMethod != null) {
605 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
606 MSG_UNBIND_INPUT, mCurMethod));
607 }
608 }
609 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
610 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
611 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 // Call setActive(false) on the old client
614 try {
615 mCurClient.client.setActive(false);
616 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 + mCurClient.pid + " uid " + mCurClient.uid);
619 }
620 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Project10592532009-03-18 17:39:46 -0700622 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 private int getImeShowFlags() {
627 int flags = 0;
628 if (mShowForced) {
629 flags |= InputMethod.SHOW_FORCED
630 | InputMethod.SHOW_EXPLICIT;
631 } else if (mShowExplicitlyRequested) {
632 flags |= InputMethod.SHOW_EXPLICIT;
633 }
634 return flags;
635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private int getAppShowFlags() {
638 int flags = 0;
639 if (mShowForced) {
640 flags |= InputMethodManager.SHOW_FORCED;
641 } else if (!mShowExplicitlyRequested) {
642 flags |= InputMethodManager.SHOW_IMPLICIT;
643 }
644 return flags;
645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
648 if (!mBoundToMethod) {
649 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
650 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
651 mBoundToMethod = true;
652 }
653 final SessionState session = mCurClient.curSession;
654 if (initial) {
655 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
656 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
657 } else {
658 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
659 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
660 }
661 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800663 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 return needResult
666 ? new InputBindResult(session.session, mCurId, mCurSeq)
667 : null;
668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 InputBindResult startInputLocked(IInputMethodClient client,
671 IInputContext inputContext, EditorInfo attribute,
672 boolean initial, boolean needResult) {
673 // If no method is currently selected, do nothing.
674 if (mCurMethodId == null) {
675 return mNoBinding;
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 ClientState cs = mClients.get(client.asBinder());
679 if (cs == null) {
680 throw new IllegalArgumentException("unknown client "
681 + client.asBinder());
682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 try {
685 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
686 // Check with the window manager to make sure this client actually
687 // has a window with focus. If not, reject. This is thread safe
688 // because if the focus changes some time before or after, the
689 // next client receiving focus that has any interest in input will
690 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
693 return null;
694 }
695 } catch (RemoteException e) {
696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (mCurClient != cs) {
699 // If the client is changing, we need to switch over to the new
700 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700701 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 + cs.client.asBinder());
704
705 // If the screen is on, inform the new client it is active
706 if (mScreenOn) {
707 try {
708 cs.client.setActive(mScreenOn);
709 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + cs.pid + " uid " + cs.uid);
712 }
713 }
714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 // Bump up the sequence for this client and attach it.
717 mCurSeq++;
718 if (mCurSeq <= 0) mCurSeq = 1;
719 mCurClient = cs;
720 mCurInputContext = inputContext;
721 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 // Check if the input method is changing.
724 if (mCurId != null && mCurId.equals(mCurMethodId)) {
725 if (cs.curSession != null) {
726 // Fast case: if we are already connected to the input method,
727 // then just return it.
728 return attachNewInputLocked(initial, needResult);
729 }
730 if (mHaveConnection) {
731 if (mCurMethod != null) {
732 if (!cs.sessionRequested) {
733 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
736 MSG_CREATE_SESSION, mCurMethod,
737 new MethodCallback(mCurMethod)));
738 }
739 // Return to client, and we will get back with it when
740 // we have had a session made for it.
741 return new InputBindResult(null, mCurId, mCurSeq);
742 } else if (SystemClock.uptimeMillis()
743 < (mLastBindTime+TIME_TO_RECONNECT)) {
744 // In this case we have connected to the service, but
745 // don't yet have its interface. If it hasn't been too
746 // long since we did the connection, we'll return to
747 // the client and wait to get the service interface so
748 // we can report back. If it has been too long, we want
749 // to fall through so we can try a disconnect/reconnect
750 // to see if we can get back in touch with the service.
751 return new InputBindResult(null, mCurId, mCurSeq);
752 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
754 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 }
757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700759 return startInputInnerLocked();
760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700762 InputBindResult startInputInnerLocked() {
763 if (mCurMethodId == null) {
764 return mNoBinding;
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700767 if (!mSystemReady) {
768 // If the system is not yet ready, we shouldn't be running third
769 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700770 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 InputMethodInfo info = mMethodMap.get(mCurMethodId);
774 if (info == null) {
775 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700778 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
781 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700782 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
783 com.android.internal.R.string.input_method_binding_label);
784 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
785 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
787 mLastBindTime = SystemClock.uptimeMillis();
788 mHaveConnection = true;
789 mCurId = info.getId();
790 mCurToken = new Binder();
791 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 mIWindowManager.addWindowToken(mCurToken,
794 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
795 } catch (RemoteException e) {
796 }
797 return new InputBindResult(null, mCurId, mCurSeq);
798 } else {
799 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 + mCurIntent);
802 }
803 return null;
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 public InputBindResult startInput(IInputMethodClient client,
807 IInputContext inputContext, EditorInfo attribute,
808 boolean initial, boolean needResult) {
809 synchronized (mMethodMap) {
810 final long ident = Binder.clearCallingIdentity();
811 try {
812 return startInputLocked(client, inputContext, attribute,
813 initial, needResult);
814 } finally {
815 Binder.restoreCallingIdentity(ident);
816 }
817 }
818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 public void finishInput(IInputMethodClient client) {
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 public void onServiceConnected(ComponentName name, IBinder service) {
824 synchronized (mMethodMap) {
825 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
826 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700827 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700829 unbindCurrentMethodLocked(false);
830 return;
831 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800832 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700833 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
834 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700839 MSG_CREATE_SESSION, mCurMethod,
840 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842 }
843 }
844 }
845
846 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
847 synchronized (mMethodMap) {
848 if (mCurMethod != null && method != null
849 && mCurMethod.asBinder() == method.asBinder()) {
850 if (mCurClient != null) {
851 mCurClient.curSession = new SessionState(mCurClient,
852 method, session);
853 mCurClient.sessionRequested = false;
854 InputBindResult res = attachNewInputLocked(true, true);
855 if (res.method != null) {
856 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
857 MSG_BIND_METHOD, mCurClient.client, res));
858 }
859 }
860 }
861 }
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700864 void unbindCurrentMethodLocked(boolean reportToClient) {
865 if (mHaveConnection) {
866 mContext.unbindService(this);
867 mHaveConnection = false;
868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700870 if (mCurToken != null) {
871 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800872 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 mIWindowManager.removeWindowToken(mCurToken);
874 } catch (RemoteException e) {
875 }
876 mCurToken = null;
877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Project10592532009-03-18 17:39:46 -0700879 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700880 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700882 if (reportToClient && mCurClient != null) {
883 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
884 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
885 }
886 }
887
Devin Taylor0c33ed22010-02-23 13:26:46 -0600888 private void finishSession(SessionState sessionState) {
889 if (sessionState != null && sessionState.session != null) {
890 try {
891 sessionState.session.finishSession();
892 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700893 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600894 }
895 }
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700898 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 if (mCurMethod != null) {
900 for (ClientState cs : mClients.values()) {
901 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600902 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 cs.curSession = null;
904 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600905
906 finishSession(mEnabledSession);
907 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mCurMethod = null;
909 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700910 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 public void onServiceDisconnected(ComponentName name) {
914 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800915 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 + " mCurIntent=" + mCurIntent);
917 if (mCurMethod != null && mCurIntent != null
918 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700919 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 // We consider this to be a new bind attempt, since the system
921 // should now try to restart the service for us.
922 mLastBindTime = SystemClock.uptimeMillis();
923 mShowRequested = mInputShown;
924 mInputShown = false;
925 if (mCurClient != null) {
926 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
927 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
928 }
929 }
930 }
931 }
932
933 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
934 long ident = Binder.clearCallingIdentity();
935 try {
936 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800937 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 return;
939 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 synchronized (mMethodMap) {
942 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800943 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700944 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800946 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700947 mStatusBar.setIcon("ime", packageName, iconId, 0);
948 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
950 }
951 } finally {
952 Binder.restoreCallingIdentity(ident);
953 }
954 }
955
956 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700957 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
958 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
959 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
960 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 String id = Settings.Secure.getString(mContext.getContentResolver(),
962 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700963 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 try {
965 setInputMethodLocked(id);
966 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800967 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700968 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700969 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700971 } else {
972 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700973 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700974 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 void setInputMethodLocked(String id) {
979 InputMethodInfo info = mMethodMap.get(id);
980 if (info == null) {
981 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (id.equals(mCurMethodId)) {
985 return;
986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 final long ident = Binder.clearCallingIdentity();
989 try {
990 mCurMethodId = id;
991 Settings.Secure.putString(mContext.getContentResolver(),
992 Settings.Secure.DEFAULT_INPUT_METHOD, id);
993
994 if (ActivityManagerNative.isSystemReady()) {
995 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800996 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 intent.putExtra("input_method_id", id);
998 mContext.sendBroadcast(intent);
999 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001000 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 } finally {
1002 Binder.restoreCallingIdentity(ident);
1003 }
1004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Project4df24232009-03-05 14:34:35 -08001006 public boolean showSoftInput(IInputMethodClient client, int flags,
1007 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 long ident = Binder.clearCallingIdentity();
1009 try {
1010 synchronized (mMethodMap) {
1011 if (mCurClient == null || client == null
1012 || mCurClient.client.asBinder() != client.asBinder()) {
1013 try {
1014 // We need to check if this is the current client with
1015 // focus in the window manager, to allow this call to
1016 // be made before input is started in it.
1017 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 Slog.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001019 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001022 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001025
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001027 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029 } finally {
1030 Binder.restoreCallingIdentity(ident);
1031 }
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Project4df24232009-03-05 14:34:35 -08001034 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 mShowRequested = true;
1036 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1037 mShowExplicitlyRequested = true;
1038 }
1039 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1040 mShowExplicitlyRequested = true;
1041 mShowForced = true;
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
Dianne Hackborncc278702009-09-02 23:07:23 -07001044 if (!mSystemReady) {
1045 return false;
1046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001047
The Android Open Source Project4df24232009-03-05 14:34:35 -08001048 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001050 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1051 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1052 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001054 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 } else if (mHaveConnection && SystemClock.uptimeMillis()
1056 < (mLastBindTime+TIME_TO_RECONNECT)) {
1057 // The client has asked to have the input method shown, but
1058 // we have been sitting here too long with a connection to the
1059 // service and no interface received, so let's disconnect/connect
1060 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 SystemClock.uptimeMillis()-mLastBindTime,1);
1063 mContext.unbindService(this);
1064 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001066
The Android Open Source Project4df24232009-03-05 14:34:35 -08001067 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
The Android Open Source Project4df24232009-03-05 14:34:35 -08001070 public boolean hideSoftInput(IInputMethodClient client, int flags,
1071 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 long ident = Binder.clearCallingIdentity();
1073 try {
1074 synchronized (mMethodMap) {
1075 if (mCurClient == null || client == null
1076 || mCurClient.client.asBinder() != client.asBinder()) {
1077 try {
1078 // We need to check if this is the current client with
1079 // focus in the window manager, to allow this call to
1080 // be made before input is started in it.
1081 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 Slog.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001083 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001091 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 } finally {
1094 Binder.restoreCallingIdentity(ident);
1095 }
1096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001097
The Android Open Source Project4df24232009-03-05 14:34:35 -08001098 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1100 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001101 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001103 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001106 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001108 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001110 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1113 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1114 res = true;
1115 } else {
1116 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 mInputShown = false;
1119 mShowRequested = false;
1120 mShowExplicitlyRequested = false;
1121 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001125 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1127 boolean first, int windowFlags) {
1128 long ident = Binder.clearCallingIdentity();
1129 try {
1130 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001131 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 + " viewHasFocus=" + viewHasFocus
1133 + " isTextEditor=" + isTextEditor
1134 + " softInputMode=#" + Integer.toHexString(softInputMode)
1135 + " first=" + first + " flags=#"
1136 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (mCurClient == null || client == null
1139 || mCurClient.client.asBinder() != client.asBinder()) {
1140 try {
1141 // We need to check if this is the current client with
1142 // focus in the window manager, to allow this call to
1143 // be made before input is started in it.
1144 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001145 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 return;
1147 }
1148 } catch (RemoteException e) {
1149 }
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001152 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001153 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001154 return;
1155 }
1156 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1159 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1160 if (!isTextEditor || (softInputMode &
1161 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1162 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1163 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1164 // There is no focus view, and this window will
1165 // be behind any soft input window, so hide the
1166 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001167 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001168 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 } else if (isTextEditor && (softInputMode &
1171 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1172 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1173 && (softInputMode &
1174 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1175 // There is a focus view, and we are navigating forward
1176 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001177 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
1180 break;
1181 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1182 // Do nothing.
1183 break;
1184 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1185 if ((softInputMode &
1186 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001187 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001188 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 break;
1191 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001192 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001193 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 break;
1195 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1196 if ((softInputMode &
1197 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001198 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001199 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 break;
1202 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001204 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 break;
1206 }
1207 }
1208 } finally {
1209 Binder.restoreCallingIdentity(ident);
1210 }
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1214 synchronized (mMethodMap) {
1215 if (mCurClient == null || client == null
1216 || mCurClient.client.asBinder() != client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001217 Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219
1220 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1221 }
1222 }
1223
1224 public void setInputMethod(IBinder token, String id) {
1225 synchronized (mMethodMap) {
1226 if (token == null) {
1227 if (mContext.checkCallingOrSelfPermission(
1228 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1229 != PackageManager.PERMISSION_GRANTED) {
1230 throw new SecurityException(
1231 "Using null token requires permission "
1232 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1233 }
1234 } else if (mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001235 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 return;
1237 }
1238
1239 long ident = Binder.clearCallingIdentity();
1240 try {
1241 setInputMethodLocked(id);
1242 } finally {
1243 Binder.restoreCallingIdentity(ident);
1244 }
1245 }
1246 }
1247
1248 public void hideMySoftInput(IBinder token, int flags) {
1249 synchronized (mMethodMap) {
1250 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 return;
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 long ident = Binder.clearCallingIdentity();
1255 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001256 hideCurrentInputLocked(flags, null);
1257 } finally {
1258 Binder.restoreCallingIdentity(ident);
1259 }
1260 }
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project4df24232009-03-05 14:34:35 -08001263 public void showMySoftInput(IBinder token, int flags) {
1264 synchronized (mMethodMap) {
1265 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001266 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001267 return;
1268 }
1269 long ident = Binder.clearCallingIdentity();
1270 try {
1271 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 } finally {
1273 Binder.restoreCallingIdentity(ident);
1274 }
1275 }
1276 }
1277
1278 void setEnabledSessionInMainThread(SessionState session) {
1279 if (mEnabledSession != session) {
1280 if (mEnabledSession != null) {
1281 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001282 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mEnabledSession.method.setSessionEnabled(
1284 mEnabledSession.session, false);
1285 } catch (RemoteException e) {
1286 }
1287 }
1288 mEnabledSession = session;
1289 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001290 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 session.method.setSessionEnabled(
1292 session.session, true);
1293 } catch (RemoteException e) {
1294 }
1295 }
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 public boolean handleMessage(Message msg) {
1299 HandlerCaller.SomeArgs args;
1300 switch (msg.what) {
1301 case MSG_SHOW_IM_PICKER:
1302 showInputMethodMenu();
1303 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 case MSG_UNBIND_INPUT:
1308 try {
1309 ((IInputMethod)msg.obj).unbindInput();
1310 } catch (RemoteException e) {
1311 // There is nothing interesting about the method dying.
1312 }
1313 return true;
1314 case MSG_BIND_INPUT:
1315 args = (HandlerCaller.SomeArgs)msg.obj;
1316 try {
1317 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1318 } catch (RemoteException e) {
1319 }
1320 return true;
1321 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001322 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1325 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 } catch (RemoteException e) {
1327 }
1328 return true;
1329 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001332 ((IInputMethod)args.arg1).hideSoftInput(0,
1333 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 } catch (RemoteException e) {
1335 }
1336 return true;
1337 case MSG_ATTACH_TOKEN:
1338 args = (HandlerCaller.SomeArgs)msg.obj;
1339 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001340 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1342 } catch (RemoteException e) {
1343 }
1344 return true;
1345 case MSG_CREATE_SESSION:
1346 args = (HandlerCaller.SomeArgs)msg.obj;
1347 try {
1348 ((IInputMethod)args.arg1).createSession(
1349 (IInputMethodCallback)args.arg2);
1350 } catch (RemoteException e) {
1351 }
1352 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 case MSG_START_INPUT:
1356 args = (HandlerCaller.SomeArgs)msg.obj;
1357 try {
1358 SessionState session = (SessionState)args.arg1;
1359 setEnabledSessionInMainThread(session);
1360 session.method.startInput((IInputContext)args.arg2,
1361 (EditorInfo)args.arg3);
1362 } catch (RemoteException e) {
1363 }
1364 return true;
1365 case MSG_RESTART_INPUT:
1366 args = (HandlerCaller.SomeArgs)msg.obj;
1367 try {
1368 SessionState session = (SessionState)args.arg1;
1369 setEnabledSessionInMainThread(session);
1370 session.method.restartInput((IInputContext)args.arg2,
1371 (EditorInfo)args.arg3);
1372 } catch (RemoteException e) {
1373 }
1374 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 case MSG_UNBIND_METHOD:
1379 try {
1380 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1381 } catch (RemoteException e) {
1382 // There is nothing interesting about the last client dying.
1383 }
1384 return true;
1385 case MSG_BIND_METHOD:
1386 args = (HandlerCaller.SomeArgs)msg.obj;
1387 try {
1388 ((IInputMethodClient)args.arg1).onBindMethod(
1389 (InputBindResult)args.arg2);
1390 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001391 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393 return true;
1394 }
1395 return false;
1396 }
1397
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001398 private boolean isSystemIme(InputMethodInfo inputMethod) {
1399 return (inputMethod.getServiceInfo().applicationInfo.flags
1400 & ApplicationInfo.FLAG_SYSTEM) != 0;
1401 }
1402
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001403 private boolean chooseNewDefaultIMELocked() {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001404 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1405 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001406 // We'd prefer to fall back on a system IME, since that is safer.
1407 int i=enabled.size();
1408 while (i > 0) {
1409 i--;
1410 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1411 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1412 break;
1413 }
1414 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001415 Settings.Secure.putString(mContext.getContentResolver(),
1416 Settings.Secure.DEFAULT_INPUT_METHOD,
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001417 enabled.get(i).getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001418 return true;
1419 }
1420
1421 return false;
1422 }
1423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1425 HashMap<String, InputMethodInfo> map) {
1426 list.clear();
1427 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001430 final Configuration config = mContext.getResources().getConfiguration();
1431 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1432 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1433 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1434 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435
1436 List<ResolveInfo> services = pm.queryIntentServices(
1437 new Intent(InputMethod.SERVICE_INTERFACE),
1438 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 for (int i = 0; i < services.size(); ++i) {
1441 ResolveInfo ri = services.get(i);
1442 ServiceInfo si = ri.serviceInfo;
1443 ComponentName compName = new ComponentName(si.packageName, si.name);
1444 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1445 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001446 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 + ": it does not require the permission "
1448 + android.Manifest.permission.BIND_INPUT_METHOD);
1449 continue;
1450 }
1451
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453
1454 try {
1455 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1456 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001457 final String id = p.getId();
1458 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
Amith Yamasanie861ec12010-03-24 21:39:27 -07001460 // System IMEs are enabled by default, unless there's a hard keyboard
1461 // and the system IME was explicitly disabled
1462 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1463 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001464 }
1465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001471 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001473 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001476
1477 String defaultIme = Settings.Secure.getString(mContext
1478 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1479 if (!map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001480 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001481 updateFromSettingsLocked();
1482 }
1483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 void showInputMethodMenu() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 String lastInputMethodId = Settings.Secure.getString(context
1496 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001497 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001501 if (immis == null) {
1502 return;
1503 }
1504
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001505 synchronized (mMethodMap) {
1506 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001508 int N = immis.size();
1509
1510 mItems = new CharSequence[N];
1511 mIms = new InputMethodInfo[N];
1512
1513 int j = 0;
1514 for (int i = 0; i < N; ++i) {
1515 InputMethodInfo property = immis.get(i);
1516 if (property == null) {
1517 continue;
1518 }
1519 mItems[j] = property.loadLabel(pm);
1520 mIms[j] = property;
1521 j++;
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001522 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001523
1524 int checkedItem = 0;
1525 for (int i = 0; i < N; ++i) {
1526 if (mIms[i].getId().equals(lastInputMethodId)) {
1527 checkedItem = i;
1528 break;
1529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001531
1532 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1533 public void onClick(DialogInterface dialog, int which) {
1534 hideInputMethodMenu();
1535 }
1536 };
1537
1538 TypedArray a = context.obtainStyledAttributes(null,
1539 com.android.internal.R.styleable.DialogPreference,
1540 com.android.internal.R.attr.alertDialogStyle, 0);
1541 mDialogBuilder = new AlertDialog.Builder(context)
1542 .setTitle(com.android.internal.R.string.select_input_method)
1543 .setOnCancelListener(new OnCancelListener() {
1544 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001546 }
1547 })
1548 .setIcon(a.getDrawable(
1549 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1550 a.recycle();
1551
1552 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1553 new AlertDialog.OnClickListener() {
1554 public void onClick(DialogInterface dialog, int which) {
1555 synchronized (mMethodMap) {
1556 if (mIms == null || mIms.length <= which) {
1557 return;
1558 }
1559 InputMethodInfo im = mIms[which];
1560 hideInputMethodMenu();
1561 if (im != null) {
1562 setInputMethodLocked(im.getId());
1563 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001566 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 mSwitchingDialog = mDialogBuilder.create();
1569 mSwitchingDialog.getWindow().setType(
1570 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1571 mSwitchingDialog.show();
1572 }
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001576 synchronized (mMethodMap) {
1577 hideInputMethodMenuLocked();
1578 }
1579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
The Android Open Source Project10592532009-03-18 17:39:46 -07001581 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583
The Android Open Source Project10592532009-03-18 17:39:46 -07001584 if (mSwitchingDialog != null) {
1585 mSwitchingDialog.dismiss();
1586 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
The Android Open Source Project10592532009-03-18 17:39:46 -07001589 mDialogBuilder = null;
1590 mItems = null;
1591 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 public boolean setInputMethodEnabled(String id, boolean enabled) {
1597 synchronized (mMethodMap) {
1598 if (mContext.checkCallingOrSelfPermission(
1599 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1600 != PackageManager.PERMISSION_GRANTED) {
1601 throw new SecurityException(
1602 "Requires permission "
1603 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1604 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 long ident = Binder.clearCallingIdentity();
1607 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001608 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 } finally {
1610 Binder.restoreCallingIdentity(ident);
1611 }
1612 }
1613 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001614
1615 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1616 // Make sure this is a valid input method.
1617 InputMethodInfo imm = mMethodMap.get(id);
1618 if (imm == null) {
1619 if (imm == null) {
1620 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1621 }
1622 }
1623
1624 StringBuilder builder = new StringBuilder(256);
1625
1626 boolean removed = false;
1627 String firstId = null;
1628
1629 // Look through the currently enabled input methods.
1630 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1631 Settings.Secure.ENABLED_INPUT_METHODS);
1632 if (enabledStr != null) {
1633 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1634 splitter.setString(enabledStr);
1635 while (splitter.hasNext()) {
1636 String curId = splitter.next();
1637 if (curId.equals(id)) {
1638 if (enabled) {
1639 // We are enabling this input method, but it is
1640 // already enabled. Nothing to do. The previous
1641 // state was enabled.
1642 return true;
1643 }
1644 // We are disabling this input method, and it is
1645 // currently enabled. Skip it to remove from the
1646 // new list.
1647 removed = true;
1648 } else if (!enabled) {
1649 // We are building a new list of input methods that
1650 // doesn't contain the given one.
1651 if (firstId == null) firstId = curId;
1652 if (builder.length() > 0) builder.append(':');
1653 builder.append(curId);
1654 }
1655 }
1656 }
1657
1658 if (!enabled) {
1659 if (!removed) {
1660 // We are disabling the input method but it is already
1661 // disabled. Nothing to do. The previous state was
1662 // disabled.
1663 return false;
1664 }
1665 // Update the setting with the new list of input methods.
1666 Settings.Secure.putString(mContext.getContentResolver(),
1667 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1668 // We the disabled input method is currently selected, switch
1669 // to another one.
1670 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1671 Settings.Secure.DEFAULT_INPUT_METHOD);
1672 if (id.equals(selId)) {
1673 Settings.Secure.putString(mContext.getContentResolver(),
1674 Settings.Secure.DEFAULT_INPUT_METHOD,
1675 firstId != null ? firstId : "");
1676 }
1677 // Previous state was enabled.
1678 return true;
1679 }
1680
1681 // Add in the newly enabled input method.
1682 if (enabledStr == null || enabledStr.length() == 0) {
1683 enabledStr = id;
1684 } else {
1685 enabledStr = enabledStr + ':' + id;
1686 }
1687
1688 Settings.Secure.putString(mContext.getContentResolver(),
1689 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
1690
1691 // Previous state was disabled.
1692 return false;
1693 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 @Override
1698 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1699 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1700 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1703 + Binder.getCallingPid()
1704 + ", uid=" + Binder.getCallingUid());
1705 return;
1706 }
1707
1708 IInputMethod method;
1709 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 synchronized (mMethodMap) {
1714 p.println("Current Input Method Manager state:");
1715 int N = mMethodList.size();
1716 p.println(" Input Methods:");
1717 for (int i=0; i<N; i++) {
1718 InputMethodInfo info = mMethodList.get(i);
1719 p.println(" InputMethod #" + i + ":");
1720 info.dump(p, " ");
1721 }
1722 p.println(" Clients:");
1723 for (ClientState ci : mClients.values()) {
1724 p.println(" Client " + ci + ":");
1725 p.println(" client=" + ci.client);
1726 p.println(" inputContext=" + ci.inputContext);
1727 p.println(" sessionRequested=" + ci.sessionRequested);
1728 p.println(" curSession=" + ci.curSession);
1729 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001730 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001732 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1733 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1735 + " mBoundToMethod=" + mBoundToMethod);
1736 p.println(" mCurToken=" + mCurToken);
1737 p.println(" mCurIntent=" + mCurIntent);
1738 method = mCurMethod;
1739 p.println(" mCurMethod=" + mCurMethod);
1740 p.println(" mEnabledSession=" + mEnabledSession);
1741 p.println(" mShowRequested=" + mShowRequested
1742 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1743 + " mShowForced=" + mShowForced
1744 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001745 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 if (client != null) {
1749 p.println(" ");
1750 pw.flush();
1751 try {
1752 client.client.asBinder().dump(fd, args);
1753 } catch (RemoteException e) {
1754 p.println("Input method client dead: " + e);
1755 }
1756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 if (method != null) {
1759 p.println(" ");
1760 pw.flush();
1761 try {
1762 method.asBinder().dump(fd, args);
1763 } catch (RemoteException e) {
1764 p.println("Input method service dead: " + e);
1765 }
1766 }
1767 }
1768}