blob: 405dc2e34ca0642e7238bc0e768d24573e810712 [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
19import com.android.internal.os.HandlerCaller;
20import com.android.internal.view.IInputContext;
21import com.android.internal.view.IInputMethod;
22import com.android.internal.view.IInputMethodCallback;
23import com.android.internal.view.IInputMethodClient;
24import com.android.internal.view.IInputMethodManager;
25import com.android.internal.view.IInputMethodSession;
26import com.android.internal.view.InputBindResult;
27
28import com.android.server.status.IconData;
29import com.android.server.status.StatusBarService;
30
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;
48import android.content.res.Resources;
49import android.content.res.TypedArray;
50import android.database.ContentObserver;
51import android.net.Uri;
52import 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;
63import android.text.TextUtils;
64import android.util.EventLog;
65import android.util.Log;
66import android.util.PrintWriterPrinter;
67import android.util.Printer;
68import android.view.IWindowManager;
69import android.view.WindowManager;
70import android.view.inputmethod.InputBinding;
71import android.view.inputmethod.InputMethod;
72import android.view.inputmethod.InputMethodInfo;
73import android.view.inputmethod.InputMethodManager;
74import android.view.inputmethod.EditorInfo;
75
76import java.io.FileDescriptor;
77import java.io.IOException;
78import java.io.PrintWriter;
79import java.util.ArrayList;
80import java.util.HashMap;
81import java.util.List;
82
83/**
84 * This class provides a system service that manages input methods.
85 */
86public class InputMethodManagerService extends IInputMethodManager.Stub
87 implements ServiceConnection, Handler.Callback {
88 static final boolean DEBUG = false;
89 static final String TAG = "InputManagerService";
90
91 static final int MSG_SHOW_IM_PICKER = 1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 static final int MSG_UNBIND_INPUT = 1000;
94 static final int MSG_BIND_INPUT = 1010;
95 static final int MSG_SHOW_SOFT_INPUT = 1020;
96 static final int MSG_HIDE_SOFT_INPUT = 1030;
97 static final int MSG_ATTACH_TOKEN = 1040;
98 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 static final int MSG_START_INPUT = 2000;
101 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 static final int MSG_UNBIND_METHOD = 3000;
104 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 final Context mContext;
109 final Handler mHandler;
110 final SettingsObserver mSettingsObserver;
111 final StatusBarService mStatusBar;
112 final IBinder mInputMethodIcon;
113 final IconData mInputMethodData;
114 final IWindowManager mIWindowManager;
115 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 // All known input methods. mMethodMap also serves as the global
120 // lock for this class.
121 final ArrayList<InputMethodInfo> mMethodList
122 = new ArrayList<InputMethodInfo>();
123 final HashMap<String, InputMethodInfo> mMethodMap
124 = new HashMap<String, InputMethodInfo>();
125
126 final TextUtils.SimpleStringSplitter mStringColonSplitter
127 = new TextUtils.SimpleStringSplitter(':');
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 class SessionState {
130 final ClientState client;
131 final IInputMethod method;
132 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 @Override
135 public String toString() {
136 return "SessionState{uid " + client.uid + " pid " + client.pid
137 + " method " + Integer.toHexString(
138 System.identityHashCode(method))
139 + " session " + Integer.toHexString(
140 System.identityHashCode(session))
141 + "}";
142 }
143
144 SessionState(ClientState _client, IInputMethod _method,
145 IInputMethodSession _session) {
146 client = _client;
147 method = _method;
148 session = _session;
149 }
150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 class ClientState {
153 final IInputMethodClient client;
154 final IInputContext inputContext;
155 final int uid;
156 final int pid;
157 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 boolean sessionRequested;
160 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 @Override
163 public String toString() {
164 return "ClientState{" + Integer.toHexString(
165 System.identityHashCode(this)) + " uid " + uid
166 + " pid " + pid + "}";
167 }
168
169 ClientState(IInputMethodClient _client, IInputContext _inputContext,
170 int _uid, int _pid) {
171 client = _client;
172 inputContext = _inputContext;
173 uid = _uid;
174 pid = _pid;
175 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
176 }
177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final HashMap<IBinder, ClientState> mClients
180 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700183 * Set once the system is ready to run third party code.
184 */
185 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Id of the currently selected input method.
189 */
190 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * The current binding sequence number, incremented every time there is
194 * a new bind performed.
195 */
196 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 /**
199 * The client that is currently bound to an input method.
200 */
201 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700204 * The last window token that gained focus.
205 */
206 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * The input context last provided by the current client.
210 */
211 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 /**
214 * The attributes last provided by the current client.
215 */
216 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 /**
219 * The input method ID of the input method service that we are currently
220 * connected to or in the process of connecting to.
221 */
222 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 /**
225 * Set to true if our ServiceConnection is currently actively bound to
226 * a service (whether or not we have gotten its IBinder back yet).
227 */
228 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * Set if the client has asked for the input method to be shown.
232 */
233 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * Set if we were explicitly told to show the input method.
237 */
238 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
241 * Set if we were forced to be shown.
242 */
243 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Set if we last told the input method to show itself.
247 */
248 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * The Intent used to connect to the current input method.
252 */
253 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * The token we have made for the currently active input method, to
257 * identify it in the future.
258 */
259 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * If non-null, this is the input method service we are currently connected
263 * to.
264 */
265 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Time that we last initiated a bind to the input method, to determine
269 * if we should try to disconnect and reconnect to it.
270 */
271 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
274 * Have we called mCurMethod.bindInput()?
275 */
276 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Currently enabled session. Only touched by service thread, not
280 * protected by a lock.
281 */
282 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * True if the screen is on. The value is true initially.
286 */
287 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 AlertDialog.Builder mDialogBuilder;
290 AlertDialog mSwitchingDialog;
291 InputMethodInfo[] mIms;
292 CharSequence[] mItems;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 class SettingsObserver extends ContentObserver {
295 SettingsObserver(Handler handler) {
296 super(handler);
297 ContentResolver resolver = mContext.getContentResolver();
298 resolver.registerContentObserver(Settings.Secure.getUriFor(
299 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 @Override public void onChange(boolean selfChange) {
303 synchronized (mMethodMap) {
304 updateFromSettingsLocked();
305 }
306 }
307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
313 mScreenOn = true;
314 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
315 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700316 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
317 hideInputMethodMenu();
318 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 } else {
320 Log.w(TAG, "Unexpected intent " + intent);
321 }
322
323 // Inform the current client of the change in active status
324 try {
325 if (mCurClient != null && mCurClient.client != null) {
326 mCurClient.client.setActive(mScreenOn);
327 }
328 } catch (RemoteException e) {
329 Log.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
330 + mCurClient.pid + " uid " + mCurClient.uid);
331 }
332 }
333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 class PackageReceiver extends android.content.BroadcastReceiver {
336 @Override
337 public void onReceive(Context context, Intent intent) {
338 synchronized (mMethodMap) {
339 buildInputMethodListLocked(mMethodList, mMethodMap);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 InputMethodInfo curIm = null;
342 String curInputMethodId = Settings.Secure.getString(context
343 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
344 final int N = mMethodList.size();
345 if (curInputMethodId != null) {
346 for (int i=0; i<N; i++) {
347 if (mMethodList.get(i).getId().equals(curInputMethodId)) {
348 curIm = mMethodList.get(i);
349 }
350 }
351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Uri uri = intent.getData();
356 String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
357 if (curIm != null && curIm.getPackageName().equals(pkg)) {
358 ServiceInfo si = null;
359 try {
360 si = mContext.getPackageManager().getServiceInfo(
361 curIm.getComponent(), 0);
362 } catch (PackageManager.NameNotFoundException ex) {
363 }
364 if (si == null) {
365 // Uh oh, current input method is no longer around!
366 // Pick another one...
367 Log.i(TAG, "Current input method removed: " + curInputMethodId);
Brandon Ballinger6da35a02009-10-21 00:38:13 -0700368 if (!chooseNewDefaultIME()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 changed = true;
370 curIm = null;
371 curInputMethodId = "";
372 Log.i(TAG, "Unsetting current input method");
373 Settings.Secure.putString(mContext.getContentResolver(),
374 Settings.Secure.DEFAULT_INPUT_METHOD,
375 curInputMethodId);
376 }
377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 } else if (curIm == null) {
380 // We currently don't have a default input method... is
381 // one now available?
Brandon Ballinger6da35a02009-10-21 00:38:13 -0700382 changed = chooseNewDefaultIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 if (changed) {
386 updateFromSettingsLocked();
387 }
388 }
389 }
390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 class MethodCallback extends IInputMethodCallback.Stub {
393 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 MethodCallback(IInputMethod method) {
396 mMethod = method;
397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 public void finishedEvent(int seq, boolean handled) throws RemoteException {
400 }
401
402 public void sessionCreated(IInputMethodSession session) throws RemoteException {
403 onSessionCreated(mMethod, session);
404 }
405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 public InputMethodManagerService(Context context, StatusBarService statusBar) {
408 mContext = context;
409 mHandler = new Handler(this);
410 mIWindowManager = IWindowManager.Stub.asInterface(
411 ServiceManager.getService(Context.WINDOW_SERVICE));
412 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
413 public void executeMessage(Message msg) {
414 handleMessage(msg);
415 }
416 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 IntentFilter packageFilt = new IntentFilter();
419 packageFilt.addAction(Intent.ACTION_PACKAGE_ADDED);
420 packageFilt.addAction(Intent.ACTION_PACKAGE_CHANGED);
421 packageFilt.addAction(Intent.ACTION_PACKAGE_REMOVED);
422 packageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
423 packageFilt.addDataScheme("package");
424 mContext.registerReceiver(new PackageReceiver(), packageFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 IntentFilter screenOnOffFilt = new IntentFilter();
427 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
428 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700429 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 buildInputMethodListLocked(mMethodList, mMethodMap);
433
434 final String enabledStr = Settings.Secure.getString(
435 mContext.getContentResolver(),
436 Settings.Secure.ENABLED_INPUT_METHODS);
437 Log.i(TAG, "Enabled input methods: " + enabledStr);
438 if (enabledStr == null) {
439 Log.i(TAG, "Enabled input methods has not been set, enabling all");
440 InputMethodInfo defIm = null;
441 StringBuilder sb = new StringBuilder(256);
442 final int N = mMethodList.size();
443 for (int i=0; i<N; i++) {
444 InputMethodInfo imi = mMethodList.get(i);
445 Log.i(TAG, "Adding: " + imi.getId());
446 if (i > 0) sb.append(':');
447 sb.append(imi.getId());
448 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
449 try {
450 Resources res = mContext.createPackageContext(
451 imi.getPackageName(), 0).getResources();
452 if (res.getBoolean(imi.getIsDefaultResourceId())) {
453 defIm = imi;
454 Log.i(TAG, "Selected default: " + imi.getId());
455 }
456 } catch (PackageManager.NameNotFoundException ex) {
457 } catch (Resources.NotFoundException ex) {
458 }
459 }
460 }
461 if (defIm == null && N > 0) {
462 defIm = mMethodList.get(0);
463 Log.i(TAG, "No default found, using " + defIm.getId());
464 }
465 Settings.Secure.putString(mContext.getContentResolver(),
466 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
467 if (defIm != null) {
468 Settings.Secure.putString(mContext.getContentResolver(),
469 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
470 }
471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 mStatusBar = statusBar;
474 mInputMethodData = IconData.makeIcon("ime", null, 0, 0, 0);
475 mInputMethodIcon = statusBar.addIcon(mInputMethodData, null);
476 statusBar.setIconVisibility(mInputMethodIcon, false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mSettingsObserver = new SettingsObserver(mHandler);
479 updateFromSettingsLocked();
480 }
481
482 @Override
483 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
484 throws RemoteException {
485 try {
486 return super.onTransact(code, data, reply, flags);
487 } catch (RuntimeException e) {
488 // The input method manager only throws security exceptions, so let's
489 // log all others.
490 if (!(e instanceof SecurityException)) {
491 Log.e(TAG, "Input Method Manager Crash", e);
492 }
493 throw e;
494 }
495 }
496
497 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700498 synchronized (mMethodMap) {
499 if (!mSystemReady) {
500 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700501 try {
502 startInputInnerLocked();
503 } catch (RuntimeException e) {
504 Log.w(TAG, "Unexpected exception", e);
505 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700506 }
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public List<InputMethodInfo> getInputMethodList() {
511 synchronized (mMethodMap) {
512 return new ArrayList<InputMethodInfo>(mMethodList);
513 }
514 }
515
516 public List<InputMethodInfo> getEnabledInputMethodList() {
517 synchronized (mMethodMap) {
518 return getEnabledInputMethodListLocked();
519 }
520 }
521
522 List<InputMethodInfo> getEnabledInputMethodListLocked() {
523 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 final String enabledStr = Settings.Secure.getString(
526 mContext.getContentResolver(),
527 Settings.Secure.ENABLED_INPUT_METHODS);
528 if (enabledStr != null) {
529 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
530 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 while (splitter.hasNext()) {
533 InputMethodInfo info = mMethodMap.get(splitter.next());
534 if (info != null) {
535 res.add(info);
536 }
537 }
538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 return res;
541 }
542
543 public void addClient(IInputMethodClient client,
544 IInputContext inputContext, int uid, int pid) {
545 synchronized (mMethodMap) {
546 mClients.put(client.asBinder(), new ClientState(client,
547 inputContext, uid, pid));
548 }
549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public void removeClient(IInputMethodClient client) {
552 synchronized (mMethodMap) {
553 mClients.remove(client.asBinder());
554 }
555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 void executeOrSendMessage(IInterface target, Message msg) {
558 if (target.asBinder() instanceof Binder) {
559 mCaller.sendMessage(msg);
560 } else {
561 handleMessage(msg);
562 msg.recycle();
563 }
564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700566 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 if (mCurClient != null) {
568 if (DEBUG) Log.v(TAG, "unbindCurrentInputLocked: client = "
569 + mCurClient.client.asBinder());
570 if (mBoundToMethod) {
571 mBoundToMethod = false;
572 if (mCurMethod != null) {
573 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
574 MSG_UNBIND_INPUT, mCurMethod));
575 }
576 }
577 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
578 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
579 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 // Call setActive(false) on the old client
582 try {
583 mCurClient.client.setActive(false);
584 } catch (RemoteException e) {
585 Log.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
586 + mCurClient.pid + " uid " + mCurClient.uid);
587 }
588 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project10592532009-03-18 17:39:46 -0700590 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 private int getImeShowFlags() {
595 int flags = 0;
596 if (mShowForced) {
597 flags |= InputMethod.SHOW_FORCED
598 | InputMethod.SHOW_EXPLICIT;
599 } else if (mShowExplicitlyRequested) {
600 flags |= InputMethod.SHOW_EXPLICIT;
601 }
602 return flags;
603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 private int getAppShowFlags() {
606 int flags = 0;
607 if (mShowForced) {
608 flags |= InputMethodManager.SHOW_FORCED;
609 } else if (!mShowExplicitlyRequested) {
610 flags |= InputMethodManager.SHOW_IMPLICIT;
611 }
612 return flags;
613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
616 if (!mBoundToMethod) {
617 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
618 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
619 mBoundToMethod = true;
620 }
621 final SessionState session = mCurClient.curSession;
622 if (initial) {
623 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
624 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
625 } else {
626 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
627 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
628 }
629 if (mShowRequested) {
630 if (DEBUG) Log.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800631 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 return needResult
634 ? new InputBindResult(session.session, mCurId, mCurSeq)
635 : null;
636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 InputBindResult startInputLocked(IInputMethodClient client,
639 IInputContext inputContext, EditorInfo attribute,
640 boolean initial, boolean needResult) {
641 // If no method is currently selected, do nothing.
642 if (mCurMethodId == null) {
643 return mNoBinding;
644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 ClientState cs = mClients.get(client.asBinder());
647 if (cs == null) {
648 throw new IllegalArgumentException("unknown client "
649 + client.asBinder());
650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 try {
653 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
654 // Check with the window manager to make sure this client actually
655 // has a window with focus. If not, reject. This is thread safe
656 // because if the focus changes some time before or after, the
657 // next client receiving focus that has any interest in input will
658 // be calling through here after that change happens.
659 Log.w(TAG, "Starting input on non-focused client " + cs.client
660 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
661 return null;
662 }
663 } catch (RemoteException e) {
664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 if (mCurClient != cs) {
667 // If the client is changing, we need to switch over to the new
668 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700669 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 if (DEBUG) Log.v(TAG, "switching to client: client = "
671 + cs.client.asBinder());
672
673 // If the screen is on, inform the new client it is active
674 if (mScreenOn) {
675 try {
676 cs.client.setActive(mScreenOn);
677 } catch (RemoteException e) {
678 Log.w(TAG, "Got RemoteException sending setActive notification to pid "
679 + cs.pid + " uid " + cs.uid);
680 }
681 }
682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 // Bump up the sequence for this client and attach it.
685 mCurSeq++;
686 if (mCurSeq <= 0) mCurSeq = 1;
687 mCurClient = cs;
688 mCurInputContext = inputContext;
689 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 // Check if the input method is changing.
692 if (mCurId != null && mCurId.equals(mCurMethodId)) {
693 if (cs.curSession != null) {
694 // Fast case: if we are already connected to the input method,
695 // then just return it.
696 return attachNewInputLocked(initial, needResult);
697 }
698 if (mHaveConnection) {
699 if (mCurMethod != null) {
700 if (!cs.sessionRequested) {
701 cs.sessionRequested = true;
702 if (DEBUG) Log.v(TAG, "Creating new session for client " + cs);
703 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
704 MSG_CREATE_SESSION, mCurMethod,
705 new MethodCallback(mCurMethod)));
706 }
707 // Return to client, and we will get back with it when
708 // we have had a session made for it.
709 return new InputBindResult(null, mCurId, mCurSeq);
710 } else if (SystemClock.uptimeMillis()
711 < (mLastBindTime+TIME_TO_RECONNECT)) {
712 // In this case we have connected to the service, but
713 // don't yet have its interface. If it hasn't been too
714 // long since we did the connection, we'll return to
715 // the client and wait to get the service interface so
716 // we can report back. If it has been too long, we want
717 // to fall through so we can try a disconnect/reconnect
718 // to see if we can get back in touch with the service.
719 return new InputBindResult(null, mCurId, mCurSeq);
720 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
722 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724 }
725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700727 return startInputInnerLocked();
728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700730 InputBindResult startInputInnerLocked() {
731 if (mCurMethodId == null) {
732 return mNoBinding;
733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700735 if (!mSystemReady) {
736 // If the system is not yet ready, we shouldn't be running third
737 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700738 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 InputMethodInfo info = mMethodMap.get(mCurMethodId);
742 if (info == null) {
743 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700746 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
749 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700750 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
751 com.android.internal.R.string.input_method_binding_label);
752 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
753 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
755 mLastBindTime = SystemClock.uptimeMillis();
756 mHaveConnection = true;
757 mCurId = info.getId();
758 mCurToken = new Binder();
759 try {
760 if (DEBUG) Log.v(TAG, "Adding window token: " + mCurToken);
761 mIWindowManager.addWindowToken(mCurToken,
762 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
763 } catch (RemoteException e) {
764 }
765 return new InputBindResult(null, mCurId, mCurSeq);
766 } else {
767 mCurIntent = null;
768 Log.w(TAG, "Failure connecting to input method service: "
769 + mCurIntent);
770 }
771 return null;
772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 public InputBindResult startInput(IInputMethodClient client,
775 IInputContext inputContext, EditorInfo attribute,
776 boolean initial, boolean needResult) {
777 synchronized (mMethodMap) {
778 final long ident = Binder.clearCallingIdentity();
779 try {
780 return startInputLocked(client, inputContext, attribute,
781 initial, needResult);
782 } finally {
783 Binder.restoreCallingIdentity(ident);
784 }
785 }
786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 public void finishInput(IInputMethodClient client) {
789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 public void onServiceConnected(ComponentName name, IBinder service) {
792 synchronized (mMethodMap) {
793 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
794 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700795 if (mCurToken == null) {
796 Log.w(TAG, "Service connected without a token!");
797 unbindCurrentMethodLocked(false);
798 return;
799 }
800 if (DEBUG) Log.v(TAG, "Initiating attach with token: " + mCurToken);
801 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
802 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if (mCurClient != null) {
Dianne Hackborncc278702009-09-02 23:07:23 -0700804 if (DEBUG) Log.v(TAG, "Creating first session while with client "
805 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700807 MSG_CREATE_SESSION, mCurMethod,
808 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
810 }
811 }
812 }
813
814 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
815 synchronized (mMethodMap) {
816 if (mCurMethod != null && method != null
817 && mCurMethod.asBinder() == method.asBinder()) {
818 if (mCurClient != null) {
819 mCurClient.curSession = new SessionState(mCurClient,
820 method, session);
821 mCurClient.sessionRequested = false;
822 InputBindResult res = attachNewInputLocked(true, true);
823 if (res.method != null) {
824 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
825 MSG_BIND_METHOD, mCurClient.client, res));
826 }
827 }
828 }
829 }
830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800831
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700832 void unbindCurrentMethodLocked(boolean reportToClient) {
833 if (mHaveConnection) {
834 mContext.unbindService(this);
835 mHaveConnection = false;
836 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800837
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700838 if (mCurToken != null) {
839 try {
840 if (DEBUG) Log.v(TAG, "Removing window token: " + mCurToken);
841 mIWindowManager.removeWindowToken(mCurToken);
842 } catch (RemoteException e) {
843 }
844 mCurToken = null;
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
The Android Open Source Project10592532009-03-18 17:39:46 -0700847 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700848 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800849
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700850 if (reportToClient && mCurClient != null) {
851 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
852 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
853 }
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700856 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 if (mCurMethod != null) {
858 for (ClientState cs : mClients.values()) {
859 cs.sessionRequested = false;
860 cs.curSession = null;
861 }
862 mCurMethod = null;
863 }
864 mStatusBar.setIconVisibility(mInputMethodIcon, false);
865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 public void onServiceDisconnected(ComponentName name) {
868 synchronized (mMethodMap) {
869 if (DEBUG) Log.v(TAG, "Service disconnected: " + name
870 + " mCurIntent=" + mCurIntent);
871 if (mCurMethod != null && mCurIntent != null
872 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // We consider this to be a new bind attempt, since the system
875 // should now try to restart the service for us.
876 mLastBindTime = SystemClock.uptimeMillis();
877 mShowRequested = mInputShown;
878 mInputShown = false;
879 if (mCurClient != null) {
880 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
881 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
882 }
883 }
884 }
885 }
886
887 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
888 long ident = Binder.clearCallingIdentity();
889 try {
890 if (token == null || mCurToken != token) {
891 Log.w(TAG, "Ignoring setInputMethod of token: " + token);
892 return;
893 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 synchronized (mMethodMap) {
896 if (iconId == 0) {
897 if (DEBUG) Log.d(TAG, "hide the small icon for the input method");
898 mStatusBar.setIconVisibility(mInputMethodIcon, false);
899 } else if (packageName != null) {
900 if (DEBUG) Log.d(TAG, "show a small icon for the input method");
901 mInputMethodData.iconId = iconId;
902 mInputMethodData.iconPackage = packageName;
903 mStatusBar.updateIcon(mInputMethodIcon, mInputMethodData, null);
904 mStatusBar.setIconVisibility(mInputMethodIcon, true);
905 }
906 }
907 } finally {
908 Binder.restoreCallingIdentity(ident);
909 }
910 }
911
912 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700913 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
914 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
915 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
916 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 String id = Settings.Secure.getString(mContext.getContentResolver(),
918 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700919 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 try {
921 setInputMethodLocked(id);
922 } catch (IllegalArgumentException e) {
923 Log.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700924 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700925 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700927 } else {
928 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700929 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700930 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 void setInputMethodLocked(String id) {
935 InputMethodInfo info = mMethodMap.get(id);
936 if (info == null) {
937 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
938 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 if (id.equals(mCurMethodId)) {
941 return;
942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 final long ident = Binder.clearCallingIdentity();
945 try {
946 mCurMethodId = id;
947 Settings.Secure.putString(mContext.getContentResolver(),
948 Settings.Secure.DEFAULT_INPUT_METHOD, id);
949
950 if (ActivityManagerNative.isSystemReady()) {
951 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800952 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 intent.putExtra("input_method_id", id);
954 mContext.sendBroadcast(intent);
955 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700956 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 } finally {
958 Binder.restoreCallingIdentity(ident);
959 }
960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
The Android Open Source Project4df24232009-03-05 14:34:35 -0800962 public boolean showSoftInput(IInputMethodClient client, int flags,
963 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 long ident = Binder.clearCallingIdentity();
965 try {
966 synchronized (mMethodMap) {
967 if (mCurClient == null || client == null
968 || mCurClient.client.asBinder() != client.asBinder()) {
969 try {
970 // We need to check if this is the current client with
971 // focus in the window manager, to allow this call to
972 // be made before input is started in it.
973 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
974 Log.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800975 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
977 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -0800978 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (DEBUG) Log.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800983 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 } finally {
986 Binder.restoreCallingIdentity(ident);
987 }
988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Project4df24232009-03-05 14:34:35 -0800990 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 mShowRequested = true;
992 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
993 mShowExplicitlyRequested = true;
994 }
995 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
996 mShowExplicitlyRequested = true;
997 mShowForced = true;
998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800999
Dianne Hackborncc278702009-09-02 23:07:23 -07001000 if (!mSystemReady) {
1001 return false;
1002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001003
The Android Open Source Project4df24232009-03-05 14:34:35 -08001004 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001006 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1007 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1008 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001010 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 } else if (mHaveConnection && SystemClock.uptimeMillis()
1012 < (mLastBindTime+TIME_TO_RECONNECT)) {
1013 // The client has asked to have the input method shown, but
1014 // we have been sitting here too long with a connection to the
1015 // service and no interface received, so let's disconnect/connect
1016 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001017 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 SystemClock.uptimeMillis()-mLastBindTime,1);
1019 mContext.unbindService(this);
1020 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
The Android Open Source Project4df24232009-03-05 14:34:35 -08001023 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001025
The Android Open Source Project4df24232009-03-05 14:34:35 -08001026 public boolean hideSoftInput(IInputMethodClient client, int flags,
1027 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 long ident = Binder.clearCallingIdentity();
1029 try {
1030 synchronized (mMethodMap) {
1031 if (mCurClient == null || client == null
1032 || mCurClient.client.asBinder() != client.asBinder()) {
1033 try {
1034 // We need to check if this is the current client with
1035 // focus in the window manager, to allow this call to
1036 // be made before input is started in it.
1037 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
1038 Log.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001039 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001042 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (DEBUG) Log.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001047 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 } finally {
1050 Binder.restoreCallingIdentity(ident);
1051 }
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
The Android Open Source Project4df24232009-03-05 14:34:35 -08001054 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1056 && (mShowExplicitlyRequested || mShowForced)) {
1057 if (DEBUG) Log.v(TAG,
1058 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001059 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
1062 if (DEBUG) Log.v(TAG,
1063 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001064 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001066 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001068 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1069 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1070 res = true;
1071 } else {
1072 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 mInputShown = false;
1075 mShowRequested = false;
1076 mShowExplicitlyRequested = false;
1077 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001078 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001080
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001081 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1083 boolean first, int windowFlags) {
1084 long ident = Binder.clearCallingIdentity();
1085 try {
1086 synchronized (mMethodMap) {
1087 if (DEBUG) Log.v(TAG, "windowGainedFocus: " + client.asBinder()
1088 + " viewHasFocus=" + viewHasFocus
1089 + " isTextEditor=" + isTextEditor
1090 + " softInputMode=#" + Integer.toHexString(softInputMode)
1091 + " first=" + first + " flags=#"
1092 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (mCurClient == null || client == null
1095 || mCurClient.client.asBinder() != client.asBinder()) {
1096 try {
1097 // We need to check if this is the current client with
1098 // focus in the window manager, to allow this call to
1099 // be made before input is started in it.
1100 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001101 Log.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 return;
1103 }
1104 } catch (RemoteException e) {
1105 }
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001108 if (mCurFocusedWindow == windowToken) {
1109 Log.w(TAG, "Window already focused, ignoring focus gain of: " + client);
1110 return;
1111 }
1112 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1115 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1116 if (!isTextEditor || (softInputMode &
1117 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1118 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1119 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1120 // There is no focus view, and this window will
1121 // be behind any soft input window, so hide the
1122 // soft input window if it is shown.
1123 if (DEBUG) Log.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001124 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126 } else if (isTextEditor && (softInputMode &
1127 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1128 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1129 && (softInputMode &
1130 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1131 // There is a focus view, and we are navigating forward
1132 // into the window, so show the input window for the user.
1133 if (DEBUG) Log.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 break;
1137 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1138 // Do nothing.
1139 break;
1140 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1141 if ((softInputMode &
1142 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1143 if (DEBUG) Log.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001144 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 break;
1147 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
1148 if (DEBUG) Log.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001149 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 break;
1151 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1152 if ((softInputMode &
1153 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1154 if (DEBUG) Log.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001155 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 break;
1158 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
1159 if (DEBUG) Log.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001160 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 break;
1162 }
1163 }
1164 } finally {
1165 Binder.restoreCallingIdentity(ident);
1166 }
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1170 synchronized (mMethodMap) {
1171 if (mCurClient == null || client == null
1172 || mCurClient.client.asBinder() != client.asBinder()) {
1173 Log.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
1174 }
1175
1176 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1177 }
1178 }
1179
1180 public void setInputMethod(IBinder token, String id) {
1181 synchronized (mMethodMap) {
1182 if (token == null) {
1183 if (mContext.checkCallingOrSelfPermission(
1184 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1185 != PackageManager.PERMISSION_GRANTED) {
1186 throw new SecurityException(
1187 "Using null token requires permission "
1188 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1189 }
1190 } else if (mCurToken != token) {
1191 Log.w(TAG, "Ignoring setInputMethod of token: " + token);
1192 return;
1193 }
1194
1195 long ident = Binder.clearCallingIdentity();
1196 try {
1197 setInputMethodLocked(id);
1198 } finally {
1199 Binder.restoreCallingIdentity(ident);
1200 }
1201 }
1202 }
1203
1204 public void hideMySoftInput(IBinder token, int flags) {
1205 synchronized (mMethodMap) {
1206 if (token == null || mCurToken != token) {
1207 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1208 return;
1209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 long ident = Binder.clearCallingIdentity();
1211 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001212 hideCurrentInputLocked(flags, null);
1213 } finally {
1214 Binder.restoreCallingIdentity(ident);
1215 }
1216 }
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project4df24232009-03-05 14:34:35 -08001219 public void showMySoftInput(IBinder token, int flags) {
1220 synchronized (mMethodMap) {
1221 if (token == null || mCurToken != token) {
1222 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1223 return;
1224 }
1225 long ident = Binder.clearCallingIdentity();
1226 try {
1227 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 } finally {
1229 Binder.restoreCallingIdentity(ident);
1230 }
1231 }
1232 }
1233
1234 void setEnabledSessionInMainThread(SessionState session) {
1235 if (mEnabledSession != session) {
1236 if (mEnabledSession != null) {
1237 try {
1238 if (DEBUG) Log.v(TAG, "Disabling: " + mEnabledSession);
1239 mEnabledSession.method.setSessionEnabled(
1240 mEnabledSession.session, false);
1241 } catch (RemoteException e) {
1242 }
1243 }
1244 mEnabledSession = session;
1245 try {
1246 if (DEBUG) Log.v(TAG, "Enabling: " + mEnabledSession);
1247 session.method.setSessionEnabled(
1248 session.session, true);
1249 } catch (RemoteException e) {
1250 }
1251 }
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 public boolean handleMessage(Message msg) {
1255 HandlerCaller.SomeArgs args;
1256 switch (msg.what) {
1257 case MSG_SHOW_IM_PICKER:
1258 showInputMethodMenu();
1259 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 case MSG_UNBIND_INPUT:
1264 try {
1265 ((IInputMethod)msg.obj).unbindInput();
1266 } catch (RemoteException e) {
1267 // There is nothing interesting about the method dying.
1268 }
1269 return true;
1270 case MSG_BIND_INPUT:
1271 args = (HandlerCaller.SomeArgs)msg.obj;
1272 try {
1273 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1274 } catch (RemoteException e) {
1275 }
1276 return true;
1277 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001278 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001280 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1281 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 } catch (RemoteException e) {
1283 }
1284 return true;
1285 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001286 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001288 ((IInputMethod)args.arg1).hideSoftInput(0,
1289 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 } catch (RemoteException e) {
1291 }
1292 return true;
1293 case MSG_ATTACH_TOKEN:
1294 args = (HandlerCaller.SomeArgs)msg.obj;
1295 try {
1296 if (DEBUG) Log.v(TAG, "Sending attach of token: " + args.arg2);
1297 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1298 } catch (RemoteException e) {
1299 }
1300 return true;
1301 case MSG_CREATE_SESSION:
1302 args = (HandlerCaller.SomeArgs)msg.obj;
1303 try {
1304 ((IInputMethod)args.arg1).createSession(
1305 (IInputMethodCallback)args.arg2);
1306 } catch (RemoteException e) {
1307 }
1308 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 case MSG_START_INPUT:
1312 args = (HandlerCaller.SomeArgs)msg.obj;
1313 try {
1314 SessionState session = (SessionState)args.arg1;
1315 setEnabledSessionInMainThread(session);
1316 session.method.startInput((IInputContext)args.arg2,
1317 (EditorInfo)args.arg3);
1318 } catch (RemoteException e) {
1319 }
1320 return true;
1321 case MSG_RESTART_INPUT:
1322 args = (HandlerCaller.SomeArgs)msg.obj;
1323 try {
1324 SessionState session = (SessionState)args.arg1;
1325 setEnabledSessionInMainThread(session);
1326 session.method.restartInput((IInputContext)args.arg2,
1327 (EditorInfo)args.arg3);
1328 } catch (RemoteException e) {
1329 }
1330 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 case MSG_UNBIND_METHOD:
1335 try {
1336 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1337 } catch (RemoteException e) {
1338 // There is nothing interesting about the last client dying.
1339 }
1340 return true;
1341 case MSG_BIND_METHOD:
1342 args = (HandlerCaller.SomeArgs)msg.obj;
1343 try {
1344 ((IInputMethodClient)args.arg1).onBindMethod(
1345 (InputBindResult)args.arg2);
1346 } catch (RemoteException e) {
1347 Log.w(TAG, "Client died receiving input method " + args.arg2);
1348 }
1349 return true;
1350 }
1351 return false;
1352 }
1353
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001354 private boolean isSystemIme(InputMethodInfo inputMethod) {
1355 return (inputMethod.getServiceInfo().applicationInfo.flags
1356 & ApplicationInfo.FLAG_SYSTEM) != 0;
1357 }
1358
1359 private boolean chooseNewDefaultIME() {
1360 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1361 if (enabled != null && enabled.size() > 0) {
1362 Settings.Secure.putString(mContext.getContentResolver(),
1363 Settings.Secure.DEFAULT_INPUT_METHOD,
1364 enabled.get(0).getId());
1365 return true;
1366 }
1367
1368 return false;
1369 }
1370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1372 HashMap<String, InputMethodInfo> map) {
1373 list.clear();
1374 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 PackageManager pm = mContext.getPackageManager();
1377
1378 List<ResolveInfo> services = pm.queryIntentServices(
1379 new Intent(InputMethod.SERVICE_INTERFACE),
1380 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 for (int i = 0; i < services.size(); ++i) {
1383 ResolveInfo ri = services.get(i);
1384 ServiceInfo si = ri.serviceInfo;
1385 ComponentName compName = new ComponentName(si.packageName, si.name);
1386 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1387 si.permission)) {
1388 Log.w(TAG, "Skipping input method " + compName
1389 + ": it does not require the permission "
1390 + android.Manifest.permission.BIND_INPUT_METHOD);
1391 continue;
1392 }
1393
1394 if (DEBUG) Log.d(TAG, "Checking " + compName);
1395
1396 try {
1397 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1398 list.add(p);
1399 map.put(p.getId(), p);
1400
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001401 // System IMEs are enabled by default
1402 if (isSystemIme(p)) {
1403 setInputMethodEnabled(p.getId(), true);
1404 }
1405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 if (DEBUG) {
1407 Log.d(TAG, "Found a third-party input method " + p);
1408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } catch (XmlPullParserException e) {
1411 Log.w(TAG, "Unable to load input method " + compName, e);
1412 } catch (IOException e) {
1413 Log.w(TAG, "Unable to load input method " + compName, e);
1414 }
1415 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001416
1417 String defaultIme = Settings.Secure.getString(mContext
1418 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1419 if (!map.containsKey(defaultIme)) {
1420 if (chooseNewDefaultIME()) {
1421 updateFromSettingsLocked();
1422 }
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 void showInputMethodMenu() {
1429 if (DEBUG) Log.v(TAG, "Show switching menu");
1430
1431 hideInputMethodMenu();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 String lastInputMethodId = Settings.Secure.getString(context
1438 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1439 if (DEBUG) Log.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 int N = (immis == null ? 0 : immis.size());
1444
1445 mItems = new CharSequence[N];
1446 mIms = new InputMethodInfo[N];
1447
1448 for (int i = 0; i < N; ++i) {
1449 InputMethodInfo property = immis.get(i);
1450 mItems[i] = property.loadLabel(pm);
1451 mIms[i] = property;
1452 }
1453
1454 int checkedItem = 0;
1455 for (int i = 0; i < N; ++i) {
1456 if (mIms[i].getId().equals(lastInputMethodId)) {
1457 checkedItem = i;
1458 break;
1459 }
1460 }
1461
1462 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1463 public void onClick(DialogInterface dialog, int which) {
1464 hideInputMethodMenu();
1465 }
1466 };
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 TypedArray a = context.obtainStyledAttributes(null,
1469 com.android.internal.R.styleable.DialogPreference,
1470 com.android.internal.R.attr.alertDialogStyle, 0);
1471 mDialogBuilder = new AlertDialog.Builder(context)
1472 .setTitle(com.android.internal.R.string.select_input_method)
1473 .setOnCancelListener(new OnCancelListener() {
1474 public void onCancel(DialogInterface dialog) {
1475 hideInputMethodMenu();
1476 }
1477 })
1478 .setIcon(a.getDrawable(
1479 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1480 a.recycle();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1483 new AlertDialog.OnClickListener() {
1484 public void onClick(DialogInterface dialog, int which) {
1485 synchronized (mMethodMap) {
1486 InputMethodInfo im = mIms[which];
1487 hideInputMethodMenu();
1488 setInputMethodLocked(im.getId());
1489 }
1490 }
1491 });
1492
1493 synchronized (mMethodMap) {
1494 mSwitchingDialog = mDialogBuilder.create();
1495 mSwitchingDialog.getWindow().setType(
1496 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1497 mSwitchingDialog.show();
1498 }
1499 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001502 synchronized (mMethodMap) {
1503 hideInputMethodMenuLocked();
1504 }
1505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
The Android Open Source Project10592532009-03-18 17:39:46 -07001507 void hideInputMethodMenuLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 if (DEBUG) Log.v(TAG, "Hide switching menu");
1509
The Android Open Source Project10592532009-03-18 17:39:46 -07001510 if (mSwitchingDialog != null) {
1511 mSwitchingDialog.dismiss();
1512 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
The Android Open Source Project10592532009-03-18 17:39:46 -07001515 mDialogBuilder = null;
1516 mItems = null;
1517 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 public boolean setInputMethodEnabled(String id, boolean enabled) {
1523 synchronized (mMethodMap) {
1524 if (mContext.checkCallingOrSelfPermission(
1525 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1526 != PackageManager.PERMISSION_GRANTED) {
1527 throw new SecurityException(
1528 "Requires permission "
1529 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 long ident = Binder.clearCallingIdentity();
1533 try {
1534 // Make sure this is a valid input method.
1535 InputMethodInfo imm = mMethodMap.get(id);
1536 if (imm == null) {
1537 if (imm == null) {
1538 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1539 }
1540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 StringBuilder builder = new StringBuilder(256);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 boolean removed = false;
1545 String firstId = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 // Look through the currently enabled input methods.
1548 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1549 Settings.Secure.ENABLED_INPUT_METHODS);
1550 if (enabledStr != null) {
1551 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1552 splitter.setString(enabledStr);
1553 while (splitter.hasNext()) {
1554 String curId = splitter.next();
1555 if (curId.equals(id)) {
1556 if (enabled) {
1557 // We are enabling this input method, but it is
1558 // already enabled. Nothing to do. The previous
1559 // state was enabled.
1560 return true;
1561 }
1562 // We are disabling this input method, and it is
1563 // currently enabled. Skip it to remove from the
1564 // new list.
1565 removed = true;
1566 } else if (!enabled) {
1567 // We are building a new list of input methods that
1568 // doesn't contain the given one.
1569 if (firstId == null) firstId = curId;
1570 if (builder.length() > 0) builder.append(':');
1571 builder.append(curId);
1572 }
1573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 if (!enabled) {
1577 if (!removed) {
1578 // We are disabling the input method but it is already
1579 // disabled. Nothing to do. The previous state was
1580 // disabled.
1581 return false;
1582 }
1583 // Update the setting with the new list of input methods.
1584 Settings.Secure.putString(mContext.getContentResolver(),
1585 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1586 // We the disabled input method is currently selected, switch
1587 // to another one.
1588 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1589 Settings.Secure.DEFAULT_INPUT_METHOD);
1590 if (id.equals(selId)) {
1591 Settings.Secure.putString(mContext.getContentResolver(),
1592 Settings.Secure.DEFAULT_INPUT_METHOD,
1593 firstId != null ? firstId : "");
1594 }
1595 // Previous state was enabled.
1596 return true;
1597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 // Add in the newly enabled input method.
1600 if (enabledStr == null || enabledStr.length() == 0) {
1601 enabledStr = id;
1602 } else {
1603 enabledStr = enabledStr + ':' + id;
1604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 Settings.Secure.putString(mContext.getContentResolver(),
1607 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 // Previous state was disabled.
1610 return false;
1611 } finally {
1612 Binder.restoreCallingIdentity(ident);
1613 }
1614 }
1615 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 @Override
1620 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1621 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1622 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1625 + Binder.getCallingPid()
1626 + ", uid=" + Binder.getCallingUid());
1627 return;
1628 }
1629
1630 IInputMethod method;
1631 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 synchronized (mMethodMap) {
1636 p.println("Current Input Method Manager state:");
1637 int N = mMethodList.size();
1638 p.println(" Input Methods:");
1639 for (int i=0; i<N; i++) {
1640 InputMethodInfo info = mMethodList.get(i);
1641 p.println(" InputMethod #" + i + ":");
1642 info.dump(p, " ");
1643 }
1644 p.println(" Clients:");
1645 for (ClientState ci : mClients.values()) {
1646 p.println(" Client " + ci + ":");
1647 p.println(" client=" + ci.client);
1648 p.println(" inputContext=" + ci.inputContext);
1649 p.println(" sessionRequested=" + ci.sessionRequested);
1650 p.println(" curSession=" + ci.curSession);
1651 }
1652 p.println(" mInputMethodIcon=" + mInputMethodIcon);
1653 p.println(" mInputMethodData=" + mInputMethodData);
The Android Open Source Project10592532009-03-18 17:39:46 -07001654 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001656 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1657 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1659 + " mBoundToMethod=" + mBoundToMethod);
1660 p.println(" mCurToken=" + mCurToken);
1661 p.println(" mCurIntent=" + mCurIntent);
1662 method = mCurMethod;
1663 p.println(" mCurMethod=" + mCurMethod);
1664 p.println(" mEnabledSession=" + mEnabledSession);
1665 p.println(" mShowRequested=" + mShowRequested
1666 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1667 + " mShowForced=" + mShowForced
1668 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001669 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (client != null) {
1673 p.println(" ");
1674 pw.flush();
1675 try {
1676 client.client.asBinder().dump(fd, args);
1677 } catch (RemoteException e) {
1678 p.println("Input method client dead: " + e);
1679 }
1680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (method != null) {
1683 p.println(" ");
1684 pw.flush();
1685 try {
1686 method.asBinder().dump(fd, args);
1687 } catch (RemoteException e) {
1688 p.println("Input method service dead: " + e);
1689 }
1690 }
1691 }
1692}