blob: ecad3cc0d025d6eaa5a1c567c215778bf7db1182 [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;
satok913a8922010-08-26 21:53:41 +090080import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
82import java.util.HashMap;
83import java.util.List;
satok913a8922010-08-26 21:53:41 +090084import java.util.Map;
85import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * This class provides a system service that manages input methods.
89 */
90public class InputMethodManagerService extends IInputMethodManager.Stub
91 implements ServiceConnection, Handler.Callback {
92 static final boolean DEBUG = false;
93 static final String TAG = "InputManagerService";
94
95 static final int MSG_SHOW_IM_PICKER = 1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 static final int MSG_UNBIND_INPUT = 1000;
98 static final int MSG_BIND_INPUT = 1010;
99 static final int MSG_SHOW_SOFT_INPUT = 1020;
100 static final int MSG_HIDE_SOFT_INPUT = 1030;
101 static final int MSG_ATTACH_TOKEN = 1040;
102 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_START_INPUT = 2000;
105 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 static final int MSG_UNBIND_METHOD = 3000;
108 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 final Context mContext;
113 final Handler mHandler;
114 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700115 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 final IWindowManager mIWindowManager;
117 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 // All known input methods. mMethodMap also serves as the global
122 // lock for this class.
123 final ArrayList<InputMethodInfo> mMethodList
124 = new ArrayList<InputMethodInfo>();
125 final HashMap<String, InputMethodInfo> mMethodMap
126 = new HashMap<String, InputMethodInfo>();
127
128 final TextUtils.SimpleStringSplitter mStringColonSplitter
129 = new TextUtils.SimpleStringSplitter(':');
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 class SessionState {
132 final ClientState client;
133 final IInputMethod method;
134 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 @Override
137 public String toString() {
138 return "SessionState{uid " + client.uid + " pid " + client.pid
139 + " method " + Integer.toHexString(
140 System.identityHashCode(method))
141 + " session " + Integer.toHexString(
142 System.identityHashCode(session))
143 + "}";
144 }
145
146 SessionState(ClientState _client, IInputMethod _method,
147 IInputMethodSession _session) {
148 client = _client;
149 method = _method;
150 session = _session;
151 }
152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 class ClientState {
155 final IInputMethodClient client;
156 final IInputContext inputContext;
157 final int uid;
158 final int pid;
159 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 boolean sessionRequested;
162 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 @Override
165 public String toString() {
166 return "ClientState{" + Integer.toHexString(
167 System.identityHashCode(this)) + " uid " + uid
168 + " pid " + pid + "}";
169 }
170
171 ClientState(IInputMethodClient _client, IInputContext _inputContext,
172 int _uid, int _pid) {
173 client = _client;
174 inputContext = _inputContext;
175 uid = _uid;
176 pid = _pid;
177 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
178 }
179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final HashMap<IBinder, ClientState> mClients
182 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700185 * Set once the system is ready to run third party code.
186 */
187 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * Id of the currently selected input method.
191 */
192 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
195 * The current binding sequence number, incremented every time there is
196 * a new bind performed.
197 */
198 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
201 * The client that is currently bound to an input method.
202 */
203 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700206 * The last window token that gained focus.
207 */
208 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * The input context last provided by the current client.
212 */
213 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
216 * The attributes last provided by the current client.
217 */
218 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
221 * The input method ID of the input method service that we are currently
222 * connected to or in the process of connecting to.
223 */
224 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * Set to true if our ServiceConnection is currently actively bound to
228 * a service (whether or not we have gotten its IBinder back yet).
229 */
230 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 /**
233 * Set if the client has asked for the input method to be shown.
234 */
235 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
238 * Set if we were explicitly told to show the input method.
239 */
240 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * Set if we were forced to be shown.
244 */
245 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
248 * Set if we last told the input method to show itself.
249 */
250 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * The Intent used to connect to the current input method.
254 */
255 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The token we have made for the currently active input method, to
259 * identify it in the future.
260 */
261 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * If non-null, this is the input method service we are currently connected
265 * to.
266 */
267 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * Time that we last initiated a bind to the input method, to determine
271 * if we should try to disconnect and reconnect to it.
272 */
273 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * Have we called mCurMethod.bindInput()?
277 */
278 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Currently enabled session. Only touched by service thread, not
282 * protected by a lock.
283 */
284 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * True if the screen is on. The value is true initially.
288 */
289 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 AlertDialog.Builder mDialogBuilder;
292 AlertDialog mSwitchingDialog;
293 InputMethodInfo[] mIms;
294 CharSequence[] mItems;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 class SettingsObserver extends ContentObserver {
297 SettingsObserver(Handler handler) {
298 super(handler);
299 ContentResolver resolver = mContext.getContentResolver();
300 resolver.registerContentObserver(Settings.Secure.getUriFor(
301 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 @Override public void onChange(boolean selfChange) {
305 synchronized (mMethodMap) {
306 updateFromSettingsLocked();
307 }
308 }
309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
312 @Override
313 public void onReceive(Context context, Intent intent) {
314 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
315 mScreenOn = true;
316 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
317 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700318 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
319 hideInputMethodMenu();
320 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800322 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324
325 // Inform the current client of the change in active status
326 try {
327 if (mCurClient != null && mCurClient.client != null) {
328 mCurClient.client.setActive(mScreenOn);
329 }
330 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800331 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 + mCurClient.pid + " uid " + mCurClient.uid);
333 }
334 }
335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800337 class MyPackageMonitor extends PackageMonitor {
338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800340 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800342 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
344 final int N = mMethodList.size();
345 if (curInputMethodId != null) {
346 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800347 InputMethodInfo imi = mMethodList.get(i);
348 if (imi.getId().equals(curInputMethodId)) {
349 for (String pkg : packages) {
350 if (imi.getPackageName().equals(pkg)) {
351 if (!doit) {
352 return true;
353 }
354
355 Settings.Secure.putString(mContext.getContentResolver(),
356 Settings.Secure.DEFAULT_INPUT_METHOD, "");
357 chooseNewDefaultIMELocked();
358 return true;
359 }
360 }
361 }
362 }
363 }
364 }
365 return false;
366 }
367
368 @Override
369 public void onSomePackagesChanged() {
370 synchronized (mMethodMap) {
371 InputMethodInfo curIm = null;
372 String curInputMethodId = Settings.Secure.getString(mContext
373 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
374 final int N = mMethodList.size();
375 if (curInputMethodId != null) {
376 for (int i=0; i<N; i++) {
377 InputMethodInfo imi = mMethodList.get(i);
378 if (imi.getId().equals(curInputMethodId)) {
379 curIm = imi;
380 }
381 int change = isPackageDisappearing(imi.getPackageName());
382 if (change == PACKAGE_TEMPORARY_CHANGE
383 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800384 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800385 + imi.getComponent());
386 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388 }
389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800391 buildInputMethodListLocked(mMethodList, mMethodMap);
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800395 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 int change = isPackageDisappearing(curIm.getPackageName());
397 if (change == PACKAGE_TEMPORARY_CHANGE
398 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800399 ServiceInfo si = null;
400 try {
401 si = mContext.getPackageManager().getServiceInfo(
402 curIm.getComponent(), 0);
403 } catch (PackageManager.NameNotFoundException ex) {
404 }
405 if (si == null) {
406 // Uh oh, current input method is no longer around!
407 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800408 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800409 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800410 changed = true;
411 curIm = null;
412 curInputMethodId = "";
Joe Onorato8a9b2202010-02-26 18:56:32 -0800413 Slog.i(TAG, "Unsetting current input method");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800414 Settings.Secure.putString(mContext.getContentResolver(),
415 Settings.Secure.DEFAULT_INPUT_METHOD,
416 curInputMethodId);
417 }
418 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800419 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800420 }
421
422 if (curIm == null) {
423 // We currently don't have a default input method... is
424 // one now available?
425 changed = chooseNewDefaultIMELocked();
426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800428 if (changed) {
429 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
431 }
432 }
433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 class MethodCallback extends IInputMethodCallback.Stub {
436 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 MethodCallback(IInputMethod method) {
439 mMethod = method;
440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public void finishedEvent(int seq, boolean handled) throws RemoteException {
443 }
444
445 public void sessionCreated(IInputMethodSession session) throws RemoteException {
446 onSessionCreated(mMethod, session);
447 }
448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
Joe Onorato089de882010-04-12 08:18:45 -0700450 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 mContext = context;
452 mHandler = new Handler(this);
453 mIWindowManager = IWindowManager.Stub.asInterface(
454 ServiceManager.getService(Context.WINDOW_SERVICE));
455 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
456 public void executeMessage(Message msg) {
457 handleMessage(msg);
458 }
459 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 IntentFilter screenOnOffFilt = new IntentFilter();
464 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
465 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700466 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
satok913a8922010-08-26 21:53:41 +0900469 mStatusBar = statusBar;
470 statusBar.setIconVisibility("ime", false);
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 buildInputMethodListLocked(mMethodList, mMethodMap);
473
474 final String enabledStr = Settings.Secure.getString(
475 mContext.getContentResolver(),
476 Settings.Secure.ENABLED_INPUT_METHODS);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 Slog.i(TAG, "Enabled input methods: " + enabledStr);
satok913a8922010-08-26 21:53:41 +0900478 final String defaultIme = Settings.Secure.getString(mContext
479 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
480 if (enabledStr == null || TextUtils.isEmpty(defaultIme)) {
481 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 -0800482 InputMethodInfo defIm = null;
483 StringBuilder sb = new StringBuilder(256);
484 final int N = mMethodList.size();
485 for (int i=0; i<N; i++) {
486 InputMethodInfo imi = mMethodList.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800487 Slog.i(TAG, "Adding: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 if (i > 0) sb.append(':');
489 sb.append(imi.getId());
490 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
491 try {
492 Resources res = mContext.createPackageContext(
493 imi.getPackageName(), 0).getResources();
494 if (res.getBoolean(imi.getIsDefaultResourceId())) {
495 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800496 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498 } catch (PackageManager.NameNotFoundException ex) {
499 } catch (Resources.NotFoundException ex) {
500 }
501 }
502 }
503 if (defIm == null && N > 0) {
504 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800505 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507 Settings.Secure.putString(mContext.getContentResolver(),
508 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
509 if (defIm != null) {
510 Settings.Secure.putString(mContext.getContentResolver(),
511 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
512 }
513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 mSettingsObserver = new SettingsObserver(mHandler);
516 updateFromSettingsLocked();
517 }
518
519 @Override
520 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
521 throws RemoteException {
522 try {
523 return super.onTransact(code, data, reply, flags);
524 } catch (RuntimeException e) {
525 // The input method manager only throws security exceptions, so let's
526 // log all others.
527 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530 throw e;
531 }
532 }
533
534 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700535 synchronized (mMethodMap) {
536 if (!mSystemReady) {
537 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700538 try {
539 startInputInnerLocked();
540 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700542 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700543 }
544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 public List<InputMethodInfo> getInputMethodList() {
548 synchronized (mMethodMap) {
549 return new ArrayList<InputMethodInfo>(mMethodList);
550 }
551 }
552
553 public List<InputMethodInfo> getEnabledInputMethodList() {
554 synchronized (mMethodMap) {
555 return getEnabledInputMethodListLocked();
556 }
557 }
558
559 List<InputMethodInfo> getEnabledInputMethodListLocked() {
560 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 final String enabledStr = Settings.Secure.getString(
563 mContext.getContentResolver(),
564 Settings.Secure.ENABLED_INPUT_METHODS);
565 if (enabledStr != null) {
566 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
567 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 while (splitter.hasNext()) {
570 InputMethodInfo info = mMethodMap.get(splitter.next());
571 if (info != null) {
572 res.add(info);
573 }
574 }
575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 return res;
578 }
579
580 public void addClient(IInputMethodClient client,
581 IInputContext inputContext, int uid, int pid) {
582 synchronized (mMethodMap) {
583 mClients.put(client.asBinder(), new ClientState(client,
584 inputContext, uid, pid));
585 }
586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 public void removeClient(IInputMethodClient client) {
589 synchronized (mMethodMap) {
590 mClients.remove(client.asBinder());
591 }
592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 void executeOrSendMessage(IInterface target, Message msg) {
595 if (target.asBinder() instanceof Binder) {
596 mCaller.sendMessage(msg);
597 } else {
598 handleMessage(msg);
599 msg.recycle();
600 }
601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800602
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700603 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800605 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 + mCurClient.client.asBinder());
607 if (mBoundToMethod) {
608 mBoundToMethod = false;
609 if (mCurMethod != null) {
610 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
611 MSG_UNBIND_INPUT, mCurMethod));
612 }
613 }
614 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
615 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
616 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 // Call setActive(false) on the old client
619 try {
620 mCurClient.client.setActive(false);
621 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800622 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 + mCurClient.pid + " uid " + mCurClient.uid);
624 }
625 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800626
The Android Open Source Project10592532009-03-18 17:39:46 -0700627 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 private int getImeShowFlags() {
632 int flags = 0;
633 if (mShowForced) {
634 flags |= InputMethod.SHOW_FORCED
635 | InputMethod.SHOW_EXPLICIT;
636 } else if (mShowExplicitlyRequested) {
637 flags |= InputMethod.SHOW_EXPLICIT;
638 }
639 return flags;
640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private int getAppShowFlags() {
643 int flags = 0;
644 if (mShowForced) {
645 flags |= InputMethodManager.SHOW_FORCED;
646 } else if (!mShowExplicitlyRequested) {
647 flags |= InputMethodManager.SHOW_IMPLICIT;
648 }
649 return flags;
650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
653 if (!mBoundToMethod) {
654 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
655 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
656 mBoundToMethod = true;
657 }
658 final SessionState session = mCurClient.curSession;
659 if (initial) {
660 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
661 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
662 } else {
663 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
664 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
665 }
666 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800667 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800668 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670 return needResult
671 ? new InputBindResult(session.session, mCurId, mCurSeq)
672 : null;
673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 InputBindResult startInputLocked(IInputMethodClient client,
676 IInputContext inputContext, EditorInfo attribute,
677 boolean initial, boolean needResult) {
678 // If no method is currently selected, do nothing.
679 if (mCurMethodId == null) {
680 return mNoBinding;
681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 ClientState cs = mClients.get(client.asBinder());
684 if (cs == null) {
685 throw new IllegalArgumentException("unknown client "
686 + client.asBinder());
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 try {
690 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
691 // Check with the window manager to make sure this client actually
692 // has a window with focus. If not, reject. This is thread safe
693 // because if the focus changes some time before or after, the
694 // next client receiving focus that has any interest in input will
695 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800696 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
698 return null;
699 }
700 } catch (RemoteException e) {
701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 if (mCurClient != cs) {
704 // If the client is changing, we need to switch over to the new
705 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700706 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800707 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 + cs.client.asBinder());
709
710 // If the screen is on, inform the new client it is active
711 if (mScreenOn) {
712 try {
713 cs.client.setActive(mScreenOn);
714 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800715 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 + cs.pid + " uid " + cs.uid);
717 }
718 }
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 // Bump up the sequence for this client and attach it.
722 mCurSeq++;
723 if (mCurSeq <= 0) mCurSeq = 1;
724 mCurClient = cs;
725 mCurInputContext = inputContext;
726 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 // Check if the input method is changing.
729 if (mCurId != null && mCurId.equals(mCurMethodId)) {
730 if (cs.curSession != null) {
731 // Fast case: if we are already connected to the input method,
732 // then just return it.
733 return attachNewInputLocked(initial, needResult);
734 }
735 if (mHaveConnection) {
736 if (mCurMethod != null) {
737 if (!cs.sessionRequested) {
738 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800739 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
741 MSG_CREATE_SESSION, mCurMethod,
742 new MethodCallback(mCurMethod)));
743 }
744 // Return to client, and we will get back with it when
745 // we have had a session made for it.
746 return new InputBindResult(null, mCurId, mCurSeq);
747 } else if (SystemClock.uptimeMillis()
748 < (mLastBindTime+TIME_TO_RECONNECT)) {
749 // In this case we have connected to the service, but
750 // don't yet have its interface. If it hasn't been too
751 // long since we did the connection, we'll return to
752 // the client and wait to get the service interface so
753 // we can report back. If it has been too long, we want
754 // to fall through so we can try a disconnect/reconnect
755 // to see if we can get back in touch with the service.
756 return new InputBindResult(null, mCurId, mCurSeq);
757 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
759 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 }
762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700764 return startInputInnerLocked();
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700767 InputBindResult startInputInnerLocked() {
768 if (mCurMethodId == null) {
769 return mNoBinding;
770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700772 if (!mSystemReady) {
773 // If the system is not yet ready, we shouldn't be running third
774 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700775 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 InputMethodInfo info = mMethodMap.get(mCurMethodId);
779 if (info == null) {
780 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700783 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
786 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700787 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
788 com.android.internal.R.string.input_method_binding_label);
789 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
790 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
792 mLastBindTime = SystemClock.uptimeMillis();
793 mHaveConnection = true;
794 mCurId = info.getId();
795 mCurToken = new Binder();
796 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800797 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 mIWindowManager.addWindowToken(mCurToken,
799 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
800 } catch (RemoteException e) {
801 }
802 return new InputBindResult(null, mCurId, mCurSeq);
803 } else {
804 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800805 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 + mCurIntent);
807 }
808 return null;
809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 public InputBindResult startInput(IInputMethodClient client,
812 IInputContext inputContext, EditorInfo attribute,
813 boolean initial, boolean needResult) {
814 synchronized (mMethodMap) {
815 final long ident = Binder.clearCallingIdentity();
816 try {
817 return startInputLocked(client, inputContext, attribute,
818 initial, needResult);
819 } finally {
820 Binder.restoreCallingIdentity(ident);
821 }
822 }
823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 public void finishInput(IInputMethodClient client) {
826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void onServiceConnected(ComponentName name, IBinder service) {
829 synchronized (mMethodMap) {
830 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
831 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700832 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700834 unbindCurrentMethodLocked(false);
835 return;
836 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700838 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
839 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700842 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700844 MSG_CREATE_SESSION, mCurMethod,
845 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
847 }
848 }
849 }
850
851 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
852 synchronized (mMethodMap) {
853 if (mCurMethod != null && method != null
854 && mCurMethod.asBinder() == method.asBinder()) {
855 if (mCurClient != null) {
856 mCurClient.curSession = new SessionState(mCurClient,
857 method, session);
858 mCurClient.sessionRequested = false;
859 InputBindResult res = attachNewInputLocked(true, true);
860 if (res.method != null) {
861 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
862 MSG_BIND_METHOD, mCurClient.client, res));
863 }
864 }
865 }
866 }
867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700869 void unbindCurrentMethodLocked(boolean reportToClient) {
870 if (mHaveConnection) {
871 mContext.unbindService(this);
872 mHaveConnection = false;
873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800874
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700875 if (mCurToken != null) {
876 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800877 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700878 mIWindowManager.removeWindowToken(mCurToken);
879 } catch (RemoteException e) {
880 }
881 mCurToken = null;
882 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800883
The Android Open Source Project10592532009-03-18 17:39:46 -0700884 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700885 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700887 if (reportToClient && mCurClient != null) {
888 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
889 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
890 }
891 }
892
Devin Taylor0c33ed22010-02-23 13:26:46 -0600893 private void finishSession(SessionState sessionState) {
894 if (sessionState != null && sessionState.session != null) {
895 try {
896 sessionState.session.finishSession();
897 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700898 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600899 }
900 }
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700903 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 if (mCurMethod != null) {
905 for (ClientState cs : mClients.values()) {
906 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600907 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 cs.curSession = null;
909 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600910
911 finishSession(mEnabledSession);
912 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 mCurMethod = null;
914 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700915 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 public void onServiceDisconnected(ComponentName name) {
919 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800920 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 + " mCurIntent=" + mCurIntent);
922 if (mCurMethod != null && mCurIntent != null
923 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700924 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 // We consider this to be a new bind attempt, since the system
926 // should now try to restart the service for us.
927 mLastBindTime = SystemClock.uptimeMillis();
928 mShowRequested = mInputShown;
929 mInputShown = false;
930 if (mCurClient != null) {
931 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
932 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
933 }
934 }
935 }
936 }
937
938 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700939 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 long ident = Binder.clearCallingIdentity();
941 try {
942 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700943 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 return;
945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 synchronized (mMethodMap) {
948 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800949 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700950 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800952 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700953 mStatusBar.setIcon("ime", packageName, iconId, 0);
954 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
956 }
957 } finally {
958 Binder.restoreCallingIdentity(ident);
959 }
960 }
961
962 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700963 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
964 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
965 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
966 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 String id = Settings.Secure.getString(mContext.getContentResolver(),
968 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700969 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 try {
971 setInputMethodLocked(id);
972 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800973 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700974 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 } else {
978 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700979 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 void setInputMethodLocked(String id) {
985 InputMethodInfo info = mMethodMap.get(id);
986 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900987 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if (id.equals(mCurMethodId)) {
991 return;
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 final long ident = Binder.clearCallingIdentity();
995 try {
996 mCurMethodId = id;
997 Settings.Secure.putString(mContext.getContentResolver(),
998 Settings.Secure.DEFAULT_INPUT_METHOD, id);
999
1000 if (ActivityManagerNative.isSystemReady()) {
1001 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001002 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 intent.putExtra("input_method_id", id);
1004 mContext.sendBroadcast(intent);
1005 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001006 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 } finally {
1008 Binder.restoreCallingIdentity(ident);
1009 }
1010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
The Android Open Source Project4df24232009-03-05 14:34:35 -08001012 public boolean showSoftInput(IInputMethodClient client, int flags,
1013 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001014 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 long ident = Binder.clearCallingIdentity();
1016 try {
1017 synchronized (mMethodMap) {
1018 if (mCurClient == null || client == null
1019 || mCurClient.client.asBinder() != client.asBinder()) {
1020 try {
1021 // We need to check if this is the current client with
1022 // focus in the window manager, to allow this call to
1023 // be made before input is started in it.
1024 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001025 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001026 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001032
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001034 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 } finally {
1037 Binder.restoreCallingIdentity(ident);
1038 }
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
The Android Open Source Project4df24232009-03-05 14:34:35 -08001041 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 mShowRequested = true;
1043 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1044 mShowExplicitlyRequested = true;
1045 }
1046 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1047 mShowExplicitlyRequested = true;
1048 mShowForced = true;
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
Dianne Hackborncc278702009-09-02 23:07:23 -07001051 if (!mSystemReady) {
1052 return false;
1053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001054
The Android Open Source Project4df24232009-03-05 14:34:35 -08001055 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001057 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1058 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1059 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001061 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 } else if (mHaveConnection && SystemClock.uptimeMillis()
1063 < (mLastBindTime+TIME_TO_RECONNECT)) {
1064 // The client has asked to have the input method shown, but
1065 // we have been sitting here too long with a connection to the
1066 // service and no interface received, so let's disconnect/connect
1067 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 SystemClock.uptimeMillis()-mLastBindTime,1);
1070 mContext.unbindService(this);
1071 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001073
The Android Open Source Project4df24232009-03-05 14:34:35 -08001074 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 public boolean hideSoftInput(IInputMethodClient client, int flags,
1078 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001079 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 long ident = Binder.clearCallingIdentity();
1081 try {
1082 synchronized (mMethodMap) {
1083 if (mCurClient == null || client == null
1084 || mCurClient.client.asBinder() != client.asBinder()) {
1085 try {
1086 // We need to check if this is the current client with
1087 // focus in the window manager, to allow this call to
1088 // be made before input is started in it.
1089 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001090 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1091 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001092 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001095 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001098
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001100 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102 } finally {
1103 Binder.restoreCallingIdentity(ident);
1104 }
1105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
The Android Open Source Project4df24232009-03-05 14:34:35 -08001107 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1109 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001119 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001121 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1122 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1123 res = true;
1124 } else {
1125 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 mInputShown = false;
1128 mShowRequested = false;
1129 mShowExplicitlyRequested = false;
1130 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001134 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1136 boolean first, int windowFlags) {
1137 long ident = Binder.clearCallingIdentity();
1138 try {
1139 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001140 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 + " viewHasFocus=" + viewHasFocus
1142 + " isTextEditor=" + isTextEditor
1143 + " softInputMode=#" + Integer.toHexString(softInputMode)
1144 + " first=" + first + " flags=#"
1145 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (mCurClient == null || client == null
1148 || mCurClient.client.asBinder() != client.asBinder()) {
1149 try {
1150 // We need to check if this is the current client with
1151 // focus in the window manager, to allow this call to
1152 // be made before input is started in it.
1153 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001154 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 return;
1156 }
1157 } catch (RemoteException e) {
1158 }
1159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001161 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001162 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001163 return;
1164 }
1165 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1168 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1169 if (!isTextEditor || (softInputMode &
1170 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1171 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1172 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1173 // There is no focus view, and this window will
1174 // be behind any soft input window, so hide the
1175 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001176 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001177 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 } else if (isTextEditor && (softInputMode &
1180 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1181 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1182 && (softInputMode &
1183 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1184 // There is a focus view, and we are navigating forward
1185 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001186 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001187 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 break;
1190 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1191 // Do nothing.
1192 break;
1193 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1194 if ((softInputMode &
1195 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001197 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 break;
1200 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001201 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001202 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 break;
1204 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1205 if ((softInputMode &
1206 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001208 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 break;
1211 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001212 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001213 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 }
1216 }
1217 } finally {
1218 Binder.restoreCallingIdentity(ident);
1219 }
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1223 synchronized (mMethodMap) {
1224 if (mCurClient == null || client == null
1225 || mCurClient.client.asBinder() != client.asBinder()) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001226 Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of uid "
1227 + Binder.getCallingUid() + ": " + 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) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001245 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1246 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 return;
1248 }
1249
1250 long ident = Binder.clearCallingIdentity();
1251 try {
1252 setInputMethodLocked(id);
1253 } finally {
1254 Binder.restoreCallingIdentity(ident);
1255 }
1256 }
1257 }
1258
1259 public void hideMySoftInput(IBinder token, int flags) {
1260 synchronized (mMethodMap) {
1261 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001262 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1263 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 return;
1265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 long ident = Binder.clearCallingIdentity();
1267 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001268 hideCurrentInputLocked(flags, null);
1269 } finally {
1270 Binder.restoreCallingIdentity(ident);
1271 }
1272 }
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project4df24232009-03-05 14:34:35 -08001275 public void showMySoftInput(IBinder token, int flags) {
1276 synchronized (mMethodMap) {
1277 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001278 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1279 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001280 return;
1281 }
1282 long ident = Binder.clearCallingIdentity();
1283 try {
1284 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 } finally {
1286 Binder.restoreCallingIdentity(ident);
1287 }
1288 }
1289 }
1290
1291 void setEnabledSessionInMainThread(SessionState session) {
1292 if (mEnabledSession != session) {
1293 if (mEnabledSession != null) {
1294 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 mEnabledSession.method.setSessionEnabled(
1297 mEnabledSession.session, false);
1298 } catch (RemoteException e) {
1299 }
1300 }
1301 mEnabledSession = session;
1302 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 session.method.setSessionEnabled(
1305 session.session, true);
1306 } catch (RemoteException e) {
1307 }
1308 }
1309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 public boolean handleMessage(Message msg) {
1312 HandlerCaller.SomeArgs args;
1313 switch (msg.what) {
1314 case MSG_SHOW_IM_PICKER:
1315 showInputMethodMenu();
1316 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 case MSG_UNBIND_INPUT:
1321 try {
1322 ((IInputMethod)msg.obj).unbindInput();
1323 } catch (RemoteException e) {
1324 // There is nothing interesting about the method dying.
1325 }
1326 return true;
1327 case MSG_BIND_INPUT:
1328 args = (HandlerCaller.SomeArgs)msg.obj;
1329 try {
1330 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1331 } catch (RemoteException e) {
1332 }
1333 return true;
1334 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001335 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1338 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 } catch (RemoteException e) {
1340 }
1341 return true;
1342 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001343 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001345 ((IInputMethod)args.arg1).hideSoftInput(0,
1346 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 } catch (RemoteException e) {
1348 }
1349 return true;
1350 case MSG_ATTACH_TOKEN:
1351 args = (HandlerCaller.SomeArgs)msg.obj;
1352 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001353 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1355 } catch (RemoteException e) {
1356 }
1357 return true;
1358 case MSG_CREATE_SESSION:
1359 args = (HandlerCaller.SomeArgs)msg.obj;
1360 try {
1361 ((IInputMethod)args.arg1).createSession(
1362 (IInputMethodCallback)args.arg2);
1363 } catch (RemoteException e) {
1364 }
1365 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 case MSG_START_INPUT:
1369 args = (HandlerCaller.SomeArgs)msg.obj;
1370 try {
1371 SessionState session = (SessionState)args.arg1;
1372 setEnabledSessionInMainThread(session);
1373 session.method.startInput((IInputContext)args.arg2,
1374 (EditorInfo)args.arg3);
1375 } catch (RemoteException e) {
1376 }
1377 return true;
1378 case MSG_RESTART_INPUT:
1379 args = (HandlerCaller.SomeArgs)msg.obj;
1380 try {
1381 SessionState session = (SessionState)args.arg1;
1382 setEnabledSessionInMainThread(session);
1383 session.method.restartInput((IInputContext)args.arg2,
1384 (EditorInfo)args.arg3);
1385 } catch (RemoteException e) {
1386 }
1387 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 case MSG_UNBIND_METHOD:
1392 try {
1393 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1394 } catch (RemoteException e) {
1395 // There is nothing interesting about the last client dying.
1396 }
1397 return true;
1398 case MSG_BIND_METHOD:
1399 args = (HandlerCaller.SomeArgs)msg.obj;
1400 try {
1401 ((IInputMethodClient)args.arg1).onBindMethod(
1402 (InputBindResult)args.arg2);
1403 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 return true;
1407 }
1408 return false;
1409 }
1410
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001411 private boolean isSystemIme(InputMethodInfo inputMethod) {
1412 return (inputMethod.getServiceInfo().applicationInfo.flags
1413 & ApplicationInfo.FLAG_SYSTEM) != 0;
1414 }
1415
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001416 private boolean chooseNewDefaultIMELocked() {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001417 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1418 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001419 // We'd prefer to fall back on a system IME, since that is safer.
1420 int i=enabled.size();
1421 while (i > 0) {
1422 i--;
1423 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1424 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1425 break;
1426 }
1427 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001428 Settings.Secure.putString(mContext.getContentResolver(),
1429 Settings.Secure.DEFAULT_INPUT_METHOD,
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001430 enabled.get(i).getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001431 return true;
1432 }
1433
1434 return false;
1435 }
1436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1438 HashMap<String, InputMethodInfo> map) {
1439 list.clear();
1440 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001443 final Configuration config = mContext.getResources().getConfiguration();
1444 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1445 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1446 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1447 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448
1449 List<ResolveInfo> services = pm.queryIntentServices(
1450 new Intent(InputMethod.SERVICE_INTERFACE),
1451 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 for (int i = 0; i < services.size(); ++i) {
1454 ResolveInfo ri = services.get(i);
1455 ServiceInfo si = ri.serviceInfo;
1456 ComponentName compName = new ComponentName(si.packageName, si.name);
1457 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1458 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001459 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 + ": it does not require the permission "
1461 + android.Manifest.permission.BIND_INPUT_METHOD);
1462 continue;
1463 }
1464
Joe Onorato8a9b2202010-02-26 18:56:32 -08001465 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466
1467 try {
1468 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1469 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001470 final String id = p.getId();
1471 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472
Amith Yamasanie861ec12010-03-24 21:39:27 -07001473 // System IMEs are enabled by default, unless there's a hard keyboard
1474 // and the system IME was explicitly disabled
1475 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1476 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001480 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001486 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001489
1490 String defaultIme = Settings.Secure.getString(mContext
1491 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001492 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001493 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001494 updateFromSettingsLocked();
1495 }
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 void showInputMethodMenu() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001502 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 String lastInputMethodId = Settings.Secure.getString(context
1509 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001514 if (immis == null) {
1515 return;
1516 }
1517
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001518 synchronized (mMethodMap) {
1519 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001521 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001522
1523 final Map<CharSequence, InputMethodInfo> imMap =
1524 new TreeMap<CharSequence, InputMethodInfo>(Collator.getInstance());
1525
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001526 for (int i = 0; i < N; ++i) {
1527 InputMethodInfo property = immis.get(i);
1528 if (property == null) {
1529 continue;
1530 }
satok913a8922010-08-26 21:53:41 +09001531 imMap.put(property.loadLabel(pm), property);
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001532 }
satok913a8922010-08-26 21:53:41 +09001533
1534 N = imMap.size();
1535 mItems = imMap.keySet().toArray(new CharSequence[N]);
1536 mIms = imMap.values().toArray(new InputMethodInfo[N]);
1537
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001538 int checkedItem = 0;
1539 for (int i = 0; i < N; ++i) {
1540 if (mIms[i].getId().equals(lastInputMethodId)) {
1541 checkedItem = i;
1542 break;
1543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001545
1546 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1547 public void onClick(DialogInterface dialog, int which) {
1548 hideInputMethodMenu();
1549 }
1550 };
1551
1552 TypedArray a = context.obtainStyledAttributes(null,
1553 com.android.internal.R.styleable.DialogPreference,
1554 com.android.internal.R.attr.alertDialogStyle, 0);
1555 mDialogBuilder = new AlertDialog.Builder(context)
1556 .setTitle(com.android.internal.R.string.select_input_method)
1557 .setOnCancelListener(new OnCancelListener() {
1558 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001560 }
1561 })
1562 .setIcon(a.getDrawable(
1563 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1564 a.recycle();
1565
1566 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1567 new AlertDialog.OnClickListener() {
1568 public void onClick(DialogInterface dialog, int which) {
1569 synchronized (mMethodMap) {
1570 if (mIms == null || mIms.length <= which) {
1571 return;
1572 }
1573 InputMethodInfo im = mIms[which];
1574 hideInputMethodMenu();
1575 if (im != null) {
1576 setInputMethodLocked(im.getId());
1577 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001580 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 mSwitchingDialog = mDialogBuilder.create();
1583 mSwitchingDialog.getWindow().setType(
1584 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1585 mSwitchingDialog.show();
1586 }
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001590 synchronized (mMethodMap) {
1591 hideInputMethodMenuLocked();
1592 }
1593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001594
The Android Open Source Project10592532009-03-18 17:39:46 -07001595 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597
The Android Open Source Project10592532009-03-18 17:39:46 -07001598 if (mSwitchingDialog != null) {
1599 mSwitchingDialog.dismiss();
1600 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
The Android Open Source Project10592532009-03-18 17:39:46 -07001603 mDialogBuilder = null;
1604 mItems = null;
1605 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 public boolean setInputMethodEnabled(String id, boolean enabled) {
1611 synchronized (mMethodMap) {
1612 if (mContext.checkCallingOrSelfPermission(
1613 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1614 != PackageManager.PERMISSION_GRANTED) {
1615 throw new SecurityException(
1616 "Requires permission "
1617 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1618 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 long ident = Binder.clearCallingIdentity();
1621 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001622 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 } finally {
1624 Binder.restoreCallingIdentity(ident);
1625 }
1626 }
1627 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001628
1629 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1630 // Make sure this is a valid input method.
1631 InputMethodInfo imm = mMethodMap.get(id);
1632 if (imm == null) {
1633 if (imm == null) {
1634 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1635 }
1636 }
1637
1638 StringBuilder builder = new StringBuilder(256);
1639
1640 boolean removed = false;
1641 String firstId = null;
1642
1643 // Look through the currently enabled input methods.
1644 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1645 Settings.Secure.ENABLED_INPUT_METHODS);
1646 if (enabledStr != null) {
1647 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1648 splitter.setString(enabledStr);
1649 while (splitter.hasNext()) {
1650 String curId = splitter.next();
1651 if (curId.equals(id)) {
1652 if (enabled) {
1653 // We are enabling this input method, but it is
1654 // already enabled. Nothing to do. The previous
1655 // state was enabled.
1656 return true;
1657 }
1658 // We are disabling this input method, and it is
1659 // currently enabled. Skip it to remove from the
1660 // new list.
1661 removed = true;
1662 } else if (!enabled) {
1663 // We are building a new list of input methods that
1664 // doesn't contain the given one.
1665 if (firstId == null) firstId = curId;
1666 if (builder.length() > 0) builder.append(':');
1667 builder.append(curId);
1668 }
1669 }
1670 }
1671
1672 if (!enabled) {
1673 if (!removed) {
1674 // We are disabling the input method but it is already
1675 // disabled. Nothing to do. The previous state was
1676 // disabled.
1677 return false;
1678 }
1679 // Update the setting with the new list of input methods.
1680 Settings.Secure.putString(mContext.getContentResolver(),
1681 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1682 // We the disabled input method is currently selected, switch
1683 // to another one.
1684 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1685 Settings.Secure.DEFAULT_INPUT_METHOD);
1686 if (id.equals(selId)) {
1687 Settings.Secure.putString(mContext.getContentResolver(),
1688 Settings.Secure.DEFAULT_INPUT_METHOD,
1689 firstId != null ? firstId : "");
1690 }
1691 // Previous state was enabled.
1692 return true;
1693 }
1694
1695 // Add in the newly enabled input method.
1696 if (enabledStr == null || enabledStr.length() == 0) {
1697 enabledStr = id;
1698 } else {
1699 enabledStr = enabledStr + ':' + id;
1700 }
1701
1702 Settings.Secure.putString(mContext.getContentResolver(),
1703 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
1704
1705 // Previous state was disabled.
1706 return false;
1707 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 @Override
1712 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1713 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1714 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1717 + Binder.getCallingPid()
1718 + ", uid=" + Binder.getCallingUid());
1719 return;
1720 }
1721
1722 IInputMethod method;
1723 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 synchronized (mMethodMap) {
1728 p.println("Current Input Method Manager state:");
1729 int N = mMethodList.size();
1730 p.println(" Input Methods:");
1731 for (int i=0; i<N; i++) {
1732 InputMethodInfo info = mMethodList.get(i);
1733 p.println(" InputMethod #" + i + ":");
1734 info.dump(p, " ");
1735 }
1736 p.println(" Clients:");
1737 for (ClientState ci : mClients.values()) {
1738 p.println(" Client " + ci + ":");
1739 p.println(" client=" + ci.client);
1740 p.println(" inputContext=" + ci.inputContext);
1741 p.println(" sessionRequested=" + ci.sessionRequested);
1742 p.println(" curSession=" + ci.curSession);
1743 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001744 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001746 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1747 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1749 + " mBoundToMethod=" + mBoundToMethod);
1750 p.println(" mCurToken=" + mCurToken);
1751 p.println(" mCurIntent=" + mCurIntent);
1752 method = mCurMethod;
1753 p.println(" mCurMethod=" + mCurMethod);
1754 p.println(" mEnabledSession=" + mEnabledSession);
1755 p.println(" mShowRequested=" + mShowRequested
1756 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1757 + " mShowForced=" + mShowForced
1758 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001759 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Jeff Brownb88102f2010-09-08 11:49:43 -07001762 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 pw.flush();
1765 try {
1766 client.client.asBinder().dump(fd, args);
1767 } catch (RemoteException e) {
1768 p.println("Input method client dead: " + e);
1769 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001770 } else {
1771 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001773
Jeff Brownb88102f2010-09-08 11:49:43 -07001774 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 pw.flush();
1777 try {
1778 method.asBinder().dump(fd, args);
1779 } catch (RemoteException e) {
1780 p.println("Input method service dead: " + e);
1781 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001782 } else {
1783 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785 }
1786}