blob: 36b3a5ecee884e8811b57f862c01bf28745a55d5 [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);
Yuncheol Heo8b70c9c2010-05-12 13:35:55 +0900472 final String defaultIme = Settings.Secure.getString(mContext
473 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
474 if (enabledStr == null || TextUtils.isEmpty(defaultIme)) {
475 Slog.i(TAG, "Enabled input methods or default IME has not been set, enabling all");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 InputMethodInfo defIm = null;
477 StringBuilder sb = new StringBuilder(256);
478 final int N = mMethodList.size();
479 for (int i=0; i<N; i++) {
480 InputMethodInfo imi = mMethodList.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800481 Slog.i(TAG, "Adding: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 if (i > 0) sb.append(':');
483 sb.append(imi.getId());
484 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
485 try {
486 Resources res = mContext.createPackageContext(
487 imi.getPackageName(), 0).getResources();
488 if (res.getBoolean(imi.getIsDefaultResourceId())) {
489 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800490 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 } catch (PackageManager.NameNotFoundException ex) {
493 } catch (Resources.NotFoundException ex) {
494 }
495 }
496 }
497 if (defIm == null && N > 0) {
498 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800499 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501 Settings.Secure.putString(mContext.getContentResolver(),
502 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
503 if (defIm != null) {
504 Settings.Secure.putString(mContext.getContentResolver(),
505 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
506 }
507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mStatusBar = statusBar;
Joe Onorato0cbda992010-05-02 16:28:15 -0700510 statusBar.setIconVisibility("ime", false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 mSettingsObserver = new SettingsObserver(mHandler);
513 updateFromSettingsLocked();
514 }
515
516 @Override
517 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
518 throws RemoteException {
519 try {
520 return super.onTransact(code, data, reply, flags);
521 } catch (RuntimeException e) {
522 // The input method manager only throws security exceptions, so let's
523 // log all others.
524 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 throw e;
528 }
529 }
530
531 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700532 synchronized (mMethodMap) {
533 if (!mSystemReady) {
534 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700535 try {
536 startInputInnerLocked();
537 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700539 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700540 }
541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 public List<InputMethodInfo> getInputMethodList() {
545 synchronized (mMethodMap) {
546 return new ArrayList<InputMethodInfo>(mMethodList);
547 }
548 }
549
550 public List<InputMethodInfo> getEnabledInputMethodList() {
551 synchronized (mMethodMap) {
552 return getEnabledInputMethodListLocked();
553 }
554 }
555
556 List<InputMethodInfo> getEnabledInputMethodListLocked() {
557 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 final String enabledStr = Settings.Secure.getString(
560 mContext.getContentResolver(),
561 Settings.Secure.ENABLED_INPUT_METHODS);
562 if (enabledStr != null) {
563 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
564 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 while (splitter.hasNext()) {
567 InputMethodInfo info = mMethodMap.get(splitter.next());
568 if (info != null) {
569 res.add(info);
570 }
571 }
572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 return res;
575 }
576
577 public void addClient(IInputMethodClient client,
578 IInputContext inputContext, int uid, int pid) {
579 synchronized (mMethodMap) {
580 mClients.put(client.asBinder(), new ClientState(client,
581 inputContext, uid, pid));
582 }
583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 public void removeClient(IInputMethodClient client) {
586 synchronized (mMethodMap) {
587 mClients.remove(client.asBinder());
588 }
589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 void executeOrSendMessage(IInterface target, Message msg) {
592 if (target.asBinder() instanceof Binder) {
593 mCaller.sendMessage(msg);
594 } else {
595 handleMessage(msg);
596 msg.recycle();
597 }
598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800599
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700600 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800602 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 + mCurClient.client.asBinder());
604 if (mBoundToMethod) {
605 mBoundToMethod = false;
606 if (mCurMethod != null) {
607 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
608 MSG_UNBIND_INPUT, mCurMethod));
609 }
610 }
611 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
612 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
613 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 // Call setActive(false) on the old client
616 try {
617 mCurClient.client.setActive(false);
618 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800619 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 + mCurClient.pid + " uid " + mCurClient.uid);
621 }
622 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800623
The Android Open Source Project10592532009-03-18 17:39:46 -0700624 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 }
626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 private int getImeShowFlags() {
629 int flags = 0;
630 if (mShowForced) {
631 flags |= InputMethod.SHOW_FORCED
632 | InputMethod.SHOW_EXPLICIT;
633 } else if (mShowExplicitlyRequested) {
634 flags |= InputMethod.SHOW_EXPLICIT;
635 }
636 return flags;
637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 private int getAppShowFlags() {
640 int flags = 0;
641 if (mShowForced) {
642 flags |= InputMethodManager.SHOW_FORCED;
643 } else if (!mShowExplicitlyRequested) {
644 flags |= InputMethodManager.SHOW_IMPLICIT;
645 }
646 return flags;
647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
650 if (!mBoundToMethod) {
651 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
652 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
653 mBoundToMethod = true;
654 }
655 final SessionState session = mCurClient.curSession;
656 if (initial) {
657 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
658 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
659 } else {
660 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
661 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
662 }
663 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800664 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800665 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 return needResult
668 ? new InputBindResult(session.session, mCurId, mCurSeq)
669 : null;
670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 InputBindResult startInputLocked(IInputMethodClient client,
673 IInputContext inputContext, EditorInfo attribute,
674 boolean initial, boolean needResult) {
675 // If no method is currently selected, do nothing.
676 if (mCurMethodId == null) {
677 return mNoBinding;
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 ClientState cs = mClients.get(client.asBinder());
681 if (cs == null) {
682 throw new IllegalArgumentException("unknown client "
683 + client.asBinder());
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 try {
687 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
688 // Check with the window manager to make sure this client actually
689 // has a window with focus. If not, reject. This is thread safe
690 // because if the focus changes some time before or after, the
691 // next client receiving focus that has any interest in input will
692 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
695 return null;
696 }
697 } catch (RemoteException e) {
698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 if (mCurClient != cs) {
701 // If the client is changing, we need to switch over to the new
702 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700703 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800704 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 + cs.client.asBinder());
706
707 // If the screen is on, inform the new client it is active
708 if (mScreenOn) {
709 try {
710 cs.client.setActive(mScreenOn);
711 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800712 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 + cs.pid + " uid " + cs.uid);
714 }
715 }
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 // Bump up the sequence for this client and attach it.
719 mCurSeq++;
720 if (mCurSeq <= 0) mCurSeq = 1;
721 mCurClient = cs;
722 mCurInputContext = inputContext;
723 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 // Check if the input method is changing.
726 if (mCurId != null && mCurId.equals(mCurMethodId)) {
727 if (cs.curSession != null) {
728 // Fast case: if we are already connected to the input method,
729 // then just return it.
730 return attachNewInputLocked(initial, needResult);
731 }
732 if (mHaveConnection) {
733 if (mCurMethod != null) {
734 if (!cs.sessionRequested) {
735 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
738 MSG_CREATE_SESSION, mCurMethod,
739 new MethodCallback(mCurMethod)));
740 }
741 // Return to client, and we will get back with it when
742 // we have had a session made for it.
743 return new InputBindResult(null, mCurId, mCurSeq);
744 } else if (SystemClock.uptimeMillis()
745 < (mLastBindTime+TIME_TO_RECONNECT)) {
746 // In this case we have connected to the service, but
747 // don't yet have its interface. If it hasn't been too
748 // long since we did the connection, we'll return to
749 // the client and wait to get the service interface so
750 // we can report back. If it has been too long, we want
751 // to fall through so we can try a disconnect/reconnect
752 // to see if we can get back in touch with the service.
753 return new InputBindResult(null, mCurId, mCurSeq);
754 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
756 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758 }
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700761 return startInputInnerLocked();
762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700764 InputBindResult startInputInnerLocked() {
765 if (mCurMethodId == null) {
766 return mNoBinding;
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700769 if (!mSystemReady) {
770 // If the system is not yet ready, we shouldn't be running third
771 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700772 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 InputMethodInfo info = mMethodMap.get(mCurMethodId);
776 if (info == null) {
777 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700780 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
783 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700784 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
785 com.android.internal.R.string.input_method_binding_label);
786 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
787 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
789 mLastBindTime = SystemClock.uptimeMillis();
790 mHaveConnection = true;
791 mCurId = info.getId();
792 mCurToken = new Binder();
793 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 mIWindowManager.addWindowToken(mCurToken,
796 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
797 } catch (RemoteException e) {
798 }
799 return new InputBindResult(null, mCurId, mCurSeq);
800 } else {
801 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800802 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 + mCurIntent);
804 }
805 return null;
806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 public InputBindResult startInput(IInputMethodClient client,
809 IInputContext inputContext, EditorInfo attribute,
810 boolean initial, boolean needResult) {
811 synchronized (mMethodMap) {
812 final long ident = Binder.clearCallingIdentity();
813 try {
814 return startInputLocked(client, inputContext, attribute,
815 initial, needResult);
816 } finally {
817 Binder.restoreCallingIdentity(ident);
818 }
819 }
820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 public void finishInput(IInputMethodClient client) {
823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 public void onServiceConnected(ComponentName name, IBinder service) {
826 synchronized (mMethodMap) {
827 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
828 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700829 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700831 unbindCurrentMethodLocked(false);
832 return;
833 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800834 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700835 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
836 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800838 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700839 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700841 MSG_CREATE_SESSION, mCurMethod,
842 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844 }
845 }
846 }
847
848 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
849 synchronized (mMethodMap) {
850 if (mCurMethod != null && method != null
851 && mCurMethod.asBinder() == method.asBinder()) {
852 if (mCurClient != null) {
853 mCurClient.curSession = new SessionState(mCurClient,
854 method, session);
855 mCurClient.sessionRequested = false;
856 InputBindResult res = attachNewInputLocked(true, true);
857 if (res.method != null) {
858 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
859 MSG_BIND_METHOD, mCurClient.client, res));
860 }
861 }
862 }
863 }
864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700866 void unbindCurrentMethodLocked(boolean reportToClient) {
867 if (mHaveConnection) {
868 mContext.unbindService(this);
869 mHaveConnection = false;
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 if (mCurToken != null) {
873 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800874 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700875 mIWindowManager.removeWindowToken(mCurToken);
876 } catch (RemoteException e) {
877 }
878 mCurToken = null;
879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800880
The Android Open Source Project10592532009-03-18 17:39:46 -0700881 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700882 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800883
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700884 if (reportToClient && mCurClient != null) {
885 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
886 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
887 }
888 }
889
Devin Taylor0c33ed22010-02-23 13:26:46 -0600890 private void finishSession(SessionState sessionState) {
891 if (sessionState != null && sessionState.session != null) {
892 try {
893 sessionState.session.finishSession();
894 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700895 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600896 }
897 }
898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700900 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (mCurMethod != null) {
902 for (ClientState cs : mClients.values()) {
903 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600904 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 cs.curSession = null;
906 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600907
908 finishSession(mEnabledSession);
909 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 mCurMethod = null;
911 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700912 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 public void onServiceDisconnected(ComponentName name) {
916 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800917 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 + " mCurIntent=" + mCurIntent);
919 if (mCurMethod != null && mCurIntent != null
920 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700921 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // We consider this to be a new bind attempt, since the system
923 // should now try to restart the service for us.
924 mLastBindTime = SystemClock.uptimeMillis();
925 mShowRequested = mInputShown;
926 mInputShown = false;
927 if (mCurClient != null) {
928 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
929 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
930 }
931 }
932 }
933 }
934
935 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
936 long ident = Binder.clearCallingIdentity();
937 try {
938 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800939 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 return;
941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 synchronized (mMethodMap) {
944 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700946 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800948 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700949 mStatusBar.setIcon("ime", packageName, iconId, 0);
950 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 }
953 } finally {
954 Binder.restoreCallingIdentity(ident);
955 }
956 }
957
958 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700959 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
960 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
961 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
962 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 String id = Settings.Secure.getString(mContext.getContentResolver(),
964 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700965 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 try {
967 setInputMethodLocked(id);
968 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800969 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700970 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700971 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700973 } else {
974 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700975 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 void setInputMethodLocked(String id) {
981 InputMethodInfo info = mMethodMap.get(id);
982 if (info == null) {
Tadashi G. Takaokad88390f52010-07-12 10:01:37 -0700983 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 if (id.equals(mCurMethodId)) {
987 return;
988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 final long ident = Binder.clearCallingIdentity();
991 try {
992 mCurMethodId = id;
993 Settings.Secure.putString(mContext.getContentResolver(),
994 Settings.Secure.DEFAULT_INPUT_METHOD, id);
995
996 if (ActivityManagerNative.isSystemReady()) {
997 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800998 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 intent.putExtra("input_method_id", id);
1000 mContext.sendBroadcast(intent);
1001 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001002 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 } finally {
1004 Binder.restoreCallingIdentity(ident);
1005 }
1006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
The Android Open Source Project4df24232009-03-05 14:34:35 -08001008 public boolean showSoftInput(IInputMethodClient client, int flags,
1009 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 long ident = Binder.clearCallingIdentity();
1011 try {
1012 synchronized (mMethodMap) {
1013 if (mCurClient == null || client == null
1014 || mCurClient.client.asBinder() != client.asBinder()) {
1015 try {
1016 // We need to check if this is the current client with
1017 // focus in the window manager, to allow this call to
1018 // be made before input is started in it.
1019 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001020 Slog.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001021 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001024 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
Joe Onorato8a9b2202010-02-26 18:56:32 -08001028 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001029 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031 } finally {
1032 Binder.restoreCallingIdentity(ident);
1033 }
1034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001035
The Android Open Source Project4df24232009-03-05 14:34:35 -08001036 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 mShowRequested = true;
1038 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1039 mShowExplicitlyRequested = true;
1040 }
1041 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1042 mShowExplicitlyRequested = true;
1043 mShowForced = true;
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
Dianne Hackborncc278702009-09-02 23:07:23 -07001046 if (!mSystemReady) {
1047 return false;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Project4df24232009-03-05 14:34:35 -08001050 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001052 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1053 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1054 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001056 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 } else if (mHaveConnection && SystemClock.uptimeMillis()
1058 < (mLastBindTime+TIME_TO_RECONNECT)) {
1059 // The client has asked to have the input method shown, but
1060 // we have been sitting here too long with a connection to the
1061 // service and no interface received, so let's disconnect/connect
1062 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001063 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 SystemClock.uptimeMillis()-mLastBindTime,1);
1065 mContext.unbindService(this);
1066 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
The Android Open Source Project4df24232009-03-05 14:34:35 -08001069 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Project4df24232009-03-05 14:34:35 -08001072 public boolean hideSoftInput(IInputMethodClient client, int flags,
1073 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 long ident = Binder.clearCallingIdentity();
1075 try {
1076 synchronized (mMethodMap) {
1077 if (mCurClient == null || client == null
1078 || mCurClient.client.asBinder() != client.asBinder()) {
1079 try {
1080 // We need to check if this is the current client with
1081 // focus in the window manager, to allow this call to
1082 // be made before input is started in it.
1083 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 Slog.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001085 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001088 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001093 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095 } finally {
1096 Binder.restoreCallingIdentity(ident);
1097 }
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project4df24232009-03-05 14:34:35 -08001100 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1102 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001103 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001105 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
1107 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001108 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001110 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001114 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1115 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1116 res = true;
1117 } else {
1118 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 mInputShown = false;
1121 mShowRequested = false;
1122 mShowExplicitlyRequested = false;
1123 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001124 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001127 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1129 boolean first, int windowFlags) {
1130 long ident = Binder.clearCallingIdentity();
1131 try {
1132 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001133 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 + " viewHasFocus=" + viewHasFocus
1135 + " isTextEditor=" + isTextEditor
1136 + " softInputMode=#" + Integer.toHexString(softInputMode)
1137 + " first=" + first + " flags=#"
1138 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (mCurClient == null || client == null
1141 || mCurClient.client.asBinder() != client.asBinder()) {
1142 try {
1143 // We need to check if this is the current client with
1144 // focus in the window manager, to allow this call to
1145 // be made before input is started in it.
1146 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001147 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 return;
1149 }
1150 } catch (RemoteException e) {
1151 }
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001154 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001155 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001156 return;
1157 }
1158 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1161 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1162 if (!isTextEditor || (softInputMode &
1163 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1164 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1165 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1166 // There is no focus view, and this window will
1167 // be behind any soft input window, so hide the
1168 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 } else if (isTextEditor && (softInputMode &
1173 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1174 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1175 && (softInputMode &
1176 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1177 // There is a focus view, and we are navigating forward
1178 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001179 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001180 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 break;
1183 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1184 // Do nothing.
1185 break;
1186 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1187 if ((softInputMode &
1188 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001189 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001190 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 break;
1193 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001194 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001195 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 break;
1197 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1198 if ((softInputMode &
1199 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001200 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001201 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 break;
1204 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001205 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001206 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 break;
1208 }
1209 }
1210 } finally {
1211 Binder.restoreCallingIdentity(ident);
1212 }
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1216 synchronized (mMethodMap) {
1217 if (mCurClient == null || client == null
1218 || mCurClient.client.asBinder() != client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001219 Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221
1222 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1223 }
1224 }
1225
1226 public void setInputMethod(IBinder token, String id) {
1227 synchronized (mMethodMap) {
1228 if (token == null) {
1229 if (mContext.checkCallingOrSelfPermission(
1230 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1231 != PackageManager.PERMISSION_GRANTED) {
1232 throw new SecurityException(
1233 "Using null token requires permission "
1234 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1235 }
1236 } else if (mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 return;
1239 }
1240
1241 long ident = Binder.clearCallingIdentity();
1242 try {
1243 setInputMethodLocked(id);
1244 } finally {
1245 Binder.restoreCallingIdentity(ident);
1246 }
1247 }
1248 }
1249
1250 public void hideMySoftInput(IBinder token, int flags) {
1251 synchronized (mMethodMap) {
1252 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001253 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 return;
1255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 long ident = Binder.clearCallingIdentity();
1257 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001258 hideCurrentInputLocked(flags, null);
1259 } finally {
1260 Binder.restoreCallingIdentity(ident);
1261 }
1262 }
1263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
The Android Open Source Project4df24232009-03-05 14:34:35 -08001265 public void showMySoftInput(IBinder token, int flags) {
1266 synchronized (mMethodMap) {
1267 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001268 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001269 return;
1270 }
1271 long ident = Binder.clearCallingIdentity();
1272 try {
1273 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 } finally {
1275 Binder.restoreCallingIdentity(ident);
1276 }
1277 }
1278 }
1279
1280 void setEnabledSessionInMainThread(SessionState session) {
1281 if (mEnabledSession != session) {
1282 if (mEnabledSession != null) {
1283 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001284 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 mEnabledSession.method.setSessionEnabled(
1286 mEnabledSession.session, false);
1287 } catch (RemoteException e) {
1288 }
1289 }
1290 mEnabledSession = session;
1291 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001292 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 session.method.setSessionEnabled(
1294 session.session, true);
1295 } catch (RemoteException e) {
1296 }
1297 }
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 public boolean handleMessage(Message msg) {
1301 HandlerCaller.SomeArgs args;
1302 switch (msg.what) {
1303 case MSG_SHOW_IM_PICKER:
1304 showInputMethodMenu();
1305 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 case MSG_UNBIND_INPUT:
1310 try {
1311 ((IInputMethod)msg.obj).unbindInput();
1312 } catch (RemoteException e) {
1313 // There is nothing interesting about the method dying.
1314 }
1315 return true;
1316 case MSG_BIND_INPUT:
1317 args = (HandlerCaller.SomeArgs)msg.obj;
1318 try {
1319 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1320 } catch (RemoteException e) {
1321 }
1322 return true;
1323 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001326 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1327 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 } catch (RemoteException e) {
1329 }
1330 return true;
1331 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001332 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001334 ((IInputMethod)args.arg1).hideSoftInput(0,
1335 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 } catch (RemoteException e) {
1337 }
1338 return true;
1339 case MSG_ATTACH_TOKEN:
1340 args = (HandlerCaller.SomeArgs)msg.obj;
1341 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001342 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1344 } catch (RemoteException e) {
1345 }
1346 return true;
1347 case MSG_CREATE_SESSION:
1348 args = (HandlerCaller.SomeArgs)msg.obj;
1349 try {
1350 ((IInputMethod)args.arg1).createSession(
1351 (IInputMethodCallback)args.arg2);
1352 } catch (RemoteException e) {
1353 }
1354 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 case MSG_START_INPUT:
1358 args = (HandlerCaller.SomeArgs)msg.obj;
1359 try {
1360 SessionState session = (SessionState)args.arg1;
1361 setEnabledSessionInMainThread(session);
1362 session.method.startInput((IInputContext)args.arg2,
1363 (EditorInfo)args.arg3);
1364 } catch (RemoteException e) {
1365 }
1366 return true;
1367 case MSG_RESTART_INPUT:
1368 args = (HandlerCaller.SomeArgs)msg.obj;
1369 try {
1370 SessionState session = (SessionState)args.arg1;
1371 setEnabledSessionInMainThread(session);
1372 session.method.restartInput((IInputContext)args.arg2,
1373 (EditorInfo)args.arg3);
1374 } catch (RemoteException e) {
1375 }
1376 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 case MSG_UNBIND_METHOD:
1381 try {
1382 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1383 } catch (RemoteException e) {
1384 // There is nothing interesting about the last client dying.
1385 }
1386 return true;
1387 case MSG_BIND_METHOD:
1388 args = (HandlerCaller.SomeArgs)msg.obj;
1389 try {
1390 ((IInputMethodClient)args.arg1).onBindMethod(
1391 (InputBindResult)args.arg2);
1392 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001393 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395 return true;
1396 }
1397 return false;
1398 }
1399
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001400 private boolean isSystemIme(InputMethodInfo inputMethod) {
1401 return (inputMethod.getServiceInfo().applicationInfo.flags
1402 & ApplicationInfo.FLAG_SYSTEM) != 0;
1403 }
1404
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001405 private boolean chooseNewDefaultIMELocked() {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001406 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1407 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001408 // We'd prefer to fall back on a system IME, since that is safer.
1409 int i=enabled.size();
1410 while (i > 0) {
1411 i--;
1412 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1413 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1414 break;
1415 }
1416 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001417 Settings.Secure.putString(mContext.getContentResolver(),
1418 Settings.Secure.DEFAULT_INPUT_METHOD,
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001419 enabled.get(i).getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001420 return true;
1421 }
1422
1423 return false;
1424 }
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1427 HashMap<String, InputMethodInfo> map) {
1428 list.clear();
1429 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001432 final Configuration config = mContext.getResources().getConfiguration();
1433 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1434 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1435 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1436 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437
1438 List<ResolveInfo> services = pm.queryIntentServices(
1439 new Intent(InputMethod.SERVICE_INTERFACE),
1440 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 for (int i = 0; i < services.size(); ++i) {
1443 ResolveInfo ri = services.get(i);
1444 ServiceInfo si = ri.serviceInfo;
1445 ComponentName compName = new ComponentName(si.packageName, si.name);
1446 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1447 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001448 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 + ": it does not require the permission "
1450 + android.Manifest.permission.BIND_INPUT_METHOD);
1451 continue;
1452 }
1453
Joe Onorato8a9b2202010-02-26 18:56:32 -08001454 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455
1456 try {
1457 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1458 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001459 final String id = p.getId();
1460 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
Amith Yamasanie861ec12010-03-24 21:39:27 -07001462 // System IMEs are enabled by default, unless there's a hard keyboard
1463 // and the system IME was explicitly disabled
1464 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1465 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001466 }
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } catch (XmlPullParserException 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 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001475 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
1477 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001478
1479 String defaultIme = Settings.Secure.getString(mContext
1480 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Yuncheol Heo8b70c9c2010-05-12 13:35:55 +09001481 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001482 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001483 updateFromSettingsLocked();
1484 }
1485 }
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 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 void showInputMethodMenu() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 String lastInputMethodId = Settings.Secure.getString(context
1498 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001503 if (immis == null) {
1504 return;
1505 }
1506
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001507 synchronized (mMethodMap) {
1508 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001510 int N = immis.size();
1511
1512 mItems = new CharSequence[N];
1513 mIms = new InputMethodInfo[N];
1514
1515 int j = 0;
1516 for (int i = 0; i < N; ++i) {
1517 InputMethodInfo property = immis.get(i);
1518 if (property == null) {
1519 continue;
1520 }
1521 mItems[j] = property.loadLabel(pm);
1522 mIms[j] = property;
1523 j++;
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001524 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001525
1526 int checkedItem = 0;
1527 for (int i = 0; i < N; ++i) {
1528 if (mIms[i].getId().equals(lastInputMethodId)) {
1529 checkedItem = i;
1530 break;
1531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001533
1534 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1535 public void onClick(DialogInterface dialog, int which) {
1536 hideInputMethodMenu();
1537 }
1538 };
1539
1540 TypedArray a = context.obtainStyledAttributes(null,
1541 com.android.internal.R.styleable.DialogPreference,
1542 com.android.internal.R.attr.alertDialogStyle, 0);
1543 mDialogBuilder = new AlertDialog.Builder(context)
1544 .setTitle(com.android.internal.R.string.select_input_method)
1545 .setOnCancelListener(new OnCancelListener() {
1546 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001548 }
1549 })
1550 .setIcon(a.getDrawable(
1551 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1552 a.recycle();
1553
1554 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1555 new AlertDialog.OnClickListener() {
1556 public void onClick(DialogInterface dialog, int which) {
1557 synchronized (mMethodMap) {
1558 if (mIms == null || mIms.length <= which) {
1559 return;
1560 }
1561 InputMethodInfo im = mIms[which];
1562 hideInputMethodMenu();
1563 if (im != null) {
1564 setInputMethodLocked(im.getId());
1565 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001568 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 mSwitchingDialog = mDialogBuilder.create();
1571 mSwitchingDialog.getWindow().setType(
1572 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1573 mSwitchingDialog.show();
1574 }
1575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001578 synchronized (mMethodMap) {
1579 hideInputMethodMenuLocked();
1580 }
1581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Project10592532009-03-18 17:39:46 -07001583 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585
The Android Open Source Project10592532009-03-18 17:39:46 -07001586 if (mSwitchingDialog != null) {
1587 mSwitchingDialog.dismiss();
1588 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
The Android Open Source Project10592532009-03-18 17:39:46 -07001591 mDialogBuilder = null;
1592 mItems = null;
1593 mIms = null;
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 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 public boolean setInputMethodEnabled(String id, boolean enabled) {
1599 synchronized (mMethodMap) {
1600 if (mContext.checkCallingOrSelfPermission(
1601 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1602 != PackageManager.PERMISSION_GRANTED) {
1603 throw new SecurityException(
1604 "Requires permission "
1605 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1606 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 long ident = Binder.clearCallingIdentity();
1609 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001610 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 } finally {
1612 Binder.restoreCallingIdentity(ident);
1613 }
1614 }
1615 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001616
1617 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1618 // Make sure this is a valid input method.
1619 InputMethodInfo imm = mMethodMap.get(id);
1620 if (imm == null) {
1621 if (imm == null) {
1622 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1623 }
1624 }
1625
1626 StringBuilder builder = new StringBuilder(256);
1627
1628 boolean removed = false;
1629 String firstId = null;
1630
1631 // Look through the currently enabled input methods.
1632 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1633 Settings.Secure.ENABLED_INPUT_METHODS);
1634 if (enabledStr != null) {
1635 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1636 splitter.setString(enabledStr);
1637 while (splitter.hasNext()) {
1638 String curId = splitter.next();
1639 if (curId.equals(id)) {
1640 if (enabled) {
1641 // We are enabling this input method, but it is
1642 // already enabled. Nothing to do. The previous
1643 // state was enabled.
1644 return true;
1645 }
1646 // We are disabling this input method, and it is
1647 // currently enabled. Skip it to remove from the
1648 // new list.
1649 removed = true;
1650 } else if (!enabled) {
1651 // We are building a new list of input methods that
1652 // doesn't contain the given one.
1653 if (firstId == null) firstId = curId;
1654 if (builder.length() > 0) builder.append(':');
1655 builder.append(curId);
1656 }
1657 }
1658 }
1659
1660 if (!enabled) {
1661 if (!removed) {
1662 // We are disabling the input method but it is already
1663 // disabled. Nothing to do. The previous state was
1664 // disabled.
1665 return false;
1666 }
1667 // Update the setting with the new list of input methods.
1668 Settings.Secure.putString(mContext.getContentResolver(),
1669 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1670 // We the disabled input method is currently selected, switch
1671 // to another one.
1672 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1673 Settings.Secure.DEFAULT_INPUT_METHOD);
1674 if (id.equals(selId)) {
1675 Settings.Secure.putString(mContext.getContentResolver(),
1676 Settings.Secure.DEFAULT_INPUT_METHOD,
1677 firstId != null ? firstId : "");
1678 }
1679 // Previous state was enabled.
1680 return true;
1681 }
1682
1683 // Add in the newly enabled input method.
1684 if (enabledStr == null || enabledStr.length() == 0) {
1685 enabledStr = id;
1686 } else {
1687 enabledStr = enabledStr + ':' + id;
1688 }
1689
1690 Settings.Secure.putString(mContext.getContentResolver(),
1691 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
1692
1693 // Previous state was disabled.
1694 return false;
1695 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 @Override
1700 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1701 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1702 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1705 + Binder.getCallingPid()
1706 + ", uid=" + Binder.getCallingUid());
1707 return;
1708 }
1709
1710 IInputMethod method;
1711 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 synchronized (mMethodMap) {
1716 p.println("Current Input Method Manager state:");
1717 int N = mMethodList.size();
1718 p.println(" Input Methods:");
1719 for (int i=0; i<N; i++) {
1720 InputMethodInfo info = mMethodList.get(i);
1721 p.println(" InputMethod #" + i + ":");
1722 info.dump(p, " ");
1723 }
1724 p.println(" Clients:");
1725 for (ClientState ci : mClients.values()) {
1726 p.println(" Client " + ci + ":");
1727 p.println(" client=" + ci.client);
1728 p.println(" inputContext=" + ci.inputContext);
1729 p.println(" sessionRequested=" + ci.sessionRequested);
1730 p.println(" curSession=" + ci.curSession);
1731 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001732 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001734 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1735 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1737 + " mBoundToMethod=" + mBoundToMethod);
1738 p.println(" mCurToken=" + mCurToken);
1739 p.println(" mCurIntent=" + mCurIntent);
1740 method = mCurMethod;
1741 p.println(" mCurMethod=" + mCurMethod);
1742 p.println(" mEnabledSession=" + mEnabledSession);
1743 p.println(" mShowRequested=" + mShowRequested
1744 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1745 + " mShowForced=" + mShowForced
1746 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001747 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 if (client != null) {
1751 p.println(" ");
1752 pw.flush();
1753 try {
1754 client.client.asBinder().dump(fd, args);
1755 } catch (RemoteException e) {
1756 p.println("Input method client dead: " + e);
1757 }
1758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 if (method != null) {
1761 p.println(" ");
1762 pw.flush();
1763 try {
1764 method.asBinder().dump(fd, args);
1765 } catch (RemoteException e) {
1766 p.println("Input method service dead: " + e);
1767 }
1768 }
1769 }
1770}