blob: 5a995aed847a9903093187f6173616fc77248ae0 [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
29import com.android.server.status.IconData;
30import com.android.server.status.StatusBarService;
31
32import org.xmlpull.v1.XmlPullParserException;
33
34import android.app.ActivityManagerNative;
35import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070036import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ComponentName;
38import android.content.ContentResolver;
39import android.content.Context;
40import android.content.DialogInterface;
41import android.content.IntentFilter;
42import android.content.DialogInterface.OnCancelListener;
43import android.content.Intent;
44import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070045import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.pm.PackageManager;
47import android.content.pm.ResolveInfo;
48import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070049import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.res.Resources;
51import android.content.res.TypedArray;
52import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Binder;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.IInterface;
57import android.os.Message;
58import android.os.Parcel;
59import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080060import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
63import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.provider.Settings.Secure;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.PrintWriterPrinter;
69import android.util.Printer;
70import android.view.IWindowManager;
71import android.view.WindowManager;
72import android.view.inputmethod.InputBinding;
73import android.view.inputmethod.InputMethod;
74import android.view.inputmethod.InputMethodInfo;
75import android.view.inputmethod.InputMethodManager;
76import android.view.inputmethod.EditorInfo;
77
78import java.io.FileDescriptor;
79import java.io.IOException;
80import java.io.PrintWriter;
Mac Wangcf06e2c2010-07-23 16:18:01 +080081import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.List;
Mac Wangcf06e2c2010-07-23 16:18:01 +080085import java.util.Map;
86import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * This class provides a system service that manages input methods.
90 */
91public class InputMethodManagerService extends IInputMethodManager.Stub
92 implements ServiceConnection, Handler.Callback {
93 static final boolean DEBUG = false;
94 static final String TAG = "InputManagerService";
95
96 static final int MSG_SHOW_IM_PICKER = 1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 static final int MSG_UNBIND_INPUT = 1000;
99 static final int MSG_BIND_INPUT = 1010;
100 static final int MSG_SHOW_SOFT_INPUT = 1020;
101 static final int MSG_HIDE_SOFT_INPUT = 1030;
102 static final int MSG_ATTACH_TOKEN = 1040;
103 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 static final int MSG_START_INPUT = 2000;
106 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 static final int MSG_UNBIND_METHOD = 3000;
109 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 final Context mContext;
114 final Handler mHandler;
115 final SettingsObserver mSettingsObserver;
116 final StatusBarService mStatusBar;
117 final IBinder mInputMethodIcon;
118 final IconData mInputMethodData;
119 final IWindowManager mIWindowManager;
120 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 // All known input methods. mMethodMap also serves as the global
125 // lock for this class.
126 final ArrayList<InputMethodInfo> mMethodList
127 = new ArrayList<InputMethodInfo>();
128 final HashMap<String, InputMethodInfo> mMethodMap
129 = new HashMap<String, InputMethodInfo>();
130
131 final TextUtils.SimpleStringSplitter mStringColonSplitter
132 = new TextUtils.SimpleStringSplitter(':');
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 class SessionState {
135 final ClientState client;
136 final IInputMethod method;
137 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 @Override
140 public String toString() {
141 return "SessionState{uid " + client.uid + " pid " + client.pid
142 + " method " + Integer.toHexString(
143 System.identityHashCode(method))
144 + " session " + Integer.toHexString(
145 System.identityHashCode(session))
146 + "}";
147 }
148
149 SessionState(ClientState _client, IInputMethod _method,
150 IInputMethodSession _session) {
151 client = _client;
152 method = _method;
153 session = _session;
154 }
155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 class ClientState {
158 final IInputMethodClient client;
159 final IInputContext inputContext;
160 final int uid;
161 final int pid;
162 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 boolean sessionRequested;
165 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 @Override
168 public String toString() {
169 return "ClientState{" + Integer.toHexString(
170 System.identityHashCode(this)) + " uid " + uid
171 + " pid " + pid + "}";
172 }
173
174 ClientState(IInputMethodClient _client, IInputContext _inputContext,
175 int _uid, int _pid) {
176 client = _client;
177 inputContext = _inputContext;
178 uid = _uid;
179 pid = _pid;
180 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
181 }
182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final HashMap<IBinder, ClientState> mClients
185 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700188 * Set once the system is ready to run third party code.
189 */
190 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Id of the currently selected input method.
194 */
195 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
198 * The current binding sequence number, incremented every time there is
199 * a new bind performed.
200 */
201 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
204 * The client that is currently bound to an input method.
205 */
206 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700209 * The last window token that gained focus.
210 */
211 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * The input context last provided by the current client.
215 */
216 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 /**
219 * The attributes last provided by the current client.
220 */
221 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
224 * The input method ID of the input method service that we are currently
225 * connected to or in the process of connecting to.
226 */
227 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
230 * Set to true if our ServiceConnection is currently actively bound to
231 * a service (whether or not we have gotten its IBinder back yet).
232 */
233 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * Set if the client has asked for the input method to be shown.
237 */
238 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
241 * Set if we were explicitly told to show the input method.
242 */
243 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Set if we were forced to be shown.
247 */
248 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Set if we last told the input method to show itself.
252 */
253 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * The Intent used to connect to the current input method.
257 */
258 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * The token we have made for the currently active input method, to
262 * identify it in the future.
263 */
264 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * If non-null, this is the input method service we are currently connected
268 * to.
269 */
270 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * Time that we last initiated a bind to the input method, to determine
274 * if we should try to disconnect and reconnect to it.
275 */
276 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Have we called mCurMethod.bindInput()?
280 */
281 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Currently enabled session. Only touched by service thread, not
285 * protected by a lock.
286 */
287 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * True if the screen is on. The value is true initially.
291 */
292 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 AlertDialog.Builder mDialogBuilder;
295 AlertDialog mSwitchingDialog;
296 InputMethodInfo[] mIms;
297 CharSequence[] mItems;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 class SettingsObserver extends ContentObserver {
300 SettingsObserver(Handler handler) {
301 super(handler);
302 ContentResolver resolver = mContext.getContentResolver();
303 resolver.registerContentObserver(Settings.Secure.getUriFor(
304 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 @Override public void onChange(boolean selfChange) {
308 synchronized (mMethodMap) {
309 updateFromSettingsLocked();
310 }
311 }
312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
315 @Override
316 public void onReceive(Context context, Intent intent) {
317 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
318 mScreenOn = true;
319 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
320 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700321 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
322 hideInputMethodMenu();
323 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800325 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327
328 // Inform the current client of the change in active status
329 try {
330 if (mCurClient != null && mCurClient.client != null) {
331 mCurClient.client.setActive(mScreenOn);
332 }
333 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800334 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 + mCurClient.pid + " uid " + mCurClient.uid);
336 }
337 }
338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800340 class MyPackageMonitor extends PackageMonitor {
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800343 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800345 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
347 final int N = mMethodList.size();
348 if (curInputMethodId != null) {
349 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800350 InputMethodInfo imi = mMethodList.get(i);
351 if (imi.getId().equals(curInputMethodId)) {
352 for (String pkg : packages) {
353 if (imi.getPackageName().equals(pkg)) {
354 if (!doit) {
355 return true;
356 }
357
358 Settings.Secure.putString(mContext.getContentResolver(),
359 Settings.Secure.DEFAULT_INPUT_METHOD, "");
360 chooseNewDefaultIMELocked();
361 return true;
362 }
363 }
364 }
365 }
366 }
367 }
368 return false;
369 }
370
371 @Override
372 public void onSomePackagesChanged() {
373 synchronized (mMethodMap) {
374 InputMethodInfo curIm = null;
375 String curInputMethodId = Settings.Secure.getString(mContext
376 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
377 final int N = mMethodList.size();
378 if (curInputMethodId != null) {
379 for (int i=0; i<N; i++) {
380 InputMethodInfo imi = mMethodList.get(i);
381 if (imi.getId().equals(curInputMethodId)) {
382 curIm = imi;
383 }
384 int change = isPackageDisappearing(imi.getPackageName());
385 if (change == PACKAGE_TEMPORARY_CHANGE
386 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800387 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800388 + imi.getComponent());
389 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
391 }
392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800394 buildInputMethodListLocked(mMethodList, mMethodMap);
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800398 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 int change = isPackageDisappearing(curIm.getPackageName());
400 if (change == PACKAGE_TEMPORARY_CHANGE
401 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800402 ServiceInfo si = null;
403 try {
404 si = mContext.getPackageManager().getServiceInfo(
405 curIm.getComponent(), 0);
406 } catch (PackageManager.NameNotFoundException ex) {
407 }
408 if (si == null) {
409 // Uh oh, current input method is no longer around!
410 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800411 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800412 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800413 changed = true;
414 curIm = null;
415 curInputMethodId = "";
Joe Onorato8a9b2202010-02-26 18:56:32 -0800416 Slog.i(TAG, "Unsetting current input method");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800417 Settings.Secure.putString(mContext.getContentResolver(),
418 Settings.Secure.DEFAULT_INPUT_METHOD,
419 curInputMethodId);
420 }
421 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800422 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 }
424
425 if (curIm == null) {
426 // We currently don't have a default input method... is
427 // one now available?
428 changed = chooseNewDefaultIMELocked();
429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 if (changed) {
432 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434 }
435 }
436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 class MethodCallback extends IInputMethodCallback.Stub {
439 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 MethodCallback(IInputMethod method) {
442 mMethod = method;
443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public void finishedEvent(int seq, boolean handled) throws RemoteException {
446 }
447
448 public void sessionCreated(IInputMethodSession session) throws RemoteException {
449 onSessionCreated(mMethod, session);
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public InputMethodManagerService(Context context, StatusBarService statusBar) {
454 mContext = context;
455 mHandler = new Handler(this);
456 mIWindowManager = IWindowManager.Stub.asInterface(
457 ServiceManager.getService(Context.WINDOW_SERVICE));
458 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
459 public void executeMessage(Message msg) {
460 handleMessage(msg);
461 }
462 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 IntentFilter screenOnOffFilt = new IntentFilter();
467 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
468 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700469 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Srilakshmi Punuruefcac2f2010-08-09 23:48:12 -0700472 mStatusBar = statusBar;
473 mInputMethodData = IconData.makeIcon("ime", null, 0, 0, 0);
474 mInputMethodIcon = statusBar.addIcon(mInputMethodData, null);
475 statusBar.setIconVisibility(mInputMethodIcon, false);
476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 buildInputMethodListLocked(mMethodList, mMethodMap);
478
479 final String enabledStr = Settings.Secure.getString(
480 mContext.getContentResolver(),
481 Settings.Secure.ENABLED_INPUT_METHODS);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800482 Slog.i(TAG, "Enabled input methods: " + enabledStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 if (enabledStr == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800484 Slog.i(TAG, "Enabled input methods has not been set, enabling all");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 InputMethodInfo defIm = null;
486 StringBuilder sb = new StringBuilder(256);
487 final int N = mMethodList.size();
488 for (int i=0; i<N; i++) {
489 InputMethodInfo imi = mMethodList.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800490 Slog.i(TAG, "Adding: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 if (i > 0) sb.append(':');
492 sb.append(imi.getId());
493 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
494 try {
495 Resources res = mContext.createPackageContext(
496 imi.getPackageName(), 0).getResources();
497 if (res.getBoolean(imi.getIsDefaultResourceId())) {
498 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800499 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501 } catch (PackageManager.NameNotFoundException ex) {
502 } catch (Resources.NotFoundException ex) {
503 }
504 }
505 }
506 if (defIm == null && N > 0) {
507 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 Settings.Secure.putString(mContext.getContentResolver(),
511 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
512 if (defIm != null) {
513 Settings.Secure.putString(mContext.getContentResolver(),
514 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
515 }
516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 mSettingsObserver = new SettingsObserver(mHandler);
519 updateFromSettingsLocked();
520 }
521
522 @Override
523 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
524 throws RemoteException {
525 try {
526 return super.onTransact(code, data, reply, flags);
527 } catch (RuntimeException e) {
528 // The input method manager only throws security exceptions, so let's
529 // log all others.
530 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 throw e;
534 }
535 }
536
537 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700538 synchronized (mMethodMap) {
539 if (!mSystemReady) {
540 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700541 try {
542 startInputInnerLocked();
543 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800544 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700545 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700546 }
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public List<InputMethodInfo> getInputMethodList() {
551 synchronized (mMethodMap) {
552 return new ArrayList<InputMethodInfo>(mMethodList);
553 }
554 }
555
556 public List<InputMethodInfo> getEnabledInputMethodList() {
557 synchronized (mMethodMap) {
558 return getEnabledInputMethodListLocked();
559 }
560 }
561
562 List<InputMethodInfo> getEnabledInputMethodListLocked() {
563 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 final String enabledStr = Settings.Secure.getString(
566 mContext.getContentResolver(),
567 Settings.Secure.ENABLED_INPUT_METHODS);
568 if (enabledStr != null) {
569 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
570 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 while (splitter.hasNext()) {
573 InputMethodInfo info = mMethodMap.get(splitter.next());
574 if (info != null) {
575 res.add(info);
576 }
577 }
578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 return res;
581 }
582
583 public void addClient(IInputMethodClient client,
584 IInputContext inputContext, int uid, int pid) {
585 synchronized (mMethodMap) {
586 mClients.put(client.asBinder(), new ClientState(client,
587 inputContext, uid, pid));
588 }
589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 public void removeClient(IInputMethodClient client) {
592 synchronized (mMethodMap) {
593 mClients.remove(client.asBinder());
594 }
595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 void executeOrSendMessage(IInterface target, Message msg) {
598 if (target.asBinder() instanceof Binder) {
599 mCaller.sendMessage(msg);
600 } else {
601 handleMessage(msg);
602 msg.recycle();
603 }
604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800605
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700606 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800608 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 + mCurClient.client.asBinder());
610 if (mBoundToMethod) {
611 mBoundToMethod = false;
612 if (mCurMethod != null) {
613 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
614 MSG_UNBIND_INPUT, mCurMethod));
615 }
616 }
617 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
618 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
619 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 // Call setActive(false) on the old client
622 try {
623 mCurClient.client.setActive(false);
624 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800625 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 + mCurClient.pid + " uid " + mCurClient.uid);
627 }
628 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project10592532009-03-18 17:39:46 -0700630 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private int getImeShowFlags() {
635 int flags = 0;
636 if (mShowForced) {
637 flags |= InputMethod.SHOW_FORCED
638 | InputMethod.SHOW_EXPLICIT;
639 } else if (mShowExplicitlyRequested) {
640 flags |= InputMethod.SHOW_EXPLICIT;
641 }
642 return flags;
643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 private int getAppShowFlags() {
646 int flags = 0;
647 if (mShowForced) {
648 flags |= InputMethodManager.SHOW_FORCED;
649 } else if (!mShowExplicitlyRequested) {
650 flags |= InputMethodManager.SHOW_IMPLICIT;
651 }
652 return flags;
653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
656 if (!mBoundToMethod) {
657 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
658 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
659 mBoundToMethod = true;
660 }
661 final SessionState session = mCurClient.curSession;
662 if (initial) {
663 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
664 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
665 } else {
666 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
667 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
668 }
669 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800670 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800671 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 return needResult
674 ? new InputBindResult(session.session, mCurId, mCurSeq)
675 : null;
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 InputBindResult startInputLocked(IInputMethodClient client,
679 IInputContext inputContext, EditorInfo attribute,
680 boolean initial, boolean needResult) {
681 // If no method is currently selected, do nothing.
682 if (mCurMethodId == null) {
683 return mNoBinding;
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 ClientState cs = mClients.get(client.asBinder());
687 if (cs == null) {
688 throw new IllegalArgumentException("unknown client "
689 + client.asBinder());
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 try {
693 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
694 // Check with the window manager to make sure this client actually
695 // has a window with focus. If not, reject. This is thread safe
696 // because if the focus changes some time before or after, the
697 // next client receiving focus that has any interest in input will
698 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
701 return null;
702 }
703 } catch (RemoteException e) {
704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mCurClient != cs) {
707 // If the client is changing, we need to switch over to the new
708 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700709 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + cs.client.asBinder());
712
713 // If the screen is on, inform the new client it is active
714 if (mScreenOn) {
715 try {
716 cs.client.setActive(mScreenOn);
717 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800718 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 + cs.pid + " uid " + cs.uid);
720 }
721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 // Bump up the sequence for this client and attach it.
725 mCurSeq++;
726 if (mCurSeq <= 0) mCurSeq = 1;
727 mCurClient = cs;
728 mCurInputContext = inputContext;
729 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 // Check if the input method is changing.
732 if (mCurId != null && mCurId.equals(mCurMethodId)) {
733 if (cs.curSession != null) {
734 // Fast case: if we are already connected to the input method,
735 // then just return it.
736 return attachNewInputLocked(initial, needResult);
737 }
738 if (mHaveConnection) {
739 if (mCurMethod != null) {
740 if (!cs.sessionRequested) {
741 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
744 MSG_CREATE_SESSION, mCurMethod,
745 new MethodCallback(mCurMethod)));
746 }
747 // Return to client, and we will get back with it when
748 // we have had a session made for it.
749 return new InputBindResult(null, mCurId, mCurSeq);
750 } else if (SystemClock.uptimeMillis()
751 < (mLastBindTime+TIME_TO_RECONNECT)) {
752 // In this case we have connected to the service, but
753 // don't yet have its interface. If it hasn't been too
754 // long since we did the connection, we'll return to
755 // the client and wait to get the service interface so
756 // we can report back. If it has been too long, we want
757 // to fall through so we can try a disconnect/reconnect
758 // to see if we can get back in touch with the service.
759 return new InputBindResult(null, mCurId, mCurSeq);
760 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
762 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 }
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700767 return startInputInnerLocked();
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700770 InputBindResult startInputInnerLocked() {
771 if (mCurMethodId == null) {
772 return mNoBinding;
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700775 if (!mSystemReady) {
776 // If the system is not yet ready, we shouldn't be running third
777 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700778 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 InputMethodInfo info = mMethodMap.get(mCurMethodId);
782 if (info == null) {
783 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700786 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
789 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700790 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
791 com.android.internal.R.string.input_method_binding_label);
792 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
793 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
795 mLastBindTime = SystemClock.uptimeMillis();
796 mHaveConnection = true;
797 mCurId = info.getId();
798 mCurToken = new Binder();
799 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 mIWindowManager.addWindowToken(mCurToken,
802 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
803 } catch (RemoteException e) {
804 }
805 return new InputBindResult(null, mCurId, mCurSeq);
806 } else {
807 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800808 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 + mCurIntent);
810 }
811 return null;
812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 public InputBindResult startInput(IInputMethodClient client,
815 IInputContext inputContext, EditorInfo attribute,
816 boolean initial, boolean needResult) {
817 synchronized (mMethodMap) {
818 final long ident = Binder.clearCallingIdentity();
819 try {
820 return startInputLocked(client, inputContext, attribute,
821 initial, needResult);
822 } finally {
823 Binder.restoreCallingIdentity(ident);
824 }
825 }
826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void finishInput(IInputMethodClient client) {
829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 public void onServiceConnected(ComponentName name, IBinder service) {
832 synchronized (mMethodMap) {
833 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
834 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700835 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 unbindCurrentMethodLocked(false);
838 return;
839 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800840 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700841 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
842 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800844 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700845 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700847 MSG_CREATE_SESSION, mCurMethod,
848 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 }
851 }
852 }
853
854 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
855 synchronized (mMethodMap) {
856 if (mCurMethod != null && method != null
857 && mCurMethod.asBinder() == method.asBinder()) {
858 if (mCurClient != null) {
859 mCurClient.curSession = new SessionState(mCurClient,
860 method, session);
861 mCurClient.sessionRequested = false;
862 InputBindResult res = attachNewInputLocked(true, true);
863 if (res.method != null) {
864 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
865 MSG_BIND_METHOD, mCurClient.client, res));
866 }
867 }
868 }
869 }
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 void unbindCurrentMethodLocked(boolean reportToClient) {
873 if (mHaveConnection) {
874 mContext.unbindService(this);
875 mHaveConnection = false;
876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700878 if (mCurToken != null) {
879 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800880 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 mIWindowManager.removeWindowToken(mCurToken);
882 } catch (RemoteException e) {
883 }
884 mCurToken = null;
885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
The Android Open Source Project10592532009-03-18 17:39:46 -0700887 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700888 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700890 if (reportToClient && mCurClient != null) {
891 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
892 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
893 }
894 }
895
Devin Taylor0c33ed22010-02-23 13:26:46 -0600896 private void finishSession(SessionState sessionState) {
897 if (sessionState != null && sessionState.session != null) {
898 try {
899 sessionState.session.finishSession();
900 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700901 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600902 }
903 }
904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700906 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 if (mCurMethod != null) {
908 for (ClientState cs : mClients.values()) {
909 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600910 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 cs.curSession = null;
912 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600913
914 finishSession(mEnabledSession);
915 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mCurMethod = null;
917 }
918 mStatusBar.setIconVisibility(mInputMethodIcon, false);
919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void onServiceDisconnected(ComponentName name) {
922 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800923 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 + " mCurIntent=" + mCurIntent);
925 if (mCurMethod != null && mCurIntent != null
926 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700927 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 // We consider this to be a new bind attempt, since the system
929 // should now try to restart the service for us.
930 mLastBindTime = SystemClock.uptimeMillis();
931 mShowRequested = mInputShown;
932 mInputShown = false;
933 if (mCurClient != null) {
934 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
935 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
936 }
937 }
938 }
939 }
940
941 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
942 long ident = Binder.clearCallingIdentity();
943 try {
944 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 return;
947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 synchronized (mMethodMap) {
950 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800951 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 mStatusBar.setIconVisibility(mInputMethodIcon, false);
953 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800954 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 mInputMethodData.iconId = iconId;
956 mInputMethodData.iconPackage = packageName;
957 mStatusBar.updateIcon(mInputMethodIcon, mInputMethodData, null);
958 mStatusBar.setIconVisibility(mInputMethodIcon, true);
959 }
960 }
961 } finally {
962 Binder.restoreCallingIdentity(ident);
963 }
964 }
965
966 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
968 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
969 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
970 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 String id = Settings.Secure.getString(mContext.getContentResolver(),
972 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700973 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 try {
975 setInputMethodLocked(id);
976 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800977 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700978 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700981 } else {
982 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700983 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 void setInputMethodLocked(String id) {
989 InputMethodInfo info = mMethodMap.get(id);
990 if (info == null) {
991 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (id.equals(mCurMethodId)) {
995 return;
996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 final long ident = Binder.clearCallingIdentity();
999 try {
1000 mCurMethodId = id;
1001 Settings.Secure.putString(mContext.getContentResolver(),
1002 Settings.Secure.DEFAULT_INPUT_METHOD, id);
1003
1004 if (ActivityManagerNative.isSystemReady()) {
1005 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001006 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 intent.putExtra("input_method_id", id);
1008 mContext.sendBroadcast(intent);
1009 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001010 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 } finally {
1012 Binder.restoreCallingIdentity(ident);
1013 }
1014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Project4df24232009-03-05 14:34:35 -08001016 public boolean showSoftInput(IInputMethodClient client, int flags,
1017 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 long ident = Binder.clearCallingIdentity();
1019 try {
1020 synchronized (mMethodMap) {
1021 if (mCurClient == null || client == null
1022 || mCurClient.client.asBinder() != client.asBinder()) {
1023 try {
1024 // We need to check if this is the current client with
1025 // focus in the window manager, to allow this call to
1026 // be made before input is started in it.
1027 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001028 Slog.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001029 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001032 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001035
Joe Onorato8a9b2202010-02-26 18:56:32 -08001036 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001037 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039 } finally {
1040 Binder.restoreCallingIdentity(ident);
1041 }
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project4df24232009-03-05 14:34:35 -08001044 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 mShowRequested = true;
1046 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1047 mShowExplicitlyRequested = true;
1048 }
1049 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1050 mShowExplicitlyRequested = true;
1051 mShowForced = true;
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
Dianne Hackborncc278702009-09-02 23:07:23 -07001054 if (!mSystemReady) {
1055 return false;
1056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001057
The Android Open Source Project4df24232009-03-05 14:34:35 -08001058 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001060 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1061 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1062 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001064 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 } else if (mHaveConnection && SystemClock.uptimeMillis()
1066 < (mLastBindTime+TIME_TO_RECONNECT)) {
1067 // The client has asked to have the input method shown, but
1068 // we have been sitting here too long with a connection to the
1069 // service and no interface received, so let's disconnect/connect
1070 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 SystemClock.uptimeMillis()-mLastBindTime,1);
1073 mContext.unbindService(this);
1074 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079
The Android Open Source Project4df24232009-03-05 14:34:35 -08001080 public boolean hideSoftInput(IInputMethodClient client, int flags,
1081 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 long ident = Binder.clearCallingIdentity();
1083 try {
1084 synchronized (mMethodMap) {
1085 if (mCurClient == null || client == null
1086 || mCurClient.client.asBinder() != client.asBinder()) {
1087 try {
1088 // We need to check if this is the current client with
1089 // focus in the window manager, to allow this call to
1090 // be made before input is started in it.
1091 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 Slog.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001093 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
Joe Onorato8a9b2202010-02-26 18:56:32 -08001100 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001101 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 } finally {
1104 Binder.restoreCallingIdentity(ident);
1105 }
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Project4df24232009-03-05 14:34:35 -08001108 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1110 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001113 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001120 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1123 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1124 res = true;
1125 } else {
1126 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 mInputShown = false;
1129 mShowRequested = false;
1130 mShowExplicitlyRequested = false;
1131 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001135 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1137 boolean first, int windowFlags) {
1138 long ident = Binder.clearCallingIdentity();
1139 try {
1140 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 + " viewHasFocus=" + viewHasFocus
1143 + " isTextEditor=" + isTextEditor
1144 + " softInputMode=#" + Integer.toHexString(softInputMode)
1145 + " first=" + first + " flags=#"
1146 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (mCurClient == null || client == null
1149 || mCurClient.client.asBinder() != client.asBinder()) {
1150 try {
1151 // We need to check if this is the current client with
1152 // focus in the window manager, to allow this call to
1153 // be made before input is started in it.
1154 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001155 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 return;
1157 }
1158 } catch (RemoteException e) {
1159 }
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001162 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001163 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001164 return;
1165 }
1166 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1169 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1170 if (!isTextEditor || (softInputMode &
1171 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1172 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1173 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1174 // There is no focus view, and this window will
1175 // be behind any soft input window, so hide the
1176 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001177 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
1180 } else if (isTextEditor && (softInputMode &
1181 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1182 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1183 && (softInputMode &
1184 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1185 // There is a focus view, and we are navigating forward
1186 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001187 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001188 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 break;
1191 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1192 // Do nothing.
1193 break;
1194 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1195 if ((softInputMode &
1196 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001197 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001198 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 break;
1201 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001202 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001203 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 break;
1205 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1206 if ((softInputMode &
1207 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001209 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 break;
1212 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001214 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 break;
1216 }
1217 }
1218 } finally {
1219 Binder.restoreCallingIdentity(ident);
1220 }
1221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1224 synchronized (mMethodMap) {
1225 if (mCurClient == null || client == null
1226 || mCurClient.client.asBinder() != client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001227 Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229
1230 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1231 }
1232 }
1233
1234 public void setInputMethod(IBinder token, String id) {
1235 synchronized (mMethodMap) {
1236 if (token == null) {
1237 if (mContext.checkCallingOrSelfPermission(
1238 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1239 != PackageManager.PERMISSION_GRANTED) {
1240 throw new SecurityException(
1241 "Using null token requires permission "
1242 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1243 }
1244 } else if (mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001245 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 return;
1247 }
1248
1249 long ident = Binder.clearCallingIdentity();
1250 try {
1251 setInputMethodLocked(id);
1252 } finally {
1253 Binder.restoreCallingIdentity(ident);
1254 }
1255 }
1256 }
1257
1258 public void hideMySoftInput(IBinder token, int flags) {
1259 synchronized (mMethodMap) {
1260 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001261 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 return;
1263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 long ident = Binder.clearCallingIdentity();
1265 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001266 hideCurrentInputLocked(flags, null);
1267 } finally {
1268 Binder.restoreCallingIdentity(ident);
1269 }
1270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project4df24232009-03-05 14:34:35 -08001273 public void showMySoftInput(IBinder token, int flags) {
1274 synchronized (mMethodMap) {
1275 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001277 return;
1278 }
1279 long ident = Binder.clearCallingIdentity();
1280 try {
1281 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 } finally {
1283 Binder.restoreCallingIdentity(ident);
1284 }
1285 }
1286 }
1287
1288 void setEnabledSessionInMainThread(SessionState session) {
1289 if (mEnabledSession != session) {
1290 if (mEnabledSession != null) {
1291 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001292 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 mEnabledSession.method.setSessionEnabled(
1294 mEnabledSession.session, false);
1295 } catch (RemoteException e) {
1296 }
1297 }
1298 mEnabledSession = session;
1299 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001300 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 session.method.setSessionEnabled(
1302 session.session, true);
1303 } catch (RemoteException e) {
1304 }
1305 }
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 public boolean handleMessage(Message msg) {
1309 HandlerCaller.SomeArgs args;
1310 switch (msg.what) {
1311 case MSG_SHOW_IM_PICKER:
1312 showInputMethodMenu();
1313 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 case MSG_UNBIND_INPUT:
1318 try {
1319 ((IInputMethod)msg.obj).unbindInput();
1320 } catch (RemoteException e) {
1321 // There is nothing interesting about the method dying.
1322 }
1323 return true;
1324 case MSG_BIND_INPUT:
1325 args = (HandlerCaller.SomeArgs)msg.obj;
1326 try {
1327 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1328 } catch (RemoteException e) {
1329 }
1330 return true;
1331 case MSG_SHOW_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).showSoftInput(msg.arg1,
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_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001340 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001342 ((IInputMethod)args.arg1).hideSoftInput(0,
1343 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 } catch (RemoteException e) {
1345 }
1346 return true;
1347 case MSG_ATTACH_TOKEN:
1348 args = (HandlerCaller.SomeArgs)msg.obj;
1349 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001350 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1352 } catch (RemoteException e) {
1353 }
1354 return true;
1355 case MSG_CREATE_SESSION:
1356 args = (HandlerCaller.SomeArgs)msg.obj;
1357 try {
1358 ((IInputMethod)args.arg1).createSession(
1359 (IInputMethodCallback)args.arg2);
1360 } catch (RemoteException e) {
1361 }
1362 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 case MSG_START_INPUT:
1366 args = (HandlerCaller.SomeArgs)msg.obj;
1367 try {
1368 SessionState session = (SessionState)args.arg1;
1369 setEnabledSessionInMainThread(session);
1370 session.method.startInput((IInputContext)args.arg2,
1371 (EditorInfo)args.arg3);
1372 } catch (RemoteException e) {
1373 }
1374 return true;
1375 case MSG_RESTART_INPUT:
1376 args = (HandlerCaller.SomeArgs)msg.obj;
1377 try {
1378 SessionState session = (SessionState)args.arg1;
1379 setEnabledSessionInMainThread(session);
1380 session.method.restartInput((IInputContext)args.arg2,
1381 (EditorInfo)args.arg3);
1382 } catch (RemoteException e) {
1383 }
1384 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 case MSG_UNBIND_METHOD:
1389 try {
1390 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1391 } catch (RemoteException e) {
1392 // There is nothing interesting about the last client dying.
1393 }
1394 return true;
1395 case MSG_BIND_METHOD:
1396 args = (HandlerCaller.SomeArgs)msg.obj;
1397 try {
1398 ((IInputMethodClient)args.arg1).onBindMethod(
1399 (InputBindResult)args.arg2);
1400 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001401 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 return true;
1404 }
1405 return false;
1406 }
1407
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001408 private boolean isSystemIme(InputMethodInfo inputMethod) {
1409 return (inputMethod.getServiceInfo().applicationInfo.flags
1410 & ApplicationInfo.FLAG_SYSTEM) != 0;
1411 }
1412
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001413 private boolean chooseNewDefaultIMELocked() {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001414 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1415 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001416 // We'd prefer to fall back on a system IME, since that is safer.
1417 int i=enabled.size();
1418 while (i > 0) {
1419 i--;
1420 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1421 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1422 break;
1423 }
1424 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001425 Settings.Secure.putString(mContext.getContentResolver(),
1426 Settings.Secure.DEFAULT_INPUT_METHOD,
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001427 enabled.get(i).getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001428 return true;
1429 }
1430
1431 return false;
1432 }
1433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1435 HashMap<String, InputMethodInfo> map) {
1436 list.clear();
1437 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001440 final Configuration config = mContext.getResources().getConfiguration();
1441 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1442 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1443 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1444 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445
1446 List<ResolveInfo> services = pm.queryIntentServices(
1447 new Intent(InputMethod.SERVICE_INTERFACE),
1448 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 for (int i = 0; i < services.size(); ++i) {
1451 ResolveInfo ri = services.get(i);
1452 ServiceInfo si = ri.serviceInfo;
1453 ComponentName compName = new ComponentName(si.packageName, si.name);
1454 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1455 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 + ": it does not require the permission "
1458 + android.Manifest.permission.BIND_INPUT_METHOD);
1459 continue;
1460 }
1461
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463
1464 try {
1465 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1466 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001467 final String id = p.getId();
1468 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469
Amith Yamasanie861ec12010-03-24 21:39:27 -07001470 // System IMEs are enabled by default, unless there's a hard keyboard
1471 // and the system IME was explicitly disabled
1472 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1473 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001477 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001481 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001486
1487 String defaultIme = Settings.Secure.getString(mContext
1488 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1489 if (!map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001490 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001491 updateFromSettingsLocked();
1492 }
1493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 void showInputMethodMenu() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 String lastInputMethodId = Settings.Secure.getString(context
1506 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001511 if (immis == null) {
1512 return;
1513 }
1514
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001515 synchronized (mMethodMap) {
1516 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001518 int N = immis.size();
Mac Wangcf06e2c2010-07-23 16:18:01 +08001519
1520 final Map<CharSequence, InputMethodInfo> imMap =
1521 new TreeMap<CharSequence, InputMethodInfo>(Collator.getInstance());
1522
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001523 for (int i = 0; i < N; ++i) {
1524 InputMethodInfo property = immis.get(i);
1525 if (property == null) {
1526 continue;
1527 }
Mac Wangcf06e2c2010-07-23 16:18:01 +08001528 imMap.put(property.loadLabel(pm), property);
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001529 }
Mac Wangcf06e2c2010-07-23 16:18:01 +08001530
1531 N = imMap.size();
1532 mItems = imMap.keySet().toArray(new CharSequence[N]);
1533 mIms = imMap.values().toArray(new InputMethodInfo[N]);
1534
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001535 int checkedItem = 0;
1536 for (int i = 0; i < N; ++i) {
1537 if (mIms[i].getId().equals(lastInputMethodId)) {
1538 checkedItem = i;
1539 break;
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001542
1543 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1544 public void onClick(DialogInterface dialog, int which) {
1545 hideInputMethodMenu();
1546 }
1547 };
1548
1549 TypedArray a = context.obtainStyledAttributes(null,
1550 com.android.internal.R.styleable.DialogPreference,
1551 com.android.internal.R.attr.alertDialogStyle, 0);
1552 mDialogBuilder = new AlertDialog.Builder(context)
1553 .setTitle(com.android.internal.R.string.select_input_method)
1554 .setOnCancelListener(new OnCancelListener() {
1555 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001557 }
1558 })
1559 .setIcon(a.getDrawable(
1560 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1561 a.recycle();
1562
1563 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1564 new AlertDialog.OnClickListener() {
1565 public void onClick(DialogInterface dialog, int which) {
1566 synchronized (mMethodMap) {
1567 if (mIms == null || mIms.length <= which) {
1568 return;
1569 }
1570 InputMethodInfo im = mIms[which];
1571 hideInputMethodMenu();
1572 if (im != null) {
1573 setInputMethodLocked(im.getId());
1574 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001577 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 mSwitchingDialog = mDialogBuilder.create();
1580 mSwitchingDialog.getWindow().setType(
1581 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1582 mSwitchingDialog.show();
1583 }
1584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001587 synchronized (mMethodMap) {
1588 hideInputMethodMenuLocked();
1589 }
1590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001591
The Android Open Source Project10592532009-03-18 17:39:46 -07001592 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001593 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
The Android Open Source Project10592532009-03-18 17:39:46 -07001595 if (mSwitchingDialog != null) {
1596 mSwitchingDialog.dismiss();
1597 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
The Android Open Source Project10592532009-03-18 17:39:46 -07001600 mDialogBuilder = null;
1601 mItems = null;
1602 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 public boolean setInputMethodEnabled(String id, boolean enabled) {
1608 synchronized (mMethodMap) {
1609 if (mContext.checkCallingOrSelfPermission(
1610 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1611 != PackageManager.PERMISSION_GRANTED) {
1612 throw new SecurityException(
1613 "Requires permission "
1614 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1615 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 long ident = Binder.clearCallingIdentity();
1618 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001619 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } finally {
1621 Binder.restoreCallingIdentity(ident);
1622 }
1623 }
1624 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001625
1626 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1627 // Make sure this is a valid input method.
1628 InputMethodInfo imm = mMethodMap.get(id);
1629 if (imm == null) {
1630 if (imm == null) {
1631 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1632 }
1633 }
1634
1635 StringBuilder builder = new StringBuilder(256);
1636
1637 boolean removed = false;
1638 String firstId = null;
1639
1640 // Look through the currently enabled input methods.
1641 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1642 Settings.Secure.ENABLED_INPUT_METHODS);
1643 if (enabledStr != null) {
1644 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1645 splitter.setString(enabledStr);
1646 while (splitter.hasNext()) {
1647 String curId = splitter.next();
1648 if (curId.equals(id)) {
1649 if (enabled) {
1650 // We are enabling this input method, but it is
1651 // already enabled. Nothing to do. The previous
1652 // state was enabled.
1653 return true;
1654 }
1655 // We are disabling this input method, and it is
1656 // currently enabled. Skip it to remove from the
1657 // new list.
1658 removed = true;
1659 } else if (!enabled) {
1660 // We are building a new list of input methods that
1661 // doesn't contain the given one.
1662 if (firstId == null) firstId = curId;
1663 if (builder.length() > 0) builder.append(':');
1664 builder.append(curId);
1665 }
1666 }
1667 }
1668
1669 if (!enabled) {
1670 if (!removed) {
1671 // We are disabling the input method but it is already
1672 // disabled. Nothing to do. The previous state was
1673 // disabled.
1674 return false;
1675 }
1676 // Update the setting with the new list of input methods.
1677 Settings.Secure.putString(mContext.getContentResolver(),
1678 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1679 // We the disabled input method is currently selected, switch
1680 // to another one.
1681 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1682 Settings.Secure.DEFAULT_INPUT_METHOD);
1683 if (id.equals(selId)) {
1684 Settings.Secure.putString(mContext.getContentResolver(),
1685 Settings.Secure.DEFAULT_INPUT_METHOD,
1686 firstId != null ? firstId : "");
1687 }
1688 // Previous state was enabled.
1689 return true;
1690 }
1691
1692 // Add in the newly enabled input method.
1693 if (enabledStr == null || enabledStr.length() == 0) {
1694 enabledStr = id;
1695 } else {
1696 enabledStr = enabledStr + ':' + id;
1697 }
1698
1699 Settings.Secure.putString(mContext.getContentResolver(),
1700 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
1701
1702 // Previous state was disabled.
1703 return false;
1704 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 @Override
1709 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1710 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1711 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1714 + Binder.getCallingPid()
1715 + ", uid=" + Binder.getCallingUid());
1716 return;
1717 }
1718
1719 IInputMethod method;
1720 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 synchronized (mMethodMap) {
1725 p.println("Current Input Method Manager state:");
1726 int N = mMethodList.size();
1727 p.println(" Input Methods:");
1728 for (int i=0; i<N; i++) {
1729 InputMethodInfo info = mMethodList.get(i);
1730 p.println(" InputMethod #" + i + ":");
1731 info.dump(p, " ");
1732 }
1733 p.println(" Clients:");
1734 for (ClientState ci : mClients.values()) {
1735 p.println(" Client " + ci + ":");
1736 p.println(" client=" + ci.client);
1737 p.println(" inputContext=" + ci.inputContext);
1738 p.println(" sessionRequested=" + ci.sessionRequested);
1739 p.println(" curSession=" + ci.curSession);
1740 }
1741 p.println(" mInputMethodIcon=" + mInputMethodIcon);
1742 p.println(" mInputMethodData=" + mInputMethodData);
The Android Open Source Project10592532009-03-18 17:39:46 -07001743 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001745 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1746 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1748 + " mBoundToMethod=" + mBoundToMethod);
1749 p.println(" mCurToken=" + mCurToken);
1750 p.println(" mCurIntent=" + mCurIntent);
1751 method = mCurMethod;
1752 p.println(" mCurMethod=" + mCurMethod);
1753 p.println(" mEnabledSession=" + mEnabledSession);
1754 p.println(" mShowRequested=" + mShowRequested
1755 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1756 + " mShowForced=" + mShowForced
1757 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001758 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 if (client != null) {
1762 p.println(" ");
1763 pw.flush();
1764 try {
1765 client.client.asBinder().dump(fd, args);
1766 } catch (RemoteException e) {
1767 p.println("Input method client dead: " + e);
1768 }
1769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 if (method != null) {
1772 p.println(" ");
1773 pw.flush();
1774 try {
1775 method.asBinder().dump(fd, args);
1776 } catch (RemoteException e) {
1777 p.println("Input method service dead: " + e);
1778 }
1779 }
1780 }
1781}